From 7a80548ea89c9a07686a33df0ae0836e407d7a1b Mon Sep 17 00:00:00 2001 From: David Howden Date: Sun, 3 May 2015 09:59:03 +1000 Subject: [PATCH] Add some basic tests for utils --- util_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 util_test.go diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000..aae0bcd --- /dev/null +++ b/util_test.go @@ -0,0 +1,81 @@ +// Copyright 2015, David Howden +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package tag + +import ( + "testing" +) + +func TestGetBit(t *testing.T) { + for i := uint(0); i < 8; i++ { + b := byte(1 << i) + got := getBit(b, i) + if !got { + t.Errorf("getBit(%v, %v) = %v, expected %v", b, i, got, true) + } + } +} + +func TestGet7BitChunkedInt(t *testing.T) { + tests := []struct { + input []byte + output int + }{ + { + []byte{}, + 0, + }, + { + []byte{0x01}, + 1, + }, + { + []byte{0x7F, 0x7F}, + 0x3FFF, + }, + } + + for ii, tt := range tests { + got := get7BitChunkedInt(tt.input) + if got != tt.output { + t.Errorf("[%d] get7BitChunkedInt(%v) = %v, expected %v", ii, tt.input, got, tt.output) + } + } +} + +func TestGetInt(t *testing.T) { + tests := []struct { + input []byte + output int + }{ + { + []byte{}, + 0, + }, + { + []byte{0x01}, + 1, + }, + { + []byte{0xF1, 0xF2}, + 0xF1F2, + }, + { + []byte{0xF1, 0xF2, 0xF3}, + 0xF1F2F3, + }, + { + []byte{0xF1, 0xF2, 0xF3, 0xF4}, + 0xF1F2F3F4, + }, + } + + for ii, tt := range tests { + got := getInt(tt.input) + if got != tt.output { + t.Errorf("[%d] getInt(%v) = %v, expected %v", ii, tt.input, got, tt.output) + } + } +}