Skip to content

Commit d7f7500

Browse files
committed
Refactor 3-clause for loop in array test & validation code
This commit refactored 3-clause for loops in array test and validation code to improve maintainability by using: - range over integers - range over slices NOTE: This change does not affect non-test code.
1 parent b7e3587 commit d7f7500

6 files changed

Lines changed: 347 additions & 329 deletions

array_bench_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func setupArray(b *testing.B, r *rand.Rand, storage *atree.PersistentSlabStorage
198198
array, err := atree.NewArray(storage, address, typeInfo)
199199
require.NoError(b, err)
200200

201-
for i := 0; i < initialArrayCount; i++ {
201+
for range initialArrayCount {
202202
v := RandomValue(r)
203203
err := array.Append(v)
204204
require.NoError(b, err)
@@ -231,8 +231,8 @@ func benchmarkArrayGet(b *testing.B, initialArrayCount, numberOfOps int) {
231231

232232
b.StartTimer()
233233

234-
for i := 0; i < b.N; i++ {
235-
for i := 0; i < numberOfOps; i++ {
234+
for range b.N {
235+
for range numberOfOps {
236236
index := r.Intn(int(array.Count()))
237237
value, _ = array.Get(uint64(index))
238238
}
@@ -249,15 +249,15 @@ func benchmarkArrayInsert(b *testing.B, initialArrayCount, numberOfOps int) {
249249

250250
storage := newTestPersistentStorage(b)
251251

252-
for i := 0; i < b.N; i++ {
252+
for range b.N {
253253

254254
b.StopTimer()
255255

256256
array := setupArray(b, r, storage, initialArrayCount)
257257

258258
b.StartTimer()
259259

260-
for i := 0; i < numberOfOps; i++ {
260+
for range numberOfOps {
261261
index := r.Intn(int(array.Count()))
262262
v := RandomValue(r)
263263
_ = array.Insert(uint64(index), v)
@@ -273,15 +273,15 @@ func benchmarkArrayRemove(b *testing.B, initialArrayCount, numberOfOps int) {
273273

274274
storage := newTestPersistentStorage(b)
275275

276-
for i := 0; i < b.N; i++ {
276+
for range b.N {
277277

278278
b.StopTimer()
279279

280280
array := setupArray(b, r, storage, initialArrayCount)
281281

282282
b.StartTimer()
283283

284-
for i := 0; i < numberOfOps; i++ {
284+
for range numberOfOps {
285285
index := r.Intn(int(array.Count()))
286286
_, _ = array.Remove(uint64(index))
287287
}
@@ -298,7 +298,7 @@ func benchmarkArrayRemoveAll(b *testing.B, initialArrayCount int) {
298298

299299
var storable atree.Storable
300300

301-
for i := 0; i < b.N; i++ {
301+
for range b.N {
302302

303303
b.StopTimer()
304304

@@ -324,7 +324,7 @@ func benchmarkArrayPopIterate(b *testing.B, initialArrayCount int) {
324324

325325
var storable atree.Storable
326326

327-
for i := 0; i < b.N; i++ {
327+
for range b.N {
328328

329329
b.StopTimer()
330330

@@ -355,7 +355,7 @@ func benchmarkNewArrayFromAppend(b *testing.B, initialArrayCount int) {
355355

356356
b.StartTimer()
357357

358-
for i := 0; i < b.N; i++ {
358+
for range b.N {
359359
copied, _ := atree.NewArray(storage, array.Address(), array.Type())
360360

361361
_ = array.IterateReadOnly(func(value atree.Value) (bool, error) {
@@ -381,7 +381,7 @@ func benchmarkNewArrayFromBatchData(b *testing.B, initialArrayCount int) {
381381

382382
b.StartTimer()
383383

384-
for i := 0; i < b.N; i++ {
384+
for range b.N {
385385
iter, err := array.ReadOnlyIterator()
386386
require.NoError(b, err)
387387

array_benchmark_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
8989
var totalLookupTime time.Duration
9090

9191
// setup
92-
for i := 0; i < initialArrayCount; i++ {
92+
for range initialArrayCount {
9393
v := RandomValue(r)
9494
storable, err := v.Storable(storage, array.Address(), atree.MaxInlineArrayElementSize())
9595
require.NoError(b, err)
@@ -107,7 +107,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
107107
start = time.Now()
108108
array, err = atree.NewArrayWithRootID(storage, arrayID)
109109
require.NoError(b, err)
110-
for i := 0; i < numberOfElements; i++ {
110+
for range numberOfElements {
111111
v := RandomValue(r)
112112

113113
storable, err := v.Storable(storage, array.Address(), atree.MaxInlineArrayElementSize())
@@ -127,7 +127,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
127127
array, err = atree.NewArrayWithRootID(storage, arrayID)
128128
require.NoError(b, err)
129129

130-
for i := 0; i < numberOfElements; i++ {
130+
for range numberOfElements {
131131
ind := r.Intn(int(array.Count()))
132132
storable, err := array.Remove(uint64(ind))
133133
require.NoError(b, err)
@@ -142,7 +142,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
142142
array, err = atree.NewArrayWithRootID(storage, arrayID)
143143
require.NoError(b, err)
144144

145-
for i := 0; i < numberOfElements; i++ {
145+
for range numberOfElements {
146146
ind := r.Intn(int(array.Count()))
147147
v := RandomValue(r)
148148

@@ -163,7 +163,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
163163
array, err = atree.NewArrayWithRootID(storage, arrayID)
164164
require.NoError(b, err)
165165

166-
for i := 0; i < numberOfElements; i++ {
166+
for range numberOfElements {
167167
ind := r.Intn(int(array.Count()))
168168
_, err := array.Get(uint64(ind))
169169
require.NoError(b, err)
@@ -217,7 +217,7 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArrayCount, numberOfOp
217217
var totalRawDataSize uint32
218218

219219
// setup
220-
for i := 0; i < initialArrayCount; i++ {
220+
for range initialArrayCount {
221221
v := RandomValue(r)
222222

223223
storable, err := v.Storable(storage, array.Address(), atree.MaxInlineArrayElementSize())
@@ -231,7 +231,7 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArrayCount, numberOfOp
231231
require.NoError(b, storage.Commit())
232232
b.ResetTimer()
233233

234-
for i := 0; i < numberOfOps; i++ {
234+
for range numberOfOps {
235235
ind := r.Intn(int(array.Count()))
236236
// select opt
237237
switch r.Intn(2) {

array_serialization_verify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (v *serializationVerifier) arrayDataSlabEqual(expected, actual *ArrayDataSl
203203
}
204204

205205
// Compare element
206-
for i := 0; i < len(expected.elements); i++ {
206+
for i := range expected.elements {
207207
ee := expected.elements[i]
208208
ae := actual.elements[i]
209209

@@ -408,7 +408,7 @@ func hasInlinedComposite(data []byte) (bool, error) {
408408
if err != nil {
409409
return false, NewDecodingError(err)
410410
}
411-
for i := uint64(0); i < extraDataCount; i++ {
411+
for range extraDataCount {
412412
tagNum, err := dec.DecodeTagNumber()
413413
if err != nil {
414414
return false, NewDecodingError(err)

0 commit comments

Comments
 (0)