ints_test.mx raw

   1  package ints
   2  
   3  import "testing"
   4  
   5  type marshalCase struct {
   6  	n    uint64
   7  	want string
   8  }
   9  
  10  // TestMarshal covers the base-10^4 chunking across every power boundary.
  11  func TestMarshal(t *testing.T) {
  12  	cases := []marshalCase{
  13  		{0, "0"},
  14  		{1, "1"},
  15  		{9, "9"},
  16  		{10, "10"},
  17  		{9999, "9999"},
  18  		{10000, "10000"},
  19  		{10001, "10001"},
  20  		{99999999, "99999999"},
  21  		{100000000, "100000000"},
  22  		{123456789, "123456789"},
  23  		{12345678901234567890, "12345678901234567890"},
  24  	}
  25  	for _, c := range cases {
  26  		got := string(New(c.n).Marshal(nil))
  27  		if got != c.want {
  28  			t.Fatalf("Marshal(%d) = %s, want %s", c.n, got, c.want)
  29  		}
  30  	}
  31  }
  32  
  33  // TestMarshalAppends checks that Marshal writes after existing dst content, the
  34  // way it is used when building a JSON array by hand.
  35  func TestMarshalAppends(t *testing.T) {
  36  	if got := string(New(uint64(42)).Marshal([]byte("x="))); got != "x=42" {
  37  		t.Fatalf("Marshal into dst: got %s", got)
  38  	}
  39  	if got := string(New(uint64(0)).Marshal([]byte("x="))); got != "x=0" {
  40  		t.Fatalf("Marshal zero into dst: got %s", got)
  41  	}
  42  }
  43  
  44  func TestUnmarshal(t *testing.T) {
  45  	n := New(uint64(0))
  46  	rem, err := n.Unmarshal([]byte("12345"))
  47  	if err != nil {
  48  		t.Fatal(err)
  49  	}
  50  	if n.Uint64() != 12345 || len(rem) != 0 {
  51  		t.Fatalf("Unmarshal 12345: got %d", n.Uint64())
  52  	}
  53  	// A trailing delimiter stays in the remainder.
  54  	rem, err = n.Unmarshal([]byte("678,"))
  55  	if err != nil {
  56  		t.Fatal(err)
  57  	}
  58  	if n.Uint64() != 678 || string(rem) != "," {
  59  		t.Fatalf("Unmarshal remainder: got %s", string(rem))
  60  	}
  61  	// A leading zero is a complete value on its own.
  62  	rem, err = n.Unmarshal([]byte("0abc"))
  63  	if err != nil {
  64  		t.Fatal(err)
  65  	}
  66  	if n.Uint64() != 0 || string(rem) != "abc" {
  67  		t.Fatalf("Unmarshal zero: got %s", string(rem))
  68  	}
  69  	if _, err = n.Unmarshal(nil); err == nil {
  70  		t.Fatal("Unmarshal of nothing should fail")
  71  	}
  72  	if _, err = n.Unmarshal([]byte("1234567890123456789012")); err == nil {
  73  		t.Fatal("Unmarshal of an over-long number should fail")
  74  	}
  75  }
  76  
  77  // TestRoundTrip is the property the JSON writers rely on.
  78  func TestRoundTrip(t *testing.T) {
  79  	vals := []uint64{0, 1, 10, 9999, 10000, 4294967296, 12345678901234567890}
  80  	for _, v := range vals {
  81  		text := New(v).Marshal(nil)
  82  		n := New(uint64(0))
  83  		if _, err := n.Unmarshal(text); err != nil {
  84  			t.Fatal(err)
  85  		}
  86  		if n.Uint64() != v {
  87  			t.Fatalf("roundtrip: got %d want %d", n.Uint64(), v)
  88  		}
  89  	}
  90  }
  91  
  92  func TestAccessors(t *testing.T) {
  93  	n := New(uint64(65535))
  94  	if n.Int64() != 65535 {
  95  		t.Fatal("Int64")
  96  	}
  97  	if n.Uint16() != 65535 {
  98  		t.Fatal("Uint16")
  99  	}
 100  }
 101