From 3bec109f5cc36065ecbd5493f8dd7c891d5f953b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Wed, 28 Aug 2024 16:49:44 -0700 Subject: [PATCH 1/3] test update of enum key in dictionary --- runtime/tests/interpreter/values_test.go | 70 +++++++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/runtime/tests/interpreter/values_test.go b/runtime/tests/interpreter/values_test.go index 27e2f3342c..498ad60a79 100644 --- a/runtime/tests/interpreter/values_test.go +++ b/runtime/tests/interpreter/values_test.go @@ -279,7 +279,7 @@ func TestInterpretRandomMapOperations(t *testing.T) { newEntries.foreach(func(orgKey, orgValue interpreter.Value) (exit bool) { removedValue := dictionary.Remove(inter, interpreter.EmptyLocationRange, orgKey) - assert.IsType(t, &interpreter.SomeValue{}, removedValue) + require.IsType(t, &interpreter.SomeValue{}, removedValue) someValue := removedValue.(*interpreter.SomeValue) // Removed value must be same as the original value @@ -339,7 +339,7 @@ func TestInterpretRandomMapOperations(t *testing.T) { newEntries.foreach(func(orgKey, orgValue interpreter.Value) (exit bool) { removedValue := dictionary.Remove(inter, interpreter.EmptyLocationRange, orgKey) - assert.IsType(t, &interpreter.SomeValue{}, removedValue) + require.IsType(t, &interpreter.SomeValue{}, removedValue) someValue := removedValue.(*interpreter.SomeValue) // Removed value must be same as the original value @@ -359,6 +359,70 @@ func TestInterpretRandomMapOperations(t *testing.T) { assert.Equal(t, startingSlabCounts, slabCounts) }) + t.Run("update enum key", func(t *testing.T) { + + dictionary := interpreter.NewDictionaryValueWithAddress( + inter, + interpreter.EmptyLocationRange, + &interpreter.DictionaryStaticType{ + KeyType: interpreter.PrimitiveStaticTypeAnyStruct, + ValueType: interpreter.PrimitiveStaticTypeAnyStruct, + }, + orgOwner, + ) + + require.Equal(t, 0, dictionary.Count()) + + newEntries := newValueMap(numberOfValues) + + value := interpreter.NewUnmeteredIntValueFromInt64(1) + + keyValues := make([][2]interpreter.Value, numberOfValues) + for i := 0; i < numberOfValues; i++ { + // Create a random enum as key + key := r.generateRandomHashableValue(inter, randomValueKindEnum) + + newEntries.put(inter, key, value) + + keyValues[i][0] = key + keyValues[i][1] = value + } + + // Insert + for _, keyValue := range keyValues { + dictionary.Insert(inter, interpreter.EmptyLocationRange, keyValue[0], keyValue[1]) + } + + value = interpreter.NewUnmeteredIntValueFromInt64(2) + + // Update + newEntries.foreach(func(orgKey, orgValue interpreter.Value) (exit bool) { + oldValue := dictionary.Insert(inter, interpreter.EmptyLocationRange, orgKey.Clone(inter), value) + + require.IsType(t, &interpreter.SomeValue{}, oldValue) + someValue := oldValue.(*interpreter.SomeValue) + + // Removed value must be same as the original value + innerValue := someValue.InnerValue(inter, interpreter.EmptyLocationRange) + utils.AssertValuesEqual(t, inter, orgValue, innerValue) + + return false + }) + + // Check the values + newEntries.foreach(func(key, _ interpreter.Value) (exit bool) { + readValue := dictionary.GetKey(inter, interpreter.EmptyLocationRange, key) + + require.IsType(t, &interpreter.SomeValue{}, readValue) + someValue := readValue.(*interpreter.SomeValue) + + innerValue := someValue.InnerValue(inter, interpreter.EmptyLocationRange) + utils.AssertValuesEqual(t, inter, value, innerValue) + + return false + }) + }) + t.Run("random insert & remove", func(t *testing.T) { keyValues := make([][2]interpreter.Value, numberOfValues) for i := 0; i < numberOfValues; i++ { @@ -437,7 +501,7 @@ func TestInterpretRandomMapOperations(t *testing.T) { removedValue := dictionary.Remove(inter, interpreter.EmptyLocationRange, key) - assert.IsType(t, &interpreter.SomeValue{}, removedValue) + require.IsType(t, &interpreter.SomeValue{}, removedValue) someValue := removedValue.(*interpreter.SomeValue) // Removed value must be same as the original value From dcf1686310c6b24cf5d2fcd5609df1c7a8989e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 29 Aug 2024 09:33:25 -0700 Subject: [PATCH 2/3] make it clearer that the key is updated to a different value --- runtime/tests/interpreter/values_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/runtime/tests/interpreter/values_test.go b/runtime/tests/interpreter/values_test.go index 498ad60a79..e4214ed953 100644 --- a/runtime/tests/interpreter/values_test.go +++ b/runtime/tests/interpreter/values_test.go @@ -375,17 +375,18 @@ func TestInterpretRandomMapOperations(t *testing.T) { newEntries := newValueMap(numberOfValues) - value := interpreter.NewUnmeteredIntValueFromInt64(1) + value1 := interpreter.NewUnmeteredIntValueFromInt64(1) + value2 := interpreter.NewUnmeteredIntValueFromInt64(2) keyValues := make([][2]interpreter.Value, numberOfValues) for i := 0; i < numberOfValues; i++ { // Create a random enum as key key := r.generateRandomHashableValue(inter, randomValueKindEnum) - newEntries.put(inter, key, value) + newEntries.put(inter, key, value1) keyValues[i][0] = key - keyValues[i][1] = value + keyValues[i][1] = value1 } // Insert @@ -393,11 +394,15 @@ func TestInterpretRandomMapOperations(t *testing.T) { dictionary.Insert(inter, interpreter.EmptyLocationRange, keyValue[0], keyValue[1]) } - value = interpreter.NewUnmeteredIntValueFromInt64(2) - // Update newEntries.foreach(func(orgKey, orgValue interpreter.Value) (exit bool) { - oldValue := dictionary.Insert(inter, interpreter.EmptyLocationRange, orgKey.Clone(inter), value) + oldValue := dictionary.Insert( + inter, + interpreter.EmptyLocationRange, + orgKey.Clone(inter), + // change value1 to value2 + value2, + ) require.IsType(t, &interpreter.SomeValue{}, oldValue) someValue := oldValue.(*interpreter.SomeValue) From 96ac39915cb8b99fa4fc519dbd87dc1fd14b868f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 29 Aug 2024 10:47:23 -0700 Subject: [PATCH 3/3] remove use of value map --- runtime/tests/interpreter/values_test.go | 48 +++++++++++++----------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/runtime/tests/interpreter/values_test.go b/runtime/tests/interpreter/values_test.go index e4214ed953..0144838fc7 100644 --- a/runtime/tests/interpreter/values_test.go +++ b/runtime/tests/interpreter/values_test.go @@ -373,34 +373,37 @@ func TestInterpretRandomMapOperations(t *testing.T) { require.Equal(t, 0, dictionary.Count()) - newEntries := newValueMap(numberOfValues) - value1 := interpreter.NewUnmeteredIntValueFromInt64(1) value2 := interpreter.NewUnmeteredIntValueFromInt64(2) - keyValues := make([][2]interpreter.Value, numberOfValues) + keys := make([]interpreter.Value, numberOfValues) for i := 0; i < numberOfValues; i++ { // Create a random enum as key key := r.generateRandomHashableValue(inter, randomValueKindEnum) - newEntries.put(inter, key, value1) - - keyValues[i][0] = key - keyValues[i][1] = value1 + keys[i] = key } // Insert - for _, keyValue := range keyValues { - dictionary.Insert(inter, interpreter.EmptyLocationRange, keyValue[0], keyValue[1]) + for _, key := range keys { + dictionary.Insert( + inter, + interpreter.EmptyLocationRange, + // Need to clone the key, as it is transferred, and we want to keep using it. + key.Clone(inter), + // Always insert value1 + value1, + ) } // Update - newEntries.foreach(func(orgKey, orgValue interpreter.Value) (exit bool) { + for _, key := range keys { oldValue := dictionary.Insert( inter, interpreter.EmptyLocationRange, - orgKey.Clone(inter), - // change value1 to value2 + // Need to clone the key, as it is transferred, and we want to keep using it. + key.Clone(inter), + // Change all value1 to value2 value2, ) @@ -409,23 +412,24 @@ func TestInterpretRandomMapOperations(t *testing.T) { // Removed value must be same as the original value innerValue := someValue.InnerValue(inter, interpreter.EmptyLocationRange) - utils.AssertValuesEqual(t, inter, orgValue, innerValue) - - return false - }) + utils.AssertValuesEqual(t, inter, value1, innerValue) + } // Check the values - newEntries.foreach(func(key, _ interpreter.Value) (exit bool) { - readValue := dictionary.GetKey(inter, interpreter.EmptyLocationRange, key) + for _, key := range keys { + readValue := dictionary.GetKey( + inter, + interpreter.EmptyLocationRange, + key, + ) require.IsType(t, &interpreter.SomeValue{}, readValue) someValue := readValue.(*interpreter.SomeValue) + // Read value must be updated value innerValue := someValue.InnerValue(inter, interpreter.EmptyLocationRange) - utils.AssertValuesEqual(t, inter, value, innerValue) - - return false - }) + utils.AssertValuesEqual(t, inter, value2, innerValue) + } }) t.Run("random insert & remove", func(t *testing.T) {