ref: 6aa9d480a88e2ff2ba73ad61faa4d29bb3b4ab35
parent: 3717f0fc47461529c56475309dbe6a757aa3a993
author: Ori Bernstein <[email protected]>
date: Wed Dec 18 08:19:15 EST 2013
Add test for endian reading.
--- a/bio.myr
+++ b/bio.myr
@@ -55,7 +55,9 @@
/* formatted i/o */
const put : (f : file#, fmt : byte[:], args : ... -> std.size)
- const fill : (f : file#, sz : std.size -> std.size)
+ /* FIXME: internal decls leaking out for generics. */
+ const fill
+ const ensureread
;;
const Bufsz = 16*std.KiB
@@ -217,8 +219,11 @@
/* reads a single byte from the input stream */
const getb = {f
+ var b
if ensureread(f, 1)
- -> f.rbuf[f.rstart++]
+ b = f.rbuf[f.rstart]
+ f.rstart++
+ -> b
;;
-> -1
}
@@ -305,12 +310,14 @@
*/
generic getle = {f -> @a::(tcnum,tcint,tctest)
var ret
+ var b
var i
ret = 0
ensureread(f, sizeof(@a))
for i = 0; i < sizeof(@a); i++
- ret = ret | ((getb(f) << 8*i) castto(@a::(tcnum,tcint,tctest)))
+ b = getb(f) castto(@a::(tcnum,tcint,tctest))
+ ret = ret | (b << (8*i))
;;
-> ret
}
--- /dev/null
+++ b/test/bio-endianrd.myr
@@ -1,0 +1,45 @@
+use std
+use bio
+
+const main = {
+ var b : byte
+ var w : uint16
+ var l : uint32
+ var q : uint64
+ var f
+
+ /* use the expected write data as read data */
+ match bio.open("data/bio-endianwr-expected", bio.Rd)
+ | `std.Some bio: f = bio
+ | `std.None: std.fatal(1, "Unable to open data file")
+ ;;
+
+ /* byte */
+ /*
+ /* FIXME: compiler bug. multiplication on byte
+ values is currently broken. */
+ b = 0xaa
+ std.assert(bio.getle(f) == b, "le byte broken\n")
+ std.assert(bio.getbe(f) == b, "be byte broken\n")
+ */
+
+ /* word */
+ w = 0xaabb
+ std.assert(bio.getle(f) == w, "le word broken\n")
+ std.assert(bio.getbe(f) == w, "be word broken\n")
+
+ /* long */
+ l = 0xaabbccdd
+ std.assert(bio.getle(f) == l, "le long broken\n")
+ std.assert(bio.getbe(f) == l, "be long broken\n")
+
+ /* quad */
+ q = 0x11223344aabbccdd castto(uint64)
+ std.assert(bio.getle(f) == q, "le quad broken\n")
+ std.assert(bio.getbe(f) == q, "be quad broken\n")
+
+ /* and test for flush on close */
+ bio.close(f);
+
+ std.put("success: all reads matched\n")
+}
--- /dev/null
+++ b/test/data/bio-endianrd-expected
@@ -1,0 +1,1 @@
+success: all reads matched
--- a/test/tests
+++ b/test/tests
@@ -24,7 +24,7 @@
B bio-write F tmpout/test-write
B bio-delim C
B bio-endianwr F tmpout/test-endianwr
-#B bio-endianrd C
+B bio-endianrd C
#B bio-unit C
#B bio-peek C
#B bio-fmt C