|
| 1 | +import com.lambda.util.collections.LimitedDecayQueue |
| 2 | +import java.util.concurrent.TimeUnit |
| 3 | +import kotlin.test.BeforeTest |
| 4 | +import kotlin.test.Test |
| 5 | +import kotlin.test.assertEquals |
| 6 | +import kotlin.test.assertFalse |
| 7 | +import kotlin.test.assertTrue |
| 8 | + |
| 9 | +/* |
| 10 | + * Copyright 2025 Lambda |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation, either version 3 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +class LimitedDecayQueueTest { |
| 27 | + |
| 28 | + private lateinit var queue: LimitedDecayQueue<String> |
| 29 | + private lateinit var onDecayCalled: MutableList<String> |
| 30 | + |
| 31 | + @BeforeTest |
| 32 | + fun setUp() { |
| 33 | + // Initialize the onDecay callback |
| 34 | + onDecayCalled = mutableListOf() |
| 35 | + queue = LimitedDecayQueue(3, 1000) { onDecayCalled.add(it) } // 1 second decay time |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `test add element to queue`() { |
| 40 | + // Add an element |
| 41 | + val result = queue.add("Element1") |
| 42 | + |
| 43 | + assertTrue(result) |
| 44 | + assertEquals(1, queue.size) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `test add element beyond size limit`() { |
| 49 | + queue.add("Element1") |
| 50 | + queue.add("Element2") |
| 51 | + queue.add("Element3") |
| 52 | + |
| 53 | + // Try adding a 4th element when size limit is 3 |
| 54 | + val result = queue.add("Element4") |
| 55 | + |
| 56 | + assertFalse(result) // Should fail to add |
| 57 | + assertEquals(3, queue.size) // Size should remain 3 |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `test elements expire after max age`() { |
| 62 | + queue.add("Element1") |
| 63 | + queue.add("Element2") |
| 64 | + |
| 65 | + // Simulate passage of time (greater than maxAge) |
| 66 | + TimeUnit.MILLISECONDS.sleep(1500) |
| 67 | + |
| 68 | + // Add new element after expiration |
| 69 | + queue.add("Element3") |
| 70 | + |
| 71 | + // Ensure expired elements are removed and "onDecay" callback is triggered |
| 72 | + assertEquals(1, queue.size) |
| 73 | + assertTrue(onDecayCalled.contains("Element1")) |
| 74 | + assertTrue(onDecayCalled.contains("Element2")) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `test add all elements`() { |
| 79 | + queue.add("Element1") |
| 80 | + queue.add("Element2") |
| 81 | + |
| 82 | + val result = queue.addAll(listOf("Element3", "Element4")) |
| 83 | + |
| 84 | + assertTrue(result) |
| 85 | + assertEquals(3, queue.size) // Size limit is 3 |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `test remove element`() { |
| 90 | + queue.add("Element1") |
| 91 | + queue.add("Element2") |
| 92 | + |
| 93 | + val result = queue.remove("Element1") |
| 94 | + |
| 95 | + assertTrue(result) |
| 96 | + assertEquals(1, queue.size) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun `test remove all elements`() { |
| 101 | + queue.add("Element1") |
| 102 | + queue.add("Element2") |
| 103 | + queue.add("Element3") |
| 104 | + |
| 105 | + val result = queue.removeAll(listOf("Element1", "Element2")) |
| 106 | + |
| 107 | + assertTrue(result) |
| 108 | + assertEquals(1, queue.size) // Only "Element3" should remain |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `test retain all elements`() { |
| 113 | + queue.add("Element1") |
| 114 | + queue.add("Element2") |
| 115 | + queue.add("Element3") |
| 116 | + |
| 117 | + val result = queue.retainAll(listOf("Element2", "Element3")) |
| 118 | + |
| 119 | + assertTrue(result) |
| 120 | + assertEquals(2, queue.size) // Only "Element2" and "Element3" should remain |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun `test clear the queue`() { |
| 125 | + queue.add("Element1") |
| 126 | + queue.add("Element2") |
| 127 | + queue.clear() |
| 128 | + |
| 129 | + assertEquals(0, queue.size) // Queue should be empty |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun `test set max size`() { |
| 134 | + queue.add("Element1") |
| 135 | + queue.add("Element2") |
| 136 | + queue.add("Element3") |
| 137 | + |
| 138 | + queue.setMaxSize(2) // Reduce size limit to 2 |
| 139 | + |
| 140 | + queue.add("Element4") |
| 141 | + |
| 142 | + // Verify that the queue is limited to 2 elements, and oldest element is removed |
| 143 | + assertEquals(2, queue.size) |
| 144 | + assertTrue(onDecayCalled.contains("Element1")) // "Element1" should be decayed |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun `test set decay time`() { |
| 149 | + queue.add("Element1") |
| 150 | + queue.add("Element2") |
| 151 | + |
| 152 | + queue.setDecayTime(500) // Set a shorter decay time of 500 ms |
| 153 | + |
| 154 | + // Simulate passage of time (greater than decay time) |
| 155 | + TimeUnit.MILLISECONDS.sleep(600) |
| 156 | + |
| 157 | + queue.add("Element3") |
| 158 | + |
| 159 | + // Ensure expired elements are removed and "onDecay" callback is triggered |
| 160 | + assertEquals(1, queue.size) |
| 161 | + assertTrue(onDecayCalled.contains("Element1")) |
| 162 | + assertTrue(onDecayCalled.contains("Element2")) |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + fun `test clean up function when iterating`() { |
| 167 | + queue.add("Element1") |
| 168 | + queue.add("Element2") |
| 169 | + |
| 170 | + // Simulate some delay to allow elements to decay |
| 171 | + TimeUnit.MILLISECONDS.sleep(1500) |
| 172 | + |
| 173 | + queue.add("Element3") |
| 174 | + |
| 175 | + // Iterator should only return "Element3" because the others are expired |
| 176 | + val elements = queue.toList() |
| 177 | + assertEquals(1, elements.size) |
| 178 | + assertEquals("Element3", elements[0]) |
| 179 | + } |
| 180 | +} |
0 commit comments