package ber import ( "bytes" "testing" ) type bitTest struct { bytes []byte bits BitString } var bitTestData = []bitTest{ { []byte{0, 8, 0}, BitString{ false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, }, }, { []byte{7, 0x80}, BitString{true}, }, { []byte{4, 0x20}, BitString{false, false, true, false}, }, } func bitsEqual(a, b BitString) bool { if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true } func TestBitString(t *testing.T) { for _, test := range bitTestData { bi := UnmarshalBitString(test.bytes) if !bitsEqual(bi, test.bits) { t.Error(test.bytes, "expexted", test.bits, "got", bi) } by := MarshalBitString(test.bits) if !bytes.Equal(by, test.bytes) { t.Error(test.bits, "expexted", test.bytes, "got", by) } } } func TestSet(t *testing.T) { bs := BitString{true, false} bs.Set(4) ex := BitString{true, false, false, false, true, false, false, false} if !bs.Equal(ex) { t.Error("expexted", ex, "got", bs) } bs = bs[:4] bs.Set(3) ex = BitString{true, false, false, true} if !bs.Equal(ex) { t.Error("expexted", ex, "got", bs) } bs.Clear(0) if bs.IsSet(0) { t.Error("got", bs) } }