|
| 1 | +/* |
| 2 | +Copyright (c) 2016, Theodore Butler |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | +* Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + this list of conditions and the following disclaimer in the documentation |
| 13 | + and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | +*/ |
| 26 | + |
| 27 | +package bitarray |
| 28 | + |
| 29 | +import ( |
| 30 | + "testing" |
| 31 | + |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | +) |
| 34 | + |
| 35 | +func TestBitmap32_PopCount(t *testing.T) { |
| 36 | + b := []uint32{ |
| 37 | + uint32(0x55555555), // 0x55555555 = 01010101 01010101 01010101 01010101 |
| 38 | + uint32(0x33333333), // 0x33333333 = 00110011 00110011 00110011 00110011 |
| 39 | + uint32(0x0F0F0F0F), // 0x0F0F0F0F = 00001111 00001111 00001111 00001111 |
| 40 | + uint32(0x00FF00FF), // 0x00FF00FF = 00000000 11111111 00000000 11111111 |
| 41 | + uint32(0x0000FFFF), // 0x0000FFFF = 00000000 00000000 11111111 11111111 |
| 42 | + } |
| 43 | + for _, x := range b { |
| 44 | + assert.Equal(t, 16, Bitmap32(x).PopCount()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestBitmap64_PopCount(t *testing.T) { |
| 49 | + b := []uint64{ |
| 50 | + uint64(0x5555555555555555), |
| 51 | + uint64(0x3333333333333333), |
| 52 | + uint64(0x0F0F0F0F0F0F0F0F), |
| 53 | + uint64(0x00FF00FF00FF00FF), |
| 54 | + uint64(0x0000FFFF0000FFFF), |
| 55 | + } |
| 56 | + for _, x := range b { |
| 57 | + assert.Equal(t, 32, Bitmap64(x).PopCount()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestBitmap32_SetBit(t *testing.T) { |
| 62 | + m := Bitmap32(0) |
| 63 | + assert.Equal(t, Bitmap32(0x4), m.SetBit(2)) |
| 64 | +} |
| 65 | + |
| 66 | +func TestBitmap32_ClearBit(t *testing.T) { |
| 67 | + m := Bitmap32(0x4) |
| 68 | + assert.Equal(t, Bitmap32(0), m.ClearBit(2)) |
| 69 | +} |
| 70 | + |
| 71 | +func TestBitmap32_zGetBit(t *testing.T) { |
| 72 | + m := Bitmap32(0x55555555) |
| 73 | + assert.Equal(t, true, m.GetBit(2)) |
| 74 | +} |
| 75 | + |
| 76 | +func TestBitmap64_SetBit(t *testing.T) { |
| 77 | + m := Bitmap64(0) |
| 78 | + assert.Equal(t, Bitmap64(0x4), m.SetBit(2)) |
| 79 | +} |
| 80 | + |
| 81 | +func TestBitmap64_ClearBit(t *testing.T) { |
| 82 | + m := Bitmap64(0x4) |
| 83 | + assert.Equal(t, Bitmap64(0), m.ClearBit(2)) |
| 84 | +} |
| 85 | + |
| 86 | +func TestBitmap64_GetBit(t *testing.T) { |
| 87 | + m := Bitmap64(0x55555555) |
| 88 | + assert.Equal(t, true, m.GetBit(2)) |
| 89 | +} |
| 90 | + |
| 91 | +func BenchmarkBitmap32_PopCount(b *testing.B) { |
| 92 | + m := Bitmap32(0x33333333) |
| 93 | + b.ResetTimer() |
| 94 | + for i := b.N; i > 0; i-- { |
| 95 | + m.PopCount() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func BenchmarkBitmap64_PopCount(b *testing.B) { |
| 100 | + m := Bitmap64(0x3333333333333333) |
| 101 | + b.ResetTimer() |
| 102 | + for i := b.N; i > 0; i-- { |
| 103 | + m.PopCount() |
| 104 | + } |
| 105 | +} |
0 commit comments