Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoke test update of enum key in dictionary #3556

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions runtime/tests/interpreter/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
})
SupunS marked this conversation as resolved.
Show resolved Hide resolved

// Check the values
newEntries.foreach(func(key, _ interpreter.Value) (exit bool) {
turbolent marked this conversation as resolved.
Show resolved Hide resolved
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++ {
Expand Down Expand Up @@ -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
Expand Down
Loading