From b482ec1252a7b04bec9613439f8f44cfb1443c7d Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:32:45 +0100 Subject: [PATCH] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- examples/gno.land/p/demo/avl/list/list.gno | 62 ++-------- .../gno.land/p/demo/avl/list/list_test.gno | 112 +++++------------- 2 files changed, 38 insertions(+), 136 deletions(-) diff --git a/examples/gno.land/p/demo/avl/list/list.gno b/examples/gno.land/p/demo/avl/list/list.gno index 25842438879..b0c1fd1012d 100644 --- a/examples/gno.land/p/demo/avl/list/list.gno +++ b/examples/gno.land/p/demo/avl/list/list.gno @@ -8,7 +8,7 @@ // Example usage: // // // Create a new list and add elements -// l := list.New() +// var l list.List // l.Append(1, 2, 3) // // // Get and set elements @@ -47,41 +47,6 @@ type List struct { idGen seqid.ID } -// New creates a new empty list. -// -// Example: -// -// l := list.New() -// l.Append(1, 2, 3) -// println(l.Len()) // Output: 3 -func New() *List { - return &List{} -} - -// Make creates a new list with the specified length, filled with the given default value. -// If defaultValue is nil, the list will be filled with nil values. -// Returns an empty list if length is negative. -// -// Example: -// -// l := list.Make(3, "default") -// println(l.Get(0)) // Output: "default" -// println(l.Len()) // Output: 3 -// -// // With nil default value -// l2 := list.Make(2, nil) -// println(l2.Get(0)) // Output: nil -func Make(length int, defaultValue interface{}) *List { - if length < 0 { - return New() - } - l := New() - for i := 0; i < length; i++ { - l.Append(defaultValue) - } - return l -} - // Len returns the number of elements in the list. // // Example: @@ -209,7 +174,7 @@ func (l *List) Delete(index int) (interface{}, bool) { return value, true } -// Range returns a slice of values from startIndex (inclusive) to endIndex (exclusive). +// Slice returns a slice of values from startIndex (inclusive) to endIndex (exclusive). // Returns nil if the range is invalid. // // Example: @@ -217,11 +182,11 @@ func (l *List) Delete(index int) (interface{}, bool) { // l := list.New() // l.Append(1, 2, 3, 4, 5) // -// println(l.Range(1, 4)) // Output: [2 3 4] -// println(l.Range(-1, 2)) // Output: [1 2] -// println(l.Range(3, 999)) // Output: [4 5] -// println(l.Range(3, 2)) // Output: nil -func (l *List) Range(startIndex, endIndex int) []interface{} { +// println(l.Slice(1, 4)) // Output: [2 3 4] +// println(l.Slice(-1, 2)) // Output: [1 2] +// println(l.Slice(3, 999)) // Output: [4 5] +// println(l.Slice(3, 2)) // Output: nil +func (l *List) Slice(startIndex, endIndex int) []interface{} { size := l.tree.Size() // Normalize bounds @@ -370,16 +335,3 @@ func (l *List) DeleteRange(startIndex, endIndex int) int { return len(keysToDelete) } - -// Clear removes all elements from the list. -// -// Example: -// -// l := list.New() -// l.Append(1, 2, 3) -// l.Clear() -// println(l.Len()) // Output: 0 -func (l *List) Clear() { - l.tree = avl.Tree{} // Reset to empty tree - l.idGen = seqid.ID(0) // Reset ID generator -} diff --git a/examples/gno.land/p/demo/avl/list/list_test.gno b/examples/gno.land/p/demo/avl/list/list_test.gno index c8afc0a3756..265fbdb5eb1 100644 --- a/examples/gno.land/p/demo/avl/list/list_test.gno +++ b/examples/gno.land/p/demo/avl/list/list_test.gno @@ -5,7 +5,7 @@ import ( ) func TestList_Basic(t *testing.T) { - l := New() + var l List // Test empty list if l.Len() != 0 { @@ -39,7 +39,7 @@ func TestList_Basic(t *testing.T) { } func TestList_Set(t *testing.T) { - l := New() + var l List l.Append(1, 2, 3) // Test valid set within bounds @@ -76,7 +76,7 @@ func TestList_Set(t *testing.T) { } func TestList_Delete(t *testing.T) { - l := New() + var l List l.Append(1, 2, 3) // Test valid delete @@ -99,31 +99,31 @@ func TestList_Delete(t *testing.T) { } } -func TestList_Range(t *testing.T) { - l := New() +func TestList_Slice(t *testing.T) { + var l List l.Append(1, 2, 3, 4, 5) // Test valid ranges - values := l.Range(1, 4) + values := l.Slice(1, 4) expected := []interface{}{2, 3, 4} if !sliceEqual(values, expected) { - t.Errorf("Range(1,4) = %v; want %v", values, expected) + t.Errorf("Slice(1,4) = %v; want %v", values, expected) } // Test edge cases - if values := l.Range(-1, 2); !sliceEqual(values, []interface{}{1, 2}) { - t.Errorf("Range(-1,2) = %v; want [1 2]", values) + if values := l.Slice(-1, 2); !sliceEqual(values, []interface{}{1, 2}) { + t.Errorf("Slice(-1,2) = %v; want [1 2]", values) } - if values := l.Range(3, 10); !sliceEqual(values, []interface{}{4, 5}) { - t.Errorf("Range(3,10) = %v; want [4 5]", values) + if values := l.Slice(3, 10); !sliceEqual(values, []interface{}{4, 5}) { + t.Errorf("Slice(3,10) = %v; want [4 5]", values) } - if values := l.Range(3, 2); values != nil { - t.Errorf("Range(3,2) = %v; want nil", values) + if values := l.Slice(3, 2); values != nil { + t.Errorf("Slice(3,2) = %v; want nil", values) } } func TestList_ForEach(t *testing.T) { - l := New() + var l List l.Append(1, 2, 3) sum := 0 @@ -149,7 +149,7 @@ func TestList_ForEach(t *testing.T) { } func TestList_Clone(t *testing.T) { - l := New() + var l List l.Append(1, 2, 3) clone := l.Clone() @@ -174,7 +174,7 @@ func TestList_Clone(t *testing.T) { } func TestList_DeleteRange(t *testing.T) { - l := New() + var l List l.Append(1, 2, 3, 4, 5) // Test valid range delete @@ -193,7 +193,7 @@ func TestList_DeleteRange(t *testing.T) { } // Test edge cases - l = New() + l = List{} l.Append(1, 2, 3) // Delete with negative start @@ -202,7 +202,7 @@ func TestList_DeleteRange(t *testing.T) { } // Delete with end > length - l = New() + l = List{} l.Append(1, 2, 3) if deleted := l.DeleteRange(1, 5); deleted != 2 { t.Errorf("DeleteRange(1,5) deleted %d elements; want 2", deleted) @@ -220,7 +220,7 @@ func TestList_DeleteRange(t *testing.T) { } func TestList_EmptyOperations(t *testing.T) { - l := New() + var l List // Operations on empty list if v := l.Get(0); v != nil { @@ -235,17 +235,17 @@ func TestList_EmptyOperations(t *testing.T) { t.Errorf("Get(0) after Set = %v; want 1", v) } - l = New() // Reset to empty list + l = List{} // Reset to empty list if v, ok := l.Delete(0); ok || v != nil { t.Errorf("Delete(0) on empty list = %v, %v; want nil, false", v, ok) } - if values := l.Range(0, 1); values != nil { + if values := l.Slice(0, 1); values != nil { t.Errorf("Range(0,1) on empty list = %v; want nil", values) } } func TestList_DifferentTypes(t *testing.T) { - l := New() + var l List // Test with different types l.Append(42, "hello", true, 3.14) @@ -265,7 +265,7 @@ func TestList_DifferentTypes(t *testing.T) { } func TestList_LargeOperations(t *testing.T) { - l := New() + var l List // Test with larger number of elements n := 1000 @@ -278,7 +278,7 @@ func TestList_LargeOperations(t *testing.T) { } // Test range on large list - values := l.Range(n-3, n) + values := l.Slice(n-3, n) expected := []interface{}{n - 3, n - 2, n - 1} if !sliceEqual(values, expected) { t.Errorf("Range(%d,%d) = %v; want %v", n-3, n, values, expected) @@ -295,7 +295,7 @@ func TestList_LargeOperations(t *testing.T) { } func TestList_ChainedOperations(t *testing.T) { - l := New() + var l List // Test sequence of operations l.Append(1, 2, 3) @@ -312,7 +312,7 @@ func TestList_ChainedOperations(t *testing.T) { } func TestList_RangeEdgeCases(t *testing.T) { - l := New() + var l List l.Append(1, 2, 3, 4, 5) // Test various edge cases for Range @@ -329,65 +329,15 @@ func TestList_RangeEdgeCases(t *testing.T) { } for _, tc := range cases { - got := l.Range(tc.start, tc.end) + got := l.Slice(tc.start, tc.end) if !sliceEqual(got, tc.want) { - t.Errorf("Range(%d,%d) = %v; want %v", tc.start, tc.end, got, tc.want) + t.Errorf("Slice(%d,%d) = %v; want %v", tc.start, tc.end, got, tc.want) } } } -func TestList_Clear(t *testing.T) { - l := New() - l.Append(1, 2, 3) - - // Test clear - l.Clear() - if l.Len() != 0 { - t.Errorf("after Clear(), Len() = %d; want 0", l.Len()) - } - - // Test that we can still append after clearing - l.Append(4) - if l.Len() != 1 { - t.Errorf("after Clear() and Append(), Len() = %d; want 1", l.Len()) - } - if v := l.Get(0); v != 4 { - t.Errorf("after Clear() and Append(), Get(0) = %v; want 4", v) - } -} - -func TestList_Make(t *testing.T) { - // Test with default value - l := Make(3, "default") - if l.Len() != 3 { - t.Errorf("Make(3, default) Len() = %d; want 3", l.Len()) - } - for i := 0; i < l.Len(); i++ { - if v := l.Get(i); v != "default" { - t.Errorf("index %d = %v; want 'default'", i, v) - } - } - - // Test with nil default value - l = Make(2, nil) - if l.Len() != 2 { - t.Errorf("Make(2, nil) Len() = %d; want 2", l.Len()) - } - for i := 0; i < l.Len(); i++ { - if v := l.Get(i); v != nil { - t.Errorf("index %d = %v; want nil", i, v) - } - } - - // Test with negative length - l = Make(-1, "default") - if l.Len() != 0 { - t.Errorf("Make(-1, default) Len() = %d; want 0", l.Len()) - } -} - func TestList_IndexConsistency(t *testing.T) { - l := New() + var l List // Initial additions l.Append(1, 2, 3, 4, 5) // [1,2,3,4,5] @@ -418,9 +368,9 @@ func TestList_IndexConsistency(t *testing.T) { } // Verify all indices are accessible - allValues := l.Range(0, l.Len()) + allValues := l.Slice(0, l.Len()) if !sliceEqual(allValues, expected) { - t.Errorf("Range(0, Len()) = %v; want %v", allValues, expected) + t.Errorf("Slice(0, Len()) = %v; want %v", allValues, expected) } // Verify no gaps in iteration