github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/encoding/printer_test.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package encoding_test 12 13 import ( 14 "bytes" 15 "fmt" 16 "testing" 17 18 "github.com/cockroachdb/cockroach/pkg/roachpb" 19 "github.com/cockroachdb/cockroach/pkg/util/encoding" 20 ) 21 22 func TestUndoPrefixEnd(t *testing.T) { 23 for _, tc := range []struct { 24 in []byte 25 out []byte 26 }{ 27 {[]byte{0x00, 0x01}, []byte{0x00, 0x00}}, 28 {[]byte{0x01, 0x02, 0x03, 0x05}, []byte{0x01, 0x02, 0x03, 0x04}}, 29 {[]byte{0xff, 0xff}, []byte{0xff, 0xfe}}, 30 {[]byte{0xff}, []byte{0xfe}}, 31 32 // Invalid keys 33 {[]byte{0x00}, nil}, 34 {[]byte{0x01, 0x00}, nil}, 35 } { 36 t.Run(fmt.Sprintf("undo-prefix/key=%q", tc.in), func(t *testing.T) { 37 result, ok := encoding.UndoPrefixEnd(tc.in) 38 if !ok { 39 result = nil 40 } 41 if !bytes.Equal(tc.out, result) { 42 t.Errorf("expected %q but got %q", tc.out, result) 43 } 44 }) 45 } 46 47 for _, k := range [][]byte{ 48 {0x00}, 49 {0x00, 0x00}, 50 {0x00, 0x01}, 51 {0x01, 0x00, 0xff, 0x00}, 52 {0x00, 0x00, 0x00, 0x00}, 53 {0x01, 0x02, 0x03, 0x04}, 54 // Keys that end in 0xff do not roundtrip. 55 } { 56 t.Run(fmt.Sprintf("roundtrip/key=%q", k), func(t *testing.T) { 57 if r, ok := encoding.UndoPrefixEnd(roachpb.Key(k).PrefixEnd()); !ok || !bytes.Equal(k, r) { 58 t.Errorf("roundtripping resulted in %q", r) 59 } 60 }) 61 } 62 63 }