您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RollingListTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2013 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 com.dmdirc.util.collections.RollingList;
  24. import org.junit.Test;
  25. import static org.junit.Assert.*;
  26. public class RollingListTest {
  27. @Test
  28. public void testIsEmpty() {
  29. final RollingList<String> rl = new RollingList<String>(1);
  30. assertTrue(rl.isEmpty());
  31. assertTrue(rl.getList().isEmpty());
  32. rl.add("Foo");
  33. assertFalse(rl.isEmpty());
  34. assertFalse(rl.getList().isEmpty());
  35. }
  36. @Test
  37. public void testRolling() {
  38. final RollingList<String> rl = new RollingList<String>(1);
  39. rl.add("Foo");
  40. rl.add("Bar");
  41. rl.add("Baz");
  42. assertEquals("Baz", rl.get(0));
  43. assertEquals(1, rl.getList().size());
  44. assertFalse(rl.contains("Bar"));
  45. }
  46. @Test
  47. public void testClear() {
  48. final RollingList<String> rl = new RollingList<String>(3);
  49. rl.add("Foo");
  50. rl.add("Bar");
  51. rl.add("Baz");
  52. rl.clear();
  53. assertTrue(rl.isEmpty());
  54. assertTrue(rl.getList().isEmpty());
  55. }
  56. @Test
  57. public void testPositions() {
  58. final RollingList<String> rl = new RollingList<String>(3);
  59. rl.add("Foo");
  60. rl.add("Bar");
  61. rl.add("Baz");
  62. assertEquals(0, rl.getPosition());
  63. rl.seekToEnd();
  64. assertEquals(3, rl.getPosition());
  65. rl.seekToStart();
  66. assertEquals(0, rl.getPosition());
  67. }
  68. @Test
  69. public void testSetPosition() {
  70. final RollingList<String> rl = new RollingList<String>(3);
  71. rl.add("Foo");
  72. rl.add("Bar");
  73. rl.add("Baz");
  74. assertEquals(0, rl.getPosition());
  75. rl.setPosition(1);
  76. assertEquals(1, rl.getPosition());
  77. assertEquals("Baz", rl.getNext());
  78. rl.setPosition(0);
  79. assertEquals(0, rl.getPosition());
  80. assertEquals("Bar", rl.getNext());
  81. }
  82. @Test
  83. public void testPrevNext() {
  84. final RollingList<String> rl = new RollingList<String>(3);
  85. rl.add("Foo");
  86. rl.add("Bar");
  87. rl.add("Baz");
  88. assertEquals("Bar", rl.getNext());
  89. assertEquals("Baz", rl.getNext());
  90. assertFalse(rl.hasNext());
  91. assertTrue(rl.hasPrevious());
  92. assertEquals("Bar", rl.getPrevious());
  93. assertEquals("Foo", rl.getPrevious());
  94. assertFalse(rl.hasPrevious());
  95. assertTrue(rl.hasNext());
  96. }
  97. @Test
  98. public void testEmpty() {
  99. final RollingList<String> rl = new RollingList<String>(1, "Meep");
  100. rl.add("Foo");
  101. assertEquals("Meep", rl.getNext());
  102. assertFalse(rl.hasNext());
  103. rl.add("Bar");
  104. // The position moves when adding
  105. assertTrue(rl.hasNext());
  106. assertFalse(rl.hasPrevious());
  107. assertEquals("Meep", rl.getNext());
  108. assertEquals("Bar", rl.getPrevious());
  109. }
  110. }