Skip to content

Commit b2ad25d

Browse files
committed
Added collection tests
1 parent f0ad176 commit b2ad25d

File tree

2 files changed

+300
-0
lines changed

2 files changed

+300
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import com.lambda.util.collections.LimitedOrderedSet
2+
import kotlin.test.BeforeTest
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertFalse
6+
import kotlin.test.assertTrue
7+
8+
/*
9+
* Copyright 2025 Lambda
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
class LimitedOrderedSetTest {
26+
27+
private lateinit var set: LimitedOrderedSet<String>
28+
29+
@BeforeTest
30+
fun setUp() {
31+
// Initialize the set with a max size of 3
32+
set = LimitedOrderedSet(3)
33+
}
34+
35+
@Test
36+
fun `test adding elements to the set`() {
37+
val added = set.add("Element1")
38+
assertTrue(added)
39+
assertEquals(1, set.size)
40+
}
41+
42+
@Test
43+
fun `test adding more elements than maxSize`() {
44+
set.add("Element1")
45+
set.add("Element2")
46+
set.add("Element3")
47+
48+
// Adding a fourth element when the max size is 3
49+
val added = set.add("Element4")
50+
assertTrue(added)
51+
assertEquals(3, set.size) // The size should not exceed maxSize
52+
53+
// The first element ("Element1") should be removed, as the set is maintaining order
54+
assertFalse(set.contains("Element1"))
55+
assertTrue(set.contains("Element2"))
56+
assertTrue(set.contains("Element3"))
57+
assertTrue(set.contains("Element4"))
58+
}
59+
60+
@Test
61+
fun `test maintaining the order of elements`() {
62+
set.add("Element1")
63+
set.add("Element2")
64+
set.add("Element3")
65+
66+
// Add a fourth element, and the first one should be removed
67+
set.add("Element4")
68+
69+
// The order should now be Element2, Element3, Element4
70+
val expectedOrder = listOf("Element2", "Element3", "Element4")
71+
assertEquals(expectedOrder, set.toList())
72+
}
73+
74+
@Test
75+
fun `test addAll method`() {
76+
// Initially, the set is empty
77+
set.add("Element1")
78+
set.add("Element2")
79+
80+
// Adding multiple elements
81+
val added = set.addAll(listOf("Element3", "Element4"))
82+
83+
assertTrue(added)
84+
assertEquals(3, set.size) // Set should contain up to maxSize elements
85+
assertFalse(set.contains("Element1")) // Element1 should be removed because we are over the max size
86+
assertTrue(set.contains("Element2"))
87+
assertTrue(set.contains("Element3"))
88+
assertTrue(set.contains("Element4"))
89+
}
90+
91+
@Test
92+
fun `test adding more elements than maxSize with addAll`() {
93+
set.addAll(listOf("Element1", "Element2", "Element3"))
94+
95+
// Add more elements, exceeding the max size
96+
val added = set.addAll(listOf("Element4", "Element5"))
97+
98+
assertTrue(added)
99+
assertEquals(3, set.size) // The set should maintain the max size
100+
assertFalse(set.contains("Element1"))
101+
assertFalse(set.contains("Element2"))
102+
assertTrue(set.contains("Element3"))
103+
assertTrue(set.contains("Element4"))
104+
assertTrue(set.contains("Element5"))
105+
}
106+
107+
@Test
108+
fun `test the set size does not exceed maxSize`() {
109+
set.add("Element1")
110+
set.add("Element2")
111+
set.add("Element3")
112+
113+
set.add("Element4") // This should push out "Element1"
114+
assertEquals(3, set.size)
115+
assertFalse(set.contains("Element1"))
116+
assertTrue(set.contains("Element2"))
117+
assertTrue(set.contains("Element3"))
118+
assertTrue(set.contains("Element4"))
119+
}
120+
}

0 commit comments

Comments
 (0)