1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package big 6 7 import ( 8 "bytes" 9 "fmt" 10 "math" 11 "strconv" 12 "strings" 13 "testing" 14 ) 15 16 type StringTest struct { 17 in, out string 18 ok bool 19 } 20 21 var setStringTests = []StringTest{ 22 {"0", "0", true}, 23 {"-0", "0", true}, 24 {"1", "1", true}, 25 {"-1", "-1", true}, 26 {"1.", "1", true}, 27 {"1e0", "1", true}, 28 {"1.e1", "10", true}, 29 {in: "1e"}, 30 {in: "1.e"}, 31 {in: "1e+14e-5"}, 32 {in: "1e4.5"}, 33 {in: "r"}, 34 {in: "a/b"}, 35 {in: "a.b"}, 36 {"-0.1", "-1/10", true}, 37 {"-.1", "-1/10", true}, 38 {"2/4", "1/2", true}, 39 {".25", "1/4", true}, 40 {"-1/5", "-1/5", true}, 41 {"8129567.7690E14", "812956776900000000000", true}, 42 {"78189e+4", "781890000", true}, 43 {"553019.8935e+8", "55301989350000", true}, 44 {"98765432109876543210987654321e-10", "98765432109876543210987654321/10000000000", true}, 45 {"9877861857500000E-7", "3951144743/4", true}, 46 {"2169378.417e-3", "2169378417/1000000", true}, 47 {"884243222337379604041632732738665534", "884243222337379604041632732738665534", true}, 48 {"53/70893980658822810696", "53/70893980658822810696", true}, 49 {"106/141787961317645621392", "53/70893980658822810696", true}, 50 {"204211327800791583.81095", "4084226556015831676219/20000", true}, 51 {in: "1/0"}, 52 } 53 54 // These are not supported by fmt.Fscanf. 55 var setStringTests2 = []StringTest{ 56 {"0x10", "16", true}, 57 {"-010/1", "-8", true}, // TODO(gri) should we even permit octal here? 58 {"-010.", "-10", true}, 59 {"0x10/0x20", "1/2", true}, 60 {"0b1000/3", "8/3", true}, 61 // TODO(gri) add more tests 62 } 63 64 func TestRatSetString(t *testing.T) { 65 var tests []StringTest 66 tests = append(tests, setStringTests...) 67 tests = append(tests, setStringTests2...) 68 69 for i, test := range tests { 70 x, ok := new(Rat).SetString(test.in) 71 72 if ok { 73 if !test.ok { 74 t.Errorf("#%d SetString(%q) expected failure", i, test.in) 75 } else if x.RatString() != test.out { 76 t.Errorf("#%d SetString(%q) got %s want %s", i, test.in, x.RatString(), test.out) 77 } 78 } else if x != nil { 79 t.Errorf("#%d SetString(%q) got %p want nil", i, test.in, x) 80 } 81 } 82 } 83 84 func TestRatScan(t *testing.T) { 85 var buf bytes.Buffer 86 for i, test := range setStringTests { 87 x := new(Rat) 88 buf.Reset() 89 buf.WriteString(test.in) 90 91 _, err := fmt.Fscanf(&buf, "%v", x) 92 if err == nil != test.ok { 93 if test.ok { 94 t.Errorf("#%d (%s) error: %s", i, test.in, err) 95 } else { 96 t.Errorf("#%d (%s) expected error", i, test.in) 97 } 98 continue 99 } 100 if err == nil && x.RatString() != test.out { 101 t.Errorf("#%d got %s want %s", i, x.RatString(), test.out) 102 } 103 } 104 } 105 106 var floatStringTests = []struct { 107 in string 108 prec int 109 out string 110 }{ 111 {"0", 0, "0"}, 112 {"0", 4, "0.0000"}, 113 {"1", 0, "1"}, 114 {"1", 2, "1.00"}, 115 {"-1", 0, "-1"}, 116 {".25", 2, "0.25"}, 117 {".25", 1, "0.3"}, 118 {".25", 3, "0.250"}, 119 {"-1/3", 3, "-0.333"}, 120 {"-2/3", 4, "-0.6667"}, 121 {"0.96", 1, "1.0"}, 122 {"0.999", 2, "1.00"}, 123 {"0.9", 0, "1"}, 124 {".25", -1, "0"}, 125 {".55", -1, "1"}, 126 } 127 128 func TestFloatString(t *testing.T) { 129 for i, test := range floatStringTests { 130 x, _ := new(Rat).SetString(test.in) 131 132 if x.FloatString(test.prec) != test.out { 133 t.Errorf("#%d got %s want %s", i, x.FloatString(test.prec), test.out) 134 } 135 } 136 } 137 138 // Test inputs to Rat.SetString. The prefix "long:" causes the test 139 // to be skipped in --test.short mode. (The threshold is about 500us.) 140 var float64inputs = []string{ 141 // Constants plundered from strconv/testfp.txt. 142 143 // Table 1: Stress Inputs for Conversion to 53-bit Binary, < 1/2 ULP 144 "5e+125", 145 "69e+267", 146 "999e-026", 147 "7861e-034", 148 "75569e-254", 149 "928609e-261", 150 "9210917e+080", 151 "84863171e+114", 152 "653777767e+273", 153 "5232604057e-298", 154 "27235667517e-109", 155 "653532977297e-123", 156 "3142213164987e-294", 157 "46202199371337e-072", 158 "231010996856685e-073", 159 "9324754620109615e+212", 160 "78459735791271921e+049", 161 "272104041512242479e+200", 162 "6802601037806061975e+198", 163 "20505426358836677347e-221", 164 "836168422905420598437e-234", 165 "4891559871276714924261e+222", 166 167 // Table 2: Stress Inputs for Conversion to 53-bit Binary, > 1/2 ULP 168 "9e-265", 169 "85e-037", 170 "623e+100", 171 "3571e+263", 172 "81661e+153", 173 "920657e-023", 174 "4603285e-024", 175 "87575437e-309", 176 "245540327e+122", 177 "6138508175e+120", 178 "83356057653e+193", 179 "619534293513e+124", 180 "2335141086879e+218", 181 "36167929443327e-159", 182 "609610927149051e-255", 183 "3743626360493413e-165", 184 "94080055902682397e-242", 185 "899810892172646163e+283", 186 "7120190517612959703e+120", 187 "25188282901709339043e-252", 188 "308984926168550152811e-052", 189 "6372891218502368041059e+064", 190 191 // Table 14: Stress Inputs for Conversion to 24-bit Binary, <1/2 ULP 192 "5e-20", 193 "67e+14", 194 "985e+15", 195 "7693e-42", 196 "55895e-16", 197 "996622e-44", 198 "7038531e-32", 199 "60419369e-46", 200 "702990899e-20", 201 "6930161142e-48", 202 "25933168707e+13", 203 "596428896559e+20", 204 205 // Table 15: Stress Inputs for Conversion to 24-bit Binary, >1/2 ULP 206 "3e-23", 207 "57e+18", 208 "789e-35", 209 "2539e-18", 210 "76173e+28", 211 "887745e-11", 212 "5382571e-37", 213 "82381273e-35", 214 "750486563e-38", 215 "3752432815e-39", 216 "75224575729e-45", 217 "459926601011e+15", 218 219 // Constants plundered from strconv/atof_test.go. 220 221 "0", 222 "1", 223 "+1", 224 "1e23", 225 "1E23", 226 "100000000000000000000000", 227 "1e-100", 228 "123456700", 229 "99999999999999974834176", 230 "100000000000000000000001", 231 "100000000000000008388608", 232 "100000000000000016777215", 233 "100000000000000016777216", 234 "-1", 235 "-0.1", 236 "-0", // NB: exception made for this input 237 "1e-20", 238 "625e-3", 239 240 // largest float64 241 "1.7976931348623157e308", 242 "-1.7976931348623157e308", 243 // next float64 - too large 244 "1.7976931348623159e308", 245 "-1.7976931348623159e308", 246 // the border is ...158079 247 // borderline - okay 248 "1.7976931348623158e308", 249 "-1.7976931348623158e308", 250 // borderline - too large 251 "1.797693134862315808e308", 252 "-1.797693134862315808e308", 253 254 // a little too large 255 "1e308", 256 "2e308", 257 "1e309", 258 259 // way too large 260 "1e310", 261 "-1e310", 262 "1e400", 263 "-1e400", 264 "long:1e400000", 265 "long:-1e400000", 266 267 // denormalized 268 "1e-305", 269 "1e-306", 270 "1e-307", 271 "1e-308", 272 "1e-309", 273 "1e-310", 274 "1e-322", 275 // smallest denormal 276 "5e-324", 277 "4e-324", 278 "3e-324", 279 // too small 280 "2e-324", 281 // way too small 282 "1e-350", 283 "long:1e-400000", 284 // way too small, negative 285 "-1e-350", 286 "long:-1e-400000", 287 288 // try to overflow exponent 289 // [Disabled: too slow and memory-hungry with rationals.] 290 // "1e-4294967296", 291 // "1e+4294967296", 292 // "1e-18446744073709551616", 293 // "1e+18446744073709551616", 294 295 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 296 "2.2250738585072012e-308", 297 // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ 298 "2.2250738585072011e-308", 299 300 // A very large number (initially wrongly parsed by the fast algorithm). 301 "4.630813248087435e+307", 302 303 // A different kind of very large number. 304 "22.222222222222222", 305 "long:2." + strings.Repeat("2", 4000) + "e+1", 306 307 // Exactly halfway between 1 and math.Nextafter(1, 2). 308 // Round to even (down). 309 "1.00000000000000011102230246251565404236316680908203125", 310 // Slightly lower; still round down. 311 "1.00000000000000011102230246251565404236316680908203124", 312 // Slightly higher; round up. 313 "1.00000000000000011102230246251565404236316680908203126", 314 // Slightly higher, but you have to read all the way to the end. 315 "long:1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", 316 317 // Smallest denormal, 2^(-1022-52) 318 "4.940656458412465441765687928682213723651e-324", 319 // Half of smallest denormal, 2^(-1022-53) 320 "2.470328229206232720882843964341106861825e-324", 321 // A little more than the exact half of smallest denormal 322 // 2^-1075 + 2^-1100. (Rounds to 1p-1074.) 323 "2.470328302827751011111470718709768633275e-324", 324 // The exact halfway between smallest normal and largest denormal: 325 // 2^-1022 - 2^-1075. (Rounds to 2^-1022.) 326 "2.225073858507201136057409796709131975935e-308", 327 328 "1152921504606846975", // 1<<60 - 1 329 "-1152921504606846975", // -(1<<60 - 1) 330 "1152921504606846977", // 1<<60 + 1 331 "-1152921504606846977", // -(1<<60 + 1) 332 333 "1/3", 334 } 335 336 // isFinite reports whether f represents a finite rational value. 337 // It is equivalent to !math.IsNan(f) && !math.IsInf(f, 0). 338 func isFinite(f float64) bool { 339 return math.Abs(f) <= math.MaxFloat64 340 } 341 342 func TestFloat32SpecialCases(t *testing.T) { 343 for _, input := range float64inputs { 344 if strings.HasPrefix(input, "long:") { 345 if testing.Short() { 346 continue 347 } 348 input = input[len("long:"):] 349 } 350 351 r, ok := new(Rat).SetString(input) 352 if !ok { 353 t.Errorf("Rat.SetString(%q) failed", input) 354 continue 355 } 356 f, exact := r.Float32() 357 358 // 1. Check string -> Rat -> float32 conversions are 359 // consistent with strconv.ParseFloat. 360 // Skip this check if the input uses "a/b" rational syntax. 361 if !strings.Contains(input, "/") { 362 e64, _ := strconv.ParseFloat(input, 32) 363 e := float32(e64) 364 365 // Careful: negative Rats too small for 366 // float64 become -0, but Rat obviously cannot 367 // preserve the sign from SetString("-0"). 368 switch { 369 case math.Float32bits(e) == math.Float32bits(f): 370 // Ok: bitwise equal. 371 case f == 0 && r.Num().BitLen() == 0: 372 // Ok: Rat(0) is equivalent to both +/- float64(0). 373 default: 374 t.Errorf("strconv.ParseFloat(%q) = %g (%b), want %g (%b); delta = %g", input, e, e, f, f, f-e) 375 } 376 } 377 378 if !isFinite(float64(f)) { 379 continue 380 } 381 382 // 2. Check f is best approximation to r. 383 if !checkIsBestApprox32(t, f, r) { 384 // Append context information. 385 t.Errorf("(input was %q)", input) 386 } 387 388 // 3. Check f->R->f roundtrip is non-lossy. 389 checkNonLossyRoundtrip32(t, f) 390 391 // 4. Check exactness using slow algorithm. 392 if wasExact := new(Rat).SetFloat64(float64(f)).Cmp(r) == 0; wasExact != exact { 393 t.Errorf("Rat.SetString(%q).Float32().exact = %t, want %t", input, exact, wasExact) 394 } 395 } 396 } 397 398 func TestFloat64SpecialCases(t *testing.T) { 399 for _, input := range float64inputs { 400 if strings.HasPrefix(input, "long:") { 401 if testing.Short() { 402 continue 403 } 404 input = input[len("long:"):] 405 } 406 407 r, ok := new(Rat).SetString(input) 408 if !ok { 409 t.Errorf("Rat.SetString(%q) failed", input) 410 continue 411 } 412 f, exact := r.Float64() 413 414 // 1. Check string -> Rat -> float64 conversions are 415 // consistent with strconv.ParseFloat. 416 // Skip this check if the input uses "a/b" rational syntax. 417 if !strings.Contains(input, "/") { 418 e, _ := strconv.ParseFloat(input, 64) 419 420 // Careful: negative Rats too small for 421 // float64 become -0, but Rat obviously cannot 422 // preserve the sign from SetString("-0"). 423 switch { 424 case math.Float64bits(e) == math.Float64bits(f): 425 // Ok: bitwise equal. 426 case f == 0 && r.Num().BitLen() == 0: 427 // Ok: Rat(0) is equivalent to both +/- float64(0). 428 default: 429 t.Errorf("strconv.ParseFloat(%q) = %g (%b), want %g (%b); delta = %g", input, e, e, f, f, f-e) 430 } 431 } 432 433 if !isFinite(f) { 434 continue 435 } 436 437 // 2. Check f is best approximation to r. 438 if !checkIsBestApprox64(t, f, r) { 439 // Append context information. 440 t.Errorf("(input was %q)", input) 441 } 442 443 // 3. Check f->R->f roundtrip is non-lossy. 444 checkNonLossyRoundtrip64(t, f) 445 446 // 4. Check exactness using slow algorithm. 447 if wasExact := new(Rat).SetFloat64(f).Cmp(r) == 0; wasExact != exact { 448 t.Errorf("Rat.SetString(%q).Float64().exact = %t, want %t", input, exact, wasExact) 449 } 450 } 451 } 452