github.com/tunabay/go-bitarray@v1.3.1/bitarray_shift_test.go (about) 1 // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved. 2 // Use of this source code is governed by the MIT license that can be found in 3 // the LICENSE file. 4 5 package bitarray_test 6 7 import ( 8 "math/rand" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/tunabay/go-bitarray" 14 ) 15 16 func TestBitArray_Reverse_rand(t *testing.T) { 17 const testIterations = 30000 18 rand.Seed(time.Now().UnixNano()) 19 for i := 0; i < testIterations; i++ { 20 var nBits int 21 switch rand.Intn(5) { 22 case 0: 23 nBits = rand.Intn(25) 24 case 1: 25 nBits = 8*(1+rand.Intn(8)) - 1 + rand.Intn(3) 26 default: 27 nBits = rand.Intn(512) 28 } 29 sb0, sbR := make([]byte, nBits), make([]byte, nBits) 30 for j := 0; j < nBits; j++ { 31 b := '0' + byte(rand.Intn(2)) 32 sb0[j], sbR[nBits-1-j] = b, b 33 } 34 ba0 := bitarray.MustParse(string(sb0)) 35 exp := bitarray.MustParse(string(sbR)) 36 37 baR := ba0.Reverse() 38 baR.V() 39 baE, baO := ba0.ZExpand(), ba0.ZOptimize() 40 baER, baOR := baE.Reverse(), baO.Reverse() 41 baRE, baRO := baR.ZExpand(), baR.ZOptimize() 42 43 switch { 44 case !baR.Equal(exp): 45 t.Error("unexpected Reverse result:") 46 case !baER.Equal(baR), !baOR.Equal(baR): 47 t.Error("unexpected Reverse result(2):") 48 case !baRE.Equal(baR), !baRO.Equal(baR): 49 t.Error("unexpected Reverse result(3):") 50 } 51 if t.Failed() { 52 t.Logf(" src: %#b", ba0) 53 t.Logf(" got: %#b", baR) 54 t.Logf("want: %#b", exp) 55 t.FailNow() 56 } 57 // if i < 32 { 58 // t.Logf(" src: %#b", ba0) 59 // t.Logf("pass: %#b", baR) 60 // t.Logf("pass: %s", baR.D()) 61 // } 62 } 63 } 64 65 func TestBitArray_ShiftLeft_rand(t *testing.T) { 66 const testIterations = 16000 67 rand.Seed(time.Now().UnixNano()) 68 for i := 0; i < testIterations; i++ { 69 var nBits int 70 switch rand.Intn(5) { 71 case 0: 72 nBits = rand.Intn(25) 73 case 1: 74 nBits = 8*(1+rand.Intn(8)) - 1 + rand.Intn(3) 75 default: 76 nBits = rand.Intn(512) 77 } 78 var sbb strings.Builder 79 sbb.Grow(nBits) 80 for j := 0; j < nBits; j++ { 81 sbb.WriteByte('0' + byte(rand.Intn(2))) 82 } 83 sb0 := sbb.String() 84 ba0 := bitarray.MustParse(sb0) 85 86 nShift := 4 87 if 16 < nBits { 88 nShift = 8 89 } 90 for j := 0; j < nShift; j++ { 91 var leftK int 92 switch rand.Intn(40) { 93 case 0: 94 leftK = nBits + rand.Intn(nBits+10) 95 case 1: 96 leftK = -nBits - rand.Intn(nBits+10) 97 default: 98 leftK = -nBits + rand.Intn(nBits*2+1) 99 } 100 101 expS := sb0 102 switch { 103 case nBits < leftK, leftK < -nBits: 104 expS = strings.Repeat("0", nBits) 105 case 0 < leftK: 106 expS = sb0[leftK:] + strings.Repeat("0", leftK) 107 case leftK < 0: 108 expS = strings.Repeat("0", -leftK) + sb0[:nBits+leftK] 109 } 110 expB := bitarray.MustParse(expS) 111 expBE, expBO := expB.ZExpand(), expB.ZOptimize() 112 113 gotB := ba0.ShiftLeft(leftK) 114 gotBE := ba0.ZExpand().ShiftLeft(leftK) 115 gotBO := ba0.ZOptimize().ShiftLeft(leftK) 116 gotB.V() 117 gotBE.V() 118 gotBO.V() 119 120 switch { 121 case !gotB.Equal(expB), !gotB.Equal(expBE), !gotB.Equal(expBO): 122 t.Errorf("unexpected result: leftK=%d", leftK) 123 t.Logf(" src: %s", ba0.D()) 124 case !gotBE.Equal(expB): 125 t.Errorf("unexpected result (e): leftK=%d", leftK) 126 t.Logf(" src: %s", ba0.ZExpand().D()) 127 case !gotBO.Equal(expB): 128 t.Errorf("unexpected result (o): leftK=%d", leftK) 129 t.Logf(" src: %s", ba0.ZOptimize().D()) 130 } 131 if t.Failed() { 132 t.Logf(" src: %#b", ba0) 133 t.Logf(" got: %#b", gotB) 134 t.Logf(" got: %s", gotB.D()) 135 t.Logf("want: %#b", expB) 136 t.FailNow() 137 } 138 // if i < 8 { 139 // t.Logf("pass: leftK=%d", leftK) 140 // t.Logf(" src: %#b", ba0) 141 // t.Logf(" got: %#b", gotB) 142 // t.Logf(" got: %s", gotB.D()) 143 // } 144 } 145 } 146 } 147 148 func TestBitArray_RotateLeft_rand(t *testing.T) { 149 const testIterations = 16000 150 rand.Seed(time.Now().UnixNano()) 151 for i := 0; i < testIterations; i++ { 152 var nBits int 153 switch rand.Intn(5) { 154 case 0: 155 nBits = rand.Intn(25) 156 case 1: 157 nBits = 8*(1+rand.Intn(8)) - 1 + rand.Intn(3) 158 default: 159 nBits = rand.Intn(512) 160 } 161 var sbb strings.Builder 162 sbb.Grow(nBits) 163 for j := 0; j < nBits; j++ { 164 sbb.WriteByte('0' + byte(rand.Intn(2))) 165 } 166 sb0 := sbb.String() 167 ba0 := bitarray.MustParse(sb0) 168 169 nShift := 4 170 if 16 < nBits { 171 nShift = 8 172 } 173 for j := 0; j < nShift; j++ { 174 var leftK int 175 switch rand.Intn(40) { 176 case 0: 177 leftK = nBits + rand.Intn(nBits+10) 178 case 1: 179 leftK = -nBits - rand.Intn(nBits+10) 180 default: 181 leftK = -nBits + rand.Intn(nBits*2+1) 182 } 183 184 expS := sb0 185 switch { 186 case nBits == 0: 187 case 0 < leftK: 188 effLK := leftK % nBits 189 expS = sb0[effLK:] + sb0[0:effLK] 190 case leftK < 0: 191 effRK := (-leftK) % nBits 192 expS = sb0[nBits-effRK:] + sb0[:nBits-effRK] 193 } 194 expB := bitarray.MustParse(expS) 195 expBE, expBO := expB.ZExpand(), expB.ZOptimize() 196 197 gotB := ba0.RotateLeft(leftK) 198 gotBE := ba0.ZExpand().RotateLeft(leftK) 199 gotBO := ba0.ZOptimize().RotateLeft(leftK) 200 gotB.V() 201 gotBE.V() 202 gotBO.V() 203 204 switch { 205 case !gotB.Equal(expB), !gotB.Equal(expBE), !gotB.Equal(expBO): 206 t.Errorf("unexpected result: leftK=%d", leftK) 207 t.Logf(" src: %s", ba0.D()) 208 case !gotBE.Equal(expB): 209 t.Errorf("unexpected result (e): leftK=%d", leftK) 210 t.Logf(" src: %s", ba0.ZExpand().D()) 211 case !gotBO.Equal(expB): 212 t.Errorf("unexpected result (o): leftK=%d", leftK) 213 t.Logf(" src: %s", ba0.ZOptimize().D()) 214 } 215 if t.Failed() { 216 t.Logf(" src: %#b", ba0) 217 t.Logf(" got: %#b", gotB) 218 t.Logf(" got: %s", gotB.D()) 219 t.Logf("want: %#b", expB) 220 t.FailNow() 221 } 222 // if i < 8 { 223 // t.Logf("pass: leftK=%d", leftK) 224 // t.Logf(" src: %#b", ba0) 225 // t.Logf(" got: %#b", gotB) 226 // t.Logf(" got: %s", gotB.D()) 227 // } 228 } 229 } 230 }