Chromium Code Reviews| Index: common/data/base128/base128_test.go |
| diff --git a/common/data/base128/base128_test.go b/common/data/base128/base128_test.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74d387857ca6e5add88c556e23cec20ad2c76fc9 |
| --- /dev/null |
| +++ b/common/data/base128/base128_test.go |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2016 The LUCI Authors. All rights reserved. |
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +package base128 |
| + |
| +import ( |
| + "bytes" |
| + "fmt" |
| + "testing" |
| + |
| + . "github.com/smartystreets/goconvey/convey" |
| +) |
| + |
| +func TestBase128(t *testing.T) { |
| + t.Parallel() |
| + |
| + Convey("base128", t, func() { |
| + Convey("lengths", func() { |
| + cases := []struct { |
| + dec, enc int |
| + }{ |
| + {0, 0}, |
| + {1, 2}, |
| + {6, 7}, |
| + {7, 8}, |
| + {8, 10}, |
| + {9, 11}, |
|
dnj
2016/11/29 02:03:13
It's a conspiracy!
|
| + } |
| + for _, c := range cases { |
| + Convey(fmt.Sprintf("enc:%d dec:%d", c.enc, c.dec), func() { |
| + So(DecodedLen(c.enc), ShouldEqual, c.dec) |
| + So(EncodedLen(c.dec), ShouldEqual, c.enc) |
| + So(EncodedLen(DecodedLen(c.enc)), ShouldEqual, c.enc) |
| + So(DecodedLen(EncodedLen(c.dec)), ShouldEqual, c.dec) |
| + }) |
| + } |
| + }) |
| + |
| + Convey("errors", func() { |
| + Convey("bad length", func() { |
| + _, err := Decode(nil, []byte{0xff}) |
| + So(err, ShouldEqual, ErrLength) |
| + |
| + _, err = Decode(nil, bytes.Repeat([]byte{0xff}, 9)) |
| + So(err, ShouldEqual, ErrLength) |
| + |
| + So(func() { Decode(nil, []byte{0xff, 0xff}) }, ShouldPanic) |
| + }) |
| + |
| + Convey("bad byte", func() { |
| + _, err := Decode([]byte{0}, []byte{0x7f, 0xff}) |
| + So(err, ShouldEqual, ErrBit) |
| + }) |
| + }) |
| + |
| + Convey("bytes", func() { |
| + cases := []struct { |
| + dec, enc []byte |
| + }{ |
| + {[]byte{}, []byte{}}, |
| + {[]byte{0xff}, []byte{0x7f, 0x40}}, |
| + { |
| + bytes.Repeat([]byte{0xff}, 6), |
| + append(bytes.Repeat([]byte{0x7f}, 6), 0x7e), |
| + }, |
| + { |
| + bytes.Repeat([]byte{0xff}, 7), |
| + bytes.Repeat([]byte{0x7f}, 8), |
| + }, |
| + { |
| + bytes.Repeat([]byte{0xff}, 8), |
| + append(bytes.Repeat([]byte{0x7f}, 9), 0x40), |
| + }, |
| + } |
| + for _, c := range cases { |
| + Convey(fmt.Sprintf("%q -> %q", c.dec, c.enc), func() { |
| + buf := make([]byte, len(c.enc)) |
| + So(Encode(buf, c.dec), ShouldEqual, len(buf)) |
| + So(buf, ShouldResemble, c.enc) |
| + |
| + buf = make([]byte, len(c.dec)) |
| + n, err := Decode(buf, c.enc) |
| + So(err, ShouldBeNil) |
| + So(n, ShouldEqual, len(buf)) |
| + So(buf, ShouldResemble, c.dec) |
| + }) |
| + } |
| + }) |
| + }) |
| +} |