def test_bit_count_random(self): for n in list(range(50)) + [randint(1000, 2000)]: a = urandom(n) b = urandom(n) self.assertEqual(count_and(a, b), (a & b).count()) self.assertEqual(count_or(a, b), (a | b).count()) self.assertEqual(count_xor(a, b), (a ^ b).count())
def test_bit_count1(self): a = bitarray('001111') aa = a.copy() b = bitarray('010011') bb = b.copy() self.assertEqual(count_and(a, b), 2) self.assertEqual(count_or(a, b), 5) self.assertEqual(count_xor(a, b), 3) for f in count_and, count_or, count_xor: # not two arguments self.assertRaises(TypeError, f) self.assertRaises(TypeError, f, a) self.assertRaises(TypeError, f, a, b, 3) # wrong argument types self.assertRaises(TypeError, f, a, '') self.assertRaises(TypeError, f, '1', b) self.assertRaises(TypeError, f, a, 4) self.assertEQUAL(a, aa) self.assertEQUAL(b, bb) b.append(1) for f in count_and, count_or, count_xor: self.assertRaises(ValueError, f, a, b) self.assertRaises(ValueError, f, bitarray('110', 'big'), bitarray('101', 'little'))
def test_count_byte(self): ones = bitarray(8) ones.setall(1) zeros = bitarray(8) zeros.setall(0) for i in range(0, 256): a = bitarray() a.frombytes(bytes(bytearray([i]))) cnt = a.count() self.assertEqual(count_and(a, zeros), 0) self.assertEqual(count_and(a, ones), cnt) self.assertEqual(count_and(a, a), cnt) self.assertEqual(count_or(a, zeros), cnt) self.assertEqual(count_or(a, ones), 8) self.assertEqual(count_or(a, a), cnt) self.assertEqual(count_xor(a, zeros), cnt) self.assertEqual(count_xor(a, ones), 8 - cnt) self.assertEqual(count_xor(a, a), 0)
def test_bit_count2(self): for n in list(range(50)) + [randint(1000, 2000)]: a = bitarray() a.frombytes(os.urandom(bits2bytes(n))) del a[n:] b = bitarray() b.frombytes(os.urandom(bits2bytes(n))) del b[n:] self.assertEqual(count_and(a, b), (a & b).count()) self.assertEqual(count_or(a, b), (a | b).count()) self.assertEqual(count_xor(a, b), (a ^ b).count())
def compare(A, B, n, printNums=False): from bitarray.util import count_xor diff_count = count_xor(A, B) if diff_count == 0: print("The lists are equal") return print("The lists differ on ", diff_count, " points") diff = A ^ B A_not_B = A & diff B_not_A = B & diff if A_not_B.any(): print("A has ", A_not_B.count(), " points which are not in B") if printNums: print(getNums(A_not_B, n)) print('') if B_not_A.any(): print("B has ", B_not_A.count(), " points which are not in A") if printNums: print(getNums(B_not_A, n)) print('') return
def CascadeLocalTest(testsNumber=100, keyLengthInBytes=100, testQber=0.05, iterationsNumber=4, testPing=0): correct_keys = 0 wrong_keys = 0 global parity_requests parity_requests = 0 errorsSum = 0 print("Processing tests", testsNumber) print("Key length in bits before reconciliation", keyLengthInBytes * 8) print("QBER", testQber) for i in range(testsNumber): global correctKey global correctKeyFull correctKeyFull = RandomBitArray(keyLengthInBytes) correctKey = correctKeyFull[correctKeyFull.length() // 2:] rawKey = AddNoise(testQber) start = time.time() siftedKey = Cascade('rawKey', iterationsNumber, None, testQber) errors = util.count_xor(siftedKey, correctKey) errorsSum += errors if errors: wrong_keys += 1 else: correct_keys += 1 finish = time.time() print("wrong", wrong_keys) print("correct", correct_keys) print("avg ber x10000 ", errorsSum / testsNumber * 10000 / keyLengthInBytes * 8) print("parity requests in average", round(float(parity_requests) / testsNumber, 1)) print( "total time in average", round( parity_requests / testsNumber * 2 * testPing * 0.001 + finish - start, 1))
def test_bit_count_frozen(self): a = frozenbitarray('001111') b = frozenbitarray('010011') self.assertEqual(count_and(a, b), 2) self.assertEqual(count_or(a, b), 5) self.assertEqual(count_xor(a, b), 3)
def equals(A, B): from bitarray.util import count_xor return not count_xor(A, B)
def score(self, other: 'Node') -> int: """ "Hamming distance" score where lower is better :param other: The node to compare against """ return count_xor(self.filter.filter, other.filter.filter)