1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import org.junit.Test;
19
20 public class IntListTest {
21 @Test
22 public void testEmpty_DefaultCapacity() {
23 final IntList i = new IntList();
24 assertEquals(0, i.size());
25 try {
26 i.get(0);
27 fail("Accepted 0 index on empty list");
28 } catch (ArrayIndexOutOfBoundsException e) {
29 assertTrue(true);
30 }
31 }
32
33 @Test
34 public void testEmpty_SpecificCapacity() {
35 final IntList i = new IntList(5);
36 assertEquals(0, i.size());
37 try {
38 i.get(0);
39 fail("Accepted 0 index on empty list");
40 } catch (ArrayIndexOutOfBoundsException e) {
41 assertTrue(true);
42 }
43 }
44
45 @Test
46 public void testAdd_SmallGroup() {
47 final IntList i = new IntList();
48 final int n = 5;
49 for (int v = 0; v < n; v++)
50 i.add(10 + v);
51 assertEquals(n, i.size());
52
53 for (int v = 0; v < n; v++)
54 assertEquals(10 + v, i.get(v));
55 try {
56 i.get(n);
57 fail("Accepted out of bound index on list");
58 } catch (ArrayIndexOutOfBoundsException e) {
59 assertTrue(true);
60 }
61 }
62
63 @Test
64 public void testAdd_ZeroCapacity() {
65 final IntList i = new IntList(0);
66 assertEquals(0, i.size());
67 i.add(1);
68 assertEquals(1, i.get(0));
69 }
70
71 @Test
72 public void testAdd_LargeGroup() {
73 final IntList i = new IntList();
74 final int n = 500;
75 for (int v = 0; v < n; v++)
76 i.add(10 + v);
77 assertEquals(n, i.size());
78
79 for (int v = 0; v < n; v++)
80 assertEquals(10 + v, i.get(v));
81 try {
82 i.get(n);
83 fail("Accepted out of bound index on list");
84 } catch (ArrayIndexOutOfBoundsException e) {
85 assertTrue(true);
86 }
87 }
88
89 @Test
90 public void testFillTo0() {
91 final IntList i = new IntList();
92 i.fillTo(0, Integer.MIN_VALUE);
93 assertEquals(0, i.size());
94 }
95
96 @Test
97 public void testFillTo1() {
98 final IntList i = new IntList();
99 i.fillTo(1, Integer.MIN_VALUE);
100 assertEquals(1, i.size());
101 i.add(0);
102 assertEquals(Integer.MIN_VALUE, i.get(0));
103 assertEquals(0, i.get(1));
104 }
105
106 @Test
107 public void testFillTo100() {
108 final IntList i = new IntList();
109 i.fillTo(100, Integer.MIN_VALUE);
110 assertEquals(100, i.size());
111 i.add(3);
112 assertEquals(Integer.MIN_VALUE, i.get(99));
113 assertEquals(3, i.get(100));
114 }
115
116 @Test
117 public void testClear() {
118 final IntList i = new IntList();
119 final int n = 5;
120 for (int v = 0; v < n; v++)
121 i.add(10 + v);
122 assertEquals(n, i.size());
123
124 i.clear();
125 assertEquals(0, i.size());
126 try {
127 i.get(0);
128 fail("Accepted 0 index on empty list");
129 } catch (ArrayIndexOutOfBoundsException e) {
130 assertTrue(true);
131 }
132 }
133
134 @Test
135 public void testSet() {
136 final IntList i = new IntList();
137 i.add(1);
138 assertEquals(1, i.size());
139 assertEquals(1, i.get(0));
140
141 i.set(0, 5);
142 assertEquals(5, i.get(0));
143
144 try {
145 i.set(5, 5);
146 fail("accepted set of 5 beyond end of list");
147 } catch (ArrayIndexOutOfBoundsException e){
148 assertTrue(true);
149 }
150
151 i.set(1, 2);
152 assertEquals(2, i.size());
153 assertEquals(2, i.get(1));
154 }
155
156 @Test
157 public void testContains() {
158 IntList i = new IntList();
159 i.add(1);
160 i.add(4);
161 assertTrue(i.contains(1));
162 assertTrue(i.contains(4));
163 assertFalse(i.contains(2));
164 }
165
166 @Test
167 public void testToString() {
168 final IntList i = new IntList();
169 i.add(1);
170 assertEquals("[1]", i.toString());
171 i.add(13);
172 i.add(5);
173 assertEquals("[1, 13, 5]", i.toString());
174 }
175
176 }