/platform/util/testSrc/com/intellij/util/SmartListTest.java

https://bitbucket.org/nbargnesi/idea · Java · 136 lines · 102 code · 16 blank · 18 comment · 0 complexity · e2a61d98117b37df1865229240690d8d MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.util;
  17. import com.intellij.util.containers.EmptyIterator;
  18. import junit.framework.TestCase;
  19. import java.util.ConcurrentModificationException;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. /**
  23. * @author max
  24. */
  25. public class SmartListTest extends TestCase {
  26. public void testEmpty() {
  27. assertEquals(0, new SmartList<Integer>().size());
  28. }
  29. public void testOneElement() {
  30. List<Integer> l = new SmartList<Integer>();
  31. l.add(new Integer(1));
  32. assertEquals(1, l.size());
  33. assertEquals(1, l.get(0).intValue());
  34. }
  35. public void testTwoElement() {
  36. List<Integer> l = new SmartList<Integer>();
  37. l.add(new Integer(1));
  38. l.add(new Integer(2));
  39. assertEquals(2, l.size());
  40. assertEquals(1, l.get(0).intValue());
  41. assertEquals(2, l.get(1).intValue());
  42. }
  43. public void testThreeElement() {
  44. List<Integer> l = new SmartList<Integer>();
  45. l.add(new Integer(1));
  46. l.add(new Integer(2));
  47. l.add(new Integer(3));
  48. assertEquals(3, l.size());
  49. assertEquals(1, l.get(0).intValue());
  50. assertEquals(2, l.get(1).intValue());
  51. assertEquals(3, l.get(2).intValue());
  52. }
  53. public void testFourElement() {
  54. SmartList<Integer> l = new SmartList<Integer>();
  55. int modCount = 0;
  56. assertEquals(modCount, l.getModificationCount());
  57. l.add(new Integer(1)); assertEquals(++modCount, l.getModificationCount());
  58. l.add(new Integer(2)); assertEquals(++modCount, l.getModificationCount());
  59. l.add(new Integer(3)); assertEquals(++modCount, l.getModificationCount());
  60. l.add(new Integer(4)); assertEquals(++modCount, l.getModificationCount());
  61. assertEquals(4, l.size());
  62. assertEquals(1, l.get(0).intValue());
  63. assertEquals(2, l.get(1).intValue());
  64. assertEquals(3, l.get(2).intValue());
  65. assertEquals(4, l.get(3).intValue());
  66. assertEquals(modCount, l.getModificationCount());
  67. l.remove(2);
  68. assertEquals(3, l.size());
  69. assertEquals(++modCount, l.getModificationCount());
  70. assertEquals("[1, 2, 4]", l.toString());
  71. l.set(2, 3);
  72. assertEquals(3, l.size());
  73. assertEquals(modCount, l.getModificationCount());
  74. assertEquals("[1, 2, 3]", l.toString());
  75. l.clear();
  76. assertEquals(0, l.size());
  77. assertEquals(++modCount, l.getModificationCount());
  78. assertEquals("[]", l.toString());
  79. boolean thrown = false;
  80. try {
  81. l.set(1, 3);
  82. }
  83. catch (IndexOutOfBoundsException e) {
  84. thrown = true;
  85. }
  86. assertTrue("IndexOutOfBoundsException must be thrown", thrown);
  87. l.clear();
  88. assertEquals(0, l.size());
  89. assertEquals(++modCount, l.getModificationCount());
  90. assertEquals("[]", l.toString());
  91. Iterator<Integer> iterator = l.iterator();
  92. assertSame(EmptyIterator.getInstance(), iterator);
  93. assertFalse(iterator.hasNext());
  94. l.add(-2);
  95. iterator = l.iterator();
  96. assertNotSame(EmptyIterator.getInstance(), iterator);
  97. assertTrue(iterator.hasNext());
  98. assertEquals(-2, iterator.next().intValue());
  99. assertFalse(iterator.hasNext());
  100. thrown = false;
  101. try {
  102. l.get(1);
  103. }
  104. catch (IndexOutOfBoundsException e) {
  105. thrown = true;
  106. }
  107. assertTrue("IndexOutOfBoundsException must be thrown", thrown);
  108. l.addAll(l);
  109. assertEquals(2, l.size());
  110. assertEquals("[-2, -2]", l.toString());
  111. thrown = false;
  112. try {
  113. l.addAll(l);
  114. }
  115. catch (ConcurrentModificationException e) {
  116. thrown = true;
  117. }
  118. assertTrue("ConcurrentModificationException must be thrown", thrown);
  119. }
  120. }