summaryrefslogtreecommitdiff
path: root/lib/std/test/fmt.myr
blob: be8395293fa2263b8091c6f26c45a7ae1bd5400c (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std

type blah
type blah = struct
	a : byte[:]
	b : int
;;
type u = union
	`First
	`Second int
	`Third byte[:]
;;
type pair = struct
	x : int16
	y : int32
;;

const check = {expected, fmt, args : ...
	var buf : byte[2048]
	var sl, ap

	ap = std.vastart(&args)
	sl = std.bfmtv(buf[:], fmt, &ap)
	if !std.sleq(expected, sl)
		std.fatal("mismatched fmt: got \"{}\", expected \"{}\"\n", sl, expected)
	;;
}

type aligncheck = union
	`Foo
	`Bar int64
;;

const main = {
	builtins()
	installed()
}

const builtins = {
	var s : blah
	var m : u

	/* basic types */
	check("      abcd", "{w=10}", "abcd")
	check("00000bdcae", "{p=0,w=10}", "bdcae")
	check("abcdefghijkl", "{p=0,w=10}", "abcdefghijkl")
	check("a", "{w=0,p=1}", "a")
	check("        10", "{w=10}", 10)
	check("0000000010", "{p=0,w=10}", 10)
	check("4294967295", "{p=0,w=10}", (-1 : uint))
	check("-000000001", "{p=0,w=10}", -1)
	check("xxxxxxxx-1", "{p=x,w=10}", -1)
	check("        -1", "{w=10}", -1)
	check("100000"    , "{w=3}", 100000)
	check("foobarbaz", "{}bar{}", "foo", "baz")
	check("\\n\\r\\xff", "{e}", "\n\r\xff")
	check("{}barbaz", "{{}}bar{}", "baz")
	check("{barbaz}", "{{bar{}}}", "baz")
	check("      Tlön", "{w=10}", "Tlön")
	check("     háček", "{w=10}", "háček")
	check("_____即是多多即", "{w=15,p=_}", "即是多多即")
	check("   τῶν ῾Ελλήνων", "{w=15}", "τῶν ῾Ελλήνων")
	check("abcd", "{}", "abcd")
	check("123", "{}", 123)
	check("7b", "{x}", 123)
	check("0x7b", "0x{x}", 123)
	check("0.0", "{}", 0.0)
	check("0.3", "{}", 0.3)
	check("0.3", "{}", (0.3 : flt32))
	check("1.0", "{}", 1.0)
	check("100.0", "{}", 100.0)
	check("666.91972", "{}", 666.91972)
	check("1.0001", "{}", 1.0001)
	check("0.000101323461002", "{}", 0.000101323461002)
	check("(1, `Bar 123, `Foo)", "{}", (1, `Bar 123, `Foo))

	/* tricky cases: min values for integers */
	check("-128", "{}", (-128 : int8))
	check("-32768", "{}", (-32768 : int16))
	check("-2147483648", "{}", (-2147483648 : int32))
	check("-9223372036854775808", "{}", -9223372036854775808l)

	/* 
	compound types, followed by single value to make
	sure we consume the right byte count.
	*/
	check("(1, 2) true", "{} {}", (1, 2), true)
	check("(1,) true", "{} {}", (1,), true)

	s = [.a="foo true", .b=123]
	/*check("[.a=foo, .b=123] true", "{} {}", s, true) BUSTED */

	m = `First
	check("`First true", "{} {}", m, true)
	m = `Second 123
	check("`Second 123 true", "{} {}", m, true)
	m = `Third "foo"
	check("`Third foo true", "{} {}", m, true)

	check("[1, 2, 3] true", "{} {}", [1,2,3], true)
	check("[1, 2, 3] true", "{} {}", [1,2,3][:], true)
	check("1:2:3 true", "{j=:} {}", [1,2,3][:], true)
	check("1... 2... 3 true", "{j=... } {}", [1,2,3][:], true)
}

const installed = {
	var x : int
	var p : pair

	x = 0
	p = [.x=0, .y=0]
	std.fmtinstall(std.typeof(x), intfmt, [][:])
	std.fmtinstall(std.typeof(p), pairfmt, [
		("x", true),
		("y", false)
	][:])

	/* single value */
	check("formatted an int: 0", "{}", 0)
	check("formatted an int: -10", "{}", -10)

	/* multiple values */
	check("formatted an int: 0, formatted an int: 10", "{}, {}", 0, 10)
	check("formatted an int: -10, formatted an int: 20", "{}, {}", -10, 20)

	/* single value, no options */
	p = [.x=0, .y=0]
	check("formatted a pair: [0, 0]", "{}", p)
	/* single value, option combos */
	p = [.x=-10, .y=-10]
	check("formatted a pair: [-10, -10]", "{}", p)
	check("formatted a pair: [-10, -10] x=foo", "{x=foo}", p)
	check("formatted a pair: [-10, -10] y present", "{y}", p)
	check("formatted a pair: [-10, -10] x=bar y present", "{x=bar,y}", p)
	check("formatted a pair: [-10, -10] x=bar y present", "{x=bar,y}", p)

	/* multiple values */
	check("formatted a pair: [-10, -10], formatted a pair: [-10, -10]", "{}, {}", p, p)
	/* multiple values of different types */
	check("11, formatted a pair: [-10, -10], formatted an int: 111", "{}, {}, {}", (11 : byte), p, 111)

	/* in aggregates */
	check("[formatted a pair: [-10, -10]]", "{}", [p])
	check("[formatted a pair: [-10, -10]]", "{}", [p][:])

}

const intfmt = {sb, ap, opts
	var x : int

	std.assert(opts.len == 0, "options passed where none should be")
	x = std.vanext(ap)
	/* cast to other int type so we don't recurse */
	std.sbfmt(sb, "formatted an int: {}", (x : int32))
}

const pairfmt = {sb, ap, opts
	var x : pair

	x = std.vanext(ap)
	std.sbfmt(sb, "formatted a pair: [{}, {}]", x.x, x.y)
	for opt : opts
		std.sbputc(sb, ' ')
		match opt
		| ("x", val):	std.sbfmt(sb, "x={}", val)
		| ("y", ""):	std.sbfmt(sb, "y present")
		| _:	std.fatal("unknown option")
		;;
	;;
}