You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockListTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) 2011, 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. package org.eclipse.jgit.util;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertSame;
  47. import static org.junit.Assert.assertTrue;
  48. import java.util.Iterator;
  49. import org.junit.Test;
  50. public class BlockListTest {
  51. @Test
  52. public void testEmptyList() {
  53. BlockList<String> empty;
  54. empty = new BlockList<String>();
  55. assertEquals(0, empty.size());
  56. assertTrue(empty.isEmpty());
  57. assertFalse(empty.iterator().hasNext());
  58. empty = new BlockList<String>(0);
  59. assertEquals(0, empty.size());
  60. assertTrue(empty.isEmpty());
  61. assertFalse(empty.iterator().hasNext());
  62. empty = new BlockList<String>(1);
  63. assertEquals(0, empty.size());
  64. assertTrue(empty.isEmpty());
  65. assertFalse(empty.iterator().hasNext());
  66. empty = new BlockList<String>(64);
  67. assertEquals(0, empty.size());
  68. assertTrue(empty.isEmpty());
  69. assertFalse(empty.iterator().hasNext());
  70. }
  71. @Test
  72. public void testGet() {
  73. BlockList<String> list = new BlockList<String>(4);
  74. try {
  75. list.get(-1);
  76. } catch (IndexOutOfBoundsException badIndex) {
  77. assertEquals(String.valueOf(-1), badIndex.getMessage());
  78. }
  79. try {
  80. list.get(0);
  81. } catch (IndexOutOfBoundsException badIndex) {
  82. assertEquals(String.valueOf(0), badIndex.getMessage());
  83. }
  84. try {
  85. list.get(4);
  86. } catch (IndexOutOfBoundsException badIndex) {
  87. assertEquals(String.valueOf(4), badIndex.getMessage());
  88. }
  89. String fooStr = "foo";
  90. String barStr = "bar";
  91. String foobarStr = "foobar";
  92. list.add(fooStr);
  93. list.add(barStr);
  94. list.add(foobarStr);
  95. assertSame(fooStr, list.get(0));
  96. assertSame(barStr, list.get(1));
  97. assertSame(foobarStr, list.get(2));
  98. try {
  99. list.get(3);
  100. } catch (IndexOutOfBoundsException badIndex) {
  101. assertEquals(String.valueOf(3), badIndex.getMessage());
  102. }
  103. }
  104. @Test
  105. public void testSet() {
  106. BlockList<String> list = new BlockList<String>(4);
  107. try {
  108. list.set(-1, "foo");
  109. } catch (IndexOutOfBoundsException badIndex) {
  110. assertEquals(String.valueOf(-1), badIndex.getMessage());
  111. }
  112. try {
  113. list.set(0, "foo");
  114. } catch (IndexOutOfBoundsException badIndex) {
  115. assertEquals(String.valueOf(0), badIndex.getMessage());
  116. }
  117. try {
  118. list.set(4, "foo");
  119. } catch (IndexOutOfBoundsException badIndex) {
  120. assertEquals(String.valueOf(4), badIndex.getMessage());
  121. }
  122. String fooStr = "foo";
  123. String barStr = "bar";
  124. String foobarStr = "foobar";
  125. list.add(fooStr);
  126. list.add(barStr);
  127. list.add(foobarStr);
  128. assertSame(fooStr, list.get(0));
  129. assertSame(barStr, list.get(1));
  130. assertSame(foobarStr, list.get(2));
  131. assertSame(fooStr, list.set(0, barStr));
  132. assertSame(barStr, list.set(1, fooStr));
  133. assertSame(barStr, list.get(0));
  134. assertSame(fooStr, list.get(1));
  135. try {
  136. list.set(3, "bar");
  137. } catch (IndexOutOfBoundsException badIndex) {
  138. assertEquals(String.valueOf(3), badIndex.getMessage());
  139. }
  140. }
  141. @Test
  142. public void testAddToEnd() {
  143. BlockList<Integer> list = new BlockList<Integer>(4);
  144. int cnt = BlockList.BLOCK_SIZE * 3;
  145. for (int i = 0; i < cnt; i++)
  146. list.add(Integer.valueOf(42 + i));
  147. assertEquals(cnt, list.size());
  148. for (int i = 0; i < cnt; i++)
  149. assertEquals(Integer.valueOf(42 + i), list.get(i));
  150. list.clear();
  151. assertEquals(0, list.size());
  152. assertTrue(list.isEmpty());
  153. for (int i = 0; i < cnt; i++)
  154. list.add(i, Integer.valueOf(42 + i));
  155. assertEquals(cnt, list.size());
  156. for (int i = 0; i < cnt; i++)
  157. assertEquals(Integer.valueOf(42 + i), list.get(i));
  158. }
  159. @Test
  160. public void testAddSlowPath() {
  161. BlockList<String> list = new BlockList<String>(4);
  162. String fooStr = "foo";
  163. String barStr = "bar";
  164. String foobarStr = "foobar";
  165. String firstStr = "first";
  166. String zeroStr = "zero";
  167. list.add(fooStr);
  168. list.add(barStr);
  169. list.add(foobarStr);
  170. assertEquals(3, list.size());
  171. list.add(1, firstStr);
  172. assertEquals(4, list.size());
  173. assertSame(fooStr, list.get(0));
  174. assertSame(firstStr, list.get(1));
  175. assertSame(barStr, list.get(2));
  176. assertSame(foobarStr, list.get(3));
  177. list.add(0, zeroStr);
  178. assertEquals(5, list.size());
  179. assertSame(zeroStr, list.get(0));
  180. assertSame(fooStr, list.get(1));
  181. assertSame(firstStr, list.get(2));
  182. assertSame(barStr, list.get(3));
  183. assertSame(foobarStr, list.get(4));
  184. }
  185. @Test
  186. public void testRemoveFromEnd() {
  187. BlockList<String> list = new BlockList<String>(4);
  188. String fooStr = "foo";
  189. String barStr = "bar";
  190. String foobarStr = "foobar";
  191. list.add(fooStr);
  192. list.add(barStr);
  193. list.add(foobarStr);
  194. assertSame(foobarStr, list.remove(2));
  195. assertEquals(2, list.size());
  196. assertSame(barStr, list.remove(1));
  197. assertEquals(1, list.size());
  198. assertSame(fooStr, list.remove(0));
  199. assertEquals(0, list.size());
  200. }
  201. @Test
  202. public void testRemoveSlowPath() {
  203. BlockList<String> list = new BlockList<String>(4);
  204. String fooStr = "foo";
  205. String barStr = "bar";
  206. String foobarStr = "foobar";
  207. list.add(fooStr);
  208. list.add(barStr);
  209. list.add(foobarStr);
  210. assertSame(barStr, list.remove(1));
  211. assertEquals(2, list.size());
  212. assertSame(fooStr, list.get(0));
  213. assertSame(foobarStr, list.get(1));
  214. assertSame(fooStr, list.remove(0));
  215. assertEquals(1, list.size());
  216. assertSame(foobarStr, list.get(0));
  217. assertSame(foobarStr, list.remove(0));
  218. assertEquals(0, list.size());
  219. }
  220. @Test
  221. public void testAddRemoveAdd() {
  222. BlockList<Integer> list = new BlockList<Integer>();
  223. for (int i = 0; i < BlockList.BLOCK_SIZE + 1; i++)
  224. list.add(Integer.valueOf(i));
  225. assertEquals(Integer.valueOf(BlockList.BLOCK_SIZE),
  226. list.remove(list.size() - 1));
  227. assertEquals(Integer.valueOf(BlockList.BLOCK_SIZE - 1),
  228. list.remove(list.size() - 1));
  229. assertTrue(list.add(Integer.valueOf(1)));
  230. assertEquals(Integer.valueOf(1), list.get(list.size() - 1));
  231. }
  232. @Test
  233. public void testAddAllFromOtherList() {
  234. BlockList<Integer> src = new BlockList<Integer>(4);
  235. int cnt = BlockList.BLOCK_SIZE * 2;
  236. for (int i = 0; i < cnt; i++)
  237. src.add(Integer.valueOf(42 + i));
  238. src.add(Integer.valueOf(1));
  239. BlockList<Integer> dst = new BlockList<Integer>(4);
  240. dst.add(Integer.valueOf(255));
  241. dst.addAll(src);
  242. assertEquals(cnt + 2, dst.size());
  243. for (int i = 0; i < cnt; i++)
  244. assertEquals(Integer.valueOf(42 + i), dst.get(i + 1));
  245. assertEquals(Integer.valueOf(1), dst.get(dst.size() - 1));
  246. }
  247. @Test
  248. public void testFastIterator() {
  249. BlockList<Integer> list = new BlockList<Integer>(4);
  250. int cnt = BlockList.BLOCK_SIZE * 3;
  251. for (int i = 0; i < cnt; i++)
  252. list.add(Integer.valueOf(42 + i));
  253. assertEquals(cnt, list.size());
  254. Iterator<Integer> itr = list.iterator();
  255. for (int i = 0; i < cnt; i++) {
  256. assertTrue(itr.hasNext());
  257. assertEquals(Integer.valueOf(42 + i), itr.next());
  258. }
  259. assertFalse(itr.hasNext());
  260. }
  261. @Test
  262. public void testAddRejectsBadIndexes() {
  263. BlockList<Integer> list = new BlockList<Integer>(4);
  264. list.add(Integer.valueOf(41));
  265. try {
  266. list.add(-1, Integer.valueOf(42));
  267. } catch (IndexOutOfBoundsException badIndex) {
  268. assertEquals(String.valueOf(-1), badIndex.getMessage());
  269. }
  270. try {
  271. list.add(4, Integer.valueOf(42));
  272. } catch (IndexOutOfBoundsException badIndex) {
  273. assertEquals(String.valueOf(4), badIndex.getMessage());
  274. }
  275. }
  276. @Test
  277. public void testRemoveRejectsBadIndexes() {
  278. BlockList<Integer> list = new BlockList<Integer>(4);
  279. list.add(Integer.valueOf(41));
  280. try {
  281. list.remove(-1);
  282. } catch (IndexOutOfBoundsException badIndex) {
  283. assertEquals(String.valueOf(-1), badIndex.getMessage());
  284. }
  285. try {
  286. list.remove(4);
  287. } catch (IndexOutOfBoundsException badIndex) {
  288. assertEquals(String.valueOf(4), badIndex.getMessage());
  289. }
  290. }
  291. }