github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/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), parserOpts{}) 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 cfgs := []string{"default", "multiInstance"} 33 for i := range cfgs { 34 t.Run(fmt.Sprintf("config=%s", cfgs[i]), func(t *testing.T) { 35 cfg := defaultConfig() 36 if cfgs[i] == "multiInstance" { 37 cfg = multiInstanceConfig() 38 cfg.numInstances = 2 39 } 40 ops := generate(randvar.NewRand(), 10000, cfg, newKeyManager(cfg.numInstances)) 41 src := formatOps(ops) 42 43 parsedOps, err := parse([]byte(src), parserOpts{}) 44 if err != nil { 45 t.Fatalf("%s\n%s", err, src) 46 } 47 require.Equal(t, ops, parsedOps) 48 }) 49 } 50 } 51 52 func TestParserNilBounds(t *testing.T) { 53 formatted := formatOps([]op{ 54 &newIterOp{ 55 readerID: makeObjID(dbTag, 1), 56 iterID: makeObjID(iterTag, 1), 57 iterOpts: iterOpts{}, 58 }, 59 }) 60 parsedOps, err := parse([]byte(formatted), parserOpts{}) 61 require.NoError(t, err) 62 require.Equal(t, 1, len(parsedOps)) 63 v := parsedOps[0].(*newIterOp) 64 require.Nil(t, v.lower) 65 require.Nil(t, v.upper) 66 }