github.com/philpearl/plenc@v0.0.15/plenccodec/int_test.go (about) 1 package plenccodec_test 2 3 import ( 4 "math" 5 "strconv" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/philpearl/plenc" 10 "github.com/philpearl/plenc/plenccore" 11 ) 12 13 func TestVarUint(t *testing.T) { 14 tests := []uint64{ 15 0, 1, 2, 126, 127, 128, 255, 256, 257, 1024, 2048, 4096, 8192, 457239847, 27384971293, 98235472397459, math.MaxUint64, 16 } 17 18 for _, test := range tests { 19 t.Run(strconv.FormatUint(uint64(test), 10), func(t *testing.T) { 20 s := plenccore.SizeVarUint(test) 21 b := make([]byte, 0, s) 22 b = plenccore.AppendVarUint(b, test) 23 actual, l := plenccore.ReadVarUint(b) 24 25 if l != s { 26 t.Errorf("read %d bytes, expected %d", l, s) 27 } 28 29 if actual != test { 30 t.Errorf("actual %d does not match expected %d. %v", actual, test, b) 31 } 32 }) 33 t.Run(strconv.FormatUint(uint64(test), 10)+"_marshal", func(t *testing.T) { 34 data, err := plenc.Marshal(nil, &test) 35 if err != nil { 36 t.Fatal(err) 37 } 38 var out uint64 39 if err := plenc.Unmarshal(data, &out); err != nil { 40 t.Fatal(err) 41 } 42 if test != out { 43 t.Errorf("Result incorrect for %d- got %d", test, out) 44 } 45 }) 46 } 47 } 48 49 func TestVarInt(t *testing.T) { 50 tests := []int64{ 51 0, 1, -1, 2, -1, 126, -126, 127, -127, 128, -128, 254, 255, 256, 257, -254, -255, -256, -257, 1024, 2048, 4096, 8192, 457239847, 27384971293, 98235472397459, math.MaxInt64, math.MinInt64, 25018898, 52 } 53 54 for _, test := range tests { 55 t.Run(strconv.FormatInt(int64(test), 10), func(t *testing.T) { 56 s := plenccore.SizeVarInt(test) 57 b := make([]byte, 0, s) 58 b = plenccore.AppendVarInt(b, test) 59 actual, l := plenccore.ReadVarInt(b) 60 61 if l != s { 62 t.Errorf("read %d bytes, expected %d", l, s) 63 } 64 65 if actual != test { 66 t.Errorf("actual %d does not match expected %d. %v", actual, test, b) 67 } 68 }) 69 t.Run(strconv.FormatInt(int64(test), 10)+"_marshal", func(t *testing.T) { 70 data, err := plenc.Marshal(nil, &test) 71 if err != nil { 72 t.Fatal(err) 73 } 74 var out int64 75 if err := plenc.Unmarshal(data, &out); err != nil { 76 t.Fatal(err) 77 } 78 if test != out { 79 t.Errorf("Result incorrect for %d- got %d", test, out) 80 } 81 }) 82 } 83 } 84 85 func TestInts(t *testing.T) { 86 type my struct { 87 A int `plenc:"1"` 88 B int `plenc:"2,flat"` 89 C uint `plenc:"3"` 90 } 91 92 tests := []my{ 93 {A: 1, B: 2, C: 3}, 94 {A: 0, B: 0, C: 0}, 95 {A: -1, B: -2, C: 0}, 96 {A: math.MaxInt64, B: math.MaxInt64, C: math.MaxUint64}, 97 {A: math.MinInt64, B: math.MinInt64, C: 0}, 98 } 99 100 for _, test := range tests { 101 t.Run("", func(t *testing.T) { 102 data, err := plenc.Marshal(nil, &test) 103 if err != nil { 104 t.Fatal(err) 105 } 106 var out my 107 if err := plenc.Unmarshal(data, &out); err != nil { 108 t.Fatal(err) 109 } 110 if diff := cmp.Diff(test, out); diff != "" { 111 t.Fatal(diff) 112 } 113 }) 114 } 115 } 116 117 func TestZigZag(t *testing.T) { 118 tests := []struct { 119 in int64 120 exp uint64 121 }{ 122 {0, 0}, 123 {-1, 1}, 124 {1, 2}, 125 {-2, 3}, 126 {2, 4}, 127 {-2147483648, 4294967295}, 128 {12509449, 25018898}, 129 } 130 131 for _, test := range tests { 132 t.Run(strconv.FormatInt(test.in, 10), func(t *testing.T) { 133 if z := plenccore.ZigZag(test.in); z != test.exp { 134 t.Errorf("Expected %d got %d", test.exp, z) 135 } 136 }) 137 } 138 }