├── go.mod ├── LICENSE ├── README.md ├── conv.go └── conv_test.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tidwall/conv 2 | 3 | go 1.19 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2023 Josh Baker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # conv 2 | 3 | [![GoDoc](https://godoc.org/github.com/tidwall/conv?status.svg)](https://godoc.org/github.com/tidwall/conv) 4 | 5 | A Go package for converting various primitive types. 6 | 7 | Designed to follow [ECMA-262](https://en.wikipedia.org/wiki/ECMAScript), 8 | therefore it differs slightly from how the built in `strconv` package works. 9 | 10 | Please see the [docs](https://godoc.org/github.com/tidwall/conv) for more details. 11 | 12 | ## Functions 13 | 14 | ```go 15 | Ttoi(t bool) int64 // bool -> int64 16 | Ttou(t bool) uint64 // bool -> uint64 17 | Ttof(t bool) float64 // bool -> float64 18 | Ttoa(t bool) string // bool -> string 19 | Ttov(t bool) any // bool -> any 20 | 21 | Ftot(f float64) bool // float64 -> bool 22 | Ftoi(f float64) int64 // float64 -> int64 23 | Ftou(f float64) uint64 // float64 -> uint64 24 | Ftoa(f float64) string // float64 -> string 25 | Ftov(f float64) any // float64 -> any 26 | 27 | Itot(i int64) bool // int64 -> bool 28 | Itof(i int64) float64 // int64 -> float64 29 | Itou(i int64) uint64 // int64 -> uint64 30 | Itoa(i int64) string // int64 -> string 31 | Itov(i int64) any // int64 -> any 32 | 33 | Utot(u uint64) bool // uint64 -> bool 34 | Utof(u uint64) float64 // uint64 -> float64 35 | Utoi(u uint64) int64 // uint64 -> int64 36 | Utoa(u uint64) string // uint64 -> string 37 | Utov(u uint64) any // uint64 -> any 38 | 39 | Atot(a string) bool // string -> bool 40 | Atof(a string) float64 // string -> float64 41 | Atoi(a string) int64 // string -> int64 42 | Atou(a string) uint64 // string -> uint64 43 | Atov(a string) any // string -> any 44 | 45 | Vtot(v any) bool // any -> bool 46 | Vtof(v any) float64 // any -> float64 47 | Vtoi(v any) int64 // any -> int64 48 | Vtou(v any) uint64 // any -> uint64 49 | Vtoa(v any) string // any -> string 50 | ``` 51 | -------------------------------------------------------------------------------- /conv.go: -------------------------------------------------------------------------------- 1 | package conv 2 | 3 | import ( 4 | "math" 5 | "strconv" 6 | ) 7 | 8 | type ( 9 | booler interface{ Bool() bool } 10 | floater interface{ Float64() float64 } 11 | inter interface{ Int64() int64 } 12 | uinter interface{ Uint64() uint64 } 13 | stringer interface{ String() string } 14 | ) 15 | 16 | /////////////////////////////////////////// 17 | // Bool 18 | /////////////////////////////////////////// 19 | 20 | // Ttoi converts bool to int64 21 | func Ttoi(t bool) int64 { 22 | if t { 23 | return 1 24 | } 25 | return 0 26 | } 27 | 28 | // Ttou converts bool to uint64 29 | func Ttou(t bool) uint64 { 30 | if t { 31 | return 1 32 | } 33 | return 0 34 | } 35 | 36 | // Ttof converts bool to float64 37 | func Ttof(t bool) float64 { 38 | if t { 39 | return 1 40 | } 41 | return 0 42 | } 43 | 44 | // Ttoa converts bool to string 45 | func Ttoa(t bool) string { 46 | if t { 47 | return "true" 48 | } 49 | return "false" 50 | } 51 | 52 | // Ttov converts bool to any 53 | func Ttov(t bool) any { 54 | return t 55 | } 56 | 57 | /////////////////////////////////////////// 58 | // Float64 59 | /////////////////////////////////////////// 60 | 61 | const maxUint64 = uint64(18446744073709551615) 62 | const minInt64 = int64(-9223372036854775808) 63 | const maxInt64 = int64(9223372036854775807) 64 | 65 | var maxUint64Float = math.Nextafter(math.MaxUint64, 0) 66 | var maxInt64Float = math.Nextafter(math.MaxInt64, 0) 67 | var minInt64Float = math.Nextafter(math.MinInt64, 1) 68 | 69 | // Ftot converts float64 to bool 70 | func Ftot(f float64) bool { 71 | return f < 0 || f > 0 72 | } 73 | 74 | // Ftoi converts float64 to int64 75 | func Ftoi(f float64) int64 { 76 | if math.IsNaN(f) { 77 | return 0 78 | } 79 | if f < -9007199254740991 || f > 9007199254740991 { 80 | // The number is outside of the range for correct binary 81 | // representation of floating point as an integer value. 82 | // https://tc39.es/ecma262/#sec-number.min_safe_integer 83 | // https://tc39.es/ecma262/#sec-number.max_safe_integer 84 | if f < 0 { 85 | f = math.Ceil(f) 86 | if f < minInt64Float { 87 | return minInt64 88 | } 89 | } else { 90 | f = math.Floor(f) 91 | if f > maxInt64Float { 92 | return maxInt64 93 | } 94 | } 95 | } 96 | return int64(f) 97 | } 98 | 99 | // Ftou converts float64 to uint64 100 | func Ftou(f float64) uint64 { 101 | if math.IsNaN(f) { 102 | return 0 103 | } 104 | if f < 0 { 105 | return 0 106 | } 107 | if f > 9007199254740991 { 108 | // Outside range: See Ftoi for description. 109 | f = math.Floor(f) 110 | if f > maxUint64Float { 111 | return maxUint64 112 | } 113 | } 114 | return uint64(f) 115 | } 116 | 117 | // Ftoa converts float64 to string 118 | // Returns 'Infinity' or '-Infinity', not 'Inf' or '-Inf', for infinite numbers. 119 | // Returns 'NaN' for not-a-number. 120 | func Ftoa(f float64) string { 121 | switch { 122 | case math.IsNaN(f): 123 | return "NaN" 124 | case math.IsInf(f, +1): 125 | return "Infinity" 126 | case math.IsInf(f, -1): 127 | return "-Infinity" 128 | default: 129 | return strconv.FormatFloat(f, 'f', -1, 64) 130 | } 131 | } 132 | 133 | // Ftov converts float64 to any 134 | func Ftov(f float64) any { 135 | return f 136 | } 137 | 138 | /////////////////////////////////////////// 139 | // Int64 140 | /////////////////////////////////////////// 141 | 142 | // Itot converts int64 to bool 143 | func Itot(i int64) bool { 144 | return i != 0 145 | } 146 | 147 | // Itof converts int64 to float64 148 | func Itof(i int64) float64 { 149 | return float64(i) 150 | } 151 | 152 | // Itou converts int64 to uint64 153 | func Itou(i int64) uint64 { 154 | if i < 0 { 155 | return 0 156 | } 157 | return uint64(i) 158 | } 159 | 160 | // Itoa converts int64 to string 161 | func Itoa(i int64) string { 162 | return strconv.FormatInt(i, 10) 163 | } 164 | 165 | // Itov converts int64 to any 166 | func Itov(i int64) any { 167 | return i 168 | } 169 | 170 | /////////////////////////////////////////// 171 | // Uint64 172 | /////////////////////////////////////////// 173 | 174 | // Itot converts uint64 to bool 175 | func Utot(u uint64) bool { 176 | return u != 0 177 | } 178 | 179 | // Utof converts uint64 to float64 180 | func Utof(u uint64) float64 { 181 | return float64(u) 182 | } 183 | 184 | // Utoi converts uint64 to int64 185 | func Utoi(u uint64) int64 { 186 | if u > math.MaxInt64 { 187 | return math.MaxInt64 188 | } 189 | return int64(u) 190 | } 191 | 192 | // Utoa converts uint64 to string 193 | func Utoa(u uint64) string { 194 | return strconv.FormatUint(u, 10) 195 | } 196 | 197 | // Utov converts uint64 to any 198 | func Utov(u uint64) any { 199 | return u 200 | } 201 | 202 | /////////////////////////////////////////// 203 | // String 204 | /////////////////////////////////////////// 205 | 206 | func isnumch(c byte) bool { 207 | return (c >= '0' && c <= '9') || c == '.' 208 | } 209 | func parseFloat(a string) (float64, error) { 210 | if a == "" { 211 | return 0, strconv.ErrSyntax 212 | } 213 | if len(a) == 1 || isnumch(a[0]) || (a[0] == '-' && isnumch(a[1])) || 214 | (a[0] == '+' && isnumch(a[1])) { 215 | return strconv.ParseFloat(a, 64) 216 | } 217 | switch a { 218 | case "+Infinity", "Infinity": 219 | return math.Inf(+1), nil 220 | case "-Infinity": 221 | return math.Inf(-1), nil 222 | case "NaN": 223 | return math.NaN(), nil 224 | default: 225 | return 0, strconv.ErrSyntax 226 | } 227 | } 228 | 229 | // Atot converts string to bool 230 | // Always returns true unless string is empty. 231 | func Atot(a string) bool { 232 | return a != "" 233 | } 234 | 235 | // Atof converts string to float64 236 | // For infinte numbers use 'Infinity' or '-Infinity', not 'Inf' or '-Inf'. 237 | // Returns NaN for invalid syntax 238 | func Atof(a string) float64 { 239 | f, err := parseFloat(a) 240 | if err != nil { 241 | return math.NaN() 242 | } 243 | return f 244 | } 245 | 246 | // Atoi converts string to int64 247 | // Returns 0 for invalid syntax 248 | func Atoi(a string) int64 { 249 | x, err := strconv.ParseInt(a, 10, 64) 250 | if err == nil { 251 | return x 252 | } 253 | f, err := parseFloat(a) 254 | if err == nil { 255 | return Ftoi(f) 256 | } 257 | return 0 258 | } 259 | 260 | // Atou converts string to uint64 261 | // Returns 0 for invalid syntax 262 | func Atou(a string) uint64 { 263 | x, err := strconv.ParseUint(a, 10, 64) 264 | if err == nil { 265 | return x 266 | } 267 | f, err := parseFloat(a) 268 | if err == nil { 269 | return Ftou(f) 270 | } 271 | return 0 272 | } 273 | 274 | // Atov converts string to any 275 | func Atov(a string) any { 276 | return a 277 | } 278 | 279 | /////////////////////////////////////////// 280 | // Any 281 | /////////////////////////////////////////// 282 | 283 | // Vtot converts any to bool 284 | func Vtot(v any) bool { 285 | switch v := v.(type) { 286 | case bool: 287 | return v 288 | case int: 289 | return Itot(int64(v)) 290 | case int8: 291 | return Itot(int64(v)) 292 | case int16: 293 | return Itot(int64(v)) 294 | case int32: 295 | return Itot(int64(v)) 296 | case int64: 297 | return Itot(v) 298 | case uint: 299 | return Utot(uint64(v)) 300 | case uint8: 301 | return Utot(uint64(v)) 302 | case uint16: 303 | return Utot(uint64(v)) 304 | case uint32: 305 | return Utot(uint64(v)) 306 | case uint64: 307 | return Utot(v) 308 | case float64: 309 | return Ftot(v) 310 | case float32: 311 | return Ftot(float64(v)) 312 | case string: 313 | return Atot(v) 314 | default: 315 | // order matters (bool,int,uint,float,string) 316 | switch v := v.(type) { 317 | case booler: 318 | return v.Bool() 319 | case inter: 320 | return Itot(v.Int64()) 321 | case uinter: 322 | return Utot(v.Uint64()) 323 | case stringer: 324 | return Atot(v.String()) 325 | case floater: 326 | return Ftot(v.Float64()) 327 | default: 328 | return false 329 | } 330 | } 331 | } 332 | 333 | // Vtof converts any to float64 334 | func Vtof(v any) float64 { 335 | switch v := v.(type) { 336 | case bool: 337 | return Ttof(v) 338 | case int: 339 | return Itof(int64(v)) 340 | case int8: 341 | return Itof(int64(v)) 342 | case int16: 343 | return Itof(int64(v)) 344 | case int32: 345 | return Itof(int64(v)) 346 | case int64: 347 | return Itof(v) 348 | case uint: 349 | return Utof(uint64(v)) 350 | case uint8: 351 | return Utof(uint64(v)) 352 | case uint16: 353 | return Utof(uint64(v)) 354 | case uint32: 355 | return Utof(uint64(v)) 356 | case uint64: 357 | return Utof(v) 358 | case float64: 359 | return v 360 | case float32: 361 | return float64(v) 362 | case string: 363 | return Atof(v) 364 | default: 365 | // order matters (float,int,uint,string,bool) 366 | switch v := v.(type) { 367 | case floater: 368 | return v.Float64() 369 | case inter: 370 | return Itof(v.Int64()) 371 | case uinter: 372 | return Utof(v.Uint64()) 373 | case stringer: 374 | return Atof(v.String()) 375 | case booler: 376 | return Ttof(v.Bool()) 377 | default: 378 | return math.NaN() 379 | } 380 | } 381 | } 382 | 383 | // Vtoi converts any to int64 384 | func Vtoi(v any) int64 { 385 | switch v := v.(type) { 386 | case bool: 387 | return Ttoi(v) 388 | case int: 389 | return int64(v) 390 | case int8: 391 | return int64(v) 392 | case int16: 393 | return int64(v) 394 | case int32: 395 | return int64(v) 396 | case int64: 397 | return v 398 | case uint: 399 | return Utoi(uint64(v)) 400 | case uint8: 401 | return Utoi(uint64(v)) 402 | case uint16: 403 | return Utoi(uint64(v)) 404 | case uint32: 405 | return Utoi(uint64(v)) 406 | case uint64: 407 | return Utoi(v) 408 | case float64: 409 | return Ftoi(v) 410 | case float32: 411 | return Ftoi(float64(v)) 412 | case string: 413 | return Atoi(v) 414 | default: 415 | // order matters (int,uint,float,string,bool) 416 | switch v := v.(type) { 417 | case inter: 418 | return v.Int64() 419 | case uinter: 420 | return Utoi(v.Uint64()) 421 | case floater: 422 | return Ftoi(v.Float64()) 423 | case stringer: 424 | return Atoi(v.String()) 425 | case booler: 426 | return Ttoi(v.Bool()) 427 | default: 428 | return 0 429 | } 430 | } 431 | } 432 | 433 | // Vtou converts any to uint64 434 | func Vtou(v any) uint64 { 435 | switch v := v.(type) { 436 | case bool: 437 | return Ttou(v) 438 | case int: 439 | return Itou(int64(v)) 440 | case int8: 441 | return Itou(int64(v)) 442 | case int16: 443 | return Itou(int64(v)) 444 | case int32: 445 | return Itou(int64(v)) 446 | case int64: 447 | return Itou(v) 448 | case uint: 449 | return uint64(v) 450 | case uint8: 451 | return uint64(v) 452 | case uint16: 453 | return uint64(v) 454 | case uint32: 455 | return uint64(v) 456 | case uint64: 457 | return v 458 | case float64: 459 | return Ftou(v) 460 | case float32: 461 | return Ftou(float64(v)) 462 | case string: 463 | return Atou(v) 464 | default: 465 | // order matters (uint,int,float,string,bool) 466 | switch v := v.(type) { 467 | case uinter: 468 | return v.Uint64() 469 | case inter: 470 | return Itou(v.Int64()) 471 | case floater: 472 | return Ftou(v.Float64()) 473 | case stringer: 474 | return Atou(v.String()) 475 | case booler: 476 | return Ttou(v.Bool()) 477 | default: 478 | return 0 479 | } 480 | } 481 | } 482 | 483 | // Vtoa converts any to string 484 | func Vtoa(v any) string { 485 | switch v := v.(type) { 486 | case bool: 487 | return Ttoa(v) 488 | case int: 489 | return Itoa(int64(v)) 490 | case int8: 491 | return Itoa(int64(v)) 492 | case int16: 493 | return Itoa(int64(v)) 494 | case int32: 495 | return Itoa(int64(v)) 496 | case int64: 497 | return Itoa(v) 498 | case uint: 499 | return Utoa(uint64(v)) 500 | case uint8: 501 | return Utoa(uint64(v)) 502 | case uint16: 503 | return Utoa(uint64(v)) 504 | case uint32: 505 | return Utoa(uint64(v)) 506 | case uint64: 507 | return Utoa(v) 508 | case float64: 509 | return Ftoa(v) 510 | case float32: 511 | return Ftoa(float64(v)) 512 | case string: 513 | return v 514 | default: 515 | // order matters (string,int,uint,float,bool) 516 | switch v := v.(type) { 517 | case stringer: 518 | return v.String() 519 | case inter: 520 | return Itoa(v.Int64()) 521 | case uinter: 522 | return Utoa(v.Uint64()) 523 | case floater: 524 | return Ftoa(v.Float64()) 525 | case booler: 526 | return Ttoa(v.Bool()) 527 | default: 528 | return "" 529 | } 530 | } 531 | } 532 | -------------------------------------------------------------------------------- /conv_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Joshua J Baker. All rights reserved. 2 | // Use of this source code is governed by an MIT-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package conv 6 | 7 | import ( 8 | "math" 9 | "testing" 10 | ) 11 | 12 | func assert(t *testing.T, cond bool) { 13 | t.Helper() 14 | if !cond { 15 | t.Fatal("assertion failed") 16 | } 17 | } 18 | 19 | type tbooler bool 20 | type tfloater float64 21 | type tstringer string 22 | type tinter int64 23 | type tuinter uint64 24 | 25 | func (t tbooler) Bool() bool { return bool(t) } 26 | func (t tfloater) Float64() float64 { return float64(t) } 27 | func (t tinter) Int64() int64 { return int64(t) } 28 | func (t tuinter) Uint64() uint64 { return uint64(t) } 29 | func (t tstringer) String() string { return string(t) } 30 | 31 | func TestConv(t *testing.T) { 32 | // Bool 33 | assert(t, Ttof(true) == 1) 34 | assert(t, Ttof(false) == 0) 35 | assert(t, Ttoi(true) == 1) 36 | assert(t, Ttoi(false) == 0) 37 | assert(t, Ttou(true) == 1) 38 | assert(t, Ttou(false) == 0) 39 | assert(t, Ttoa(true) == "true") 40 | assert(t, Ttoa(false) == "false") 41 | assert(t, Ttov(true) == true) 42 | assert(t, Ttov(false) == false) 43 | 44 | // Float 45 | assert(t, Ftot(0) == false) 46 | assert(t, Ftot(1) == true) 47 | assert(t, Ftot(-1) == true) 48 | assert(t, Ftot(math.Inf(-1)) == true) 49 | assert(t, Ftot(math.Inf(0)) == true) 50 | assert(t, Ftot(math.Inf(+1)) == true) 51 | assert(t, Ftot(math.NaN()) == false) 52 | assert(t, Ftoi(0) == 0) 53 | assert(t, Ftoi(math.MaxUint64) == math.MaxInt64) 54 | assert(t, Ftoi(math.MaxUint64) == math.MaxInt64) 55 | assert(t, Ftoi(math.MinInt64) == math.MinInt64) 56 | assert(t, Ftoi(math.NaN()) == 0) 57 | assert(t, Ftoi(math.Inf(-1)) == math.MinInt64) 58 | assert(t, Ftoi(math.Inf(0)) == math.MaxInt64) 59 | assert(t, Ftoi(math.Inf(+1)) == math.MaxInt64) 60 | assert(t, Ftou(0) == 0) 61 | assert(t, Ftou(math.MaxUint64) == math.MaxUint64) 62 | assert(t, Ftou(math.MinInt64) == 0) 63 | assert(t, Ftou(math.NaN()) == 0) 64 | assert(t, Ftou(math.Inf(-1)) == 0) 65 | assert(t, Ftou(math.Inf(0)) == math.MaxUint64) 66 | assert(t, Ftou(math.Inf(+1)) == math.MaxUint64) 67 | assert(t, Ftoa(0) == "0") 68 | assert(t, Ftoa(math.NaN()) == "NaN") 69 | assert(t, Ftoa(math.Inf(+1)) == "Infinity") 70 | assert(t, Ftoa(math.Inf(-1)) == "-Infinity") 71 | assert(t, Ftov(0) == 0.0) 72 | assert(t, math.IsNaN(Ftov(math.NaN()).(float64))) 73 | 74 | // Int 75 | assert(t, Itot(0) == false) 76 | assert(t, Itot(1) == true) 77 | assert(t, Itot(-1) == true) 78 | assert(t, Itof(0) == 0) 79 | assert(t, Itof(1) == 1) 80 | assert(t, Itof(-1) == -1) 81 | assert(t, Itou(0) == 0) 82 | assert(t, Itou(1) == 1) 83 | assert(t, Itou(-1) == 0) 84 | assert(t, Itoa(0) == "0") 85 | assert(t, Itoa(1) == "1") 86 | assert(t, Itoa(-1) == "-1") 87 | assert(t, Itov(-1) == int64(-1)) 88 | 89 | // Uint 90 | assert(t, Utot(0) == false) 91 | assert(t, Utot(1) == true) 92 | assert(t, Utof(0) == 0) 93 | assert(t, Utof(1) == 1) 94 | assert(t, Utoi(0) == 0) 95 | assert(t, Utoi(1) == 1) 96 | assert(t, Utoi(math.MaxUint64) == math.MaxInt64) 97 | assert(t, Utoa(0) == "0") 98 | assert(t, Utoa(1) == "1") 99 | assert(t, Utov(1) == uint64(1)) 100 | 101 | // String 102 | assert(t, Atot("") == false) 103 | assert(t, Atot("0") == true) 104 | assert(t, Atot("1") == true) 105 | assert(t, Atot("false") == true) 106 | assert(t, Atot("true") == true) 107 | assert(t, Atof("0") == 0) 108 | assert(t, math.IsNaN(Atof(""))) 109 | assert(t, math.IsNaN(Atof("p1o1"))) 110 | assert(t, math.IsNaN(Atof("NaN"))) 111 | assert(t, !math.IsNaN(Atof("12312"))) 112 | assert(t, math.IsNaN(Atof("+Inf"))) 113 | assert(t, math.IsNaN(Atof("+inf"))) 114 | assert(t, math.IsNaN(Atof("+infinity"))) 115 | assert(t, math.IsNaN(Atof("Inf"))) 116 | assert(t, math.IsNaN(Atof("inf"))) 117 | assert(t, math.IsNaN(Atof("infinity"))) 118 | assert(t, math.IsNaN(Atof("-Inf"))) 119 | assert(t, math.IsNaN(Atof("-inf"))) 120 | assert(t, math.IsNaN(Atof("-infinity"))) 121 | assert(t, math.IsInf(Atof("+Infinity"), +1)) 122 | assert(t, math.IsInf(Atof("Infinity"), +1)) 123 | assert(t, math.IsInf(Atof("-Infinity"), -1)) 124 | assert(t, Atoi("0") == 0) 125 | assert(t, Atoi("1") == 1) 126 | assert(t, Atoi("-1") == -1) 127 | assert(t, Atoi("op1") == 0) 128 | assert(t, Atoi("123129319238019283121231231") == math.MaxInt64) 129 | assert(t, Atou("0") == 0) 130 | assert(t, Atou("1") == 1) 131 | assert(t, Atou("op1") == 0) 132 | assert(t, Atou("123129319238019283121231231") == math.MaxUint64) 133 | assert(t, Atov("123") == "123") 134 | assert(t, Atov("") == "") 135 | 136 | ///////////////////////////////////////////////////////// 137 | // Any(bool) 138 | // any(bool) -> bool 139 | assert(t, Vtot(true) == true) 140 | assert(t, Vtot(false) == false) 141 | // any(float*) -> bool 142 | assert(t, Vtot(float64(0)) == false) 143 | assert(t, Vtot(float64(1)) == true) 144 | assert(t, Vtot(float32(0)) == false) 145 | assert(t, Vtot(float32(1)) == true) 146 | // any(int*) -> bool 147 | assert(t, Vtot(int(0)) == false) 148 | assert(t, Vtot(int(1)) == true) 149 | assert(t, Vtot(int8(0)) == false) 150 | assert(t, Vtot(int8(1)) == true) 151 | assert(t, Vtot(int16(0)) == false) 152 | assert(t, Vtot(int16(1)) == true) 153 | assert(t, Vtot(int32(0)) == false) 154 | assert(t, Vtot(int32(1)) == true) 155 | assert(t, Vtot(int64(0)) == false) 156 | assert(t, Vtot(int64(1)) == true) 157 | // any(uint*) -> bool 158 | assert(t, Vtot(uint(0)) == false) 159 | assert(t, Vtot(uint(1)) == true) 160 | assert(t, Vtot(uint8(0)) == false) 161 | assert(t, Vtot(uint8(1)) == true) 162 | assert(t, Vtot(uint16(0)) == false) 163 | assert(t, Vtot(uint16(1)) == true) 164 | assert(t, Vtot(uint32(0)) == false) 165 | assert(t, Vtot(uint32(1)) == true) 166 | assert(t, Vtot(uint64(0)) == false) 167 | assert(t, Vtot(uint64(1)) == true) 168 | // any(string) -> bool 169 | assert(t, Vtot("true") == true) 170 | assert(t, Vtot("false") == true) 171 | assert(t, Vtot("") == false) 172 | // any(booler) -> bool 173 | assert(t, Vtot(tbooler(true)) == true) 174 | assert(t, Vtot(tbooler(false)) == false) 175 | // any(floater) -> bool 176 | assert(t, Vtot(tfloater(math.NaN())) == false) 177 | assert(t, Vtot(tfloater(0)) == false) 178 | assert(t, Vtot(tfloater(1)) == true) 179 | // any(inter) -> bool 180 | assert(t, Vtot(tinter(0)) == false) 181 | assert(t, Vtot(tinter(1)) == true) 182 | assert(t, Vtot(tinter(-1)) == true) 183 | // any(uinter) -> bool 184 | assert(t, Vtot(tuinter(0)) == false) 185 | assert(t, Vtot(tuinter(1)) == true) 186 | // any(stringer) -> bool 187 | assert(t, Vtot(tstringer("true")) == true) 188 | assert(t, Vtot(tstringer("")) == false) 189 | assert(t, Vtot(tstringer("false")) == true) 190 | // any(fallback) -> bool 191 | assert(t, Vtot(nil) == false) 192 | 193 | ///////////////////////////////////////////////////////// 194 | // Any(Float) 195 | ///////////////////////////////////////////////////////// 196 | // any(bool) -> float64 197 | assert(t, Vtof(true) == 1) 198 | assert(t, Vtof(false) == 0) 199 | // any(int*) -> float64 200 | assert(t, Vtof(int(1)) == 1) 201 | assert(t, Vtof(int(0)) == 0) 202 | assert(t, Vtof(int8(1)) == 1) 203 | assert(t, Vtof(int8(0)) == 0) 204 | assert(t, Vtof(int16(1)) == 1) 205 | assert(t, Vtof(int16(0)) == 0) 206 | assert(t, Vtof(int32(1)) == 1) 207 | assert(t, Vtof(int32(0)) == 0) 208 | assert(t, Vtof(int64(1)) == 1) 209 | assert(t, Vtof(int64(0)) == 0) 210 | // any(uint*) -> float64 211 | assert(t, Vtof(uint(1)) == 1) 212 | assert(t, Vtof(uint(0)) == 0) 213 | assert(t, Vtof(uint8(1)) == 1) 214 | assert(t, Vtof(uint8(0)) == 0) 215 | assert(t, Vtof(uint16(1)) == 1) 216 | assert(t, Vtof(uint16(0)) == 0) 217 | assert(t, Vtof(uint32(1)) == 1) 218 | assert(t, Vtof(uint32(0)) == 0) 219 | assert(t, Vtof(uint64(1)) == 1) 220 | assert(t, Vtof(uint64(0)) == 0) 221 | // any(float*) -> float64 222 | assert(t, Vtof(float32(1)) == 1) 223 | assert(t, Vtof(float32(0)) == 0) 224 | assert(t, Vtof(float64(1)) == 1) 225 | assert(t, Vtof(float64(0)) == 0) 226 | // any(string) -> float64 227 | assert(t, Vtof(string("1")) == 1) 228 | assert(t, Vtof(string("0")) == 0) 229 | assert(t, math.IsNaN(Vtof(string("wqer9812039")))) 230 | // any(booler) -> float64 231 | assert(t, Vtof(tbooler(true)) == 1) 232 | assert(t, Vtof(tbooler(false)) == 0) 233 | // any(floater) -> float64 234 | assert(t, Vtof(tfloater(1)) == 1) 235 | assert(t, Vtof(tfloater(0)) == 0) 236 | // any(inter) -> float64 237 | assert(t, Vtof(tinter(1)) == 1) 238 | assert(t, Vtof(tinter(0)) == 0) 239 | assert(t, Vtof(tinter(-1)) == -1) 240 | // any(uinter) -> float64 241 | assert(t, Vtof(tuinter(1)) == 1) 242 | assert(t, Vtof(tuinter(0)) == 0) 243 | // any(stringer) -> float64 244 | assert(t, Vtof(tstringer("1")) == 1) 245 | assert(t, Vtof(tstringer("0")) == 0) 246 | assert(t, math.IsNaN(Vtof(tstringer("wqer9812039")))) 247 | // any(fallback) -> float64 248 | assert(t, math.IsNaN(Vtof(nil))) 249 | 250 | ///////////////////////////////////////////////////////// 251 | // Any(Int) 252 | ///////////////////////////////////////////////////////// 253 | // any(bool) -> int64 254 | assert(t, Vtoi(true) == 1) 255 | assert(t, Vtoi(false) == 0) 256 | // any(int*) -> int64 257 | assert(t, Vtoi(int(1)) == 1) 258 | assert(t, Vtoi(int(0)) == 0) 259 | assert(t, Vtoi(int8(1)) == 1) 260 | assert(t, Vtoi(int8(0)) == 0) 261 | assert(t, Vtoi(int16(1)) == 1) 262 | assert(t, Vtoi(int16(0)) == 0) 263 | assert(t, Vtoi(int32(1)) == 1) 264 | assert(t, Vtoi(int32(0)) == 0) 265 | assert(t, Vtoi(int64(1)) == 1) 266 | assert(t, Vtoi(int64(0)) == 0) 267 | // any(uint*) -> int64 268 | assert(t, Vtoi(uint(1)) == 1) 269 | assert(t, Vtoi(uint(0)) == 0) 270 | assert(t, Vtoi(uint8(1)) == 1) 271 | assert(t, Vtoi(uint8(0)) == 0) 272 | assert(t, Vtoi(uint16(1)) == 1) 273 | assert(t, Vtoi(uint16(0)) == 0) 274 | assert(t, Vtoi(uint32(1)) == 1) 275 | assert(t, Vtoi(uint32(0)) == 0) 276 | assert(t, Vtoi(uint64(1)) == 1) 277 | assert(t, Vtoi(uint64(0)) == 0) 278 | // any(float*) -> int64 279 | assert(t, Vtoi(float32(1)) == 1) 280 | assert(t, Vtoi(float32(0)) == 0) 281 | assert(t, Vtoi(float64(1)) == 1) 282 | assert(t, Vtoi(float64(0)) == 0) 283 | // any(string) -> int64 284 | assert(t, Vtoi(string("1")) == 1) 285 | assert(t, Vtoi(string("0")) == 0) 286 | assert(t, Vtoi(string("wqer9812039")) == 0) 287 | // any(booler) -> int64 288 | assert(t, Vtoi(tbooler(true)) == 1) 289 | assert(t, Vtoi(tbooler(false)) == 0) 290 | // any(floater) -> int64 291 | assert(t, Vtoi(tfloater(1)) == 1) 292 | assert(t, Vtoi(tfloater(0)) == 0) 293 | // any(inter) -> int64 294 | assert(t, Vtoi(tinter(1)) == 1) 295 | assert(t, Vtoi(tinter(0)) == 0) 296 | assert(t, Vtoi(tinter(-1)) == -1) 297 | // any(uinter) -> int64 298 | assert(t, Vtoi(tuinter(1)) == 1) 299 | assert(t, Vtoi(tuinter(0)) == 0) 300 | // any(stringer) -> int64 301 | assert(t, Vtoi(tstringer("1")) == 1) 302 | assert(t, Vtoi(tstringer("0")) == 0) 303 | assert(t, Vtoi(tstringer("wqer9812039")) == 0) 304 | // any(fallback) -> int64 305 | assert(t, Vtoi(nil) == 0) 306 | 307 | ///////////////////////////////////////////////////////// 308 | // Any(Uint) 309 | ///////////////////////////////////////////////////////// 310 | // any(bool) -> uint64 311 | assert(t, Vtou(true) == 1) 312 | assert(t, Vtou(false) == 0) 313 | // any(int*) -> uint64 314 | assert(t, Vtou(int(1)) == 1) 315 | assert(t, Vtou(int(0)) == 0) 316 | assert(t, Vtou(int8(1)) == 1) 317 | assert(t, Vtou(int8(0)) == 0) 318 | assert(t, Vtou(int16(1)) == 1) 319 | assert(t, Vtou(int16(0)) == 0) 320 | assert(t, Vtou(int32(1)) == 1) 321 | assert(t, Vtou(int32(0)) == 0) 322 | assert(t, Vtou(int64(1)) == 1) 323 | assert(t, Vtou(int64(0)) == 0) 324 | // any(uint*) -> uint64 325 | assert(t, Vtou(uint(1)) == 1) 326 | assert(t, Vtou(uint(0)) == 0) 327 | assert(t, Vtou(uint8(1)) == 1) 328 | assert(t, Vtou(uint8(0)) == 0) 329 | assert(t, Vtou(uint16(1)) == 1) 330 | assert(t, Vtou(uint16(0)) == 0) 331 | assert(t, Vtou(uint32(1)) == 1) 332 | assert(t, Vtou(uint32(0)) == 0) 333 | assert(t, Vtou(uint64(1)) == 1) 334 | assert(t, Vtou(uint64(0)) == 0) 335 | // any(float*) -> uint64 336 | assert(t, Vtou(float32(1)) == 1) 337 | assert(t, Vtou(float32(0)) == 0) 338 | assert(t, Vtou(float64(1)) == 1) 339 | assert(t, Vtou(float64(0)) == 0) 340 | // any(string) -> uint64 341 | assert(t, Vtou(string("1")) == 1) 342 | assert(t, Vtou(string("0")) == 0) 343 | assert(t, Vtou(string("wqer9812039")) == 0) 344 | // any(booler) -> uint64 345 | assert(t, Vtou(tbooler(true)) == 1) 346 | assert(t, Vtou(tbooler(false)) == 0) 347 | // any(floater) -> uint64 348 | assert(t, Vtou(tfloater(1)) == 1) 349 | assert(t, Vtou(tfloater(0)) == 0) 350 | // any(inter) -> uint64 351 | assert(t, Vtou(tinter(1)) == 1) 352 | assert(t, Vtou(tinter(0)) == 0) 353 | // any(uinter) -> uint64 354 | assert(t, Vtou(tuinter(1)) == 1) 355 | assert(t, Vtou(tuinter(0)) == 0) 356 | // any(stringer) -> uint64 357 | assert(t, Vtou(tstringer("1")) == 1) 358 | assert(t, Vtou(tstringer("0")) == 0) 359 | assert(t, Vtou(tstringer("wqer9812039")) == 0) 360 | // any(fallback) -> uint64 361 | assert(t, Vtou(nil) == 0) 362 | 363 | ///////////////////////////////////////////////////////// 364 | // Any(String) 365 | ///////////////////////////////////////////////////////// 366 | // any(bool) -> string 367 | assert(t, Vtoa(true) == "true") 368 | assert(t, Vtoa(false) == "false") 369 | // any(int*) -> string 370 | assert(t, Vtoa(int(1)) == "1") 371 | assert(t, Vtoa(int(0)) == "0") 372 | assert(t, Vtoa(int8(1)) == "1") 373 | assert(t, Vtoa(int8(0)) == "0") 374 | assert(t, Vtoa(int16(1)) == "1") 375 | assert(t, Vtoa(int16(0)) == "0") 376 | assert(t, Vtoa(int32(1)) == "1") 377 | assert(t, Vtoa(int32(0)) == "0") 378 | assert(t, Vtoa(int64(1)) == "1") 379 | assert(t, Vtoa(int64(0)) == "0") 380 | // any(uint*) -> string 381 | assert(t, Vtoa(uint(1)) == "1") 382 | assert(t, Vtoa(uint(0)) == "0") 383 | assert(t, Vtoa(uint8(1)) == "1") 384 | assert(t, Vtoa(uint8(0)) == "0") 385 | assert(t, Vtoa(uint16(1)) == "1") 386 | assert(t, Vtoa(uint16(0)) == "0") 387 | assert(t, Vtoa(uint32(1)) == "1") 388 | assert(t, Vtoa(uint32(0)) == "0") 389 | assert(t, Vtoa(uint64(1)) == "1") 390 | assert(t, Vtoa(uint64(0)) == "0") 391 | // any(float*) -> string 392 | assert(t, Vtoa(float32(1)) == "1") 393 | assert(t, Vtoa(float32(0)) == "0") 394 | assert(t, Vtoa(float64(1)) == "1") 395 | assert(t, Vtoa(float64(0)) == "0") 396 | assert(t, Vtoa(float64(math.NaN())) == "NaN") 397 | assert(t, Vtoa(float64(math.Inf(+1))) == "Infinity") 398 | assert(t, Vtoa(float64(math.Inf(-1))) == "-Infinity") 399 | // any(string) -> string 400 | assert(t, Vtoa(string("1")) == "1") 401 | // any(booler) -> string 402 | assert(t, Vtoa(tbooler(true)) == "true") 403 | assert(t, Vtoa(tbooler(false)) == "false") 404 | // any(floater) -> string 405 | assert(t, Vtoa(tfloater(1)) == "1") 406 | assert(t, Vtoa(tfloater(0)) == "0") 407 | assert(t, Vtoa(tfloater(math.NaN())) == "NaN") 408 | assert(t, Vtoa(tfloater(math.Inf(+1))) == "Infinity") 409 | assert(t, Vtoa(tfloater(math.Inf(-1))) == "-Infinity") 410 | // any(inter) -> string 411 | assert(t, Vtoa(tinter(1)) == "1") 412 | assert(t, Vtoa(tinter(0)) == "0") 413 | // any(uinter) -> string 414 | assert(t, Vtoa(tuinter(1)) == "1") 415 | assert(t, Vtoa(tuinter(0)) == "0") 416 | // any(stringer) -> string 417 | assert(t, Vtoa(tstringer("1")) == "1") 418 | assert(t, Vtoa(tstringer("0")) == "0") 419 | assert(t, Vtoa(tstringer("wqer9812039")) == "wqer9812039") 420 | // any(fallback) -> string 421 | assert(t, Vtoa(nil) == "") 422 | 423 | } 424 | 425 | func TestFloatConversions(t *testing.T) { 426 | assert(t, Ftoi(math.NaN()) == 0) 427 | assert(t, Ftoi(math.Inf(+1)) == math.MaxInt64) 428 | assert(t, Ftoi(math.Inf(-1)) == math.MinInt64) 429 | assert(t, Ftou(math.NaN()) == 0) 430 | assert(t, Ftou(math.Inf(+1)) == math.MaxUint64) 431 | assert(t, Ftou(math.Inf(-1)) == 0) 432 | } 433 | --------------------------------------------------------------------------------