Source file src/math/big/intmarsh_test.go

     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 TestIntGobDecode(t *testing.T) {
    78  	for _, test := range []struct {
    79  		buf  []byte
    80  		want int64
    81  	}{
    82  		{nil, 0},
    83  		{[]byte{}, 0},
    84  		{[]byte{0x02}, 0},
    85  		{[]byte{0x03}, 0},
    86  		{[]byte{0x02, 0}, 0},
    87  		{[]byte{0x03, 0}, 0},
    88  		{[]byte{0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0},
    89  		{[]byte{0x02, 1}, 1},
    90  		{[]byte{0x03, 1}, -1},
    91  		{[]byte{0x02, 0, 1}, 1},
    92  		{[]byte{0x03, 0, 1}, -1},
    93  	} {
    94  		for _, initial := range []int64{0, 1, -1} {
    95  			x := NewInt(initial)
    96  			if err := x.GobDecode(test.buf); err != nil {
    97  				t.Errorf("initial %d, GobDecode(%x): %v", initial, test.buf, err)
    98  				continue
    99  			}
   100  			if x.Cmp(NewInt(test.want)) != 0 || !isNormalized(x) {
   101  				t.Errorf("initial %d, GobDecode(%x) = %s, want normalized %d", initial, test.buf, x, test.want)
   102  				continue
   103  			}
   104  			if test.want == 0 {
   105  				// A decoded zero must be safe to use in arithmetic.
   106  				if z := new(Int).Sqrt(x); z.Sign() != 0 {
   107  					t.Errorf("Sqrt(GobDecode(%x)) = %s, want 0", test.buf, z)
   108  				}
   109  			}
   110  		}
   111  	}
   112  }
   113  
   114  func TestIntJSONEncoding(t *testing.T) {
   115  	for _, test := range encodingTests {
   116  		for _, sign := range []string{"", "+", "-"} {
   117  			x := sign + test
   118  			var tx Int
   119  			tx.SetString(x, 10)
   120  			b, err := json.Marshal(&tx)
   121  			if err != nil {
   122  				t.Errorf("marshaling of %s failed: %s", &tx, err)
   123  				continue
   124  			}
   125  			var rx Int
   126  			if err := json.Unmarshal(b, &rx); err != nil {
   127  				t.Errorf("unmarshaling of %s failed: %s", &tx, err)
   128  				continue
   129  			}
   130  			if rx.Cmp(&tx) != 0 {
   131  				t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
   132  			}
   133  		}
   134  	}
   135  }
   136  
   137  func TestIntJSONEncodingNil(t *testing.T) {
   138  	var x *Int
   139  	b, err := x.MarshalJSON()
   140  	if err != nil {
   141  		t.Fatalf("marshaling of nil failed: %s", err)
   142  	}
   143  	got := string(b)
   144  	want := "null"
   145  	if got != want {
   146  		t.Fatalf("marshaling of nil failed: got %s want %s", got, want)
   147  	}
   148  }
   149  
   150  func TestIntXMLEncoding(t *testing.T) {
   151  	for _, test := range encodingTests {
   152  		for _, sign := range []string{"", "+", "-"} {
   153  			x := sign + test
   154  			var tx Int
   155  			tx.SetString(x, 0)
   156  			b, err := xml.Marshal(&tx)
   157  			if err != nil {
   158  				t.Errorf("marshaling of %s failed: %s", &tx, err)
   159  				continue
   160  			}
   161  			var rx Int
   162  			if err := xml.Unmarshal(b, &rx); err != nil {
   163  				t.Errorf("unmarshaling of %s failed: %s", &tx, err)
   164  				continue
   165  			}
   166  			if rx.Cmp(&tx) != 0 {
   167  				t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
   168  			}
   169  		}
   170  	}
   171  }
   172  
   173  func TestIntAppendText(t *testing.T) {
   174  	for _, test := range encodingTests {
   175  		for _, sign := range []string{"", "+", "-"} {
   176  			x := sign + test
   177  			var tx Int
   178  			tx.SetString(x, 10)
   179  			buf := make([]byte, 4, 32)
   180  			b, err := tx.AppendText(buf)
   181  			if err != nil {
   182  				t.Errorf("marshaling of %s failed: %s", &tx, err)
   183  				continue
   184  			}
   185  			var rx Int
   186  			if err := rx.UnmarshalText(b[4:]); err != nil {
   187  				t.Errorf("unmarshaling of %s failed: %s", &tx, err)
   188  				continue
   189  			}
   190  			if rx.Cmp(&tx) != 0 {
   191  				t.Errorf("AppendText of %s failed: got %s want %s", &tx, &rx, &tx)
   192  			}
   193  		}
   194  	}
   195  }
   196  
   197  func TestIntAppendTextNil(t *testing.T) {
   198  	var x *Int
   199  	buf := make([]byte, 4, 16)
   200  	data, _ := x.AppendText(buf)
   201  	if string(data[4:]) != "<nil>" {
   202  		t.Errorf("got %q, want <nil>", data[4:])
   203  	}
   204  }
   205  

View as plain text