go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/recordio/size_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 recordio 16 17 import ( 18 "bytes" 19 "fmt" 20 "io" 21 "testing" 22 23 . "github.com/smartystreets/goconvey/convey" 24 ) 25 26 func TestFrameHeaderSize(t *testing.T) { 27 t.Parallel() 28 29 Convey(`Testing FrameHeaderSize`, t, func() { 30 31 Convey(`Matches actual written frame size`, func() { 32 prev := -1 33 for i := 0; i < 3; i++ { 34 base := 1 << uint64(7*i) 35 for _, delta := range []int{-1, 0, 1} { 36 base += delta 37 if base <= prev { 38 // Over/underflow, skip. 39 continue 40 } 41 prev = base 42 43 Convey(fmt.Sprintf(`Frame size %d.`, base), func() { 44 data := bytes.Repeat([]byte{0x55}, int(base)) 45 46 amt, err := WriteFrame(io.Discard, data) 47 if err != nil { 48 panic(err) 49 } 50 51 So(amt-len(data), ShouldEqual, FrameHeaderSize(int64(len(data)))) 52 }) 53 } 54 } 55 }) 56 57 Convey(`Matches written frame header size (no alloc)`, func() { 58 prev, first := int64(0), true 59 for i := 0; i < 9; i++ { 60 for _, delta := range []int64{-1, 0, 1} { 61 base := int64(1<<uint64(7*i)) + delta 62 if (!first) && base <= prev { 63 // Repeated value, skip. 64 continue 65 } 66 prev, first = base, false 67 68 Convey(fmt.Sprintf(`Frame size %d.`, base), func() { 69 amt, err := writeFrameHeader(io.Discard, uint64(base)) 70 if err != nil { 71 panic(err) 72 } 73 74 So(amt, ShouldEqual, FrameHeaderSize(int64(base))) 75 }) 76 } 77 } 78 }) 79 }) 80 }