Skip to content

Commit

Permalink
binary search messages in multicall
Browse files Browse the repository at this point in the history
Signed-off-by: Daisuke Kanda <[email protected]>
  • Loading branch information
dai1975 committed Jul 3, 2024
1 parent f00f00e commit 9af7e76
Show file tree
Hide file tree
Showing 2 changed files with 327 additions and 66 deletions.
75 changes: 75 additions & 0 deletions pkg/relay/ethereum/findItems_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ethereum

import (
"testing"
"fmt"
"slices"
)

func TestFindItems(t *testing.T) {
cases := []struct{
size int
expect int
expectLog []int
}{
{
size: 0,
expect: 0,
expectLog: []int{ },
},
{
size: 10,
expect: 10,
expectLog: []int{ 10 },
},
{
size: 10,
expect: 9,
expectLog: []int{ 10, 5, 7, 8, 9 },
},
{
size: 10,
expect: 0,
expectLog: []int{ 10, 5, 2, 1 },
},
{
size: 10,
expect: 0,
expectLog: []int{ 10, 5, 2, 1 },
},
{
size: 10,
expect: 2,
expectLog: []int{ 10, 5, 2, 3 },
},
}

for _, c := range cases {
type Data struct{
expect int
log []int
}
data := Data{ expect: c.expect, log: make([]int, 0, c.size) }
result, err := findItems(c.size, &data, func(count int, d *Data) (error) {
d.log = append(d.log, count)
if count <= data.expect {
return nil
} else {
return fmt.Errorf("fail at count=%d", count)
}
})
if c.expect == 0 {
if err == nil {
t.Errorf("findItems(%d,%d) unexpectedly returned %v", c.size, c.expect, result)
}
} else if err != nil {
t.Errorf("findItems(%d,%d) unexpectedly failed: %v", c.size, c.expect, err)
} else if result != c.expect {
t.Errorf("findItems(%d,%d) has been mistakenly resulted in %v", c.size, c.expect, result)
}
if slices.Compare(data.log, c.expectLog) != 0 {
t.Errorf("findItems(%d,%d) log unpexpected %v, expected=%v", c.size, c.expect, data.log, c.expectLog)
}
}
}

Loading

0 comments on commit 9af7e76

Please sign in to comment.