...

Source file src/math/big/ratmarsh_test.go

Documentation: math/big

		 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  	"encoding/gob"
		10  	"encoding/json"
		11  	"encoding/xml"
		12  	"testing"
		13  )
		14  
		15  func TestRatGobEncoding(t *testing.T) {
		16  	var medium bytes.Buffer
		17  	enc := gob.NewEncoder(&medium)
		18  	dec := gob.NewDecoder(&medium)
		19  	for _, test := range encodingTests {
		20  		medium.Reset() // empty buffer for each test case (in case of failures)
		21  		var tx Rat
		22  		tx.SetString(test + ".14159265")
		23  		if err := enc.Encode(&tx); err != nil {
		24  			t.Errorf("encoding of %s failed: %s", &tx, err)
		25  			continue
		26  		}
		27  		var rx Rat
		28  		if err := dec.Decode(&rx); err != nil {
		29  			t.Errorf("decoding of %s failed: %s", &tx, err)
		30  			continue
		31  		}
		32  		if rx.Cmp(&tx) != 0 {
		33  			t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
		34  		}
		35  	}
		36  }
		37  
		38  // Sending a nil Rat pointer (inside a slice) on a round trip through gob should yield a zero.
		39  // TODO: top-level nils.
		40  func TestGobEncodingNilRatInSlice(t *testing.T) {
		41  	buf := new(bytes.Buffer)
		42  	enc := gob.NewEncoder(buf)
		43  	dec := gob.NewDecoder(buf)
		44  
		45  	var in = make([]*Rat, 1)
		46  	err := enc.Encode(&in)
		47  	if err != nil {
		48  		t.Errorf("gob encode failed: %q", err)
		49  	}
		50  	var out []*Rat
		51  	err = dec.Decode(&out)
		52  	if err != nil {
		53  		t.Fatalf("gob decode failed: %q", err)
		54  	}
		55  	if len(out) != 1 {
		56  		t.Fatalf("wrong len; want 1 got %d", len(out))
		57  	}
		58  	var zero Rat
		59  	if out[0].Cmp(&zero) != 0 {
		60  		t.Fatalf("transmission of (*Int)(nil) failed: got %s want 0", out)
		61  	}
		62  }
		63  
		64  var ratNums = []string{
		65  	"-141592653589793238462643383279502884197169399375105820974944592307816406286",
		66  	"-1415926535897932384626433832795028841971",
		67  	"-141592653589793",
		68  	"-1",
		69  	"0",
		70  	"1",
		71  	"141592653589793",
		72  	"1415926535897932384626433832795028841971",
		73  	"141592653589793238462643383279502884197169399375105820974944592307816406286",
		74  }
		75  
		76  var ratDenoms = []string{
		77  	"1",
		78  	"718281828459045",
		79  	"7182818284590452353602874713526624977572",
		80  	"718281828459045235360287471352662497757247093699959574966967627724076630353",
		81  }
		82  
		83  func TestRatJSONEncoding(t *testing.T) {
		84  	for _, num := range ratNums {
		85  		for _, denom := range ratDenoms {
		86  			var tx Rat
		87  			tx.SetString(num + "/" + denom)
		88  			b, err := json.Marshal(&tx)
		89  			if err != nil {
		90  				t.Errorf("marshaling of %s failed: %s", &tx, err)
		91  				continue
		92  			}
		93  			var rx Rat
		94  			if err := json.Unmarshal(b, &rx); err != nil {
		95  				t.Errorf("unmarshaling of %s failed: %s", &tx, err)
		96  				continue
		97  			}
		98  			if rx.Cmp(&tx) != 0 {
		99  				t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
	 100  			}
	 101  		}
	 102  	}
	 103  }
	 104  
	 105  func TestRatXMLEncoding(t *testing.T) {
	 106  	for _, num := range ratNums {
	 107  		for _, denom := range ratDenoms {
	 108  			var tx Rat
	 109  			tx.SetString(num + "/" + denom)
	 110  			b, err := xml.Marshal(&tx)
	 111  			if err != nil {
	 112  				t.Errorf("marshaling of %s failed: %s", &tx, err)
	 113  				continue
	 114  			}
	 115  			var rx Rat
	 116  			if err := xml.Unmarshal(b, &rx); err != nil {
	 117  				t.Errorf("unmarshaling of %s failed: %s", &tx, err)
	 118  				continue
	 119  			}
	 120  			if rx.Cmp(&tx) != 0 {
	 121  				t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
	 122  			}
	 123  		}
	 124  	}
	 125  }
	 126  
	 127  func TestRatGobDecodeShortBuffer(t *testing.T) {
	 128  	for _, tc := range [][]byte{
	 129  		[]byte{0x2},
	 130  		[]byte{0x2, 0x0, 0x0, 0x0, 0xff},
	 131  	} {
	 132  		err := NewRat(1, 2).GobDecode(tc)
	 133  		if err == nil {
	 134  			t.Error("expected GobDecode to return error for malformed input")
	 135  		}
	 136  	}
	 137  }
	 138  

View as plain text