summaryrefslogtreecommitdiff
path: root/elki/src/test/java/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/IntegerArrayTest.java
blob: a3949206869d3ded5bd5b0fba53fddb5240709fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike;
/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2016
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Random;

import org.junit.Test;

/**
 * Unit test for this arraylist of primitive ints.
 * 
 * @author Erich Schubert
 * @since 0.7.1
 */
public class IntegerArrayTest {
  @Test
  public void basicTest() {
    IntegerArray arr = new IntegerArray(11);
    assertTrue("Allocation does not match.", arr.data.length == 11);
    assertTrue("Initial size is not empty.", arr.size() == 0);

    Random r = new Random(0L);
    for(int i = 0; i < 11; i++) {
      arr.add(r.nextInt());
    }
    assertTrue("Array is not full.", arr.data.length == arr.size());
    arr.add(r.nextInt());
    assertEquals("Allocation does not match after resize.", 17, arr.data.length);
    for(int i = 0; i < 81; i++) {
      arr.add(r.nextInt());
    }
    assertEquals("Array does not have expected size.", 11 + 1 + 81, arr.size());
    arr.sort();
    assertEquals("Allocation does not match after resize.", 139, arr.data.length);
    int prev = arr.get(0);
    for(int i = 1; i < arr.size(); i++) {
      int next = arr.get(i);
      assertTrue("Array is not sorted.", prev <= next);
      prev = next;
    }
  }
}