Skip to content

Commit cc7d9fd

Browse files
committed
Create VarIntIteratorTest.kt
1 parent 80b01e0 commit cc7d9fd

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import com.lambda.util.VarIntIterator
2+
import kotlin.test.Test
3+
import kotlin.test.assertEquals
4+
import kotlin.test.assertFails
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 VarIntIteratorTest {
26+
27+
private lateinit var iterator: VarIntIterator
28+
29+
@Test
30+
fun `test single byte varint`() {
31+
val bytes = byteArrayOf(0b01111111.toByte()) // Maximum single-byte VarInt (127)
32+
iterator = VarIntIterator(bytes)
33+
34+
assertTrue(iterator.hasNext())
35+
assertEquals(127, iterator.next())
36+
assertFalse(iterator.hasNext()) // No more elements after this
37+
}
38+
39+
@Test
40+
fun `test multi-byte varint`() {
41+
val bytes = byteArrayOf(0b10000001.toByte(), 0b00000001.toByte()) // Represents 129
42+
iterator = VarIntIterator(bytes)
43+
44+
assertTrue(iterator.hasNext())
45+
assertEquals(129, iterator.next())
46+
assertFalse(iterator.hasNext()) // No more elements after this
47+
}
48+
49+
@Test
50+
fun `test varint iterator with multiple values`() {
51+
val bytes = byteArrayOf(
52+
0b10000001.toByte(), 0b00000001.toByte(), // 129
53+
0b01111111.toByte(), // 127
54+
0b10000000.toByte(), 0b00000001.toByte() // 128
55+
)
56+
iterator = VarIntIterator(bytes)
57+
58+
assertTrue(iterator.hasNext())
59+
assertEquals(129, iterator.next())
60+
assertTrue(iterator.hasNext())
61+
assertEquals(127, iterator.next())
62+
assertTrue(iterator.hasNext())
63+
assertEquals(128, iterator.next())
64+
assertFalse(iterator.hasNext()) // No more elements after this
65+
}
66+
67+
@Test
68+
fun `test varint iterator with no elements`() {
69+
val bytes = byteArrayOf() // Empty byte array
70+
iterator = VarIntIterator(bytes)
71+
72+
assertFalse(iterator.hasNext()) // There are no elements
73+
assertFails {
74+
iterator.next() // Should throw exception since there are no elements
75+
}
76+
}
77+
78+
@Test
79+
fun `test reading varint with unexpected end of byte array`() {
80+
val bytes = byteArrayOf(0b10000001.toByte()) // Only part of a VarInt
81+
iterator = VarIntIterator(bytes)
82+
83+
assertFails {
84+
iterator.next() // Should throw exception since the VarInt is incomplete
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)