summaryrefslogtreecommitdiff
path: root/libstd/test/bytebuf.myr
blob: 48f67153e1491d9db0d80ecae01d3d410861332d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
use "bytebuf.use"

/* support */
use "die.use"
use "fmt.use"
use "sleq.use"

const main = {
	var bb, v

	bb = std.mkbytebuf()

	std.bytebufputc(bb, 'a')
	std.assert(bb.len == 1, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "a"), \
		"byte buf contents not \"a\"\n")

	std.bytebufputs(bb, "bc")
	std.assert(bb.len == 3, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abc"), \
		"byte buf contents not \"abc\"\n")

	std.bytebufputb(bb, 'd' castto(byte))
	std.assert(bb.len == 4, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcd"), \
		"byte buf contents not \"abcd\"\n")

	std.bytebufputle8(bb, 'e' castto(int64))
	std.assert(bb.len == 5, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcde"), \
		"byte buf contents not \"abcde\"\n")

	std.bytebufputbe8(bb, 'e' castto(int64))
	std.assert(bb.len == 6, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdee"), \
		"byte buf contents not \"abcdee\"\n")

	std.bytebufputle16(bb, ('f' | ('g' << 8)) castto(int64))
	std.assert(bb.len == 8, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdeefg"), \
		"byte buf contents not \"abcdeefg\"\n")

	std.bytebufputbe16(bb, ('f' | ('g' << 8)) castto(int64))
	std.assert(bb.len == 10, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggf"), \
		"byte buf contents not \"abcdeefggf\"\n")

	std.bytebufputle32(bb, ('h' | ('i' << 8) | ('j' << 16) | ('k' << 24)) castto(int64))
	std.assert(bb.len == 14, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijk"), \
		"byte buf contents not \"abcdeefggfhijk\"\n")

	std.bytebufputbe32(bb, ('h' | ('i' << 8) | ('j' << 16) | ('k' << 24)) castto(int64))
	std.assert(bb.len == 18, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjih"), \
		"byte buf contents not \"abcdeefggfhijkkji\"\n")

	v = ('l' | ('m' << 8) | ('n' << 16) | ('o' << 24)) castto(int64)
	v |= v << 32
	std.bytebufputle64(bb, v)
	std.assert(bb.len == 26, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjihlmnolmno"), \
		"byte buf contents not \"abcdeefggfhijkkjihlmnolmno\"\n")

	v = ('l' | ('m' << 8) | ('n' << 16) | ('o' << 24)) castto(int64)
	v |= v << 32
	std.bytebufputbe64(bb, v)
	std.assert(bb.len == 34, "byte buf size wrong\n")
	std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjihlmnolmnoonmlonml"), \
		"byte buf contents not \"abcdeefggfhijkkjilmnolmnoonmlonml\"\n")
}