From 683f4a90abc1ac0eef911ec8e2d5c6ce25cc6c25 Mon Sep 17 00:00:00 2001 From: "dn337t@gmail.com" Date: Wed, 20 Jun 2012 20:21:13 +0000 Subject: [PATCH] added simple test suite for tools/findbits.py --- tools/findbits_test.py | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tools/findbits_test.py diff --git a/tools/findbits_test.py b/tools/findbits_test.py new file mode 100644 index 00000000..0194813e --- /dev/null +++ b/tools/findbits_test.py @@ -0,0 +1,68 @@ +#!/usr/bin/python + +from itertools import imap +import unittest, sys, findbits + +class TestFindBits(unittest.TestCase): + def setUp(self): + self.old_stdout = sys.stdout + sys.stdout = OutputBuffer() + + def tearDown(self): + sys.stdout = self.old_stdout + + INVERT_CASES = [ + ('10', '01'), + ('', ''), + ] + def test_invert(self): + self.commutative_test(findbits.invert, self.INVERT_CASES) + + SEARCH_CASES = [ + ('1111', '10111101', ['Match at bit 2', '0<1111>0']), + ('00', '10111101', ['Not found']), + ] + def test_search(self): + for target, data, expected_fragments in self.SEARCH_CASES: + sys.stdout.clear_buffer() + findbits.search(target, data) + for fragment in expected_fragments: + self.assertIn(fragment, sys.stdout.content) + + BINSTRING_CASES = [ + (42, '101010'), + (1, '1'), + (0, ''), + ] + def test_binstring(self): + self.unary_operation_test(findbits.binstring, self.BINSTRING_CASES) + + REVERSE_CASES = [ + ('abc', 'cba'), + ('', ''), + ] + def test_stringreverse(self): + self.commutative_test(findbits.stringreverse, self.REVERSE_CASES) + + def commutative_test(self, operation, cases): + self.unary_operation_test(operation, cases) + self.unary_operation_test(operation, imap(reversed, cases)) + + def unary_operation_test(self, operation, cases): + for case_in, case_out in cases: + self.assertEqual(operation(case_in), case_out) + + +class OutputBuffer(object): + def __init__(self): + self.clear_buffer() + + def clear_buffer(self): + self.content = '' + + def write(self, data): + self.content += data + + +if __name__ == '__main__': + unittest.main() -- 2.39.2