github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/math/big/ratconv_test.go (about)

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