View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertTrue;
49  import static org.junit.Assert.fail;
50  
51  import org.junit.Test;
52  
53  public class IntListTest {
54  	@Test
55  	public void testEmpty_DefaultCapacity() {
56  		final IntList i = new IntList();
57  		assertEquals(0, i.size());
58  		try {
59  			i.get(0);
60  			fail("Accepted 0 index on empty list");
61  		} catch (ArrayIndexOutOfBoundsException e) {
62  			assertTrue(true);
63  		}
64  	}
65  
66  	@Test
67  	public void testEmpty_SpecificCapacity() {
68  		final IntList i = new IntList(5);
69  		assertEquals(0, i.size());
70  		try {
71  			i.get(0);
72  			fail("Accepted 0 index on empty list");
73  		} catch (ArrayIndexOutOfBoundsException e) {
74  			assertTrue(true);
75  		}
76  	}
77  
78  	@Test
79  	public void testAdd_SmallGroup() {
80  		final IntList i = new IntList();
81  		final int n = 5;
82  		for (int v = 0; v < n; v++)
83  			i.add(10 + v);
84  		assertEquals(n, i.size());
85  
86  		for (int v = 0; v < n; v++)
87  			assertEquals(10 + v, i.get(v));
88  		try {
89  			i.get(n);
90  			fail("Accepted out of bound index on list");
91  		} catch (ArrayIndexOutOfBoundsException e) {
92  			assertTrue(true);
93  		}
94  	}
95  
96  	@Test
97  	public void testAdd_ZeroCapacity() {
98  		final IntList i = new IntList(0);
99  		assertEquals(0, i.size());
100 		i.add(1);
101 		assertEquals(1, i.get(0));
102 	}
103 
104 	@Test
105 	public void testAdd_LargeGroup() {
106 		final IntList i = new IntList();
107 		final int n = 500;
108 		for (int v = 0; v < n; v++)
109 			i.add(10 + v);
110 		assertEquals(n, i.size());
111 
112 		for (int v = 0; v < n; v++)
113 			assertEquals(10 + v, i.get(v));
114 		try {
115 			i.get(n);
116 			fail("Accepted out of bound index on list");
117 		} catch (ArrayIndexOutOfBoundsException e) {
118 			assertTrue(true);
119 		}
120 	}
121 
122 	@Test
123 	public void testFillTo0() {
124 		final IntList i = new IntList();
125 		i.fillTo(0, Integer.MIN_VALUE);
126 		assertEquals(0, i.size());
127 	}
128 
129 	@Test
130 	public void testFillTo1() {
131 		final IntList i = new IntList();
132 		i.fillTo(1, Integer.MIN_VALUE);
133 		assertEquals(1, i.size());
134 		i.add(0);
135 		assertEquals(Integer.MIN_VALUE, i.get(0));
136 		assertEquals(0, i.get(1));
137 	}
138 
139 	@Test
140 	public void testFillTo100() {
141 		final IntList i = new IntList();
142 		i.fillTo(100, Integer.MIN_VALUE);
143 		assertEquals(100, i.size());
144 		i.add(3);
145 		assertEquals(Integer.MIN_VALUE, i.get(99));
146 		assertEquals(3, i.get(100));
147 	}
148 
149 	@Test
150 	public void testClear() {
151 		final IntList i = new IntList();
152 		final int n = 5;
153 		for (int v = 0; v < n; v++)
154 			i.add(10 + v);
155 		assertEquals(n, i.size());
156 
157 		i.clear();
158 		assertEquals(0, i.size());
159 		try {
160 			i.get(0);
161 			fail("Accepted 0 index on empty list");
162 		} catch (ArrayIndexOutOfBoundsException e) {
163 			assertTrue(true);
164 		}
165 	}
166 
167 	@Test
168 	public void testSet() {
169 		final IntList i = new IntList();
170 		i.add(1);
171 		assertEquals(1, i.size());
172 		assertEquals(1, i.get(0));
173 
174 		i.set(0, 5);
175 		assertEquals(5, i.get(0));
176 
177 		try {
178 			i.set(5, 5);
179 			fail("accepted set of 5 beyond end of list");
180 		} catch (ArrayIndexOutOfBoundsException e){
181 			assertTrue(true);
182 		}
183 
184 		i.set(1, 2);
185 		assertEquals(2, i.size());
186 		assertEquals(2, i.get(1));
187 	}
188 
189 	@Test
190 	public void testContains() {
191 		IntList i = new IntList();
192 		i.add(1);
193 		i.add(4);
194 		assertTrue(i.contains(1));
195 		assertTrue(i.contains(4));
196 		assertFalse(i.contains(2));
197 	}
198 
199 	@Test
200 	public void testToString() {
201 		final IntList i = new IntList();
202 		i.add(1);
203 		assertEquals("[1]", i.toString());
204 		i.add(13);
205 		i.add(5);
206 		assertEquals("[1, 13, 5]", i.toString());
207 	}
208 
209 }