Skip to content

Commit

Permalink
improve bytestream lib
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Jan 5, 2024
1 parent 7b9a8f6 commit 326ef8a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
4 changes: 4 additions & 0 deletions FrontEnd/Lib/bytestream.cw
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
(fun @pub FrontSlice [
(param buffer (ptr @mut (slice u8))) (param n uint)]
(union [(slice u8) OutOfBoundsError]) :
(if (<= (len (^ buffer)) n) :
(return OutOfBoundsErrorVal)
:)

(let out (slice u8) (slice_val (front (^ buffer)) n))
(stmt (IncSliceUnchecked [buffer n]))
(return out)
Expand Down
64 changes: 60 additions & 4 deletions FrontEnd/Lib/bytestream_test.cw
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,76 @@
(import test)
(import bytestream)

(fun test_all [] void :
@doc "(test::AssertSliceEq! )"

(fun test_bs_or_die [] void :
(let @mut data (array 23 u8) "\x22\x33\x44\x55\x66\x77\x88abcdefghijklmnop")
(let @mut @ref stream (slice u8) data)
(test::AssertEq! 0x22_u8
(test::AssertEq! 0x22_u8
(bytestream::FrontLeU8OrDie [(& @mut stream)]))
(test::AssertEq! 0x4433_u16
(bytestream::FrontLeU16OrDie [(& @mut stream)]))
(test::AssertEq! 0x88776655_u32
(bytestream::FrontLeU32OrDie [(& @mut stream)]))

(test::AssertSliceEq!
(bytestream::FrontSliceOrDie [(& @mut stream) 10])
"abcdefghij")
(test::AssertSliceEq!
(bytestream::FrontSliceOrDie [(& @mut stream) 1])
"k")
(let empty_slice (slice u8))
(test::AssertSliceEq!
(bytestream::FrontSliceOrDie [(& @mut stream) 0])
empty_slice)
)

(fun test_bs [] void :
(let @mut data (array 23 u8) "\x22\x33\x44\x55\x66\x77\x88abcdefghijklmnop")
(let @mut @ref stream (slice u8) data)
(test::AssertEq! 0x22_u8
(bytestream::FrontLeU8 [(& @mut stream)]))
(test::AssertEq! 0x4433_u16
(bytestream::FrontLeU16 [(& @mut stream)]))
(test::AssertEq! 0x88776655_u32
(bytestream::FrontLeU32 [(& @mut stream)]))

(let raw1 auto (bytestream::FrontSlice [(& @mut stream) 10]))
(let dummy1 auto (typeid (slice u8)))
(let dummy2 auto (typeid bytestream::OutOfBoundsError))

(try result1 (slice u8) raw1 err :
(test::AssertUnreachable!)
)
(test::AssertSliceEq! result1 "abcdefghij")


(let raw2 auto (bytestream::FrontSlice [(& @mut stream) 1000]))
(try result2 bytestream::OutOfBoundsError raw2 err :
(test::AssertUnreachable!)
)

(let raw3 auto (bytestream::FrontSlice [(& @mut stream) 1]))
(try result3 (slice u8) raw3 err :
(test::AssertUnreachable!)
)
(test::AssertSliceEq! result3 "k")

(let raw4 auto (bytestream::FrontSlice [(& @mut stream) 1000]))
(try result4 bytestream::OutOfBoundsError raw4 err :
(test::AssertUnreachable!)
)

(let empty_slice (slice u8))
(let raw5 auto (bytestream::FrontSlice [(& @mut stream) 0]))
(try result5 (slice u8) raw5 err :
(test::AssertUnreachable!)
)
(test::AssertSliceEq! result5 empty_slice)
)

(fun @cdecl main [(param argc s32) (param argv (ptr (ptr u8)))] s32 :
(stmt (test_all []))
(stmt (test_bs_or_die []))
(stmt (test_bs []))

@doc "test end"
(test::Success!)
Expand Down
10 changes: 8 additions & 2 deletions FrontEnd/Lib/test.cw
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Both must have derivable types as we use `auto`"""
$epsilon)

@doc ""
(macro @pub AssertTrue! STMT_LIST [(mparam $e_expr EXPR)] [$e_val $a_val] :
(macro @pub AssertTrue! STMT_LIST [(mparam $e_expr EXPR)] [] :
(if $e_expr : :
(SysPrint! "AssertTrue failed: ")
(SysPrint! (stringify $e_expr))
Expand All @@ -93,12 +93,18 @@ Both must have derivable types as we use `auto`"""
))

@doc ""
(macro @pub AssertFalse! STMT_LIST [(mparam $e_expr EXPR)] [$e_val $a_val] :
(macro @pub AssertFalse! STMT_LIST [(mparam $e_expr EXPR)] [] :
(if $e_expr :
(SysPrint! "AssertFalse failed: ")
(SysPrint! (stringify $e_expr))
(SysPrint! "\n")
(trap)
: ))

@doc ""
(macro @pub AssertUnreachable! STMT_LIST [] [] :
(SysPrint! "AssertUnreachable\n")
(trap)
)

)

0 comments on commit 326ef8a

Please sign in to comment.