...

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

View as plain text