Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: common/data/base128/base128_test.go

Issue 2538553003: Add base128 encoding library. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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},
dnj 2016/11/29 02:03:13 It's a conspiracy!
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 }
OLDNEW
« common/data/base128/base128.go ('K') | « common/data/base128/base128.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698