summaryrefslogtreecommitdiff
path: root/lib/bio/test/bio-endianrd.myr
blob: e44545931999b2d1e8e7456e9cf51213fa740313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std
use bio

generic try = {opt : bio.status(@a::(integral,numeric))-> @a::(integral,numeric)
	match opt
	| `bio.Ok val:	-> val
	| _: std.fatal("read failed")
	;;
}

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.Ok bio:	f = bio
	| `std.Err m:	std.fatal("Unable to open data file: {}\n", m)
	;;
	
	/* byte */
	b = 0xaa
	var r = try(bio.getle8(f))
	std.assert(r == b, "le byte broken: {x}\n", r)
	std.assert(try(bio.getbe8(f)) == b, "be byte broken\n")

	/* word */
	w = 0xaabb
	std.assert(try(bio.getle16(f)) == w, "le word broken\n")
	std.assert(try(bio.getbe16(f)) == w, "be word broken\n")

	/* long */
	l = 0xaabbccdd
	std.assert(try(bio.getle32(f)) == l, "le long broken\n")
	std.assert(try(bio.getbe32(f)) == l, "be long broken\n")

	/* quad */
	q = (0x11223344aabbccdd : uint64)
	std.assert(try(bio.getle64(f)) == q, "le quad broken\n")
	std.assert(try(bio.getbe64(f)) == q, "be quad broken\n")

	/* end of file */
	match bio.getle64(f)
	| `bio.Eof:
	| `bio.Err _:
		std.die("error on reading file\n")
	| `bio.Ok v:
		std.die("read past end of file\n")
		v = q /* shut up type inference */
	;;

	bio.close(f);

	std.put("success: all reads matched\n")
}