gitee.com/quant1x/gox@v1.21.2/encoding/binary/struc/types_test.go (about) 1 package struc 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestBadType(t *testing.T) { 9 defer func() { recover() }() 10 Type(-1).Size() 11 t.Fatal("failed to panic for invalid Type.Size()") 12 } 13 14 func TestTypeString(t *testing.T) { 15 if Pad.String() != "pad" { 16 t.Fatal("type string representation failed") 17 } 18 } 19 20 type sizeOffTest struct { 21 Size Size_t 22 Off Off_t 23 } 24 25 func TestSizeOffTypes(t *testing.T) { 26 bits := []int{8, 16, 32, 64} 27 var buf bytes.Buffer 28 test := &sizeOffTest{1, 2} 29 for _, b := range bits { 30 if err := PackWithOptions(&buf, test, &Options{PtrSize: b}); err != nil { 31 t.Fatal(err) 32 } 33 } 34 reference := []byte{ 35 1, 2, 36 0, 1, 0, 2, 37 0, 0, 0, 1, 0, 0, 0, 2, 38 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 39 } 40 if !bytes.Equal(reference, buf.Bytes()) { 41 t.Errorf("reference: %v != bytes: %v", reference, buf.Bytes()) 42 } 43 reader := bytes.NewReader(buf.Bytes()) 44 for _, b := range bits { 45 out := &sizeOffTest{} 46 if err := UnpackWithOptions(reader, out, &Options{PtrSize: b}); err != nil { 47 t.Fatal(err) 48 } 49 if out.Size != 1 || out.Off != 2 { 50 t.Errorf("Size_t/Off_t mismatch: {%d, %d}\n%v", out.Size, out.Off, buf.Bytes()) 51 } 52 } 53 }