go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/cmpbin/invertible_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmpbin
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  func TestInvertible(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("Test InvertibleByteBuffer", t, func() {
    28  		inv := Invertible(&bytes.Buffer{})
    29  
    30  		Convey("normal writing", func() {
    31  			Convey("Write", func() {
    32  				n, err := inv.Write([]byte("hello"))
    33  				So(err, ShouldBeNil)
    34  				So(n, ShouldEqual, 5)
    35  				So(inv.String(), ShouldEqual, "hello")
    36  			})
    37  			Convey("WriteString", func() {
    38  				n, err := inv.WriteString("hello")
    39  				So(err, ShouldBeNil)
    40  				So(n, ShouldEqual, 5)
    41  				So(inv.String(), ShouldEqual, "hello")
    42  			})
    43  			Convey("WriteByte", func() {
    44  				for i := byte('a'); i < 'f'; i++ {
    45  					err := inv.WriteByte(i)
    46  					So(err, ShouldBeNil)
    47  				}
    48  				So(inv.String(), ShouldEqual, "abcde")
    49  
    50  				Convey("ReadByte", func() {
    51  					for i := 0; i < 5; i++ {
    52  						b, err := inv.ReadByte()
    53  						So(err, ShouldBeNil)
    54  						So(b, ShouldEqual, byte('a')+byte(i))
    55  					}
    56  				})
    57  			})
    58  		})
    59  		Convey("inverted writing", func() {
    60  			inv.SetInvert(true)
    61  			Convey("Write", func() {
    62  				n, err := inv.Write([]byte("hello"))
    63  				So(err, ShouldBeNil)
    64  				So(n, ShouldEqual, 5)
    65  				So(inv.String(), ShouldEqual, "\x97\x9a\x93\x93\x90")
    66  			})
    67  			Convey("WriteString", func() {
    68  				n, err := inv.WriteString("hello")
    69  				So(err, ShouldBeNil)
    70  				So(n, ShouldEqual, 5)
    71  				So(inv.String(), ShouldEqual, "\x97\x9a\x93\x93\x90")
    72  			})
    73  			Convey("WriteByte", func() {
    74  				for i := byte('a'); i < 'f'; i++ {
    75  					err := inv.WriteByte(i)
    76  					So(err, ShouldBeNil)
    77  				}
    78  				So(inv.String(), ShouldEqual, "\x9e\x9d\x9c\x9b\x9a")
    79  
    80  				Convey("ReadByte", func() {
    81  					for i := 0; i < 5; i++ {
    82  						b, err := inv.ReadByte()
    83  						So(err, ShouldBeNil)
    84  						So(b, ShouldEqual, byte('a')+byte(i)) // inverted back to normal
    85  					}
    86  				})
    87  			})
    88  		})
    89  		Convey("Toggleable", func() {
    90  			inv.SetInvert(true)
    91  			n, err := inv.Write([]byte("hello"))
    92  			So(err, ShouldBeNil)
    93  			inv.SetInvert(false)
    94  			n, err = inv.Write([]byte("hello"))
    95  			So(n, ShouldEqual, 5)
    96  			So(inv.String(), ShouldEqual, "\x97\x9a\x93\x93\x90hello")
    97  		})
    98  	})
    99  }