Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions effekt/shared/src/main/scala/effekt/core/vm/Builtin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ lazy val integers: Builtins = Map(
builtin("effekt::bitwiseXor(Int, Int)") {
case As.Int(x) :: As.Int(y) :: Nil => Value.Int(x ^ y)
},
builtin("effekt::bitwiseNot(Int)") {
case As.Int(x) :: Nil => Value.Int(~x)
},

// Comparison
// ----------
Expand Down
1 change: 1 addition & 0 deletions examples/stdlib/acme.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module acme
import args
import array
import bench
import binstream
import buffer
import bytearray
import char
Expand Down
21 changes: 21 additions & 0 deletions examples/stdlib/binstream.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Binstream
✓ literal hex 10
✓ literal hex ff
✓ literal char a
✓ literal string ba
✓ int back-and-forth (17)
✓ int back-and-forth (17), explicit BE
✓ int back-and-forth (17), explicit LE
✓ byte 00101010
✓ to bits and back
✓ to bits and back LE bitorder
✓ to bits and back BE bitorder
✓ append 0 means *2
✓ pow agrees with double one
✓ LE 2s-complement
✓ BE 2s-complement

15 pass
0 fail
15 tests total

55 changes: 55 additions & 0 deletions examples/stdlib/binstream.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import binstream
import stream
import test

def main() = {
suite("Binstream", false){
test("literal hex 10"){ assert(x"10${()}", 16) }
test("literal hex ff"){ assert(x"ff${()}", 255) }
test("literal char a"){ assert(x"${'a'}", x"61${()}") }
test("literal string ba"){ assert(x"${"ba"}", x"62${()}" * 256 + x"61${()}") }
test("int back-and-forth (17)"){ assert(x"${17}", 17)}
test("int back-and-forth (17), explicit BE"){ assert(x"${17.BE}", 17) }
test("int back-and-forth (17), explicit LE"){ assert(x"${17.LE}", 17 * 256 * 256 * 256) }
test("byte 00101010"){
with on[MissingValue].default{ assert(true, false) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're changing test anyway, could we add a on[MissingValue].assertSome?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea. TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def assertNotThrown[E](ex: on[E]){ body: => Unit / Exception[E] }: Unit / Assertion = {
ex.default{ do assert(false, "Unexpected Exception") }{body}
}
def assertNoThrow[E]{ body: => Unit / Exception[E] }: Unit / Assertion = {
on[E].default{ do assert(false, "Unexpected Exception") }{body}
}
def assertThrown[E](ex: on[E]){ body: => Unit / Exception[E] }: Unit / Assertion = {
do assert(ex.default{ true }{ body(); false }, "Expected Exception, but exited normally")
}
def assertThrows[E]{body: => Unit / Exception[E] }: Unit / Assertion = {
do assert(on[E].default{ true }{ body(); false }, "Expected Exception, but exited normally")
}

WDYT?

assert(first[Byte]{groupBytes{ bit"00101010${()}" }}.toInt, 42)
}
test("to bits and back"){
with on[MissingValue].default{ assert(true, false) }
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
assert(first[Byte]{ groupBytes{ bits(v) } }, v)
}
}
test("to bits and back LE bitorder"){
with on[MissingValue].default{ assert(true, false) }
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
assert(first[Byte]{ groupBytesLE{ bitsLE(v) } }, v)
}
}
test("to bits and back BE bitorder"){
with on[MissingValue].default{ assert(true, false) }
[42.toByte, 12.toByte, 113.toByte, 0.toByte, 255.toByte].foreach{ v =>
assert(first[Byte]{ groupBytesBE{ bitsBE(v) } }, v)
}
}
test("append 0 means *2"){
with on[MissingValue].default{ assert(true, false) }
[42.toByte, 12.toByte, 127.toByte].foreach{ v =>
assert(nth[Byte](1){ groupBytes{ repeat(7){ do emit(B0()) }; bits(v); do emit(B0()) } }, (v.toInt * 2).toByte)
}
}
test("pow agrees with double one"){
assert(pow(2,5), pow(2.0,5).toInt)
}
test("LE 2s-complement"){
with on[MissingValue].default{ assert(true, false) }
assert(first[Byte]{ groupBytesLE{ twoscomplementLE{ bitsLE(6.toByte) } } }, 250.toByte)
}
test("BE 2s-complement"){
with on[MissingValue].default{ assert(true, false) }
assert(first[Byte]{ groupBytesBE{ bit"${-6.Signed.BE.OfWidth(8)}" } }, 250.toByte)
}
}
()
}
Loading