github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/math/big/floatmarsh_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  	"encoding/json"
     9  	"testing"
    10  )
    11  
    12  var floatVals = []string{
    13  	"0",
    14  	"1",
    15  	"0.1",
    16  	"2.71828",
    17  	"1234567890",
    18  	"3.14e1234",
    19  	"3.14e-1234",
    20  	"0.738957395793475734757349579759957975985497e100",
    21  	"0.73895739579347546656564656573475734957975995797598589749859834759476745986795497e100",
    22  	"inf",
    23  	"Inf",
    24  }
    25  
    26  func TestFloatJSONEncoding(t *testing.T) {
    27  	for _, test := range floatVals {
    28  		for _, sign := range []string{"", "+", "-"} {
    29  			for _, prec := range []uint{0, 1, 2, 10, 53, 64, 100, 1000} {
    30  				x := sign + test
    31  				var tx Float
    32  				_, _, err := tx.SetPrec(prec).Parse(x, 0)
    33  				if err != nil {
    34  					t.Errorf("parsing of %s (prec = %d) failed (invalid test case): %v", x, prec, err)
    35  					continue
    36  				}
    37  				b, err := json.Marshal(&tx)
    38  				if err != nil {
    39  					t.Errorf("marshaling of %v (prec = %d) failed: %v", &tx, prec, err)
    40  					continue
    41  				}
    42  				var rx Float
    43  				rx.SetPrec(prec)
    44  				if err := json.Unmarshal(b, &rx); err != nil {
    45  					t.Errorf("unmarshaling of %v (prec = %d) failed: %v", &tx, prec, err)
    46  					continue
    47  				}
    48  				if rx.Cmp(&tx) != 0 {
    49  					t.Errorf("JSON encoding of %v (prec = %d) failed: got %v want %v", &tx, prec, &rx, &tx)
    50  				}
    51  			}
    52  		}
    53  	}
    54  }