go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/cmpbin/binary_tools_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  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestBinaryTools(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	Convey("Test Join", t, func() {
    27  		Convey("returns bytes with nil separator", func() {
    28  			join := ConcatBytes([]byte("hello"), []byte("world"))
    29  			So(join, ShouldResemble, []byte("helloworld"))
    30  		})
    31  	})
    32  
    33  	Convey("Test Invert", t, func() {
    34  		Convey("returns nil for nil input", func() {
    35  			inv := InvertBytes(nil)
    36  			So(inv, ShouldBeNil)
    37  		})
    38  
    39  		Convey("returns nil for empty input", func() {
    40  			inv := InvertBytes([]byte{})
    41  			So(inv, ShouldBeNil)
    42  		})
    43  
    44  		Convey("returns byte slice of same length as input", func() {
    45  			input := []byte("こんにちは, world")
    46  			inv := InvertBytes(input)
    47  			So(len(input), ShouldEqual, len(inv))
    48  		})
    49  
    50  		Convey("returns byte slice with each byte inverted", func() {
    51  			inv := InvertBytes([]byte("foo"))
    52  			So(inv, ShouldResemble, []byte{153, 144, 144})
    53  		})
    54  	})
    55  
    56  	Convey("Test Increment", t, func() {
    57  		Convey("returns empty slice and overflow true when input is nil", func() {
    58  			incr, overflow := IncrementBytes(nil)
    59  			So(incr, ShouldBeNil)
    60  			So(overflow, ShouldBeTrue)
    61  		})
    62  
    63  		Convey("returns empty slice and overflow true when input is empty", func() {
    64  			incr, overflow := IncrementBytes([]byte{})
    65  			So(incr, ShouldBeNil)
    66  			So(overflow, ShouldBeTrue)
    67  		})
    68  
    69  		Convey("handles overflow", func() {
    70  			incr, overflow := IncrementBytes([]byte{0xFF, 0xFF})
    71  			So(incr, ShouldResemble, []byte{0, 0})
    72  			So(overflow, ShouldBeTrue)
    73  		})
    74  
    75  		Convey("increments with overflow false when there is no overflow", func() {
    76  			incr, overflow := IncrementBytes([]byte{0xCA, 0xFF, 0xFF})
    77  			So(incr, ShouldResemble, []byte{0xCB, 0, 0})
    78  			So(overflow, ShouldBeFalse)
    79  		})
    80  	})
    81  }