File: medians_test.go

package info (click to toggle)
golang-github-biogo-store 0.0~git20200525.8c94ae1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 380 kB
  • sloc: makefile: 2
file content (167 lines) | stat: -rw-r--r-- 3,472 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright ©2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package kdtree

import (
	"math/rand"
	"sort"
	"testing"

	"gopkg.in/check.v1"
)

type Ints []int

func (a Ints) Len() int                  { return len(a) }
func (a Ints) Less(i, j int) bool        { return a[i] < a[j] }
func (a Ints) Slice(s, e int) SortSlicer { return a[s:e] }
func (a Ints) Swap(i, j int)             { a[i], a[j] = a[j], a[i] }

func (s *S) TestPartition(c *check.C) {
	for p := 0; p < 100; p++ {
		list := make(Ints, 1e5)
		for i := range list {
			list[i] = rand.Int()
		}
		pi := Partition(list, rand.Intn(list.Len()))
		for i := 0; i < pi; i++ {
			c.Check(list[i] <= list[pi], check.Equals, true)
		}
		for i := pi + 1; i < len(list); i++ {
			c.Check(list[i] > list[pi], check.Equals, true)
		}
	}
}

func (s *S) TestPartitionCollision(c *check.C) {
	for p := 0; p < 10; p++ {
		list := make(Ints, 10)
		for i := range list {
			list[i] = rand.Intn(5)
		}
		pi := Partition(list, p)
		for i := 0; i < pi; i++ {
			c.Check(list[i] <= list[pi], check.Equals, true)
		}
		for i := pi + 1; i < len(list); i++ {
			c.Check(list[i] > list[pi], check.Equals, true)
		}
	}
}

func sortSelection(list Ints, k int) int {
	sort.Sort(list)
	return list[k]
}

func (s *S) TestSelect(c *check.C) {
	for k := 0; k < 2121; k++ {
		list := make(Ints, 2121)
		for i := range list {
			list[i] = rand.Intn(1000)
		}
		Select(list, k)
		sorted := append(Ints(nil), list...)
		c.Check(list[k], check.Equals, sortSelection(sorted, k), check.Commentf("\n%d\n%v\n%v", k, list, sorted))
	}
}

func (s *S) TestMedianOfMedians(c *check.C) {
	list := make(Ints, 1e4)
	for i := range list {
		list[i] = rand.Int()
	}
	p := MedianOfMedians(list)
	med := list[p]
	sort.Sort(list)
	var found bool
	for _, v := range list[len(list)*3/10 : len(list)*7/10+1] {
		if v == med {
			found = true
			break
		}
	}
	c.Check(found, check.Equals, true)
}

func (s *S) TestMedianOfRandoms(c *check.C) {
	list := make(Ints, 1e4)
	for i := range list {
		list[i] = rand.Int()
	}
	p := MedianOfRandoms(list, Randoms)
	med := list[p]
	sort.Sort(list)
	var found bool
	for _, v := range list[len(list)*3/10 : len(list)*7/10+1] {
		if v == med {
			found = true
			break
		}
	}
	c.Check(found, check.Equals, true)
}

func BenchmarkMoM(b *testing.B) {
	for i := 0; i < b.N; i++ {
		b.StopTimer()
		list := make(Ints, 1e4)
		for i := range list {
			list[i] = rand.Int()
		}
		b.StartTimer()
		_ = MedianOfMedians(list)
	}
}

func BenchmarkMoMPartition(b *testing.B) {
	for i := 0; i < b.N; i++ {
		b.StopTimer()
		list := make(Ints, 1e4)
		for i := range list {
			list[i] = rand.Int()
		}
		b.StartTimer()
		p := MedianOfMedians(list)
		p = Partition(list, p)
	}
}

func BenchmarkRM(b *testing.B) {
	b.StopTimer()
	list := make(Ints, 1e4)
	for i := range list {
		list[i] = rand.Int()
	}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		_ = MedianOfRandoms(list, list.Len()/1e3)
	}
}

func BenchmarkRMPartition(b *testing.B) {
	b.StopTimer()
	list := make(Ints, 1e4)
	for i := range list {
		list[i] = rand.Int()
	}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		p := MedianOfRandoms(list, list.Len()/1e3)
		p = Partition(list, p)
	}
}

func BenchmarkSM(b *testing.B) {
	for i := 0; i < b.N; i++ {
		b.StopTimer()
		list := make(Ints, 1e4)
		for i := range list {
			list[i] = rand.Int()
		}
		b.StartTimer()
		sort.Sort(list)
	}
}