| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package base128 |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "fmt" |
| 10 "testing" |
| 11 |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 ) |
| 14 |
| 15 func TestBase128(t *testing.T) { |
| 16 t.Parallel() |
| 17 |
| 18 Convey("base128", t, func() { |
| 19 Convey("lengths", func() { |
| 20 cases := []struct { |
| 21 dec, enc int |
| 22 }{ |
| 23 {0, 0}, |
| 24 {1, 2}, |
| 25 {6, 7}, |
| 26 {7, 8}, |
| 27 {8, 10}, |
| 28 {9, 11}, |
| 29 } |
| 30 for _, c := range cases { |
| 31 Convey(fmt.Sprintf("enc:%d dec:%d", c.enc, c.dec
), func() { |
| 32 So(DecodedLen(c.enc), ShouldEqual, c.dec
) |
| 33 So(EncodedLen(c.dec), ShouldEqual, c.enc
) |
| 34 So(EncodedLen(DecodedLen(c.enc)), Should
Equal, c.enc) |
| 35 So(DecodedLen(EncodedLen(c.dec)), Should
Equal, c.dec) |
| 36 }) |
| 37 } |
| 38 }) |
| 39 |
| 40 Convey("errors", func() { |
| 41 Convey("bad length", func() { |
| 42 _, err := Decode(nil, []byte{0xff}) |
| 43 So(err, ShouldEqual, ErrLength) |
| 44 |
| 45 _, err = Decode(nil, bytes.Repeat([]byte{0xff},
9)) |
| 46 So(err, ShouldEqual, ErrLength) |
| 47 |
| 48 So(func() { Decode(nil, []byte{0xff, 0xff}) }, S
houldPanic) |
| 49 }) |
| 50 |
| 51 Convey("bad byte", func() { |
| 52 _, err := Decode([]byte{0}, []byte{0x7f, 0xff}) |
| 53 So(err, ShouldEqual, ErrBit) |
| 54 }) |
| 55 }) |
| 56 |
| 57 Convey("bytes", func() { |
| 58 cases := []struct { |
| 59 dec, enc []byte |
| 60 }{ |
| 61 {[]byte{}, []byte{}}, |
| 62 {[]byte{0xff}, []byte{0x7f, 0x40}}, |
| 63 { |
| 64 bytes.Repeat([]byte{0xff}, 6), |
| 65 append(bytes.Repeat([]byte{0x7f}, 6), 0x
7e), |
| 66 }, |
| 67 { |
| 68 bytes.Repeat([]byte{0xff}, 7), |
| 69 bytes.Repeat([]byte{0x7f}, 8), |
| 70 }, |
| 71 { |
| 72 bytes.Repeat([]byte{0xff}, 8), |
| 73 append(bytes.Repeat([]byte{0x7f}, 9), 0x
40), |
| 74 }, |
| 75 } |
| 76 for _, c := range cases { |
| 77 Convey(fmt.Sprintf("%q -> %q", c.dec, c.enc), fu
nc() { |
| 78 buf := make([]byte, len(c.enc)) |
| 79 So(Encode(buf, c.dec), ShouldEqual, len(
buf)) |
| 80 So(buf, ShouldResemble, c.enc) |
| 81 |
| 82 buf = make([]byte, len(c.dec)) |
| 83 n, err := Decode(buf, c.enc) |
| 84 So(err, ShouldBeNil) |
| 85 So(n, ShouldEqual, len(buf)) |
| 86 So(buf, ShouldResemble, c.dec) |
| 87 }) |
| 88 } |
| 89 }) |
| 90 }) |
| 91 } |
| OLD | NEW |