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 all commits
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
79 changes: 76 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,79 @@ 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())

value1 := interpreter.NewUnmeteredIntValueFromInt64(1)
value2 := interpreter.NewUnmeteredIntValueFromInt64(2)

keys := make([]interpreter.Value, numberOfValues)
for i := 0; i < numberOfValues; i++ {
// Create a random enum as key
key := r.generateRandomHashableValue(inter, randomValueKindEnum)

keys[i] = key
}

// Insert
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
for _, key := range keys {
oldValue := dictionary.Insert(
inter,
interpreter.EmptyLocationRange,
// 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,
)

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, value1, innerValue)
}

// Check the values
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, value2, innerValue)
}
})

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 +510,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