|
| 1 | +// checkisnumberpoweroftwo_test.go |
| 2 | +// description: Test for Is the number a power of two |
| 3 | +// author(s) [red_byte](https://github.com/i-redbyte) |
| 4 | +// see checkisnumberpoweroftwo.go |
| 5 | + |
| 6 | +package binary |
| 7 | + |
| 8 | +import ( |
| 9 | + math2 "github.com/TheAlgorithms/Go/math" |
| 10 | + "math" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func getTestsForPowerOfTwo() []struct { |
| 15 | + name string |
| 16 | + a int |
| 17 | + missing bool |
| 18 | +} { |
| 19 | + var tests = []struct { |
| 20 | + name string |
| 21 | + a int |
| 22 | + missing bool |
| 23 | + }{ |
| 24 | + {"Is 64 a power of 2? - YES", 64, true}, |
| 25 | + {"Is 1 a power of 2? - YES", 1, true}, |
| 26 | + {"Is 2 a power of 2? - YES", 2, true}, |
| 27 | + {"Is 5 a power of 2? - NO", 5, false}, |
| 28 | + {"Is 1023 a power of 2? - NO", 1023, false}, |
| 29 | + {"Is 1024 a power of 2? - YES", 1024, true}, |
| 30 | + {"Is 0 a power of 2? - NO", 0, false}, |
| 31 | + {"Is 9223372036854775807 a power of 2? - NO", math.MaxInt64, false}, |
| 32 | + {"Is 9223372036854775806 a power of 2? - NO", math.MaxInt64, false}, |
| 33 | + {"Is 4611686018427387904 a power of 2? - YES", 4611686018427387904, true}, |
| 34 | + } |
| 35 | + return tests |
| 36 | +} |
| 37 | + |
| 38 | +func TestIsPowerOfTwo(t *testing.T) { |
| 39 | + tests := getTestsForPowerOfTwo() |
| 40 | + for _, tv := range tests { |
| 41 | + t.Run(tv.name, func(t *testing.T) { |
| 42 | + result := IsPowerOfTwo(tv.a) |
| 43 | + t.Log(tv.a, " ", result) |
| 44 | + if result != tv.missing { |
| 45 | + t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestIsPowerOfTwoLeftShift(t *testing.T) { |
| 52 | + tests := getTestsForPowerOfTwo() |
| 53 | + for _, tv := range tests { |
| 54 | + t.Run(tv.name, func(t *testing.T) { |
| 55 | + result := IsPowerOfTwoLeftShift(uint(tv.a)) |
| 56 | + t.Log(tv.a, " ", result) |
| 57 | + if result != tv.missing { |
| 58 | + t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestIsPowOfTwoUseLog(t *testing.T) { |
| 65 | + tests := getTestsForPowerOfTwo() |
| 66 | + for _, tv := range tests { |
| 67 | + t.Run(tv.name, func(t *testing.T) { |
| 68 | + result := math2.IsPowOfTwoUseLog(float64(tv.a)) |
| 69 | + t.Log(tv.a, " ", result) |
| 70 | + if result != tv.missing { |
| 71 | + t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func BenchmarkIsPowerOfTwoBinaryMethod(b *testing.B) { |
| 78 | + for i := 0; i < b.N; i++ { |
| 79 | + IsPowerOfTwo(1024) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func BenchmarkIsPowerOfTwoUseCycleAndLeftShift(b *testing.B) { |
| 84 | + for i := 0; i < b.N; i++ { |
| 85 | + IsPowerOfTwoLeftShift(1024) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func BenchmarkIsPowerOfTwoUseLog(b *testing.B) { |
| 90 | + for i := 0; i < b.N; i++ { |
| 91 | + math2.IsPowOfTwoUseLog(1024) |
| 92 | + } |
| 93 | +} |
0 commit comments