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