Source file
test/convert5.go
1
2
3
4
5
6
7
8
9
10
11
12 package main
13
14 import (
15 "fmt"
16 "runtime"
17 )
18
19
20
21
22
23
24
25
26 func id[T any](x T) T {
27 return x
28 }
29
30
31 func want[T comparable](name string, x, y T) {
32 if x != y {
33 _, _, line, _ := runtime.Caller(1)
34 fmt.Println("FAIL at line", line, "var =", name, "got =", x, "want =", y)
35 }
36 }
37
38
39 func log[T comparable](name string, x T) {
40 fmt.Println(name, x)
41 }
42
43 const (
44
45
46
47
48 p32 = 2147483647
49 n32 = -2147483648
50 u32 = 4294967295
51 p64 = 9223372036854775807
52 n64 = -9223372036854775808
53 u64 = 18446744073709551615
54 t44 = 1 << 44
55 )
56
57 func main() {
58 one := 1.0
59 minus1_32 := id(float32(-1.0))
60 minus1_64 := id(float64(-1.0))
61 p32_plus4k_plus1 := id(float32(p32 + 4096 + 1))
62 p64_plus4k_plus1 := id(float64(p64 + 4096 + 1))
63 n32_minus4k := id(float32(n32 - 4096))
64 n64_minus4k := id(float64(n64 - 4096))
65 inf_32 := id(float32(one / 0))
66 inf_64 := id(float64(one / 0))
67 ninf_32 := id(float32(-one / 0))
68 ninf_64 := id(float64(-one / 0))
69
70
71 int32Tests := []struct {
72 name string
73 input any
74 expected int32
75 }{
76 {"minus1_32", minus1_32, -1},
77 {"minus1_64", minus1_64, -1},
78 {"p32_plus4k_plus1", p32_plus4k_plus1, p32},
79 {"p64_plus4k_plus1", p64_plus4k_plus1, p32},
80 {"n32_minus4k", n32_minus4k, n32},
81 {"n64_minus4k", n64_minus4k, n32},
82 {"inf_32", inf_32, p32},
83 {"inf_64", inf_64, p32},
84 {"ninf_32", ninf_32, n32},
85 {"ninf_64", ninf_64, n32},
86 }
87
88 for _, test := range int32Tests {
89 var converted int32
90 switch v := test.input.(type) {
91 case float32:
92 converted = int32(v)
93 case float64:
94 converted = int32(v)
95 }
96 want(test.name, converted, test.expected)
97 }
98
99
100 int64Tests := []struct {
101 name string
102 input any
103 expected int64
104 }{
105 {"minus1_32", minus1_32, -1},
106 {"minus1_64", minus1_64, -1},
107 {"p32_plus4k_plus1", p32_plus4k_plus1, p32 + 4096 + 1},
108 {"p64_plus4k_plus1", p64_plus4k_plus1, p64},
109 {"n32_minus4k", n32_minus4k, n32 - 4096},
110 {"n64_minus4k", n64_minus4k, n64},
111 {"inf_32", inf_32, p64},
112 {"inf_64", inf_64, p64},
113 {"ninf_32", ninf_32, n64},
114 {"ninf_64", ninf_64, n64},
115 }
116
117 for _, test := range int64Tests {
118 var converted int64
119 switch v := test.input.(type) {
120 case float32:
121 converted = int64(v)
122 case float64:
123 converted = int64(v)
124 }
125 want(test.name, converted, test.expected)
126 }
127
128
129 uint32Tests := []struct {
130 name string
131 input any
132 expected uint32
133 }{
134 {"minus1_32", minus1_32, 0},
135 {"minus1_64", minus1_64, 0},
136 {"p32_plus4k_plus1", p32_plus4k_plus1, p32 + 4096 + 1},
137 {"p64_plus4k_plus1", p64_plus4k_plus1, u32},
138 {"n32_minus4k", n32_minus4k, 0},
139 {"n64_minus4k", n64_minus4k, 0},
140 {"inf_32", inf_32, u32},
141 {"inf_64", inf_64, u32},
142 {"ninf_32", ninf_32, 0},
143 {"ninf_64", ninf_64, 0},
144 }
145
146 for _, test := range uint32Tests {
147 var converted uint32
148 switch v := test.input.(type) {
149 case float32:
150 converted = uint32(v)
151 case float64:
152 converted = uint32(v)
153 }
154 want(test.name, converted, test.expected)
155 }
156
157 u64_plus4k_plus1_64 := id(float64(u64 + 4096 + 1))
158 u64_plust44_plus1_32 := id(float32(u64 + t44 + 1))
159
160
161 uint64Tests := []struct {
162 name string
163 input any
164 expected uint64
165 }{
166 {"minus1_32", minus1_32, 0},
167 {"minus1_64", minus1_64, 0},
168 {"p32_plus4k_plus1", p32_plus4k_plus1, p32 + 4096 + 1},
169 {"p64_plus4k_plus1", p64_plus4k_plus1, p64 + 4096 + 1},
170 {"n32_minus4k", n32_minus4k, 0},
171 {"n64_minus4k", n64_minus4k, 0},
172 {"inf_32", inf_32, u64},
173 {"inf_64", inf_64, u64},
174 {"ninf_32", ninf_32, 0},
175 {"ninf_64", ninf_64, 0},
176 {"u64_plus4k_plus1_64", u64_plus4k_plus1_64, u64},
177 {"u64_plust44_plus1_32", u64_plust44_plus1_32, u64},
178 }
179
180 for _, test := range uint64Tests {
181 var converted uint64
182 switch v := test.input.(type) {
183 case float32:
184 converted = uint64(v)
185 case float64:
186 converted = uint64(v)
187 }
188 want(test.name, converted, test.expected)
189 }
190
191
192
193 fmt.Println("Below this are 'golden' results to check for consistency across platforms. Overflow behavior is not necessarily what we want")
194
195 u8plus2 := id(float64(257))
196 p8minus1 := id(float32(126))
197 n8plus2 := id(float64(-126))
198 n8minusone := id(float32(-129))
199
200 fmt.Println("\nuint8 conversions")
201 uint8Tests := []struct {
202 name string
203 input any
204 }{
205 {"minus1_32", minus1_32},
206 {"minus1_64", minus1_64},
207 {"p32_plus4k_plus1", p32_plus4k_plus1},
208 {"p64_plus4k_plus1", p64_plus4k_plus1},
209 {"n32_minus4k", n32_minus4k},
210 {"n64_minus4k", n64_minus4k},
211 {"inf_32", inf_32},
212 {"inf_64", inf_64},
213 {"ninf_32", ninf_32},
214 {"ninf_64", ninf_64},
215 {"u64_plus4k_plus1_64", u64_plus4k_plus1_64},
216 {"u64_plust44_plus1_32", u64_plust44_plus1_32},
217 {"u8plus2", u8plus2},
218 {"p8minus1", p8minus1},
219 {"n8plus2", n8plus2},
220 {"n8minusone", n8minusone},
221 }
222
223 for _, test := range uint8Tests {
224 var converted uint8
225 switch v := test.input.(type) {
226 case float32:
227 converted = uint8(v)
228 case float64:
229 converted = uint8(v)
230 }
231 log(test.name, converted)
232 }
233
234 fmt.Println("\nint8 conversions")
235 int8Tests := []struct {
236 name string
237 input any
238 }{
239 {"minus1_32", minus1_32},
240 {"minus1_64", minus1_64},
241 {"p32_plus4k_plus1", p32_plus4k_plus1},
242 {"p64_plus4k_plus1", p64_plus4k_plus1},
243 {"n32_minus4k", n32_minus4k},
244 {"n64_minus4k", n64_minus4k},
245 {"inf_32", inf_32},
246 {"inf_64", inf_64},
247 {"ninf_32", ninf_32},
248 {"ninf_64", ninf_64},
249 {"u64_plus4k_plus1_64", u64_plus4k_plus1_64},
250 {"u64_plust44_plus1_32", u64_plust44_plus1_32},
251 {"u8plus2", u8plus2},
252 {"p8minus1", p8minus1},
253 {"n8plus2", n8plus2},
254 {"n8minusone", n8minusone},
255 }
256
257 for _, test := range int8Tests {
258 var converted int8
259 switch v := test.input.(type) {
260 case float32:
261 converted = int8(v)
262 case float64:
263 converted = int8(v)
264 }
265 log(test.name, converted)
266 }
267
268 }
269
View as plain text