forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckisnumberpoweroftwo_test.go
54 lines (49 loc) · 1.33 KB
/
checkisnumberpoweroftwo_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
// checkisnumberpoweroftwo_test.go
// description: Test for Is the number a power of two
// author(s) [red_byte](https://github.com/i-redbyte)
// see checkisnumberpoweroftwo.go
package math
import (
"math"
"testing"
)
func getTestsForPowerOfTwo() []struct {
name string
a int
missing bool
} {
var tests = []struct {
name string
a int
missing bool
}{
{"Is 64 a power of 2? - YES", 64, true},
{"Is 1 a power of 2? - YES", 1, true},
{"Is 2 a power of 2? - YES", 2, true},
{"Is 5 a power of 2? - NO", 5, false},
{"Is 1023 a power of 2? - NO", 1023, false},
{"Is 1024 a power of 2? - YES", 1024, true},
{"Is 0 a power of 2? - NO", 0, false},
{"Is 9223372036854775807 a power of 2? - NO", math.MaxInt64, false},
{"Is 9223372036854775806 a power of 2? - NO", math.MaxInt64, false},
{"Is 4611686018427387904 a power of 2? - YES", 4611686018427387904, true},
}
return tests
}
func TestIsPowOfTwoUseLog(t *testing.T) {
tests := getTestsForPowerOfTwo()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := IsPowOfTwoUseLog(float64(tv.a))
t.Log(tv.a, " ", result)
if result != tv.missing {
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result)
}
})
}
}
func BenchmarkIsPowerOfTwoUseLog(b *testing.B) {
for i := 0; i < b.N; i++ {
IsPowOfTwoUseLog(1024)
}
}