summaryrefslogtreecommitdiff
path: root/lib/std/test/fltfmt.myr
blob: 7e844dd7d864be22c8489c73e3b7b5e8b99844f5 (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
use std
use testr
use math

const main = {
	math.fptrap(false)
	testr.run([
		[.name = "put0", .fn = put0],
		[.name = "putNaN", .fn = putNaN],
		[.name = "putInf", .fn = putInf],
		[.name = "putinteger", .fn = putinteger],
		[.name = "putlowprec", .fn = putlowprec],
		[.name = "padding", .fn = padding],
		[.name = "sigfigs", .fn = sigfigs],
	][:])
}

const put0 = {c
	var f : flt32 = 0.0
	var g : flt64 = 0.0
	testr.check(c, std.eq(std.fmt("f is {}", f), "f is 0.0"), "0.0 should format to \"0.0\"")
	testr.check(c, std.eq(std.fmt("g is {}", g), "g is 0.0"), "0.0 should format to \"0.0\"")
}

const putNaN = {c
	var f : flt32 = std.flt32nan()
	var g : flt64 = std.flt64nan()
	var f2 : flt32 = std.flt32frombits(0x7fc00ab0)
	var g2 : flt64 = std.flt64frombits(0x7ff800000000abc0)
	testr.check(c, std.eq(std.fmt("f is {}", f), "f is NaN"), "NaN should format to \"NaN\"")
	testr.check(c, std.eq(std.fmt("g is {}", g), "g is NaN"), "NaN should format to \"NaN\"")
	testr.check(c, std.eq(std.fmt("f2 is {}", f2), "f2 is NaN"), "alt NaN should format to \"NaN\"")
	testr.check(c, std.eq(std.fmt("g2 is {}", g2), "g2 is NaN"), "alt NaN should format to \"NaN\"")
}

const putInf = {c
	var f : flt32 = std.flt32inf()
	var g : flt64 = std.flt64inf()
	testr.check(c, std.eq(std.fmt("f is {}", f), "f is Inf"), "Inf should format to \"Inf\"")
	testr.check(c, std.eq(std.fmt("g is {}", g), "g is Inf"), "Inf should format to \"Inf\"")
	f *= -1.0
	g *= -1.0
	testr.check(c, std.eq(std.fmt("f is {}", f), "f is -Inf"), "-Inf should format to \"-Inf\"")
	testr.check(c, std.eq(std.fmt("g is {}", g), "g is -Inf"), "-Inf should format to \"-Inf\"")
}

const putinteger = {c
	var data : (int, byte[:])[:] = [
		(1, "1.0"), (2, "2.0"), (184, "184.0"), (-37, "-37.0")
	][:]

	for (f, exp) : data
		var f32 : flt32 = (f : flt32)
		var f64 : flt64 = (f : flt64)
		var act32 : byte[:] = std.fmt("{}", f32)
		var act64 : byte[:] = std.fmt("{}", f64)

		testr.check(c, std.eq(act32, exp), "{} should format [flt32] to \"{}\", was \"{}\"", f, exp, act32)
		testr.check(c, std.eq(act64, exp), "{} should format [flt64] to \"{}\", was \"{}\"", f, exp, act64)
	;;
}

const putlowprec = {c
	var data : (flt64, byte[:])[:] = [
		(1.5, "1.5"), (0.0025, "0.0025"), (4.25, "4.25"), (-229.25, "-229.25")
	][:]

	for (f, exp) : data
		var f32 : flt32 = (f : flt32)
		var f64 : flt64 = (f : flt64)
		var act32 : byte[:] = std.fmt("{}", f32)
		var act64 : byte[:] = std.fmt("{}", f64)

		testr.check(c, std.eq(act32, exp), "{} should format [flt32] to \"{}\", was \"{}\"", f, exp, act32)
		testr.check(c, std.eq(act64, exp), "{} should format [flt64] to \"{}\", was \"{}\"", f, exp, act64)
	;;
}

const padding = {c
	var exp, act
	var f32 : flt32
	var f64 : flt64

	f32 = 1.0
	exp = "XXXX1.0"
	act = std.fmt("{w=7,p=X}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f64 = 1.0
	exp = "XXXX1.0"
	act = std.fmt("{w=7,p=X}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f32 = -2.5
	exp = "YYYYYY-2.5"
	act = std.fmt("{w=10,p=Y}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f64 = -100000.5
	exp = "-100000.5"
	act = std.fmt("{w=9,p=Y}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f32 = -13.25
	exp = "-013.25"
	act = std.fmt("{w=7,p=0}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f32 = -1.0 * std.flt32inf()
	exp = "0000000-Inf"
	act = std.fmt("{w=11,p=0}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f64 = std.flt64nan()
	exp = "               NaN"
	act = std.fmt("{w=18,p= }", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)
}

const sigfigs = {c
	var exp, act
	var f32 : flt32
	var f64 : flt64

	f32 = 9.1234
	exp = "9.12"
	act = std.fmt("{s=3}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f64 = 19.1234
	exp = "19.123"
	act = std.fmt("{s=5}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f64 = 104.9
	exp = "100.0"
	act = std.fmt("{s=2}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f64 = 105.1
	exp = "110.0"
	act = std.fmt("{s=2}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f64 = 12345.6789
	exp = "12345.6789"
	act = std.fmt("{s=200}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f64 = 12345.6789
	exp = "12345.679"
	act = std.fmt("{s=8}", f64)
	testr.check(c, std.eq(exp, act), "{} should format [flt64] to \"{}\", was \"{}\"", f64, exp, act)

	f32 = -13.49
	exp = "-13.5"
	act = std.fmt("{s=3}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f32 = -13.53
	exp = "-13.5"
	act = std.fmt("{s=3}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)

	f32 = -13.53
	exp = "-10.0"
	act = std.fmt("{s=-23}", f32)
	testr.check(c, std.eq(exp, act), "{} should format [flt32] to \"{}\", was \"{}\"", f32, exp, act)
}