github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/test/ken/convert.go (about) 1 // run 2 3 // Copyright 2010 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test, near-exhaustive, of converting numbers between types. 8 // No complex numbers though. 9 10 package main 11 12 var i8 int8; 13 var u8 uint8; 14 var i16 int16; 15 var u16 uint16; 16 var i32 int32; 17 var u32 uint32; 18 var i64 int64; 19 var u64 uint64; 20 var f32 float32; 21 var f64 float64; 22 23 type big float64 24 25 type t struct { 26 from, to int 27 val big 28 } 29 30 const ( 31 ti8 = iota+1 32 tu8 33 ti16 34 tu16 35 ti32 36 tu32 37 ti64 38 tu64 39 tf32 40 tf64 41 ) 42 43 var x = []t{ 44 45 /* value good in all types (10) */ 46 { ti8, ti8, 10 }, { ti8, tu8, 10 }, { ti8, ti16, 10 }, { ti8, tu16, 10 }, 47 { ti8, ti32, 10 }, { ti8, tu32, 10 }, { ti8, ti64, 10 }, { ti8, tu64, 10 }, 48 { ti8, tf32, 10 }, { ti8, tf64, 10 }, 49 50 { tu8, ti8, 10 }, { tu8, tu8, 10 }, { tu8, ti16, 10 }, { tu8, tu16, 10 }, 51 { tu8, ti32, 10 }, { tu8, tu32, 10 }, { tu8, ti64, 10 }, { tu8, tu64, 10 }, 52 { tu8, tf32, 10 }, { tu8, tf64, 10 }, 53 54 { ti16, ti8, 10 }, { ti16, tu8, 10 }, { ti16, ti16, 10 }, { ti16, tu16, 10 }, 55 { ti16, ti32, 10 }, { ti16, tu32, 10 }, { ti16, ti64, 10 }, { ti16, tu64, 10 }, 56 { ti16, tf32, 10 }, { ti16, tf64, 10 }, 57 58 { tu16, ti8, 10 }, { tu16, tu8, 10 }, { tu16, ti16, 10 }, { tu16, tu16, 10 }, 59 { tu16, ti32, 10 }, { tu16, tu32, 10 }, { tu16, ti64, 10 }, { tu16, tu64, 10 }, 60 { tu16, tf32, 10 }, { tu16, tf64, 10 }, 61 62 { ti32, ti8, 10 }, { ti32, tu8, 10 }, { ti32, ti16, 10 }, { ti32, tu16, 10 }, 63 { ti32, ti32, 10 }, { ti32, tu32, 10 }, { ti32, ti64, 10 }, { ti32, tu64, 10 }, 64 { ti32, tf32, 10 }, { ti32, tf64, 10 }, 65 66 { tu32, ti8, 10 }, { tu32, tu8, 10 }, { tu32, ti16, 10 }, { tu32, tu16, 10 }, 67 { tu32, ti32, 10 }, { tu32, tu32, 10 }, { tu32, ti64, 10 }, { tu32, tu64, 10 }, 68 { tu32, tf32, 10 }, { tu32, tf64, 10 }, 69 70 { ti64, ti8, 10 }, { ti64, tu8, 10 }, { ti64, ti16, 10 }, { ti64, tu16, 10 }, 71 { ti64, ti32, 10 }, { ti64, tu32, 10 }, { ti64, ti64, 10 }, { ti64, tu64, 10 }, 72 { ti64, tf32, 10 }, { ti64, tf64, 10 }, 73 74 { tu64, ti8, 10 }, { tu64, tu8, 10 }, { tu64, ti16, 10 }, { tu64, tu16, 10 }, 75 { tu64, ti32, 10 }, { tu64, tu32, 10 }, { tu64, ti64, 10 }, { tu64, tu64, 10 }, 76 { tu64, tf32, 10 }, { tu64, tf64, 10 }, 77 78 { tf32, ti8, 10 }, { tf32, tu8, 10 }, { tf32, ti16, 10 }, { tf32, tu16, 10 }, 79 { tf32, ti32, 10 }, { tf32, tu32, 10 }, { tf32, ti64, 10 }, { tf32, tu64, 10 }, 80 { tf32, tf32, 10 }, { tf32, tf64, 10 }, 81 82 { tf64, ti8, 10 }, { tf64, tu8, 10 }, { tf64, ti16, 10 }, { tf64, tu16, 10 }, 83 { tf64, ti32, 10 }, { tf64, tu32, 10 }, { tf64, ti64, 10 }, { tf64, tu64, 10 }, 84 { tf64, tf32, 10 }, { tf64, tf64, 10 }, 85 86 /* value good in all signed types (-4) */ 87 { ti8, ti8, -4 }, { ti8, ti16, -4 }, 88 { ti8, ti32, -4 }, { ti8, ti64, -4 }, 89 { ti8, tf32, -4 }, { ti8, tf64, -4 }, 90 91 { ti16, ti8, -4 }, { ti16, ti16, -4 }, 92 { ti16, ti32, -4 }, { ti16, ti64, -4 }, 93 { ti16, tf32, -4 }, 94 95 { ti32, ti8, -4 }, { ti32, ti16, -4 }, 96 { ti32, ti32, -4 }, { ti32, ti64, -4 }, 97 { ti32, tf32, -4 }, { ti32, tf64, -4 }, 98 99 { ti64, ti8, -4 }, { ti64, ti16, -4 }, 100 { ti64, ti32, -4 }, { ti64, ti64, -4 }, 101 { ti64, tf32, -4 }, 102 103 { tf32, ti8, -4 }, { tf32, ti16, -4 }, 104 { tf32, ti32, -4 }, { tf32, ti64, -4 }, 105 { tf32, tf32, -4 }, 106 107 { tf64, ti8, -4 }, { tf64, ti16, -4 }, 108 { tf64, ti32, -4 }, { tf64, ti64, -4 }, 109 { tf64, tf32, -4 }, { tf64, tf64, -4 }, 110 111 /* value good in u8 and up (175) */ 112 { tu8, tu8, 175 }, { tu8, ti16, 175 }, { tu8, tu16, 175 }, 113 { tu8, ti32, 175 }, { tu8, tu32, 175 }, { tu8, ti64, 175 }, { tu8, tu64, 175 }, 114 { tu8, tf32, 175 }, { tu8, tf64, 175 }, 115 116 { ti16, tu8, 175 }, { ti16, ti16, 175 }, { ti16, tu16, 175 }, 117 { ti16, ti32, 175 }, { ti16, tu32, 175 }, { ti16, ti64, 175 }, { ti16, tu64, 175 }, 118 { ti16, tf32, 175 }, { ti16, tf64, 175 }, 119 120 { tu16, tu8, 175 }, { tu16, ti16, 175 }, { tu16, tu16, 175 }, 121 { tu16, ti32, 175 }, { tu16, tu32, 175 }, { tu16, ti64, 175 }, { tu16, tu64, 175 }, 122 { tu16, tf32, 175 }, { tu16, tf64, 175 }, 123 124 { ti32, tu8, 175 }, { ti32, ti16, 175 }, { ti32, tu16, 175 }, 125 { ti32, ti32, 175 }, { ti32, tu32, 175 }, { ti32, ti64, 175 }, { ti32, tu64, 175 }, 126 { ti32, tf32, 175 }, { ti32, tf64, 175 }, 127 128 { tu32, tu8, 175 }, { tu32, ti16, 175 }, { tu32, tu16, 175 }, 129 { tu32, ti32, 175 }, { tu32, tu32, 175 }, { tu32, ti64, 175 }, { tu32, tu64, 175 }, 130 { tu32, tf32, 175 }, { tu32, tf64, 175 }, 131 132 { ti64, tu8, 175 }, { ti64, ti16, 175 }, { ti64, tu16, 175 }, 133 { ti64, ti32, 175 }, { ti64, tu32, 175 }, { ti64, ti64, 175 }, { ti64, tu64, 175 }, 134 { ti64, tf32, 175 }, { ti64, tf64, 175 }, 135 136 { tu64, tu8, 175 }, { tu64, ti16, 175 }, { tu64, tu16, 175 }, 137 { tu64, ti32, 175 }, { tu64, tu32, 175 }, { tu64, ti64, 175 }, { tu64, tu64, 175 }, 138 { tu64, tf32, 175 }, { tu64, tf64, 175 }, 139 140 { tf32, tu8, 175 }, { tf32, ti16, 175 }, { tf32, tu16, 175 }, 141 { tf32, ti32, 175 }, { tf32, tu32, 175 }, { tf32, ti64, 175 }, { tf32, tu64, 175 }, 142 { tf32, tf32, 175 }, { tf32, tf64, 175 }, 143 144 { tf64, tu8, 175 }, { tf64, ti16, 175 }, { tf64, tu16, 175 }, 145 { tf64, ti32, 175 }, { tf64, tu32, 175 }, { tf64, ti64, 175 }, { tf64, tu64, 175 }, 146 { tf64, tf32, 175 }, { tf64, tf64, 175 }, 147 148 /* value good in u16 and up (41259) */ 149 { tu16, tu16, 41259 }, 150 { tu16, ti32, 41259 }, { tu16, ti64, 41259 }, { tu16, tu64, 41259 }, 151 { tu16, tf32, 41259 }, { tu16, tf64, 41259 }, 152 153 { ti32, tu16, 41259 }, 154 { ti32, ti32, 41259 }, { ti32, tu32, 41259 }, { ti32, ti64, 41259 }, { ti32, tu64, 41259 }, 155 { ti32, tf32, 41259 }, { ti32, tf64, 41259 }, 156 157 { tu32, tu16, 41259 }, 158 { tu32, ti32, 41259 }, { tu32, tu32, 41259 }, { tu32, ti64, 41259 }, { tu32, tu64, 41259 }, 159 { tu32, tf32, 41259 }, { tu32, tf64, 41259 }, 160 161 { ti64, tu16, 41259 }, 162 { ti64, ti32, 41259 }, { ti64, tu32, 41259 }, { ti64, ti64, 41259 }, { ti64, tu64, 41259 }, 163 { ti64, tf32, 41259 }, { ti64, tf64, 41259 }, 164 165 { tu64, tu16, 41259 }, 166 { tu64, ti32, 41259 }, { tu64, tu32, 41259 }, { tu64, ti64, 41259 }, { tu64, tu64, 41259 }, 167 { tu64, tf32, 41259 }, { tu64, tf64, 41259 }, 168 169 { tf32, tu16, 41259 }, 170 { tf32, ti32, 41259 }, { tf32, tu32, 41259 }, { tf32, ti64, 41259 }, { tf32, tu64, 41259 }, 171 { tf32, tf32, 41259 }, { tf32, tf64, 41259 }, 172 173 { tf64, tu16, 41259 }, 174 { tf64, ti32, 41259 }, { tf64, tu32, 41259 }, { tf64, ti64, 41259 }, { tf64, tu64, 41259 }, 175 { tf64, tf32, 41259 }, { tf64, tf64, 41259 }, 176 177 /* value good in u32 and up (3758096384) */ 178 { tu32, tu32, 3758096384 }, { tu32, ti64, 3758096384 }, { tu32, tu64, 3758096384 }, 179 { tu32, tf32, 3758096384 }, { tu32, tf64, 3758096384 }, 180 181 { ti64, tu32, 3758096384 }, { ti64, ti64, 3758096384 }, { ti64, tu64, 3758096384 }, 182 { ti64, tf32, 3758096384 }, { ti64, tf64, 3758096384 }, 183 184 { tu64, tu32, 3758096384 }, { tu64, ti64, 3758096384 }, { tu64, tu64, 3758096384 }, 185 { tu64, tf32, 3758096384 }, { tu64, tf64, 3758096384 }, 186 187 { tf32, tu32, 3758096384 }, { tf32, ti64, 3758096384 }, { tf32, tu64, 3758096384 }, 188 { tf32, tf32, 3758096384 }, { tf32, tf64, 3758096384 }, 189 190 { tf64, tu32, 3758096384 }, { tf64, ti64, 3758096384 }, { tf64, tu64, 3758096384 }, 191 { tf64, tf32, 3758096384 }, { tf64, tf64, 3758096384 }, 192 193 /* value good in u64 and up (16717361816799281152) */ 194 { tu64, tu64, 16717361816799281152 }, 195 { tu64, tf32, 16717361816799281152 }, { tu64, tf64, 16717361816799281152 }, 196 197 { tf32, tu64, 16717361816799281152 }, 198 { tf32, tf32, 16717361816799281152 }, { tf32, tf64, 16717361816799281152 }, 199 200 { tf64, tu64, 16717361816799281152 }, 201 { tf64, tf32, 16717361816799281152 }, { tf64, tf64, 16717361816799281152 }, 202 } 203 204 func main() { 205 for i:=0; i<len(x); i++ { 206 v := x[i].val // input value 207 w := big(0) // output value 208 f := x[i].from // input type 209 t := x[i].to // output type 210 211 i8 = 0; u8 = 0; i16 = 0; u16 = 0 212 i32 = 0; u32 = 0; i64 = 0; u64 = 0 213 f32 = 0; f64 = 0 214 215 switch f*100 + t { 216 default: 217 println("missing case", i, v, f, t) 218 w = v 219 220 case ti8*100 + ti8: 221 i8 = int8(v); i8 = int8(i8); w = big(i8) 222 case ti8*100 + tu8: 223 i8 = int8(v); u8 = uint8(i8); w = big(u8) 224 case ti8*100 + ti16: 225 i8 = int8(v); i16 = int16(i8); w = big(i16) 226 case ti8*100 + tu16: 227 i8 = int8(v); u16 = uint16(i8); w = big(u16) 228 case ti8*100 + ti32: 229 i8 = int8(v); i32 = int32(i8); w = big(i32) 230 case ti8*100 + tu32: 231 i8 = int8(v); u32 = uint32(i8); w = big(u32) 232 case ti8*100 + ti64: 233 i8 = int8(v); i64 = int64(i8); w = big(i64) 234 case ti8*100 + tu64: 235 i8 = int8(v); u64 = uint64(i8); w = big(u64) 236 case ti8*100 + tf32: 237 i8 = int8(v); f32 = float32(i8); w = big(f32) 238 case ti8*100 + tf64: 239 i8 = int8(v); f64 = float64(i8); w = big(f64) 240 241 case tu8*100 + ti8: 242 u8 = uint8(v); i8 = int8(u8); w = big(i8) 243 case tu8*100 + tu8: 244 u8 = uint8(v); u8 = uint8(u8); w = big(u8) 245 case tu8*100 + ti16: 246 u8 = uint8(v); i16 = int16(u8); w = big(i16) 247 case tu8*100 + tu16: 248 u8 = uint8(v); u16 = uint16(u8); w = big(u16) 249 case tu8*100 + ti32: 250 u8 = uint8(v); i32 = int32(u8); w = big(i32) 251 case tu8*100 + tu32: 252 u8 = uint8(v); u32 = uint32(u8); w = big(u32) 253 case tu8*100 + ti64: 254 u8 = uint8(v); i64 = int64(u8); w = big(i64) 255 case tu8*100 + tu64: 256 u8 = uint8(v); u64 = uint64(u8); w = big(u64) 257 case tu8*100 + tf32: 258 u8 = uint8(v); f32 = float32(u8); w = big(f32) 259 case tu8*100 + tf64: 260 u8 = uint8(v); f64 = float64(u8); w = big(f64) 261 262 case ti16*100 + ti8: 263 i16 = int16(v); i8 = int8(i16); w = big(i8) 264 case ti16*100 + tu8: 265 i16 = int16(v); u8 = uint8(i16); w = big(u8) 266 case ti16*100 + ti16: 267 i16 = int16(v); i16 = int16(i16); w = big(i16) 268 case ti16*100 + tu16: 269 i16 = int16(v); u16 = uint16(i16); w = big(u16) 270 case ti16*100 + ti32: 271 i16 = int16(v); i32 = int32(i16); w = big(i32) 272 case ti16*100 + tu32: 273 i16 = int16(v); u32 = uint32(i16); w = big(u32) 274 case ti16*100 + ti64: 275 i16 = int16(v); i64 = int64(i16); w = big(i64) 276 case ti16*100 + tu64: 277 i16 = int16(v); u64 = uint64(i16); w = big(u64) 278 case ti16*100 + tf32: 279 i16 = int16(v); f32 = float32(i16); w = big(f32) 280 case ti16*100 + tf64: 281 i16 = int16(v); f64 = float64(i16); w = big(f64) 282 283 case tu16*100 + ti8: 284 u16 = uint16(v); i8 = int8(u16); w = big(i8) 285 case tu16*100 + tu8: 286 u16 = uint16(v); u8 = uint8(u16); w = big(u8) 287 case tu16*100 + ti16: 288 u16 = uint16(v); i16 = int16(u16); w = big(i16) 289 case tu16*100 + tu16: 290 u16 = uint16(v); u16 = uint16(u16); w = big(u16) 291 case tu16*100 + ti32: 292 u16 = uint16(v); i32 = int32(u16); w = big(i32) 293 case tu16*100 + tu32: 294 u16 = uint16(v); u32 = uint32(u16); w = big(u32) 295 case tu16*100 + ti64: 296 u16 = uint16(v); i64 = int64(u16); w = big(i64) 297 case tu16*100 + tu64: 298 u16 = uint16(v); u64 = uint64(u16); w = big(u64) 299 case tu16*100 + tf32: 300 u16 = uint16(v); f32 = float32(u16); w = big(f32) 301 case tu16*100 + tf64: 302 u16 = uint16(v); f64 = float64(u16); w = big(f64) 303 304 case ti32*100 + ti8: 305 i32 = int32(v); i8 = int8(i32); w = big(i8) 306 case ti32*100 + tu8: 307 i32 = int32(v); u8 = uint8(i32); w = big(u8) 308 case ti32*100 + ti16: 309 i32 = int32(v); i16 = int16(i32); w = big(i16) 310 case ti32*100 + tu16: 311 i32 = int32(v); u16 = uint16(i32); w = big(u16) 312 case ti32*100 + ti32: 313 i32 = int32(v); i32 = int32(i32); w = big(i32) 314 case ti32*100 + tu32: 315 i32 = int32(v); u32 = uint32(i32); w = big(u32) 316 case ti32*100 + ti64: 317 i32 = int32(v); i64 = int64(i32); w = big(i64) 318 case ti32*100 + tu64: 319 i32 = int32(v); u64 = uint64(i32); w = big(u64) 320 case ti32*100 + tf32: 321 i32 = int32(v); f32 = float32(i32); w = big(f32) 322 case ti32*100 + tf64: 323 i32 = int32(v); f64 = float64(i32); w = big(f64) 324 325 case tu32*100 + ti8: 326 u32 = uint32(v); i8 = int8(u32); w = big(i8) 327 case tu32*100 + tu8: 328 u32 = uint32(v); u8 = uint8(u32); w = big(u8) 329 case tu32*100 + ti16: 330 u32 = uint32(v); i16 = int16(u32); w = big(i16) 331 case tu32*100 + tu16: 332 u32 = uint32(v); u16 = uint16(u32); w = big(u16) 333 case tu32*100 + ti32: 334 u32 = uint32(v); i32 = int32(u32); w = big(i32) 335 case tu32*100 + tu32: 336 u32 = uint32(v); u32 = uint32(u32); w = big(u32) 337 case tu32*100 + ti64: 338 u32 = uint32(v); i64 = int64(u32); w = big(i64) 339 case tu32*100 + tu64: 340 u32 = uint32(v); u64 = uint64(u32); w = big(u64) 341 case tu32*100 + tf32: 342 u32 = uint32(v); f32 = float32(u32); w = big(f32) 343 case tu32*100 + tf64: 344 u32 = uint32(v); f64 = float64(u32); w = big(f64) 345 346 case ti64*100 + ti8: 347 i64 = int64(v); i8 = int8(i64); w = big(i8) 348 case ti64*100 + tu8: 349 i64 = int64(v); u8 = uint8(i64); w = big(u8) 350 case ti64*100 + ti16: 351 i64 = int64(v); i16 = int16(i64); w = big(i16) 352 case ti64*100 + tu16: 353 i64 = int64(v); u16 = uint16(i64); w = big(u16) 354 case ti64*100 + ti32: 355 i64 = int64(v); i32 = int32(i64); w = big(i32) 356 case ti64*100 + tu32: 357 i64 = int64(v); u32 = uint32(i64); w = big(u32) 358 case ti64*100 + ti64: 359 i64 = int64(v); i64 = int64(i64); w = big(i64) 360 case ti64*100 + tu64: 361 i64 = int64(v); u64 = uint64(i64); w = big(u64) 362 case ti64*100 + tf32: 363 i64 = int64(v); f32 = float32(i64); w = big(f32) 364 case ti64*100 + tf64: 365 i64 = int64(v); f64 = float64(i64); w = big(f64) 366 367 case tu64*100 + ti8: 368 u64 = uint64(v); i8 = int8(u64); w = big(i8) 369 case tu64*100 + tu8: 370 u64 = uint64(v); u8 = uint8(u64); w = big(u8) 371 case tu64*100 + ti16: 372 u64 = uint64(v); i16 = int16(u64); w = big(i16) 373 case tu64*100 + tu16: 374 u64 = uint64(v); u16 = uint16(u64); w = big(u16) 375 case tu64*100 + ti32: 376 u64 = uint64(v); i32 = int32(u64); w = big(i32) 377 case tu64*100 + tu32: 378 u64 = uint64(v); u32 = uint32(u64); w = big(u32) 379 case tu64*100 + ti64: 380 u64 = uint64(v); i64 = int64(u64); w = big(i64) 381 case tu64*100 + tu64: 382 u64 = uint64(v); u64 = uint64(u64); w = big(u64) 383 case tu64*100 + tf32: 384 u64 = uint64(v); f32 = float32(u64); w = big(f32) 385 case tu64*100 + tf64: 386 u64 = uint64(v); f64 = float64(u64); w = big(f64) 387 388 case tf32*100 + ti8: 389 f32 = float32(v); i8 = int8(f32); w = big(i8) 390 case tf32*100 + tu8: 391 f32 = float32(v); u8 = uint8(f32); w = big(u8) 392 case tf32*100 + ti16: 393 f32 = float32(v); i16 = int16(f32); w = big(i16) 394 case tf32*100 + tu16: 395 f32 = float32(v); u16 = uint16(f32); w = big(u16) 396 case tf32*100 + ti32: 397 f32 = float32(v); i32 = int32(f32); w = big(i32) 398 case tf32*100 + tu32: 399 f32 = float32(v); u32 = uint32(f32); w = big(u32) 400 case tf32*100 + ti64: 401 f32 = float32(v); i64 = int64(f32); w = big(i64) 402 case tf32*100 + tu64: 403 f32 = float32(v); u64 = uint64(f32); w = big(u64) 404 case tf32*100 + tf32: 405 f32 = float32(v); f32 = float32(f32); w = big(f32) 406 case tf32*100 + tf64: 407 f32 = float32(v); f64 = float64(f32); w = big(f64) 408 409 case tf64*100 + ti8: 410 f64 = float64(v); i8 = int8(f64); w = big(i8) 411 case tf64*100 + tu8: 412 f64 = float64(v); u8 = uint8(f64); w = big(u8) 413 case tf64*100 + ti16: 414 f64 = float64(v); i16 = int16(f64); w = big(i16) 415 case tf64*100 + tu16: 416 f64 = float64(v); u16 = uint16(f64); w = big(u16) 417 case tf64*100 + ti32: 418 f64 = float64(v); i32 = int32(f64); w = big(i32) 419 case tf64*100 + tu32: 420 f64 = float64(v); u32 = uint32(f64); w = big(u32) 421 case tf64*100 + ti64: 422 f64 = float64(v); i64 = int64(f64); w = big(i64) 423 case tf64*100 + tu64: 424 f64 = float64(v); u64 = uint64(f64); w = big(u64) 425 case tf64*100 + tf32: 426 f64 = float64(v); f32 = float32(f64); w = big(f32) 427 case tf64*100 + tf64: 428 f64 = float64(v); f64 = float64(f64); w = big(f64) 429 } 430 if v != w { println(i, v, w, f, t) } 431 } 432 }