github.com/cockroachdb/pebble@v1.1.2/internal/keyspan/span_test.go (about) 1 // Copyright 2022 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package keyspan 6 7 import ( 8 "bytes" 9 "fmt" 10 "strconv" 11 "strings" 12 "testing" 13 14 "github.com/cockroachdb/datadriven" 15 "github.com/stretchr/testify/require" 16 ) 17 18 // TODO(jackson): Add unit tests for all of the various Span methods. 19 20 func TestSpan_ParseRoundtrip(t *testing.T) { 21 spans := []string{ 22 "a-c:{(#5,RANGEDEL)}", 23 "a-c:{(#5,RANGEDEL) (#2,RANGEDEL)}", 24 "h-z:{(#20,RANGEKEYSET,@5,foo) (#15,RANGEKEYUNSET,@9) (#2,RANGEKEYDEL)}", 25 } 26 for _, input := range spans { 27 got := ParseSpan(input).String() 28 if got != input { 29 t.Errorf("ParseSpan(%q).String() = %q", input, got) 30 } 31 } 32 } 33 34 func TestSpan_Visible(t *testing.T) { 35 var s Span 36 datadriven.RunTest(t, "testdata/visible", func(t *testing.T, d *datadriven.TestData) string { 37 switch d.Cmd { 38 case "define": 39 s = ParseSpan(d.Input) 40 return fmt.Sprint(s) 41 case "visible": 42 var buf bytes.Buffer 43 for _, line := range strings.Split(d.Input, "\n") { 44 snapshot, err := strconv.ParseUint(line, 10, 64) 45 require.NoError(t, err) 46 fmt.Fprintf(&buf, "%-2d: %s\n", snapshot, s.Visible(snapshot)) 47 } 48 return buf.String() 49 default: 50 return fmt.Sprintf("unknown command: %s", d.Cmd) 51 } 52 }) 53 } 54 55 func TestSpan_VisibleAt(t *testing.T) { 56 var s Span 57 datadriven.RunTest(t, "testdata/visible_at", func(t *testing.T, d *datadriven.TestData) string { 58 switch d.Cmd { 59 case "define": 60 s = ParseSpan(d.Input) 61 return fmt.Sprint(s) 62 case "visible-at": 63 var buf bytes.Buffer 64 for _, line := range strings.Split(d.Input, "\n") { 65 snapshot, err := strconv.ParseUint(line, 10, 64) 66 require.NoError(t, err) 67 fmt.Fprintf(&buf, "%-2d: %t\n", snapshot, s.VisibleAt(snapshot)) 68 } 69 return buf.String() 70 default: 71 return fmt.Sprintf("unknown command: %s", d.Cmd) 72 } 73 }) 74 } 75 76 func TestSpan_CoversAt(t *testing.T) { 77 var s Span 78 datadriven.RunTest(t, "testdata/covers_at", func(t *testing.T, d *datadriven.TestData) string { 79 switch d.Cmd { 80 case "define": 81 s = ParseSpan(d.Input) 82 return fmt.Sprint(s) 83 case "covers-at": 84 var buf bytes.Buffer 85 for _, line := range strings.Split(d.Input, "\n") { 86 fields := strings.Fields(line) 87 snapshot, err := strconv.ParseUint(fields[0], 10, 64) 88 require.NoError(t, err) 89 seqNum, err := strconv.ParseUint(fields[1], 10, 64) 90 require.NoError(t, err) 91 fmt.Fprintf(&buf, "%d %d : %t\n", snapshot, seqNum, s.CoversAt(snapshot, seqNum)) 92 } 93 return buf.String() 94 default: 95 return fmt.Sprintf("unknown command: %s", d.Cmd) 96 } 97 }) 98 }