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.

RollingListTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.collections;
  23. import org.junit.Test;
  24. import static org.junit.Assert.*;
  25. public class RollingListTest {
  26. @Test
  27. public void testIsEmpty() {
  28. final RollingList<String> rl = new RollingList<>(1);
  29. assertTrue(rl.isEmpty());
  30. assertTrue(rl.getList().isEmpty());
  31. rl.add("Foo");
  32. assertFalse(rl.isEmpty());
  33. assertFalse(rl.getList().isEmpty());
  34. }
  35. @Test
  36. public void testRolling() {
  37. final RollingList<String> rl = new RollingList<>(1);
  38. rl.add("Foo");
  39. rl.add("Bar");
  40. rl.add("Baz");
  41. assertEquals("Baz", rl.get(0));
  42. assertEquals(1, rl.getList().size());
  43. assertFalse(rl.contains("Bar"));
  44. }
  45. @Test
  46. public void testClear() {
  47. final RollingList<String> rl = new RollingList<>(3);
  48. rl.add("Foo");
  49. rl.add("Bar");
  50. rl.add("Baz");
  51. rl.clear();
  52. assertTrue(rl.isEmpty());
  53. assertTrue(rl.getList().isEmpty());
  54. }
  55. @Test
  56. public void testPositions() {
  57. final RollingList<String> rl = new RollingList<>(3);
  58. rl.add("Foo");
  59. rl.add("Bar");
  60. rl.add("Baz");
  61. assertEquals(0, rl.getPosition());
  62. rl.seekToEnd();
  63. assertEquals(3, rl.getPosition());
  64. rl.seekToStart();
  65. assertEquals(0, rl.getPosition());
  66. }
  67. @Test
  68. public void testSetPosition() {
  69. final RollingList<String> rl = new RollingList<>(3);
  70. rl.add("Foo");
  71. rl.add("Bar");
  72. rl.add("Baz");
  73. assertEquals(0, rl.getPosition());
  74. rl.setPosition(1);
  75. assertEquals(1, rl.getPosition());
  76. assertEquals("Baz", rl.getNext());
  77. rl.setPosition(0);
  78. assertEquals(0, rl.getPosition());
  79. assertEquals("Bar", rl.getNext());
  80. }
  81. @Test
  82. public void testPrevNext() {
  83. final RollingList<String> rl = new RollingList<>(3);
  84. rl.add("Foo");
  85. rl.add("Bar");
  86. rl.add("Baz");
  87. assertEquals("Bar", rl.getNext());
  88. assertEquals("Baz", rl.getNext());
  89. assertFalse(rl.hasNext());
  90. assertTrue(rl.hasPrevious());
  91. assertEquals("Bar", rl.getPrevious());
  92. assertEquals("Foo", rl.getPrevious());
  93. assertFalse(rl.hasPrevious());
  94. assertTrue(rl.hasNext());
  95. }
  96. @Test
  97. public void testEmpty() {
  98. final RollingList<String> rl = new RollingList<>(1, "Meep");
  99. rl.add("Foo");
  100. assertEquals("Meep", rl.getNext());
  101. assertFalse(rl.hasNext());
  102. rl.add("Bar");
  103. // The position moves when adding
  104. assertTrue(rl.hasNext());
  105. assertFalse(rl.hasPrevious());
  106. assertEquals("Meep", rl.getNext());
  107. assertEquals("Bar", rl.getPrevious());
  108. }
  109. @Test
  110. public void testRemove() {
  111. final RollingList<String> rl = new RollingList<>(2);
  112. rl.add("Foo");
  113. rl.add("Bar");
  114. rl.remove("Bar");
  115. rl.add("Baz");
  116. assertEquals("Foo", rl.get(0));
  117. assertEquals("Baz", rl.get(1));
  118. assertFalse(rl.contains("Bar"));
  119. }
  120. }