-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daisuke Kanda <[email protected]>
- Loading branch information
Showing
2 changed files
with
327 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.