Add IntList as a more efficient representation of List<Integer>
[egit/charleso.git] / org.spearce.jgit.test / tst / org / spearce / jgit / util / IntListTest.java
blobf943075b66830772eb9e9a32137d46acedc8ce63
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 import junit.framework.TestCase;
42 public class IntListTest extends TestCase {
43 public void testEmpty_DefaultCapacity() {
44 final IntList i = new IntList();
45 assertEquals(0, i.size());
46 try {
47 i.get(0);
48 fail("Accepted 0 index on empty list");
49 } catch (ArrayIndexOutOfBoundsException e) {
50 assertTrue(true);
54 public void testEmpty_SpecificCapacity() {
55 final IntList i = new IntList(5);
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);
65 public void testAdd_SmallGroup() {
66 final IntList i = new IntList();
67 final int n = 5;
68 for (int v = 0; v < n; v++)
69 i.add(10 + v);
70 assertEquals(n, i.size());
72 for (int v = 0; v < n; v++)
73 assertEquals(10 + v, i.get(v));
74 try {
75 i.get(n);
76 fail("Accepted out of bound index on list");
77 } catch (ArrayIndexOutOfBoundsException e) {
78 assertTrue(true);
82 public void testAdd_ZeroCapacity() {
83 final IntList i = new IntList(0);
84 assertEquals(0, i.size());
85 i.add(1);
86 assertEquals(1, i.get(0));
89 public void testAdd_LargeGroup() {
90 final IntList i = new IntList();
91 final int n = 500;
92 for (int v = 0; v < n; v++)
93 i.add(10 + v);
94 assertEquals(n, i.size());
96 for (int v = 0; v < n; v++)
97 assertEquals(10 + v, i.get(v));
98 try {
99 i.get(n);
100 fail("Accepted out of bound index on list");
101 } catch (ArrayIndexOutOfBoundsException e) {
102 assertTrue(true);
106 public void testClear() {
107 final IntList i = new IntList();
108 final int n = 5;
109 for (int v = 0; v < n; v++)
110 i.add(10 + v);
111 assertEquals(n, i.size());
113 i.clear();
114 assertEquals(0, i.size());
115 try {
116 i.get(0);
117 fail("Accepted 0 index on empty list");
118 } catch (ArrayIndexOutOfBoundsException e) {
119 assertTrue(true);
123 public void testToString() {
124 final IntList i = new IntList();
125 i.add(1);
126 assertEquals("[1]", i.toString());
127 i.add(13);
128 i.add(5);
129 assertEquals("[1, 13, 5]", i.toString());