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

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