github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/keyspan/seek_test.go (about)

     1  // Copyright 2018 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/zuoyebang/bitalostable/internal/base"
    15  	"github.com/zuoyebang/bitalostable/internal/datadriven"
    16  )
    17  
    18  func TestSeek(t *testing.T) {
    19  	cmp := base.DefaultComparer.Compare
    20  	fmtKey := base.DefaultComparer.FormatKey
    21  	var iter FragmentIterator
    22  	var buf bytes.Buffer
    23  
    24  	datadriven.RunTest(t, "testdata/seek", func(d *datadriven.TestData) string {
    25  		buf.Reset()
    26  		switch d.Cmd {
    27  		case "build":
    28  			spans := buildSpans(t, cmp, fmtKey, d.Input, base.InternalKeyKindRangeDelete)
    29  			for _, s := range spans {
    30  				fmt.Fprintln(&buf, s)
    31  			}
    32  			iter = NewIter(cmp, spans)
    33  			return buf.String()
    34  		case "seek-ge", "seek-le":
    35  			seek := SeekGE
    36  			if d.Cmd == "seek-le" {
    37  				seek = SeekLE
    38  			}
    39  
    40  			for _, line := range strings.Split(d.Input, "\n") {
    41  				parts := strings.Fields(line)
    42  				if len(parts) != 2 {
    43  					return fmt.Sprintf("malformed input: %s", line)
    44  				}
    45  				seq, err := strconv.ParseUint(parts[1], 10, 64)
    46  				if err != nil {
    47  					return err.Error()
    48  				}
    49  				span := seek(cmp, iter, []byte(parts[0]))
    50  				if span != nil {
    51  					visible := span.Visible(seq)
    52  					span = &visible
    53  				}
    54  				fmt.Fprintln(&buf, span)
    55  			}
    56  			return buf.String()
    57  		default:
    58  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    59  		}
    60  	})
    61  }