-
Notifications
You must be signed in to change notification settings - Fork 0
/
counting_test.go
169 lines (147 loc) · 3.15 KB
/
counting_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
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
168
169
package math
import "testing"
func BenchmarkFastSegmentRemainder(b *testing.B) {
n := 1000
for i := 0; i < n; i++ {
FastSegmentRemainder(20, 190)
}
}
func BenchmarkFastSegmentRemainder2(b *testing.B) {
n := 1000
for i := 0; i < n; i++ {
FastSegmentRemainder2(20, 190)
}
}
func BenchmarkGreatestCommonMeasure(b *testing.B) {
n := 1000
for i := 0; i < n; i++ {
GreatestCommonMeasure(196, 42)
}
}
func BenchmarkEuclid(b *testing.B) {
n := 1000
for i := 0; i < n; i++ {
Euclid(196, 42)
}
}
func TestFastSegmentRemainder(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
args args
want int
}{
{args{196, 42}, 28},
{args{196, 200}, 196},
}
for _, test := range tests {
if got := FastSegmentRemainder(test.args.a, test.args.b); got != test.want {
t.Errorf("FastSegmentRemainder gcd(%v, %v) = %v, want = %v", test.args.a, test.args.b, got, test.want)
}
if got := FastSegmentRemainder2(test.args.a, test.args.b); got != test.want {
t.Errorf("FastSegmentRemainder2 gcd(%d, %d) = %d, want = %d", test.args.a, test.args.b, got, test.want)
}
}
}
func TestGCM(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
args args
want int
}{
{args{196, 42}, 14},
{args{196, 200}, 4},
{args{25, 200}, 25},
{args{200, 1200}, 200},
}
for _, test := range tests {
if got := GreatestCommonMeasure(test.args.a, test.args.b); got != test.want {
t.Errorf("gcd(%v, %v) = %v, want = %v", test.args.a, test.args.b, got, test.want)
}
if got := Euclid(test.args.a, test.args.b); got != test.want {
t.Errorf("Euclid's gcd(%v, %v) = %v, want = %v", test.args.a, test.args.b, got, test.want)
}
}
}
// go test -test.v
func TestSieve(t *testing.T) {
n := 1000
p := Sieve(n)
for i, isPrime := range p {
if isPrime {
t.Logf("%d", i)
}
}
}
func BenchmarkSieve(b *testing.B) {
n := 1000
for i := 0; i < b.N; i++ {
Sieve(n)
}
}
func TestMultiply(t *testing.T) {
type args struct {
n int
a int
}
tests := []struct {
args args
want int
}{
{args{2, 3}, 6},
{args{2, 5}, 10},
{args{3, 5}, 15},
}
for _, test := range tests {
if got := Multiply(test.args.n, test.args.a); got != test.want {
t.Errorf("Multiply(%v, %v) = %v, want = %v", test.args.n, test.args.a, got, test.want)
}
}
}
func TestMultiply1And2(t *testing.T) {
type args struct {
n int
a int
}
tests := []struct {
args args
want int
}{
{args{2, 3}, 6},
{args{2, 5}, 10},
{args{3, 5}, 15},
}
r := 0
for _, test := range tests {
if got := Multiply1(r, test.args.n, test.args.a); got != test.want {
t.Errorf("Multiply1(%v, %v, %v) = %v, want = %v", r, test.args.n, test.args.a, got, test.want)
}
if got := Multiply2(r, test.args.n, test.args.a); got != test.want {
t.Errorf("Multiply2(%v, %v, %v) = %v, want = %v", r, test.args.n, test.args.a, got, test.want)
}
}
}
func TestOblongNumber(t *testing.T) {
tests := []struct {
n int
want int
}{
{1, 2},
{2, 6},
{3, 12},
{4, 20},
{5, 30},
{6, 42},
{7, 56},
}
for _, test := range tests {
if got := OblongNumber(test.n); got != test.want {
t.Errorf("OblongNumber(%v) = %v, want = %v", test.n, got, test.want)
}
}
}