github.com/MetalBlockchain/metalgo@v1.11.9/database/prefixdb/db_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package prefixdb
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/MetalBlockchain/metalgo/database"
    13  	"github.com/MetalBlockchain/metalgo/database/memdb"
    14  )
    15  
    16  func TestInterface(t *testing.T) {
    17  	for name, test := range database.Tests {
    18  		t.Run(name, func(t *testing.T) {
    19  			db := memdb.New()
    20  			test(t, New([]byte("hello"), db))
    21  			test(t, New([]byte("world"), db))
    22  			test(t, New([]byte("wor"), New([]byte("ld"), db)))
    23  			test(t, New([]byte("ld"), New([]byte("wor"), db)))
    24  			test(t, NewNested([]byte("wor"), New([]byte("ld"), db)))
    25  			test(t, NewNested([]byte("ld"), New([]byte("wor"), db)))
    26  		})
    27  	}
    28  }
    29  
    30  func TestPrefixLimit(t *testing.T) {
    31  	testString := []string{"hello", "world", "a\xff", "\x01\xff\xff\xff\xff"}
    32  	expected := []string{"hellp", "worle", "b\x00", "\x02\x00\x00\x00\x00"}
    33  	for i, str := range testString {
    34  		db := newDB([]byte(str), nil)
    35  		require.Equal(t, db.dbLimit, []byte(expected[i]))
    36  	}
    37  }
    38  
    39  func FuzzKeyValue(f *testing.F) {
    40  	database.FuzzKeyValue(f, New([]byte(""), memdb.New()))
    41  }
    42  
    43  func FuzzNewIteratorWithPrefix(f *testing.F) {
    44  	database.FuzzNewIteratorWithPrefix(f, New([]byte(""), memdb.New()))
    45  }
    46  
    47  func FuzzNewIteratorWithStartAndPrefix(f *testing.F) {
    48  	database.FuzzNewIteratorWithStartAndPrefix(f, New([]byte(""), memdb.New()))
    49  }
    50  
    51  func BenchmarkInterface(b *testing.B) {
    52  	for _, size := range database.BenchmarkSizes {
    53  		keys, values := database.SetupBenchmark(b, size[0], size[1], size[2])
    54  		for name, bench := range database.Benchmarks {
    55  			b.Run(fmt.Sprintf("prefixdb_%d_pairs_%d_keys_%d_values_%s", size[0], size[1], size[2], name), func(b *testing.B) {
    56  				db := New([]byte("hello"), memdb.New())
    57  				bench(b, db, keys, values)
    58  			})
    59  		}
    60  	}
    61  }