step one
This commit is contained in:
parent
7a8d5d13fa
commit
9e663db9dc
68 changed files with 5647 additions and 2958 deletions
28
internal/numerics/search_test.go
Normal file
28
internal/numerics/search_test.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package numerics
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBisect(t *testing.T) {
|
||||
// f(i) = 10*i, monotone increasing.
|
||||
f := func(i int) float64 { return 10 * float64(i) }
|
||||
|
||||
// target = 25 → largest i with 10i < 25 is i=2
|
||||
if got := Bisect(0, 10, 25, f); got != 2 {
|
||||
t.Errorf("Bisect target=25 = %d, want 2", got)
|
||||
}
|
||||
|
||||
// target on boundary: target = 30, condition is target <= f(mid) so f(3)=30 → not less; want 2
|
||||
if got := Bisect(0, 10, 30, f); got != 2 {
|
||||
t.Errorf("Bisect target=30 = %d, want 2", got)
|
||||
}
|
||||
|
||||
// target below all values
|
||||
if got := Bisect(0, 10, -5, f); got != 0 {
|
||||
t.Errorf("Bisect target=-5 = %d, want 0", got)
|
||||
}
|
||||
|
||||
// target above all values
|
||||
if got := Bisect(0, 10, 1000, f); got != 10 {
|
||||
t.Errorf("Bisect target=1000 = %d, want 10", got)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue