28 lines
761 B
Go
28 lines
761 B
Go
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)
|
|
}
|
|
}
|