Skip to content

Commit

Permalink
fix for get_x_bits plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Oct 7, 2017
1 parent 31e0f03 commit 14e02f6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion probables/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ def get_x_bits(num, max_bits, num_bits, right_bits=True):
bits = bin(num).lstrip('0b')
bits = bits.zfill(max_bits)
if right_bits:
return int(bits[num_bits:], 2)
return int(bits[-num_bits:], 2)
return int(bits[:num_bits], 2)
71 changes: 71 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from __future__ import (unicode_literals, absolute_import, print_function)
import unittest
import os

from . utilities import (different_hash)
from probables.utilities import (is_hex_string, is_valid_file, get_x_bits)

class TestProbablesUtilities(unittest.TestCase):

def test_is_hex(self):
''' test the is valid hex function '''
self.assertTrue(is_hex_string('1234678909abcdef'))
self.assertTrue(is_hex_string('1234678909ABCDEF'))
self.assertFalse(is_hex_string('1234678909abcdfq'))
self.assertFalse(is_hex_string('1234678909ABCDEFQ'))

def test_is_valid_file(self):
''' test the is valid file function '''
self.assertFalse(is_valid_file(None))
self.assertFalse(is_valid_file('./file_doesnt_exist.txt'))
filename = './create_this_file.txt'
with open(filename, 'w'):
pass
self.assertTrue(is_valid_file(filename))
os.remove(filename)

def test_get_x_bits(self):
''' test the get x bits function '''
for i in range(8):
res = get_x_bits(i, 4, 2, True)
self.assertEqual(res, i % 4)
for i in range(8):
res = get_x_bits(i, 4, 2, False)
if i < 4:
self.assertEqual(res, 0)
else:
self.assertEqual(res, 1)

def test_get_x_bits_large(self):
''' test it on much larger numbers '''
res = different_hash('this is a test', 1)[0]
# 1010100101011011100100010101010011110000001010011010000101001011
t1 = get_x_bits(res, 64, 32, True)
t2 = get_x_bits(res, 64, 32, False)
self.assertEqual(4029260107, t1)
self.assertEqual(2841350484, t2)

t1 = get_x_bits(res, 64, 16, True)
t2 = get_x_bits(res, 64, 16, False)
self.assertEqual(41291, t1)
self.assertEqual(43355, t2)

t1 = get_x_bits(res, 64, 8, True)
t2 = get_x_bits(res, 64, 8, False)
self.assertEqual(75, t1)
self.assertEqual(169, t2)

t1 = get_x_bits(res, 64, 4, True)
t2 = get_x_bits(res, 64, 4, False)
self.assertEqual(11, t1)
self.assertEqual(10, t2)

t1 = get_x_bits(res, 64, 2, True)
t2 = get_x_bits(res, 64, 2, False)
self.assertEqual(3, t1)
self.assertEqual(2, t2)

t1 = get_x_bits(res, 64, 1, True)
t2 = get_x_bits(res, 64, 1, False)
self.assertEqual(1, t1)
self.assertEqual(1, t2)

0 comments on commit 14e02f6

Please sign in to comment.