| 1 | // Copyright ©2017 The bíogo Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package itf8 |
| 6 | |
| 7 | import "testing" |
| 8 | |
| 9 | func TestRoundTrip(t *testing.T) { |
| 10 | b := make([]byte, 6) |
| 11 | for i := uint(0); i < 32; i++ { |
| 12 | for off := -1; off <= 1; off++ { |
| 13 | in := int32(1<<i + off) |
| 14 | inn := Encode(b, in) |
| 15 | wantn := Len(in) |
| 16 | if wantn != inn { |
| 17 | t.Errorf("disagreement in number of encoded bytes required: want=%d need=%d", wantn, inn) |
| 18 | } |
| 19 | out, outn, ok := Decode(b) |
| 20 | if !ok { |
| 21 | t.Errorf("failed to decode ITF-8 bytes: %08b", b[:inn]) |
| 22 | } |
| 23 | if inn != outn { |
| 24 | t.Errorf("disagreement in number of encoded bytes: in=%d out=%d", inn, outn) |
| 25 | } |
| 26 | if in != out { |
| 27 | t.Errorf("disagreement in encoded value: in=%d (0x%[1]x) out=%d (0x%[2]x)\nencoding=%08b", in, out, b[:inn]) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestKnownValues(t *testing.T) { |
| 34 | tests := []struct { |
| 35 | bytes []byte |
| 36 | want int32 |
| 37 | }{ |
| 38 | {bytes: []byte{0xff, 0xff, 0xff, 0xff, 0x0f}, want: -1}, |
| 39 | {bytes: []byte{0xe0, 0x45, 0x4f, 0x46}, want: 4542278}, |
| 40 | } |
| 41 | |
| 42 | for _, test := range tests { |
| 43 | got, n, ok := Decode(test.bytes) |
| 44 | if !ok { |
| 45 | t.Errorf("failed to decode ITF-8 bytes: %08b", test.bytes) |
| 46 | } |
| 47 | if n != len(test.bytes) { |
| 48 | t.Errorf("disagreement in expected number of encoded bytes: n=%d len(b)=%d", n, len(test.bytes)) |
| 49 | } |
| 50 | if got != test.want { |
| 51 | t.Errorf("disagreement in encoded value: got=%d want=%d (0x%[2]x)", got, test.want) |
| 52 | } |
| 53 | } |
| 54 | } |