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.

QueuedLinkedHashSetTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 java.util.NoSuchElementException;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import static org.junit.Assert.assertEquals;
  27. import static org.junit.Assert.assertFalse;
  28. import static org.junit.Assert.assertNull;
  29. import static org.junit.Assert.assertTrue;
  30. public class QueuedLinkedHashSetTest {
  31. private QueuedLinkedHashSet<String> set;
  32. @Before
  33. public void setup() {
  34. set = new QueuedLinkedHashSet<>();
  35. }
  36. @Test(expected = NoSuchElementException.class)
  37. public void testElementWhenNewlyCreated() {
  38. set.element();
  39. }
  40. @Test(expected = NoSuchElementException.class)
  41. public void testElementWhenEmpty() {
  42. set.offer("foo");
  43. set.remove();
  44. set.element();
  45. }
  46. @Test
  47. public void testElementWhenNotEmpty() {
  48. set.offer("foo");
  49. assertEquals("foo", set.element());
  50. }
  51. @Test(expected = NoSuchElementException.class)
  52. public void testRemoveWhenNewlyCreated() {
  53. set.remove();
  54. }
  55. @Test(expected = NoSuchElementException.class)
  56. public void testRemoveWhenEmpty() {
  57. set.offer("foo");
  58. set.remove();
  59. set.remove();
  60. }
  61. @Test
  62. public void testOfferMultipleItems() {
  63. assertTrue(set.offer("one"));
  64. assertEquals(1, set.size());
  65. assertFalse(set.offer("one"));
  66. assertEquals(1, set.size());
  67. }
  68. @Test
  69. public void testRemove() {
  70. set.offer("one");
  71. set.offer("two");
  72. assertEquals("two", set.remove());
  73. assertEquals("one", set.remove());
  74. assertTrue(set.isEmpty());
  75. }
  76. @Test
  77. public void testPoll() {
  78. set.offer("one");
  79. set.offer("two");
  80. assertEquals("two", set.poll());
  81. assertEquals("one", set.poll());
  82. assertTrue(set.isEmpty());
  83. }
  84. @Test
  85. public void testPeekWhenNewlyCreated() {
  86. assertNull(set.peek());
  87. }
  88. @Test
  89. public void testPeekWhenEmpty() {
  90. set.offer("foo");
  91. set.remove();
  92. assertNull(set.peek());
  93. }
  94. @Test
  95. public void testPeek() {
  96. set.offer("one");
  97. set.offer("two");
  98. assertEquals("two", set.peek());
  99. assertEquals("two", set.peek());
  100. assertEquals(2, set.size());
  101. }
  102. @Test
  103. public void testOfferAndMoveExisting() {
  104. set.offer("one");
  105. set.offer("two");
  106. set.offerAndMove("one");
  107. assertEquals("one", set.peek());
  108. assertEquals(2, set.size());
  109. }
  110. @Test
  111. public void testOfferAndMoveNew() {
  112. set.offer("one");
  113. set.offer("two");
  114. set.offerAndMove("three");
  115. assertEquals("three", set.peek());
  116. assertEquals(3, set.size());
  117. }
  118. }