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

Use go1.21 slices package functions in tests #524

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
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
105 changes: 45 additions & 60 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math/rand"
"reflect"
"runtime"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -465,9 +466,7 @@ func TestArrayInsertAndGet(t *testing.T) {
for i := uint64(1); i < arrayCount; i += 2 {
v := test_utils.Uint64Value(i)

expectedValues = append(expectedValues, nil)
copy(expectedValues[i+1:], expectedValues[i:])
expectedValues[i] = v
expectedValues = slices.Insert(expectedValues, int(i), atree.Value(v))

err := array.Insert(i, v)
require.NoError(t, err)
Expand Down Expand Up @@ -653,8 +652,7 @@ func TestArrayRemove(t *testing.T) {
require.NoError(t, err)
}

copy(expectedValues[i:], expectedValues[i+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, int(i), int(i+1))

require.Equal(t, uint64(len(expectedValues)), array.Count())

Expand Down Expand Up @@ -2545,13 +2543,12 @@ func TestArrayInsertRandomValues(t *testing.T) {
array, err := atree.NewArray(storage, address, typeInfo)
require.NoError(t, err)

expectedValues := make([]atree.Value, arrayCount)
expectedValues := make([]atree.Value, 0, arrayCount)
for i := range arrayCount {
k := r.Intn(int(i + 1))
v := randomValue(r, atree.MaxInlineArrayElementSize())

copy(expectedValues[k+1:], expectedValues[k:])
expectedValues[k] = v
expectedValues = slices.Insert(expectedValues, k, v)

err := array.Insert(uint64(k), v)
require.NoError(t, err)
Expand Down Expand Up @@ -2600,8 +2597,7 @@ func TestArrayRemoveRandomValues(t *testing.T) {
require.NoError(t, err)
testValueEqual(t, expectedValues[k], existingValue)

copy(expectedValues[k:], expectedValues[k+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, int(k), int(k+1))

if id, ok := existingStorable.(atree.SlabIDStorable); ok {
err = storage.Remove(atree.SlabID(id))
Expand Down Expand Up @@ -2677,13 +2673,7 @@ func testArrayAppendSetInsertRemoveRandomValues(
k := getRandomUint64InRange(r, 0, array.Count()+1)
v := randomValue(r, atree.MaxInlineArrayElementSize())

if k == array.Count() {
expectedValues = append(expectedValues, v)
} else {
expectedValues = append(expectedValues, nil)
copy(expectedValues[k+1:], expectedValues[k:])
expectedValues[k] = v
}
expectedValues = slices.Insert(expectedValues, int(k), v)

err := array.Insert(k, v)
require.NoError(t, err)
Expand All @@ -2698,8 +2688,7 @@ func testArrayAppendSetInsertRemoveRandomValues(
require.NoError(t, err)
testValueEqual(t, expectedValues[k], existingValue)

copy(expectedValues[k:], expectedValues[k+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, int(k), int(k+1))

if id, ok := existingStorable.(atree.SlabIDStorable); ok {
err = storage.Remove(atree.SlabID(id))
Expand Down Expand Up @@ -4850,9 +4839,8 @@ func TestArrayFromBatchData(t *testing.T) {
}

v = test_utils.NewStringValue(strings.Repeat("a", int(atree.MaxInlineArrayElementSize()-2)))
expectedValues = append(expectedValues, nil)
copy(expectedValues[25+1:], expectedValues[25:])
expectedValues[25] = v

expectedValues = slices.Insert(expectedValues, 25, v)

err = array.Insert(25, v)
require.NoError(t, err)
Expand Down Expand Up @@ -5314,8 +5302,7 @@ func TestArrayLoadedValueIterator(t *testing.T) {
err := storage.Remove(slabID)
require.NoError(t, err)

copy(expectedValues[unloadValueIndex:], expectedValues[unloadValueIndex+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, unloadValueIndex, unloadValueIndex+1)

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5377,8 +5364,7 @@ func TestArrayLoadedValueIterator(t *testing.T) {
err := storage.Remove(childSlabID)
require.NoError(t, err)

copy(expectedValues[childArrayIndex:], expectedValues[childArrayIndex+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, int(childArrayIndex), int(childArrayIndex+1))

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5489,8 +5475,7 @@ func TestArrayLoadedValueIterator(t *testing.T) {
err := storage.Remove(slabID)
require.NoError(t, err)

copy(expectedValues[index:], expectedValues[index+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, index, index+1)

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5518,8 +5503,7 @@ func TestArrayLoadedValueIterator(t *testing.T) {
err := storage.Remove(childSlabID)
require.NoError(t, err)

copy(expectedValues[childArrayIndex:], expectedValues[childArrayIndex+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, int(childArrayIndex), int(childArrayIndex+1))

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5626,8 +5610,8 @@ func TestArrayLoadedValueIterator(t *testing.T) {
for _, childCount := range childCounts[:index] {
accumulativeCount += childCount
}
copy(expectedValues[accumulativeCount:], expectedValues[accumulativeCount+count:])
expectedValues = expectedValues[:array.Count()-uint64(count)]

expectedValues = slices.Delete(expectedValues, int(accumulativeCount), int(accumulativeCount+count))

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5722,11 +5706,9 @@ func TestArrayLoadedValueIterator(t *testing.T) {
err := storage.Remove(slabID)
require.NoError(t, err)

copy(expectedValues[i:], expectedValues[i+1:])
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, i, i+1)

copy(childSlabIDs[i:], childSlabIDs[i+1:])
childSlabIDs = childSlabIDs[:len(childSlabIDs)-1]
childSlabIDs = slices.Delete(childSlabIDs, i, i+1)

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5792,14 +5774,16 @@ func TestArrayLoadedValueIterator(t *testing.T) {
}

// Remove slabInfo to be unloaded from dataSlabInfos.
copy(dataSlabInfos[indexToUnload:], dataSlabInfos[indexToUnload+1:])
dataSlabInfos = dataSlabInfos[:len(dataSlabInfos)-1]

dataSlabInfos = slices.Delete(dataSlabInfos, indexToUnload, indexToUnload+1)

err := storage.Remove(slabInfoToUnload.id)
require.NoError(t, err)

copy(expectedValues[slabInfoToUnload.startIndex:], expectedValues[slabInfoToUnload.startIndex+slabInfoToUnload.count:])
expectedValues = expectedValues[:len(expectedValues)-slabInfoToUnload.count]
expectedValues = slices.Delete(
expectedValues,
slabInfoToUnload.startIndex,
slabInfoToUnload.startIndex+slabInfoToUnload.count)

testArrayLoadedElements(t, array, expectedValues)
}
Expand Down Expand Up @@ -5907,8 +5891,10 @@ func TestArrayLoadedValueIterator(t *testing.T) {
}
}

copy(nonrootMetadataSlabInfos[metadataSlabIndex:], nonrootMetadataSlabInfos[metadataSlabIndex+1:])
nonrootMetadataSlabInfos = nonrootMetadataSlabInfos[:len(nonrootMetadataSlabInfos)-1]
nonrootMetadataSlabInfos = slices.Delete(
nonrootMetadataSlabInfos,
metadataSlabIndex,
metadataSlabIndex+1)

case dataSlabType:
// Unload data slab at random index.
Expand All @@ -5930,8 +5916,10 @@ func TestArrayLoadedValueIterator(t *testing.T) {
slabInfo.startIndex -= count
}

copy(metaSlabInfo.children[dataSlabIndex:], metaSlabInfo.children[dataSlabIndex+1:])
metaSlabInfo.children = metaSlabInfo.children[:len(metaSlabInfo.children)-1]
metaSlabInfo.children = slices.Delete(
metaSlabInfo.children,
dataSlabIndex,
dataSlabIndex+1)

metaSlabInfo.count -= count

Expand All @@ -5945,8 +5933,10 @@ func TestArrayLoadedValueIterator(t *testing.T) {
}

if len(metaSlabInfo.children) == 0 {
copy(nonrootMetadataSlabInfos[metadataSlabIndex:], nonrootMetadataSlabInfos[metadataSlabIndex+1:])
nonrootMetadataSlabInfos = nonrootMetadataSlabInfos[:len(nonrootMetadataSlabInfos)-1]
nonrootMetadataSlabInfos = slices.Delete(
nonrootMetadataSlabInfos,
metadataSlabIndex,
metadataSlabIndex+1)
}
}

Expand All @@ -5956,8 +5946,11 @@ func TestArrayLoadedValueIterator(t *testing.T) {
if isLastSlab {
expectedValues = expectedValues[:slabInfoToBeRemoved.startIndex]
} else {
copy(expectedValues[slabInfoToBeRemoved.startIndex:], expectedValues[slabInfoToBeRemoved.startIndex+slabInfoToBeRemoved.count:])
expectedValues = expectedValues[:len(expectedValues)-slabInfoToBeRemoved.count]
expectedValues = slices.Delete(
expectedValues,
slabInfoToBeRemoved.startIndex,
slabInfoToBeRemoved.startIndex+slabInfoToBeRemoved.count,
)
}

testArrayLoadedElements(t, array, expectedValues)
Expand Down Expand Up @@ -7891,9 +7884,7 @@ func TestChildArrayWhenParentArrayIsModified(t *testing.T) {
err := parentArray.Insert(0, v)
require.NoError(t, err)

expectedValues = append(expectedValues, nil)
copy(expectedValues[1:], expectedValues)
expectedValues[0] = v
expectedValues = slices.Insert(expectedValues, 0, atree.Value(v))

for i, child := range children {
childArray := child.array
Expand Down Expand Up @@ -7934,9 +7925,7 @@ func TestChildArrayWhenParentArrayIsModified(t *testing.T) {
err = parentArray.Insert(2, v)
require.NoError(t, err)

expectedValues = append(expectedValues, nil)
copy(expectedValues[3:], expectedValues[2:])
expectedValues[2] = v
expectedValues = slices.Insert(expectedValues, 2, atree.Value(v))

for i, child := range children {
childArray := child.array
Expand Down Expand Up @@ -8021,9 +8010,7 @@ func TestChildArrayWhenParentArrayIsModified(t *testing.T) {
require.NoError(t, err)
require.Equal(t, test_utils.Uint64Value(0), existingStorable)

copy(expectedValues, expectedValues[1:])
expectedValues[len(expectedValues)-1] = nil
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, 0, 1)

for i, child := range children {
childArray := child.array
Expand Down Expand Up @@ -8064,9 +8051,7 @@ func TestChildArrayWhenParentArrayIsModified(t *testing.T) {
require.NoError(t, err)
require.Equal(t, test_utils.Uint64Value(2), existingStorable)

copy(expectedValues[1:], expectedValues[2:])
expectedValues[len(expectedValues)-1] = nil
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, 1, 2)

for i, child := range children {
childArray := child.array
Expand Down
25 changes: 4 additions & 21 deletions array_wrappervalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math/rand"
"runtime"
"slices"
"sort"
"testing"

Expand Down Expand Up @@ -1923,13 +1924,7 @@ func TestArrayWrapperValueModifyNewArrayAtLevel1(t *testing.T) {
err = array.Insert(index, v)
require.NoError(t, err)

newExpectedValue := make([]atree.Value, len(expectedValues)+1)

copy(newExpectedValue, expectedValues[:index])
newExpectedValue[index] = expected
copy(newExpectedValue[index+1:], expectedValues[index:])

expectedValues = newExpectedValue
expectedValues = slices.Insert(expectedValues, int(index), expected)
}

require.Equal(t, actualArrayCount, array.Count())
Expand Down Expand Up @@ -2192,13 +2187,7 @@ func TestArrayWrapperValueModifyNewArrayAtLevel2(t *testing.T) {
err = array.Insert(index, v)
require.NoError(t, err)

newExpectedValue := make([]atree.Value, len(expectedValues)+1)

copy(newExpectedValue, expectedValues[:index])
newExpectedValue[index] = expected
copy(newExpectedValue[index+1:], expectedValues[index:])

expectedValues = newExpectedValue
expectedValues = slices.Insert(expectedValues, int(index), expected)
}

require.Equal(t, actualArrayCount, array.Count())
Expand Down Expand Up @@ -2482,13 +2471,7 @@ func TestArrayWrapperValueModifyNewArrayAtLevel3(t *testing.T) {
err = array.Insert(index, v)
require.NoError(t, err)

newExpectedValue := make([]atree.Value, len(expectedValues)+1)

copy(newExpectedValue, expectedValues[:index])
newExpectedValue[index] = expected
copy(newExpectedValue[index+1:], expectedValues[index:])

expectedValues = newExpectedValue
expectedValues = slices.Insert(expectedValues, int(index), expected)
}

require.Equal(t, actualArrayCount, array.Count())
Expand Down
9 changes: 3 additions & 6 deletions cmd/smoke/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"reflect"
"runtime"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -440,9 +441,7 @@ func modifyArray(
if index == int(array.Count()) {
expectedValues = append(expectedValues, expectedChildValue)
} else {
expectedValues = append(expectedValues, nil)
copy(expectedValues[index+1:], expectedValues[index:])
expectedValues[index] = expectedChildValue
expectedValues = slices.Insert(expectedValues, index, expectedChildValue)
}

// Update array
Expand All @@ -464,9 +463,7 @@ func modifyArray(
oldExpectedValue := expectedValues[index]

// Update expectedValues
copy(expectedValues[index:], expectedValues[index+1:])
expectedValues[len(expectedValues)-1] = nil
expectedValues = expectedValues[:len(expectedValues)-1]
expectedValues = slices.Delete(expectedValues, index, index+1)

// Update array
existingStorable, err := array.Remove(uint64(index))
Expand Down
Loading
Loading