diff --git a/FrontEnd/Lib/bytestream.cw b/FrontEnd/Lib/bytestream.cw index 8f0f970f..77e8b32f 100644 --- a/FrontEnd/Lib/bytestream.cw +++ b/FrontEnd/Lib/bytestream.cw @@ -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) diff --git a/FrontEnd/Lib/bytestream_test.cw b/FrontEnd/Lib/bytestream_test.cw index 849d6517..36c57dcb 100644 --- a/FrontEnd/Lib/bytestream_test.cw +++ b/FrontEnd/Lib/bytestream_test.cw @@ -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!) diff --git a/FrontEnd/Lib/test.cw b/FrontEnd/Lib/test.cw index 654f46c8..3a978119 100644 --- a/FrontEnd/Lib/test.cw +++ b/FrontEnd/Lib/test.cw @@ -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)) @@ -93,7 +93,7 @@ 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)) @@ -101,4 +101,10 @@ Both must have derivable types as we use `auto`""" (trap) : )) +@doc "" +(macro @pub AssertUnreachable! STMT_LIST [] [] : + (SysPrint! "AssertUnreachable\n") + (trap) +) + )