Skip to content

Commit

Permalink
chore(examples): improve p/moul/txlink
Browse files Browse the repository at this point in the history
Escape args.

Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Feb 4, 2025
1 parent c24f69f commit 38c3d30
Show file tree
Hide file tree
Showing 36 changed files with 1,265 additions and 202 deletions.
171 changes: 87 additions & 84 deletions examples/gno.land/p/demo/btree/btree_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -523,99 +523,22 @@ func TestBTree(t *testing.T) {
}
}

func TestStress(t *testing.T) {
// Loop through creating B-Trees with a range of degrees from 3 to 12, stepping by 3.
// Insert 1000 records into each tree, then search for each record.
// Delete half of the records, skipping every other one, then search for each record.

for degree := 3; degree <= 12; degree += 3 {
t.Logf("Testing B-Tree of degree %d\n", degree)
tree := New(WithDegree(degree))

// Insert 1000 records
t.Logf("Inserting 1000 records\n")
for i := 0; i < 1000; i++ {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
tree.Insert(content)
}

// Search for all records
for i := 0; i < 1000; i++ {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
val := tree.Get(content)
if val == nil {
t.Errorf("Expected key %v, but didn't find it", content.Key)
}
}

// Delete half of the records
for i := 0; i < 1000; i += 2 {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
tree.Delete(content)
}

// Search for all records
for i := 0; i < 1000; i++ {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
val := tree.Get(content)
if i%2 == 0 {
if val != nil {
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, val.(Content).Key, val.(Content).Value)
}
} else {
if val == nil {
t.Errorf("Expected key %v, but didn't find it", content.Key)
}
}
}
}

// Now create a very large tree, with 100000 records
// Then delete roughly one third of them, using a very basic random number generation scheme
// (implement it right here) to determine which records to delete.
// Print a few lines using Logf to let the user know what's happening.

t.Logf("Testing B-Tree of degree 10 with 100000 records\n")
tree := New(WithDegree(10))

// Insert 100000 records
t.Logf("Inserting 100000 records\n")
for i := 0; i < 100000; i++ {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
tree.Insert(content)
}

// Implement a very basic random number generator
seed := 0
random := func() int {
seed = (seed*1103515245 + 12345) & 0x7fffffff
return seed
}

// Delete one third of the records
t.Logf("Deleting one third of the records\n")
for i := 0; i < 35000; i++ {
content := Content{Key: random() % 100000, Value: fmt.Sprintf("Value_%d", i)}
tree.Delete(content)
}
}

// Write a test that populates a large B-Tree with 10000 records.
// Write a test that populates a large B-Tree with 1000 records.
// It should then `Clone` the tree, make some changes to both the original and the clone,
// And then clone the clone, and make some changes to all three trees, and then check that the changes are isolated
// to the tree they were made in.

func TestBTreeCloneIsolation(t *testing.T) {
t.Logf("Creating B-Tree of degree 10 with 10000 records\n")
tree := genericSeeding(New(WithDegree(10)), 10000)
t.Logf("Creating B-Tree of degree 10 with 1000 records\n")
size := 1000
tree := genericSeeding(New(WithDegree(10)), size)

// Clone the tree
t.Logf("Cloning the tree\n")
clone := tree.Clone()

// Make some changes to the original and the clone
t.Logf("Making changes to the original and the clone\n")
for i := 0; i < 10000; i += 2 {
for i := 0; i < size; i += 2 {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
tree.Delete(content)
content = Content{Key: i + 1, Value: fmt.Sprintf("Value_%d", i+1)}
Expand All @@ -628,7 +551,7 @@ func TestBTreeCloneIsolation(t *testing.T) {

// Make some changes to all three trees
t.Logf("Making changes to all three trees\n")
for i := 0; i < 10000; i += 3 {
for i := 0; i < size; i += 3 {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
tree.Delete(content)
content = Content{Key: i, Value: fmt.Sprintf("Value_%d", i+1)}
Expand All @@ -639,7 +562,7 @@ func TestBTreeCloneIsolation(t *testing.T) {

// Check that the changes are isolated to the tree they were made in
t.Logf("Checking that the changes are isolated to the tree they were made in\n")
for i := 0; i < 10000; i++ {
for i := 0; i < size; i++ {
content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
val := tree.Get(content)

Expand Down Expand Up @@ -676,3 +599,83 @@ func TestBTreeCloneIsolation(t *testing.T) {
}
}
}

// --------------------
// Stress tests. Disabled for testing performance

//func TestStress(t *testing.T) {
// // Loop through creating B-Trees with a range of degrees from 3 to 12, stepping by 3.
// // Insert 1000 records into each tree, then search for each record.
// // Delete half of the records, skipping every other one, then search for each record.
//
// for degree := 3; degree <= 12; degree += 3 {
// t.Logf("Testing B-Tree of degree %d\n", degree)
// tree := New(WithDegree(degree))
//
// // Insert 1000 records
// t.Logf("Inserting 1000 records\n")
// for i := 0; i < 1000; i++ {
// content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
// tree.Insert(content)
// }
//
// // Search for all records
// for i := 0; i < 1000; i++ {
// content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
// val := tree.Get(content)
// if val == nil {
// t.Errorf("Expected key %v, but didn't find it", content.Key)
// }
// }
//
// // Delete half of the records
// for i := 0; i < 1000; i += 2 {
// content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
// tree.Delete(content)
// }
//
// // Search for all records
// for i := 0; i < 1000; i++ {
// content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
// val := tree.Get(content)
// if i%2 == 0 {
// if val != nil {
// t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, val.(Content).Key, val.(Content).Value)
// }
// } else {
// if val == nil {
// t.Errorf("Expected key %v, but didn't find it", content.Key)
// }
// }
// }
// }
//
// // Now create a very large tree, with 100000 records
// // Then delete roughly one third of them, using a very basic random number generation scheme
// // (implement it right here) to determine which records to delete.
// // Print a few lines using Logf to let the user know what's happening.
//
// t.Logf("Testing B-Tree of degree 10 with 100000 records\n")
// tree := New(WithDegree(10))
//
// // Insert 100000 records
// t.Logf("Inserting 100000 records\n")
// for i := 0; i < 100000; i++ {
// content := Content{Key: i, Value: fmt.Sprintf("Value_%d", i)}
// tree.Insert(content)
// }
//
// // Implement a very basic random number generator
// seed := 0
// random := func() int {
// seed = (seed*1103515245 + 12345) & 0x7fffffff
// return seed
// }
//
// // Delete one third of the records
// t.Logf("Deleting one third of the records\n")
// for i := 0; i < 35000; i++ {
// content := Content{Key: random() % 100000, Value: fmt.Sprintf("Value_%d", i)}
// tree.Delete(content)
// }
//}
13 changes: 7 additions & 6 deletions examples/gno.land/p/demo/diff/diff_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ func TestMyersDiff(t *testing.T) {
new: strings.Repeat("b", 1000),
expected: "[-" + strings.Repeat("a", 1000) + "][+" + strings.Repeat("b", 1000) + "]",
},
{
name: "Very long strings",
old: strings.Repeat("a", 10000) + "b" + strings.Repeat("a", 10000),
new: strings.Repeat("a", 10000) + "c" + strings.Repeat("a", 10000),
expected: strings.Repeat("a", 10000) + "[-b][+c]" + strings.Repeat("a", 10000),
},
//{ // disabled for testing performance
// XXX: consider adding a flag to run such tests, not like `-short`, or switching to a `-bench`, maybe.
// name: "Very long strings",
// old: strings.Repeat("a", 10000) + "b" + strings.Repeat("a", 10000),
// new: strings.Repeat("a", 10000) + "c" + strings.Repeat("a", 10000),
// expected: strings.Repeat("a", 10000) + "[-b][+c]" + strings.Repeat("a", 10000),
//},
}

for _, tc := range tests {
Expand Down
16 changes: 8 additions & 8 deletions examples/gno.land/p/demo/json/node_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func TestNode_GetBool(t *testing.T) {
}
}

func TestNode_GetBool_Fail(t *testing.T) {
func TestNode_GetBool_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"literally null node", NullNode("")},
Expand Down Expand Up @@ -357,7 +357,7 @@ func TestNode_GetNull(t *testing.T) {
}
}

func TestNode_GetNull_Fail(t *testing.T) {
func TestNode_GetNull_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"number node is null", NumberNode("", 42)},
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestNode_GetNumeric_With_Unmarshal(t *testing.T) {
}
}

func TestNode_GetNumeric_Fail(t *testing.T) {
func TestNode_GetNumeric_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"null node", NullNode("")},
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestNode_GetString(t *testing.T) {
}
}

func TestNode_GetString_Fail(t *testing.T) {
func TestNode_GetString_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"null node", NullNode("")},
Expand Down Expand Up @@ -577,7 +577,7 @@ func TestNode_GetArray(t *testing.T) {
}
}

func TestNode_GetArray_Fail(t *testing.T) {
func TestNode_GetArray_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"null node", NullNode("")},
Expand Down Expand Up @@ -736,7 +736,7 @@ func TestNode_Index(t *testing.T) {
}
}

func TestNode_Index_Fail(t *testing.T) {
func TestNode_Index_NotSucceed(t *testing.T) {
tests := []struct {
name string
node *Node
Expand Down Expand Up @@ -854,7 +854,7 @@ func TestNode_GetKey(t *testing.T) {
}
}

func TestNode_GetKey_Fail(t *testing.T) {
func TestNode_GetKey_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"null node", NullNode("")},
Expand Down Expand Up @@ -998,7 +998,7 @@ func TestNode_GetObject(t *testing.T) {
}
}

func TestNode_GetObject_Fail(t *testing.T) {
func TestNode_GetObject_NotSucceed(t *testing.T) {
tests := []simpleNode{
{"nil node", (*Node)(nil)},
{"get object from null node", NullNode("")},
Expand Down
18 changes: 14 additions & 4 deletions examples/gno.land/p/demo/ownable/ownable.gno
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,28 @@ func (o *Ownable) DropOwnership() error {
}

// Owner returns the owner address from Ownable
func (o Ownable) Owner() std.Address {
func (o *Ownable) Owner() std.Address {
if o == nil {
return std.Address("")
}
return o.owner
}

// CallerIsOwner checks if the caller of the function is the Realm's owner
func (o Ownable) CallerIsOwner() bool {
func (o *Ownable) CallerIsOwner() bool {
if o == nil {
return false
}
return std.PrevRealm().Addr() == o.owner
}

// AssertCallerIsOwner panics if the caller is not the owner
func (o Ownable) AssertCallerIsOwner() {
if std.PrevRealm().Addr() != o.owner {
func (o *Ownable) AssertCallerIsOwner() {
if o == nil {
panic(ErrUnauthorized)
}
caller := std.PrevRealm().Addr()
if caller != o.owner {
panic(ErrUnauthorized)
}
}
48 changes: 48 additions & 0 deletions examples/gno.land/p/demo/ownable/ownable_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,51 @@ func TestErrInvalidAddress(t *testing.T) {
err = o.TransferOwnership("10000000001000000000100000000010000000001000000000")
uassert.ErrorContains(t, err, ErrInvalidAddress.Error())
}

func TestAssertCallerIsOwner(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(alice))
std.TestSetOrigCaller(alice)

o := New()

// Should not panic when caller is owner
o.AssertCallerIsOwner()

// Should panic when caller is not owner
std.TestSetRealm(std.NewUserRealm(bob))
std.TestSetOrigCaller(bob)

defer func() {
r := recover()
if r == nil {
t.Error("expected panic but got none")
}
if r != ErrUnauthorized {
t.Errorf("expected ErrUnauthorized but got %v", r)
}
}()
o.AssertCallerIsOwner()
}

func TestNilReceiver(t *testing.T) {
var o *Ownable

owner := o.Owner()
if owner != std.Address("") {
t.Errorf("expected empty address but got %v", owner)
}

isOwner := o.CallerIsOwner()
uassert.False(t, isOwner)

defer func() {
r := recover()
if r == nil {
t.Error("expected panic but got none")
}
if r != ErrUnauthorized {
t.Errorf("expected ErrUnauthorized but got %v", r)
}
}()
o.AssertCallerIsOwner()
}
Loading

0 comments on commit 38c3d30

Please sign in to comment.