| 1 |
package bisect_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/c-bata/go-prompt/internal/bisect" |
| 9 |
) |
| 10 |
|
| 11 |
func Example() { |
| 12 |
in := []int{1, 2, 3, 3, 3, 6, 7} |
| 13 |
fmt.Println("Insertion position for 0 in the slice is", bisect.Right(in, 0)) |
| 14 |
fmt.Println("Insertion position for 4 in the slice is", bisect.Right(in, 4)) |
| 15 |
|
| 16 |
// Output: |
| 17 |
// Insertion position for 0 in the slice is 0 |
| 18 |
// Insertion position for 4 in the slice is 5 |
| 19 |
} |
| 20 |
|
| 21 |
func TestBisectRight(t *testing.T) { |
| 22 |
// Thanks!! https://play.golang.org/p/y9NRj_XVIW |
| 23 |
in := []int{1, 2, 3, 3, 3, 6, 7} |
| 24 |
|
| 25 |
r := bisect.Right(in, 0) |
| 26 |
if r != 0 { |
| 27 |
t.Errorf("number 0 should inserted at 0 position, but got %d", r) |
| 28 |
} |
| 29 |
|
| 30 |
r = bisect.Right(in, 4) |
| 31 |
if r != 5 { |
| 32 |
t.Errorf("number 4 should inserted at 5 position, but got %d", r) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func BenchmarkRight(b *testing.B) { |
| 37 |
rand.Seed(0) |
| 38 |
|
| 39 |
for _, l := range []int{10, 1e2, 1e3, 1e4} { |
| 40 |
x := rand.Perm(l) |
| 41 |
insertion := rand.Int() |
| 42 |
|
| 43 |
b.Run(fmt.Sprintf("arrayLength=%d", l), func(b *testing.B) { |
| 44 |
b.ResetTimer() |
| 45 |
for n := 0; n < b.N; n++ { |
| 46 |
bisect.Right(x, insertion) |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
} |