3 import unittest
, sys
, findbits
5 class TestFindBits(unittest
.TestCase
):
7 self
.old_stdout
= sys
.stdout
8 sys
.stdout
= OutputBuffer()
11 sys
.stdout
= self
.old_stdout
17 def test_invert(self
):
18 self
.commutative_test(findbits
.invert
, self
.INVERT_CASES
)
21 ('1111', '10111101', ['Match at bit 2', '0<1111>0']),
22 ('00', '10111101', ['Not found']),
24 def test_search(self
):
25 for target
, data
, expected_fragments
in self
.SEARCH_CASES
:
26 sys
.stdout
.clear_buffer()
27 findbits
.search(target
, data
)
28 for fragment
in expected_fragments
:
29 self
.assertIn(fragment
, sys
.stdout
.content
)
36 def test_binstring(self
):
37 self
.unary_operation_test(findbits
.binstring
, self
.BINSTRING_CASES
)
43 def test_stringreverse(self
):
44 self
.commutative_test(findbits
.stringreverse
, self
.REVERSE_CASES
)
46 def commutative_test(self
, operation
, cases
):
47 self
.unary_operation_test(operation
, cases
)
48 self
.unary_operation_test(operation
, map(reversed, cases
))
50 def unary_operation_test(self
, operation
, cases
):
51 for case_in
, case_out
in cases
:
52 self
.assertEqual(operation(case_in
), case_out
)
55 class OutputBuffer(object):
59 def clear_buffer(self
):
62 def write(self
, data
):
66 if __name__
== '__main__':