Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

QueuedLinkedHashSetTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(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. }