OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package rawdatastore |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "testing" |
| 10 |
| 11 . "github.com/smartystreets/goconvey/convey" |
| 12 ) |
| 13 |
| 14 func TestInvertible(t *testing.T) { |
| 15 t.Parallel() |
| 16 |
| 17 Convey("Test InvertibleByteBuffer", t, func() { |
| 18 inv := Invertible(&bytes.Buffer{}) |
| 19 |
| 20 Convey("normal writing", func() { |
| 21 Convey("Write", func() { |
| 22 n, err := inv.Write([]byte("hello")) |
| 23 So(err, ShouldBeNil) |
| 24 So(n, ShouldEqual, 5) |
| 25 So(inv.String(), ShouldEqual, "hello") |
| 26 }) |
| 27 Convey("WriteString", func() { |
| 28 n, err := inv.WriteString("hello") |
| 29 So(err, ShouldBeNil) |
| 30 So(n, ShouldEqual, 5) |
| 31 So(inv.String(), ShouldEqual, "hello") |
| 32 }) |
| 33 Convey("WriteByte", func() { |
| 34 for i := byte('a'); i < 'f'; i++ { |
| 35 err := inv.WriteByte(i) |
| 36 So(err, ShouldBeNil) |
| 37 } |
| 38 So(inv.String(), ShouldEqual, "abcde") |
| 39 |
| 40 Convey("ReadByte", func() { |
| 41 for i := 0; i < 5; i++ { |
| 42 b, err := inv.ReadByte() |
| 43 So(err, ShouldBeNil) |
| 44 So(b, ShouldEqual, byte('a')+byt
e(i)) |
| 45 } |
| 46 }) |
| 47 }) |
| 48 }) |
| 49 Convey("inverted writing", func() { |
| 50 inv.Invert() |
| 51 Convey("Write", func() { |
| 52 n, err := inv.Write([]byte("hello")) |
| 53 So(err, ShouldBeNil) |
| 54 So(n, ShouldEqual, 5) |
| 55 So(inv.String(), ShouldEqual, "\x97\x9a\x93\x93\
x90") |
| 56 }) |
| 57 Convey("WriteString", func() { |
| 58 n, err := inv.WriteString("hello") |
| 59 So(err, ShouldBeNil) |
| 60 So(n, ShouldEqual, 5) |
| 61 So(inv.String(), ShouldEqual, "\x97\x9a\x93\x93\
x90") |
| 62 }) |
| 63 Convey("WriteByte", func() { |
| 64 for i := byte('a'); i < 'f'; i++ { |
| 65 err := inv.WriteByte(i) |
| 66 So(err, ShouldBeNil) |
| 67 } |
| 68 So(inv.String(), ShouldEqual, "\x9e\x9d\x9c\x9b\
x9a") |
| 69 |
| 70 Convey("ReadByte", func() { |
| 71 for i := 0; i < 5; i++ { |
| 72 b, err := inv.ReadByte() |
| 73 So(err, ShouldBeNil) |
| 74 So(b, ShouldEqual, byte('a')+byt
e(i)) // inverted back to normal |
| 75 } |
| 76 }) |
| 77 }) |
| 78 }) |
| 79 Convey("Toggleable", func() { |
| 80 inv.Invert() |
| 81 n, err := inv.Write([]byte("hello")) |
| 82 So(err, ShouldBeNil) |
| 83 inv.Invert() |
| 84 n, err = inv.Write([]byte("hello")) |
| 85 So(n, ShouldEqual, 5) |
| 86 So(inv.String(), ShouldEqual, "\x97\x9a\x93\x93\x90hello
") |
| 87 }) |
| 88 }) |
| 89 } |
OLD | NEW |