File: bisect_test.go

package info (click to toggle)
golang-github-c-bata-go-prompt 0.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 396 kB
  • sloc: makefile: 37; python: 13; sh: 9
file content (50 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package bisect_test

import (
	"fmt"
	"math/rand"
	"testing"

	"github.com/c-bata/go-prompt/internal/bisect"
)

func Example() {
	in := []int{1, 2, 3, 3, 3, 6, 7}
	fmt.Println("Insertion position for 0 in the slice is", bisect.Right(in, 0))
	fmt.Println("Insertion position for 4 in the slice is", bisect.Right(in, 4))

	// Output:
	// Insertion position for 0 in the slice is 0
	// Insertion position for 4 in the slice is 5
}

func TestBisectRight(t *testing.T) {
	// Thanks!! https://play.golang.org/p/y9NRj_XVIW
	in := []int{1, 2, 3, 3, 3, 6, 7}

	r := bisect.Right(in, 0)
	if r != 0 {
		t.Errorf("number 0 should inserted at 0 position, but got %d", r)
	}

	r = bisect.Right(in, 4)
	if r != 5 {
		t.Errorf("number 4 should inserted at 5 position, but got %d", r)
	}
}

func BenchmarkRight(b *testing.B) {
	rand.Seed(0)

	for _, l := range []int{10, 1e2, 1e3, 1e4} {
		x := rand.Perm(l)
		insertion := rand.Int()

		b.Run(fmt.Sprintf("arrayLength=%d", l), func(b *testing.B) {
			b.ResetTimer()
			for n := 0; n < b.N; n++ {
				bisect.Right(x, insertion)
			}
		})
	}
}