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

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