def helper_test_truth_table_fill_raises(self, expr, expected_exc_type=None, **kwargs): """Helper for testing exception conditions when filling a table. :param expr: The value to pass to the ``TruthTable`` constructor. :type expr: BooleanExpression or str :param expected_exc_type: The exception type expected to be raised. :type expected_exc_type: Exception :param kwargs: Keyword args to pass to the ``TruthTable`` constructor. """ did_catch = False try: t = TruthTable(expr, fill_all=False) t.fill(**kwargs) except expected_exc_type as e: did_catch = True except Exception as e: traceback.print_exc() self.fail('Received exception of type ' + type(e).__name__ + ' but was expecting type ' + expected_exc_type.__name__ + '.') did_catch = True if not did_catch: self.fail('No exception thrown.')
def test_is_full_iterative(self): """Test is_full attr when table is iteratively filled.""" t = TruthTable('A nand B xor C', fill_all=False) self.assertFalse(t.is_full) t.fill(A=0) self.assertFalse(t.is_full) t.fill(B=1) self.assertFalse(t.is_full) t.fill(C=0) self.assertFalse(t.is_full) t.fill(B=0) self.assertTrue(t.is_full)
def test_other_is_not_full(self): """Test checking equivalence when the argument table is not full.""" full_table = TruthTable('A or B') partially_filled = TruthTable('A or B', fill_all=False) partially_filled.fill(B=1) with self.assertRaises(RequiresFullTableError): full_table.equivalent_to(partially_filled)
def test_generate_symbols_gt_26(self): """Test generating more 26 symbols (the first boundary).""" self.assertEqual(TruthTable.generate_symbols(27), [ 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA' ])
def test_table_iter_completely_filled(self): """Test iterating through a table that is completely full.""" t = TruthTable('B nand D') count = 0 for inputs, result in t: if count == 0: self.assertEqual(inputs.B, False) self.assertEqual(inputs.D, False) self.assertEqual(result, True) elif count == 1: self.assertEqual(inputs.B, False) self.assertEqual(inputs.D, True) self.assertEqual(result, True) elif count == 2: self.assertEqual(inputs.B, True) self.assertEqual(inputs.D, False) self.assertEqual(result, True) elif count == 3: self.assertEqual(inputs.B, True) self.assertEqual(inputs.D, True) self.assertEqual(result, False) count += 1 self.assertEqual(count, 4)
def test_table_iter_unfilled(self): """Test iterating through a table that has not been filled.""" t = TruthTable('A or B', fill_all=False) count = 0 for inputs, result in t: count += 1 self.assertEqual(count, 0)
def test_table_iter_partially_filled(self): """Test iterating through a table that is partially filled.""" t = TruthTable('A xor C', fill_all=False) t.fill(C=1) count = 0 for inputs, result in t: if count == 0: self.assertEqual(inputs.A, False) self.assertEqual(inputs.C, True) self.assertEqual(result, True) elif count == 1: self.assertEqual(inputs.A, True) self.assertEqual(inputs.C, True) self.assertEqual(result, False) count += 1 self.assertEqual(count, 2)
def test_caller_is_not_full(self): """Test checking equivalence when the caller is not full.""" partially_filled = TruthTable('A xor B', fill_all=False) partially_filled.fill(A=0) with self.assertRaises(RequiresFullTableError): partially_filled.equivalent_to('B xor A')
def test_equivalent_tables_with_dont_cares(self): """Test a case where two tables w/ don't cares are equivalent.""" t1 = TruthTable(from_values='0x10') t2 = TruthTable(from_values='0x10') self.assertTrue(t1.equivalent_to(t2)) self.assertTrue(t2.equivalent_to(t1))
def test_unequivalent_tables_without_dont_cares(self): """Test a case where two tables w/o don't cares are not equivalent.""" t1 = TruthTable(from_values='0001') t2 = TruthTable(from_values='0000') self.assertFalse(t1.equivalent_to(t2)) self.assertFalse(t2.equivalent_to(t1))
def test_equivalent_to_table(self): """Test when the other source of truth is a TruthTable.""" t1 = TruthTable(from_values='0001') t2 = TruthTable('C and D') self.assertTrue(t1.equivalent_to(t2)) self.assertTrue(t2.equivalent_to(t1))
def test_attempt_to_fill_table_already_iteratively_filled(self): """Ensure that we cannot fill a table already iteratively filled.""" t = TruthTable('A nand B', fill_all=False) t.fill(A=0) t.fill(A=1) with self.assertRaises(AlreadyFullTableError): t.fill(A=0)
def test_unequally_sized_tables(self): """Test that unequally sized tables are not equivalent.""" t1 = TruthTable(from_values='0110') t2 = TruthTable(from_values='0110011001100110') self.assertFalse(t1.equivalent_to(t2)) self.assertFalse(t2.equivalent_to(t1))
def test_invalid_argument_type(self): """Test passing an invalid argument type to equivalent_to.""" t = TruthTable('A or B') with self.assertRaises(InvalidArgumentTypeError): t.equivalent_to(float()) with self.assertRaises(InvalidArgumentTypeError): t.equivalent_to(None)
def test_input_combos_multiple_repeats(self): """Test getting input combos for more than one repeats.""" self.assertEqual(list(TruthTable.input_combos(2)), [( False, False, ), ( False, True, ), ( True, False, ), ( True, True, )])
def helper_test_truth_table_fill(self, expr, expected_table_str=None, init_kwargs={}, **kwargs): """Helper to test filling a truth table. :param expr: The value to pass to the ``TruthTable`` constructor. :type expr: BooleanExpression or str :param expected_table_str: The expected string representation of the table. :type expected_table_str: str :param init_kwargs: A dict to pass as the kwargs to the ``TruthTable`` constructor. :type init_kwargs: Dict :param kwargs: Keyword args to pass to the fill method. """ t = TruthTable(expr, fill_all=False, **init_kwargs) t.fill(**kwargs) self.assertEqual(expected_table_str, str(t))
def helper_test_truth_table(self, expr, expected_table_str=None, **kwargs): """Helper to test the creation of truth tables. This helper will fill up a table completely and compare its ``__str__`` representation with the passed expected string. :param expr: The value to pass to the ``TruthTable`` constructor. :type expr: BooleanExpression or str :param expected_table_str: The expected string representation of the table. :type expected_table_str: str :param kwargs: Keyword args to pass to the ``TruthTable`` constructor. """ t = TruthTable(expr, **kwargs) self.assertEqual(expected_table_str, str(t))
def test_table_getitem(self): """Test indexing the table to get its results.""" t = TruthTable('A or B', fill_all=False) for i in range(4): self.assertEqual(None, t[i]) t.fill(A=0) self.assertEqual(t[0], False) self.assertEqual(t[1], True) t.fill() self.assertEqual(t[0b00], False) self.assertEqual(t[0b01], True) self.assertEqual(t[0b10], True) self.assertEqual(t[0b11], True) t = TruthTable(from_values='1xx1') self.assertEqual(t[0b00], True) self.assertEqual(t[0b01], 'x') self.assertEqual(t[0b10], 'x') self.assertEqual(t[0b11], True)
def test_generate_symbols_lt_26(self): """Test generating less than 26 symbols.""" self.assertEqual(TruthTable.generate_symbols(5), ['A', 'B', 'C', 'D', 'E'])
def test_is_full_from_expr_in_init(self): """Test is_full attr when filled from an expr in __init__.""" t = TruthTable('A or B') self.assertTrue(t.is_full)
def test_unequivalent_other_contains_dont_cares(self): """Test an unequivalent case where the other has don't cares.""" t1 = TruthTable(from_values='0101') t2 = TruthTable(from_values='1xxx') self.assertFalse(t1.equivalent_to(t2))
def test_is_full_from_single_fill_call(self): """Test is_full attr when filled via one fill() call.""" t = TruthTable('A nand B xor C', fill_all=False) self.assertFalse(t.is_full) t.fill() self.assertTrue(t.is_full)
def test_is_full_from_values_in_init(self): """Test is_full attr when filled via from_values in __init__.""" sixteen_dont_cares = 'x' * 16 t = TruthTable(from_values=sixteen_dont_cares) self.assertTrue(t.is_full)
def _table(opts): """Run the ``table`` command.""" t = TruthTable(opts.expression) print_info(t)
def test_generate_symbols_0(self): """Test generating 0 symbols.""" self.assertEqual(TruthTable.generate_symbols(0), [])
def test_attempt_to_fill_table_already_filled_from_values(self): """Ensure that we cannot fill() a table built from specified values.""" t = TruthTable(from_values='xxxx', ordering=['A', 'B']) with self.assertRaises(AlreadyFullTableError): t.fill(A=1)
def test_input_combos_one_repeat(self): """Test getting input combos for 1 repeat.""" self.assertEqual(list(TruthTable.input_combos(1)), [(False, ), (True, )])
def test_input_combos_empty(self): """Test getting an empty set of input_combos.""" self.assertEqual(list(TruthTable.input_combos(0)), [()])
def test_generate_symbols_eq_26(self): """Test generating exactly 26 symbols.""" self.assertEqual(TruthTable.generate_symbols(26), [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ])
def test_attempt_to_fill_table_already_filled_on_init_from_expr(self): """Ensure that we cannot fill a table already filled from an expr.""" t = TruthTable('A or B') with self.assertRaises(AlreadyFullTableError): t.fill(A=0)