From b9c0c4082dbe38157a886c6121fcb4007ea8e434 Mon Sep 17 00:00:00 2001 From: Wojciech Malota-Wojcik Date: Thu, 5 Dec 2024 17:27:17 +0100 Subject: [PATCH] Add exist and delete operations to the test --- space/space_test.go | 33 ++++++++++++++++++++++----- test/helpers.go_ | 55 --------------------------------------------- 2 files changed, 27 insertions(+), 61 deletions(-) delete mode 100644 test/helpers.go_ diff --git a/space/space_test.go b/space/space_test.go index 817f2eb..61ff1fb 100644 --- a/space/space_test.go +++ b/space/space_test.go @@ -1030,10 +1030,19 @@ func TestSwitchingFromMutableToImmutablePath(t *testing.T) { // Let's locate the key hash 16 in the data item 0. - v16, err = s.NewEntry(snapshotID, key, StageData) + v16Read, err := s.NewEntry(snapshotID, key, StageData) requireT.NoError(err) - requireT.NoError(s.Find(v16)) - requireT.Equal(&pointerNode.Pointers[16].VolatileAddress, v16.nextDataNode) // Verify the next expected data node address. + requireT.NoError(s.Find(v16Read)) + // Verify the next expected data node address. + requireT.Equal(&pointerNode.Pointers[16].VolatileAddress, v16Read.nextDataNode) + + v16Exists, err := s.NewEntry(snapshotID, key, StageData) + requireT.NoError(err) + requireT.NoError(s.Find(v16Exists)) + + v16Delete, err := s.NewEntry(snapshotID, key, StageData) + requireT.NoError(err) + requireT.NoError(s.Find(v16Delete)) // Now let's split data node 0 using key hash 64. @@ -1061,15 +1070,27 @@ func TestSwitchingFromMutableToImmutablePath(t *testing.T) { requireT.NoError(s.AddPointerNode(v64, false)) requireT.True(pointerNode.Pointers[0].VolatileAddress.IsSet(types.FlagPointerNode)) - requireT.True(v16.storeRequest.Store[v16.storeRequest.PointersToStore-1].Pointer.VolatileAddress. + requireT.True(v16Read.storeRequest.Store[v16Read.storeRequest.PointersToStore-1].Pointer.VolatileAddress. IsSet(types.FlagPointerNode)) // We are now in situation where key hash 16 is no longer in the place pointed to by v16. // When walking the tree now, it should not follow the current pointer node, but go back and switch // to the immutable path. - balance, err := s.ReadKey(v16) + balance, err := s.ReadKey(v16Read) requireT.NoError(err) requireT.Equal(txtypes.Amount(16), balance) - requireT.Nil(v16.nextDataNode) + requireT.Nil(v16Read.nextDataNode) + + exists, err := s.KeyExists(v16Exists) + requireT.NoError(err) + requireT.True(exists) + requireT.Nil(v16Exists.nextDataNode) + + requireT.NoError(s.DeleteKey(v16Delete)) + requireT.Nil(v16Delete.nextDataNode) + + balance, exists = s.Query(key) + requireT.False(exists) + requireT.Equal(txtypes.Amount(0), balance) } diff --git a/test/helpers.go_ b/test/helpers.go_ deleted file mode 100644 index 36b7c96..0000000 --- a/test/helpers.go_ +++ /dev/null @@ -1,55 +0,0 @@ -package test - -import ( - "sort" - - "golang.org/x/exp/constraints" - - "github.com/outofforest/quantum/list" - "github.com/outofforest/quantum/space" - "github.com/outofforest/quantum/types" -) - -// CollectSpaceValues collects values available in space. -func CollectSpaceValues[K comparable, V constraints.Ordered](s *space.Space[K, V]) []V { - values := []V{} - for item := range s.Iterator() { - values = append(values, item.Value) - } - - sort.Slice(values, func(i, j int) bool { - return values[i] < values[j] - }) - return values -} - -// CollectSpaceKeys collects keys available in space. -func CollectSpaceKeys[K constraints.Ordered, V comparable](s *space.Space[K, V]) []K { - keys := []K{} - for item := range s.Iterator() { - keys = append(keys, item.Key) - } - - sort.Slice(keys, func(i, j int) bool { - return keys[i] < keys[j] - }) - return keys -} - -// CollectListItems collects items available in list. -func CollectListItems(l *list.List) []types.LogicalAddress { - nodes := []types.LogicalAddress{} - for pointer := range l.Iterator() { - nodes = append(nodes, pointer.LogicalAddress) - } - - sort.Slice(nodes, func(i, j int) bool { - return nodes[i] < nodes[j] - }) - return nodes -} - -// Error returns an error from the result of function call. -func Error(_ any, err error) error { - return err -}