Skip to content

Commit 98fbdcb

Browse files
committed
More Tests
1 parent 6c9f7ab commit 98fbdcb

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

Codex/src/main/java/org/operatorfoundation/codex/symbols/Trinary.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Trinary : Symbol {
1717
override fun toString(): String = "Trinary"
1818

1919
override fun decode(encodedValue: ByteArray): BigInteger {
20-
return charToValue[encodedValue.toString()]
20+
return charToValue[encodedValue.decodeToString()]
2121
?: throw IllegalArgumentException("Trinary, bad value $encodedValue")
2222
}
2323

Codex/src/test/java/org/operatorfoundation/codex/EncoderDecoderTest.kt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.operatorfoundation.codex
22

33
import org.junit.jupiter.api.Test
44
import org.junit.jupiter.api.Assertions.*
5+
import org.operatorfoundation.codex.symbols.Binary
56
import org.operatorfoundation.codex.symbols.Symbol
67
import org.operatorfoundation.codex.symbols.Number
78
import org.operatorfoundation.codex.symbols.Byte
@@ -10,6 +11,7 @@ import org.operatorfoundation.codex.symbols.CallLetterNumber
1011
import org.operatorfoundation.codex.symbols.CallLetterSpace
1112
import org.operatorfoundation.codex.symbols.GridLetter
1213
import org.operatorfoundation.codex.symbols.Power
14+
import org.operatorfoundation.codex.symbols.Trinary
1315

1416
import java.math.BigInteger
1517

@@ -96,4 +98,81 @@ class EncoderDecoderTest
9698
assertEquals("Test", finalString)
9799
}
98100

101+
@Test
102+
fun testBinaryTrinaryWithRequired()
103+
{
104+
// Test with Required('A'), Binary(), Trinary()
105+
val encoder = Encoder(listOf(Required('A'.code.toByte()), Binary(), Trinary()))
106+
107+
val testCases = listOf(
108+
0 to listOf("A", "0", "0"),
109+
1 to listOf("A", "0", "1"),
110+
2 to listOf("A", "0", "2"),
111+
3 to listOf("A", "1", "0"),
112+
4 to listOf("A", "1", "1"),
113+
5 to listOf("A", "1", "2")
114+
)
115+
116+
testCases.forEach { (input, expected) ->
117+
val encoding = encoder.encode(BigInteger.valueOf(input.toLong()))
118+
val actual = encoding.map { it.decodeToString() }
119+
120+
println("TEST RESULT input: $input, encoding: $actual")
121+
122+
assertEquals(expected, actual)
123+
}
124+
}
125+
126+
@Test
127+
fun testBinaryTrinaryWithoutRequired()
128+
{
129+
// Test with just Binary(), Trinary()
130+
val encoder = Encoder(listOf(Binary(), Trinary()))
131+
132+
val testCases = listOf(
133+
0 to listOf("0", "0"),
134+
1 to listOf("0", "1"),
135+
2 to listOf("0", "2"),
136+
3 to listOf("1", "0"),
137+
4 to listOf("1", "1"),
138+
5 to listOf("1", "2")
139+
)
140+
141+
testCases.forEach { (input, expected) ->
142+
val encoding = encoder.encode(BigInteger.valueOf(input.toLong()))
143+
val actual = encoding.map { it.decodeToString() }
144+
145+
println("TEST RESULT input: $input, encoding: $actual")
146+
147+
assertEquals(expected, actual)
148+
}
149+
}
150+
151+
@Test
152+
fun testRoundTrip()
153+
{
154+
// Test that encoding then decoding returns the original value
155+
val symbols = listOf(
156+
CallLetterNumber(),
157+
Binary(),
158+
Trinary(),
159+
Number(),
160+
CallLetterSpace()
161+
)
162+
163+
val encoder = Encoder(symbols)
164+
val decoder = encoder.decoder()
165+
166+
// Test various values
167+
val testValues = listOf(0, 1, 42, 100, 500, 1000, 5000)
168+
169+
testValues.forEach { value ->
170+
val bigIntValue = BigInteger.valueOf(value.toLong())
171+
val encoded = encoder.encode(bigIntValue)
172+
val decoded = decoder.decode(encoded)
173+
174+
assertEquals(bigIntValue, decoded, "Round-trip failed for value $value")
175+
}
176+
}
177+
99178
}

0 commit comments

Comments
 (0)