github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/bnry/bnry_test.go (about) 1 package bnry 2 3 import ( 4 "reflect" 5 "slices" 6 "testing" 7 ) 8 9 func TestMarshal(t *testing.T) { 10 a := byte(113) 11 b := uint64(2391278932173219) 12 c := "amit" 13 d := int16(10000) 14 e := []int32{1, 11, 100, 433223} 15 f := true 16 g := false 17 buf := MarshalBinary(a, b, c, d, e, f, g) 18 var aa byte 19 var bb uint64 20 var cc string 21 var dd int16 22 var ee []int32 23 var ff bool 24 var gg bool 25 if err := UnmarshalBinary( 26 buf, &aa, &bb, &cc, &dd, &ee, &ff, &gg); err != nil { 27 t.Fatalf("UnmarshalBinary(%v) failed: %v", buf, err) 28 } 29 if aa != a { 30 t.Errorf("UnmarshalBinary(...)=%v, want %v", aa, a) 31 } 32 if bb != b { 33 t.Errorf("UnmarshalBinary(...)=%v, want %v", bb, b) 34 } 35 if cc != c { 36 t.Errorf("UnmarshalBinary(...)=%v, want %v", cc, c) 37 } 38 if dd != d { 39 t.Errorf("UnmarshalBinary(...)=%v, want %v", dd, d) 40 } 41 if !slices.Equal(ee, e) { 42 t.Errorf("UnmarshalBinary(...)=%v, want %v", ee, e) 43 } 44 if ff != f { 45 t.Errorf("UnmarshalBinary(...)=%v, want %v", dd, d) 46 } 47 if gg != g { 48 t.Errorf("UnmarshalBinary(...)=%v, want %v", dd, d) 49 } 50 } 51 52 func FuzzMarshal(f *testing.F) { 53 f.Add(uint8(1), int16(1), uint32(1), int64(1), "", true, 1.0, float32(1)) 54 f.Fuzz(func(t *testing.T, a uint8, b int16, c uint32, d int64, e string, 55 g bool, h float64, i float32) { 56 buf := MarshalBinary(a, b, c, d, e, g, h, i) 57 var ( 58 aa uint8 59 bb int16 60 cc uint32 61 dd int64 62 ee string 63 gg bool 64 hh float64 65 ii float32 66 ) 67 err := UnmarshalBinary(buf, &aa, &bb, &cc, &dd, &ee, &gg, &hh, &ii) 68 if err != nil { 69 t.Fatal(err) 70 } 71 if aa != a { 72 t.Fatalf("got %v, want %v", aa, a) 73 } 74 if bb != b { 75 t.Fatalf("got %v, want %v", bb, b) 76 } 77 if cc != c { 78 t.Fatalf("got %v, want %v", cc, c) 79 } 80 if dd != d { 81 t.Fatalf("got %v, want %v", dd, d) 82 } 83 if ee != e { 84 t.Fatalf("got %v, want %v", ee, e) 85 } 86 if gg != g { 87 t.Fatalf("got %v, want %v", gg, g) 88 } 89 if hh != h { 90 t.Fatalf("got %v, want %v", hh, h) 91 } 92 if ii != i { 93 t.Fatalf("got %v, want %v", ii, i) 94 } 95 }) 96 } 97 98 func TestMarshal_slices(t *testing.T) { 99 a := []uint32{321321, 213, 4944} 100 b := []string{"this", "is", "", "a", "slice"} 101 c := []int8{100, 9, 0, -21} 102 d := []bool{true, false, false, true, true} 103 buf := MarshalBinary(slices.Clone(a), slices.Clone(b), 104 slices.Clone(c), slices.Clone(d)) 105 var ( 106 aa []uint32 107 bb []string 108 cc []int8 109 dd []bool 110 ) 111 err := UnmarshalBinary(buf, &aa, &bb, &cc, &dd) 112 if err != nil { 113 t.Fatal("UnmarshalBinary(...) failed:", err) 114 } 115 inputs := []any{a, b, c, d} 116 outputs := []any{aa, bb, cc, dd} 117 for i := range inputs { 118 if !reflect.DeepEqual(inputs[i], outputs[i]) { 119 t.Fatalf("UnmarshalBinary(...)=%v, want %v", outputs[i], inputs[i]) 120 } 121 } 122 } 123 124 func TestMarshal_single(t *testing.T) { 125 testMarshalSingle(t, int8(123)) 126 testMarshalSingle(t, uint8(123)) 127 testMarshalSingle(t, int32(12345)) 128 testMarshalSingle(t, uint32(12345)) 129 testMarshalSingle(t, int(12345)) 130 testMarshalSingle(t, uint(12345)) 131 testMarshalSingle(t, float64(33.33)) 132 testMarshalSingle(t, float32(33.33)) 133 testMarshalSingle(t, "amit") 134 } 135 136 func testMarshalSingle[T comparable](t *testing.T, val T) { 137 buf := MarshalBinary(val) 138 var got T 139 if err := UnmarshalBinary(buf, &got); err != nil { 140 t.Errorf("UnmarshalBinary(%#v) failed: %s", val, err) 141 return 142 } 143 if got != val { 144 t.Errorf("UnmarshalBinary(%#v)=%#v, want %#v", val, got, val) 145 } 146 }