aboutsummaryrefslogtreecommitdiff
path: root/ber/bitstring_test.go
blob: 3c67228b56771ad7910fc2de13e4d26a63583416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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)
	}
}