Skip to content

Commit 68b108b

Browse files
Move tests to test
1 parent 8fdc5bd commit 68b108b

File tree

3 files changed

+76
-55
lines changed

3 files changed

+76
-55
lines changed

examples/stdlib/binstream.check

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Binstream
2+
✓ literal hex 10
3+
✓ literal hex ff
4+
✓ literal char a
5+
✓ literal string ba
6+
✓ int back-and-forth (17)
7+
✓ int back-and-forth (17), explicit BE
8+
✓ int back-and-forth (17), explicit LE
9+
✓ byte 00101010
10+
✓ to bits and back
11+
✓ to bits and back LE bitorder
12+
✓ to bits and back BE bitorder
13+
✓ append 0 means *2
14+
✓ pow agrees with double one
15+
✓ LE 2s-complement
16+
✓ BE 2s-complement
17+
18+
15 pass
19+
0 fail
20+
15 tests total
21+

examples/stdlib/binstream.effekt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import binstream
2+
import stream
3+
import test
4+
5+
def main() = {
6+
suite("Binstream", false){
7+
test("literal hex 10"){ assertEqual(x"10${()}", 16) }
8+
test("literal hex ff"){ assertEqual(x"ff${()}", 255) }
9+
test("literal char a"){ assertEqual(x"${'a'}", x"61${()}") }
10+
test("literal string ba"){ assertEqual(x"${"ba"}", x"62${()}" * 256 + x"61${()}") }
11+
test("int back-and-forth (17)"){ assertEqual(x"${17}", 17)}
12+
test("int back-and-forth (17), explicit BE"){ assertEqual(x"${17.BE}", 17) }
13+
test("int back-and-forth (17), explicit LE"){ assertEqual(x"${17.LE}", 17 * 256 * 256 * 256) }
14+
test("byte 00101010"){
15+
with on[MissingValue].default{ assertEqual(true, false) }
16+
assertEqual(first[Byte]{groupBytes{ bit"00101010${()}" }}.toInt, 42)
17+
}
18+
test("to bits and back"){
19+
with on[MissingValue].default{ assertEqual(true, false) }
20+
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
21+
assertEqual(first[Byte]{ groupBytes{ bits(v) } }, v)
22+
}
23+
}
24+
test("to bits and back LE bitorder"){
25+
with on[MissingValue].default{ assertEqual(true, false) }
26+
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
27+
assertEqual(first[Byte]{ groupBytesLE{ bitsLE(v) } }, v)
28+
}
29+
}
30+
test("to bits and back BE bitorder"){
31+
with on[MissingValue].default{ assertEqual(true, false) }
32+
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
33+
assertEqual(first[Byte]{ groupBytesBE{ bitsBE(v) } }, v)
34+
}
35+
}
36+
test("append 0 means *2"){
37+
with on[MissingValue].default{ assertEqual(true, false) }
38+
[42.toByte, 12.toByte, 127.toByte].foreach{ v =>
39+
assertEqual(nth[Byte](1){ groupBytes{ repeat(7){ do emit(B0()) }; bits(v); do emit(B0()) } }, (v.toInt * 2).toByte)
40+
}
41+
}
42+
test("pow agrees with double one"){
43+
assertEqual(pow(2,5), pow(2.0,5).toInt)
44+
}
45+
test("LE 2s-complement"){
46+
with on[MissingValue].default{ assertEqual(true, false) }
47+
assertEqual(first[Byte]{ groupBytesLE{ twoscomplementLE{ bitsLE(6.toByte) } } }, 250.toByte)
48+
}
49+
test("BE 2s-complement"){
50+
with on[MissingValue].default{ assertEqual(true, false) }
51+
assertEqual(first[Byte]{ groupBytesBE{ bit"${-6.Signed.BE.OfWidth(8)}" } }, 250.toByte)
52+
}
53+
}
54+
()
55+
}

libraries/common/binstream.effekt

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -346,59 +346,4 @@ def bit{ body: => Unit / { literal, BitSplices } }: Unit / emit[Bit] = {
346346
}.reverse.each
347347
resume(())
348348
}
349-
}
350-
351-
// Simple usage examples
352-
// =====================
353-
namespace examples {
354-
def main() = {
355-
mainSuite("Simple literals"){
356-
test("literal hex 10"){ assertEqual(x"10${()}", 16) }
357-
test("literal hex ff"){ assertEqual(x"ff${()}", 255) }
358-
test("literal char a"){ assertEqual(x"${'a'}", x"61${()}") }
359-
test("literal string ba"){ assertEqual(x"${"ba"}", x"62${()}" * 256 + x"61${()}") }
360-
test("int back-and-forth (17)"){ assertEqual(x"${17}", 17)}
361-
test("int back-and-forth (17), explicit BE"){ assertEqual(x"${17.BE}", 17) }
362-
test("int back-and-forth (17), explicit LE"){ assertEqual(x"${17.LE}", 17 * 256 * 256 * 256) }
363-
test("byte 00101010"){
364-
with on[MissingValue].default{ assertEqual(true, false) }
365-
assertEqual(first[Byte]{groupBytes{ bit"00101010${()}" }}.toInt, 42)
366-
}
367-
test("to bits and back"){
368-
with on[MissingValue].default{ assertEqual(true, false) }
369-
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
370-
assertEqual(first[Byte]{ groupBytes{ bits(v) } }, v)
371-
}
372-
}
373-
test("to bits and back LE bitorder"){
374-
with on[MissingValue].default{ assertEqual(true, false) }
375-
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
376-
assertEqual(first[Byte]{ groupBytesLE{ bitsLE(v) } }, v)
377-
}
378-
}
379-
test("to bits and back BE bitorder"){
380-
with on[MissingValue].default{ assertEqual(true, false) }
381-
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
382-
assertEqual(first[Byte]{ groupBytesBE{ bitsBE(v) } }, v)
383-
}
384-
}
385-
test("append 0 means *2"){
386-
with on[MissingValue].default{ assertEqual(true, false) }
387-
[42.toByte, 12.toByte, 127.toByte].foreach{ v =>
388-
assertEqual(nth[Byte](1){ groupBytes{ repeat(7){ do emit(B0()) }; bits(v); do emit(B0()) } }, (v.toInt * 2).toByte)
389-
}
390-
}
391-
test("pow agrees with double one"){
392-
assertEqual(pow(2,5), pow(2.0,5).toInt)
393-
}
394-
test("LE 2s-complement"){
395-
with on[MissingValue].default{ assertEqual(true, false) }
396-
assertEqual(first[Byte]{ groupBytesLE{ twoscomplementLE{ bitsLE(6.toByte) } } }, 250.toByte)
397-
}
398-
test("BE 2s-complement"){
399-
with on[MissingValue].default{ assertEqual(true, false) }
400-
assertEqual(first[Byte]{ groupBytesBE{ bit"${-6.Signed.BE.OfWidth(8)}" } }, 250.toByte)
401-
}
402-
}
403-
}
404349
}

0 commit comments

Comments
 (0)