aboutsummaryrefslogtreecommitdiff
path: root/ber
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-06-26 19:40:20 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-06-26 19:40:20 +0200
commitdeb063f01cfa8ffa66cd76174691bd27ececdbcd (patch)
treed51c3431d34fdd29ee05f78a02032a20fda580de /ber
parent5300328fae556b759c17565de06d9781361aabd4 (diff)
Add bool type
Diffstat (limited to 'ber')
-rw-r--r--ber/bool.go12
-rw-r--r--ber/bool_test.go22
-rw-r--r--ber/int_test.go20
3 files changed, 44 insertions, 10 deletions
diff --git a/ber/bool.go b/ber/bool.go
new file mode 100644
index 0000000..3bff693
--- /dev/null
+++ b/ber/bool.go
@@ -0,0 +1,12 @@
+package ber
+
+func unmarshalBool(b byte) bool {
+ return b != 0x00
+}
+
+func marshalBool(b bool) byte {
+ if b {
+ return 0xFF
+ }
+ return 0x00
+}
diff --git a/ber/bool_test.go b/ber/bool_test.go
new file mode 100644
index 0000000..47cac82
--- /dev/null
+++ b/ber/bool_test.go
@@ -0,0 +1,22 @@
+package ber
+
+import "testing"
+
+func testBool(t *testing.T, v bool, b byte) {
+ a := marshalBool(v)
+ if (a & 1) != (b & 1) {
+ t.Error("BER", v, "expected", b, "got", a)
+ }
+
+ n := unmarshalBool(b)
+ if n != v {
+ t.Error("UnBER", b, "expected", v, "got", n)
+ }
+
+}
+
+func TestBool(t *testing.T) {
+ testBool(t, true, 0xFF)
+ testBool(t, true, 0x01)
+ testBool(t, false, 0x00)
+}
diff --git a/ber/int_test.go b/ber/int_test.go
index 03bb6ad..03139c1 100644
--- a/ber/int_test.go
+++ b/ber/int_test.go
@@ -5,7 +5,7 @@ import (
"testing"
)
-func test(t *testing.T, i int, b []byte) {
+func testInt(t *testing.T, i int, b []byte) {
a := marshalInt(i)
if !bytes.Equal(a, b) {
t.Error("BER", i, "expected", b, "got", a)
@@ -19,14 +19,14 @@ func test(t *testing.T, i int, b []byte) {
}
func TestInt(t *testing.T) {
- test(t, 0, []byte{0x00})
- test(t, 127, []byte{0x7F})
- test(t, 128, []byte{0x00, 0x80})
- test(t, 256, []byte{0x01, 0x00})
- test(t, -128, []byte{0x80})
- test(t, -129, []byte{0xFF, 0x7F})
+ testInt(t, 0, []byte{0x00})
+ testInt(t, 127, []byte{0x7F})
+ testInt(t, 128, []byte{0x00, 0x80})
+ testInt(t, 256, []byte{0x01, 0x00})
+ testInt(t, -128, []byte{0x80})
+ testInt(t, -129, []byte{0xFF, 0x7F})
- test(t, 8388607, []byte{0x7f, 0xFF, 0xFF})
- test(t, -136, []byte{0xFF, 0x78})
- test(t, -8388607, []byte{0x80, 0x00, 0x01})
+ testInt(t, 8388607, []byte{0x7f, 0xFF, 0xFF})
+ testInt(t, -136, []byte{0xFF, 0x78})
+ testInt(t, -8388607, []byte{0x80, 0x00, 0x01})
}