github.com/dim4egster/coreth@v0.10.2/core/rawdb/table_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2020 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package rawdb
    28  
    29  import (
    30  	"bytes"
    31  	"testing"
    32  
    33  	"github.com/dim4egster/coreth/ethdb"
    34  )
    35  
    36  func TestTableDatabase(t *testing.T)            { testTableDatabase(t, "prefix") }
    37  func TestEmptyPrefixTableDatabase(t *testing.T) { testTableDatabase(t, "") }
    38  
    39  type testReplayer struct {
    40  	puts [][]byte
    41  	dels [][]byte
    42  }
    43  
    44  func (r *testReplayer) Put(key []byte, value []byte) error {
    45  	r.puts = append(r.puts, key)
    46  	return nil
    47  }
    48  
    49  func (r *testReplayer) Delete(key []byte) error {
    50  	r.dels = append(r.dels, key)
    51  	return nil
    52  }
    53  
    54  func testTableDatabase(t *testing.T, prefix string) {
    55  	db := NewTable(NewMemoryDatabase(), prefix)
    56  
    57  	var entries = []struct {
    58  		key   []byte
    59  		value []byte
    60  	}{
    61  		{[]byte{0x01, 0x02}, []byte{0x0a, 0x0b}},
    62  		{[]byte{0x03, 0x04}, []byte{0x0c, 0x0d}},
    63  		{[]byte{0x05, 0x06}, []byte{0x0e, 0x0f}},
    64  
    65  		{[]byte{0xff, 0xff, 0x01}, []byte{0x1a, 0x1b}},
    66  		{[]byte{0xff, 0xff, 0x02}, []byte{0x1c, 0x1d}},
    67  		{[]byte{0xff, 0xff, 0x03}, []byte{0x1e, 0x1f}},
    68  	}
    69  
    70  	// Test Put/Get operation
    71  	for _, entry := range entries {
    72  		db.Put(entry.key, entry.value)
    73  	}
    74  	for _, entry := range entries {
    75  		got, err := db.Get(entry.key)
    76  		if err != nil {
    77  			t.Fatalf("Failed to get value: %v", err)
    78  		}
    79  		if !bytes.Equal(got, entry.value) {
    80  			t.Fatalf("Value mismatch: want=%v, got=%v", entry.value, got)
    81  		}
    82  	}
    83  
    84  	// Test batch operation
    85  	db = NewTable(NewMemoryDatabase(), prefix)
    86  	batch := db.NewBatch()
    87  	for _, entry := range entries {
    88  		batch.Put(entry.key, entry.value)
    89  	}
    90  	batch.Write()
    91  	for _, entry := range entries {
    92  		got, err := db.Get(entry.key)
    93  		if err != nil {
    94  			t.Fatalf("Failed to get value: %v", err)
    95  		}
    96  		if !bytes.Equal(got, entry.value) {
    97  			t.Fatalf("Value mismatch: want=%v, got=%v", entry.value, got)
    98  		}
    99  	}
   100  
   101  	// Test batch replayer
   102  	r := &testReplayer{}
   103  	batch.Replay(r)
   104  	for index, entry := range entries {
   105  		got := r.puts[index]
   106  		if !bytes.Equal(got, entry.key) {
   107  			t.Fatalf("Key mismatch: want=%v, got=%v", entry.key, got)
   108  		}
   109  	}
   110  
   111  	check := func(iter ethdb.Iterator, expCount, index int) {
   112  		count := 0
   113  		for iter.Next() {
   114  			key, value := iter.Key(), iter.Value()
   115  			if !bytes.Equal(key, entries[index].key) {
   116  				t.Fatalf("Key mismatch: want=%v, got=%v", entries[index].key, key)
   117  			}
   118  			if !bytes.Equal(value, entries[index].value) {
   119  				t.Fatalf("Value mismatch: want=%v, got=%v", entries[index].value, value)
   120  			}
   121  			index += 1
   122  			count++
   123  		}
   124  		if count != expCount {
   125  			t.Fatalf("Wrong number of elems, exp %d got %d", expCount, count)
   126  		}
   127  		iter.Release()
   128  	}
   129  	// Test iterators
   130  	check(db.NewIterator(nil, nil), 6, 0)
   131  	// Test iterators with prefix
   132  	check(db.NewIterator([]byte{0xff, 0xff}, nil), 3, 3)
   133  	// Test iterators with start point
   134  	check(db.NewIterator(nil, []byte{0xff, 0xff, 0x02}), 2, 4)
   135  	// Test iterators with prefix and start point
   136  	check(db.NewIterator([]byte{0xee}, nil), 0, 0)
   137  	check(db.NewIterator(nil, []byte{0x00}), 6, 0)
   138  }