github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/keyspan/bounded_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  	"strings"
    11  	"testing"
    12  
    13  	"github.com/cockroachdb/datadriven"
    14  	"github.com/cockroachdb/pebble/internal/testkeys"
    15  )
    16  
    17  func TestBoundedIter(t *testing.T) {
    18  	getBounds := func(td *datadriven.TestData) (lower, upper []byte) {
    19  		for _, cmdArg := range td.CmdArgs {
    20  			switch cmdArg.Key {
    21  			case "lower":
    22  				if len(cmdArg.Vals[0]) > 0 {
    23  					lower = []byte(cmdArg.Vals[0])
    24  				}
    25  			case "upper":
    26  				if len(cmdArg.Vals[0]) > 0 {
    27  					upper = []byte(cmdArg.Vals[0])
    28  				}
    29  			}
    30  		}
    31  		return lower, upper
    32  	}
    33  
    34  	cmp := testkeys.Comparer.Compare
    35  	split := testkeys.Comparer.Split
    36  	var buf bytes.Buffer
    37  	var iter BoundedIter
    38  	var hasPrefix bool
    39  	var prefix []byte
    40  	datadriven.RunTest(t, "testdata/bounded_iter", func(t *testing.T, td *datadriven.TestData) string {
    41  		switch td.Cmd {
    42  		case "define":
    43  			var spans []Span
    44  			lines := strings.Split(strings.TrimSpace(td.Input), "\n")
    45  			for _, line := range lines {
    46  				spans = append(spans, ParseSpan(line))
    47  			}
    48  			inner := &invalidatingIter{iter: NewIter(cmp, spans)}
    49  			lower, upper := getBounds(td)
    50  			iter.Init(cmp, split, inner, lower, upper, &hasPrefix, &prefix)
    51  			return ""
    52  		case "set-prefix":
    53  			hasPrefix = len(td.CmdArgs) > 0
    54  			if hasPrefix {
    55  				prefix = []byte(td.CmdArgs[0].String())
    56  				return fmt.Sprintf("set prefix to %q\n", prefix)
    57  			}
    58  			return "cleared prefix"
    59  		case "iter":
    60  			buf.Reset()
    61  			lower, upper := getBounds(td)
    62  			iter.SetBounds(lower, upper)
    63  			return runIterCmd(t, td, &iter)
    64  		default:
    65  			return fmt.Sprintf("unrecognized command %q", td.Cmd)
    66  		}
    67  	})
    68  }