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.assertTrue;
48  import static org.junit.Assert.fail;
49  
50  import org.junit.Test;
51  
52  public class IntListTest {
53  	@Test
54  	public void testEmpty_DefaultCapacity() {
55  		final IntList i = new IntList();
56  		assertEquals(0, i.size());
57  		try {
58  			i.get(0);
59  			fail("Accepted 0 index on empty list");
60  		} catch (ArrayIndexOutOfBoundsException e) {
61  			assertTrue(true);
62  		}
63  	}
64  
65  	@Test
66  	public void testEmpty_SpecificCapacity() {
67  		final IntList i = new IntList(5);
68  		assertEquals(0, i.size());
69  		try {
70  			i.get(0);
71  			fail("Accepted 0 index on empty list");
72  		} catch (ArrayIndexOutOfBoundsException e) {
73  			assertTrue(true);
74  		}
75  	}
76  
77  	@Test
78  	public void testAdd_SmallGroup() {
79  		final IntList i = new IntList();
80  		final int n = 5;
81  		for (int v = 0; v < n; v++)
82  			i.add(10 + v);
83  		assertEquals(n, i.size());
84  
85  		for (int v = 0; v < n; v++)
86  			assertEquals(10 + v, i.get(v));
87  		try {
88  			i.get(n);
89  			fail("Accepted out of bound index on list");
90  		} catch (ArrayIndexOutOfBoundsException e) {
91  			assertTrue(true);
92  		}
93  	}
94  
95  	@Test
96  	public void testAdd_ZeroCapacity() {
97  		final IntList i = new IntList(0);
98  		assertEquals(0, i.size());
99  		i.add(1);
100 		assertEquals(1, i.get(0));
101 	}
102 
103 	@Test
104 	public void testAdd_LargeGroup() {
105 		final IntList i = new IntList();
106 		final int n = 500;
107 		for (int v = 0; v < n; v++)
108 			i.add(10 + v);
109 		assertEquals(n, i.size());
110 
111 		for (int v = 0; v < n; v++)
112 			assertEquals(10 + v, i.get(v));
113 		try {
114 			i.get(n);
115 			fail("Accepted out of bound index on list");
116 		} catch (ArrayIndexOutOfBoundsException e) {
117 			assertTrue(true);
118 		}
119 	}
120 
121 	@Test
122 	public void testFillTo0() {
123 		final IntList i = new IntList();
124 		i.fillTo(0, Integer.MIN_VALUE);
125 		assertEquals(0, i.size());
126 	}
127 
128 	@Test
129 	public void testFillTo1() {
130 		final IntList i = new IntList();
131 		i.fillTo(1, Integer.MIN_VALUE);
132 		assertEquals(1, i.size());
133 		i.add(0);
134 		assertEquals(Integer.MIN_VALUE, i.get(0));
135 		assertEquals(0, i.get(1));
136 	}
137 
138 	@Test
139 	public void testFillTo100() {
140 		final IntList i = new IntList();
141 		i.fillTo(100, Integer.MIN_VALUE);
142 		assertEquals(100, i.size());
143 		i.add(3);
144 		assertEquals(Integer.MIN_VALUE, i.get(99));
145 		assertEquals(3, i.get(100));
146 	}
147 
148 	@Test
149 	public void testClear() {
150 		final IntList i = new IntList();
151 		final int n = 5;
152 		for (int v = 0; v < n; v++)
153 			i.add(10 + v);
154 		assertEquals(n, i.size());
155 
156 		i.clear();
157 		assertEquals(0, i.size());
158 		try {
159 			i.get(0);
160 			fail("Accepted 0 index on empty list");
161 		} catch (ArrayIndexOutOfBoundsException e) {
162 			assertTrue(true);
163 		}
164 	}
165 
166 	@Test
167 	public void testSet() {
168 		final IntList i = new IntList();
169 		i.add(1);
170 		assertEquals(1, i.size());
171 		assertEquals(1, i.get(0));
172 
173 		i.set(0, 5);
174 		assertEquals(5, i.get(0));
175 
176 		try {
177 			i.set(5, 5);
178 			fail("accepted set of 5 beyond end of list");
179 		} catch (ArrayIndexOutOfBoundsException e){
180 			assertTrue(true);
181 		}
182 
183 		i.set(1, 2);
184 		assertEquals(2, i.size());
185 		assertEquals(2, i.get(1));
186 	}
187 
188 	@Test
189 	public void testToString() {
190 		final IntList i = new IntList();
191 		i.add(1);
192 		assertEquals("[1]", i.toString());
193 		i.add(13);
194 		i.add(5);
195 		assertEquals("[1, 13, 5]", i.toString());
196 	}
197 
198 }