-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearches_suite_test.go
69 lines (55 loc) · 1.51 KB
/
searches_suite_test.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package searches
import (
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gmeasure"
)
var _ = Describe("Searches", func() {
var (
items []int
search func(items []int, target int) int
experiment *gmeasure.Experiment
)
AssertSortBehavior := func() {
BeforeEach(func() {
By("Create a sorted array")
items = []int{1, 6, 18, 21, 23, 24, 26, 29, 40}
By("Create experiment")
experiment = gmeasure.NewExperiment("Sort Measurement")
AddReportEntry(experiment.Name, experiment)
})
It("should can be search", func() {
By("Search one times")
Expect(search(items, 26)).To(Equal(6))
Expect(search(items, 0)).To(Equal(-1))
})
It("search 1k times", Serial, Label("measurement"), func() {
experiment.SampleDuration("Benchmark1K", func(idx int) {
search(items, 6)
}, gmeasure.SamplingConfig{N: 1 << 10}, gmeasure.Precision(time.Nanosecond))
})
It("search array 64k times", Serial, Label("measurement"), func() {
experiment.SampleDuration("Benchmark64K", func(idx int) {
search(items, 6)
}, gmeasure.SamplingConfig{N: 1 << 16}, gmeasure.Precision(time.Nanosecond))
})
}
Describe("Binary Search With Recursion", func() {
BeforeEach(func() {
search = BinarySearchByRecur
})
AssertSortBehavior()
})
Describe("Binary Search", func() {
BeforeEach(func() {
search = BinarySearch
})
AssertSortBehavior()
})
})
func TestSearches(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Searches Suite")
}