github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/metamorphic/parser_test.go (about)

     1  // Copyright 2019 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 metamorphic
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/cockroachdb/datadriven"
    12  	"github.com/cockroachdb/pebble/internal/randvar"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestParser(t *testing.T) {
    17  	datadriven.RunTest(t, "testdata/parser", func(t *testing.T, d *datadriven.TestData) string {
    18  		switch d.Cmd {
    19  		case "parse":
    20  			ops, err := parse([]byte(d.Input))
    21  			if err != nil {
    22  				return err.Error()
    23  			}
    24  			return formatOps(ops)
    25  		default:
    26  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    27  		}
    28  	})
    29  }
    30  
    31  func TestParserRandom(t *testing.T) {
    32  	ops := generate(randvar.NewRand(), 10000, defaultConfig(), newKeyManager())
    33  	src := formatOps(ops)
    34  
    35  	parsedOps, err := parse([]byte(src))
    36  	if err != nil {
    37  		t.Fatalf("%s\n%s", err, src)
    38  	}
    39  	require.Equal(t, ops, parsedOps)
    40  }
    41  
    42  func TestParserNilBounds(t *testing.T) {
    43  	formatted := formatOps([]op{
    44  		&newIterOp{
    45  			readerID: makeObjID(dbTag, 0),
    46  			iterID:   makeObjID(iterTag, 1),
    47  			iterOpts: iterOpts{},
    48  		},
    49  	})
    50  	parsedOps, err := parse([]byte(formatted))
    51  	require.NoError(t, err)
    52  	require.Equal(t, 1, len(parsedOps))
    53  	v := parsedOps[0].(*newIterOp)
    54  	require.Nil(t, v.lower)
    55  	require.Nil(t, v.upper)
    56  }