github.com/jackc/pgx/v5@v5.5.5/pgtype/bits_test.go (about) 1 package pgtype_test 2 3 import ( 4 "bytes" 5 "context" 6 "testing" 7 8 "github.com/jackc/pgx/v5/pgtype" 9 "github.com/jackc/pgx/v5/pgxtest" 10 ) 11 12 func isExpectedEqBits(a any) func(any) bool { 13 return func(v any) bool { 14 ab := a.(pgtype.Bits) 15 vb := v.(pgtype.Bits) 16 return bytes.Equal(ab.Bytes, vb.Bytes) && ab.Len == vb.Len && ab.Valid == vb.Valid 17 } 18 } 19 20 func TestBitsCodecBit(t *testing.T) { 21 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "bit(40)", []pgxtest.ValueRoundTripTest{ 22 { 23 pgtype.Bits{Bytes: []byte{0, 0, 0, 0, 0}, Len: 40, Valid: true}, 24 new(pgtype.Bits), 25 isExpectedEqBits(pgtype.Bits{Bytes: []byte{0, 0, 0, 0, 0}, Len: 40, Valid: true}), 26 }, 27 { 28 pgtype.Bits{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Valid: true}, 29 new(pgtype.Bits), 30 isExpectedEqBits(pgtype.Bits{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Valid: true}), 31 }, 32 {pgtype.Bits{}, new(pgtype.Bits), isExpectedEqBits(pgtype.Bits{})}, 33 {nil, new(pgtype.Bits), isExpectedEqBits(pgtype.Bits{})}, 34 }) 35 } 36 37 func TestBitsCodecVarbit(t *testing.T) { 38 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "varbit", []pgxtest.ValueRoundTripTest{ 39 { 40 pgtype.Bits{Bytes: []byte{}, Len: 0, Valid: true}, 41 new(pgtype.Bits), 42 isExpectedEqBits(pgtype.Bits{Bytes: []byte{}, Len: 0, Valid: true}), 43 }, 44 { 45 pgtype.Bits{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Valid: true}, 46 new(pgtype.Bits), 47 isExpectedEqBits(pgtype.Bits{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Valid: true}), 48 }, 49 { 50 pgtype.Bits{Bytes: []byte{0, 1, 128, 254, 128}, Len: 33, Valid: true}, 51 new(pgtype.Bits), 52 isExpectedEqBits(pgtype.Bits{Bytes: []byte{0, 1, 128, 254, 128}, Len: 33, Valid: true}), 53 }, 54 {pgtype.Bits{}, new(pgtype.Bits), isExpectedEqBits(pgtype.Bits{})}, 55 {nil, new(pgtype.Bits), isExpectedEqBits(pgtype.Bits{})}, 56 }) 57 }