From b123ba8a2787ac9a0cf5d87c2af2169e34038c26 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Tue, 29 Nov 2022 10:11:13 +0100 Subject: [PATCH 1/4] fix --- boolean/boolean.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/boolean/boolean.py b/boolean/boolean.py index eeffbc4..5515fb5 100644 --- a/boolean/boolean.py +++ b/boolean/boolean.py @@ -1491,7 +1491,12 @@ def absorb(self, args): i -= 1 continue else: - args[j] = b + if b in args: + del args[j] + if j < i: + i -= 1 + else: + args[j] = b j += 1 continue From 179d149025c1dea948aca50a5b0e68a96c3a71d5 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Tue, 29 Nov 2022 10:23:17 +0100 Subject: [PATCH 2/4] Add test --- boolean/test_boolean.py | 52 ++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/boolean/test_boolean.py b/boolean/test_boolean.py index f1c254f..7486fb7 100644 --- a/boolean/test_boolean.py +++ b/boolean/test_boolean.py @@ -10,26 +10,13 @@ import unittest from unittest.case import expectedFailure -from boolean import ( - TOKEN_AND, - TOKEN_FALSE, - TOKEN_LPAR, - TOKEN_NOT, - TOKEN_OR, - TOKEN_RPAR, - TOKEN_SYMBOL, - TOKEN_TRUE, - BooleanAlgebra, - ParseError, - Symbol, -) -from boolean.boolean import ( - PARSE_INVALID_EXPRESSION, - PARSE_INVALID_NESTING, - PARSE_INVALID_OPERATOR_SEQUENCE, - PARSE_INVALID_SYMBOL_SEQUENCE, - PARSE_UNKNOWN_TOKEN, -) +from boolean import (TOKEN_AND, TOKEN_FALSE, TOKEN_LPAR, TOKEN_NOT, TOKEN_OR, + TOKEN_RPAR, TOKEN_SYMBOL, TOKEN_TRUE, BooleanAlgebra, + ParseError, Symbol) +from boolean.boolean import (PARSE_INVALID_EXPRESSION, PARSE_INVALID_NESTING, + PARSE_INVALID_OPERATOR_SEQUENCE, + PARSE_INVALID_SYMBOL_SEQUENCE, + PARSE_UNKNOWN_TOKEN) class BooleanAlgebraTestCase(unittest.TestCase): @@ -760,6 +747,28 @@ def test_simplify(self): ) assert result.pretty() == expected.pretty() + def test_absorption_invariant_to_order(self): + algebra = BooleanAlgebra() + + a, b = algebra.symbols(*"ab") + + e = (~a | ~b) & b & ~a + args = [ + ~a | ~b, + ~a, + b, + ] + + result_original = e.absorb(args) + + args[1], args[2] = args[2], args[1] + result_swapped = e.absorb(args) + + assert len(result_original) == 2 + assert len(result_swapped) == 2 + assert result_original[0] == result_swapped[1] + assert result_original[1] == result_swapped[0] + @expectedFailure def test_parse_complex_expression_should_create_same_expression_as_python(self): algebra = BooleanAlgebra() @@ -1205,9 +1214,10 @@ def test_objects_return_set_of_unique_Symbol_objs(self): assert set(["a", "b", "c"]) == exp.objects def test_normalize_blowup(self): - from boolean import AND, NOT, OR from collections import defaultdict + from boolean import AND, NOT, OR + # Subclasses to count calls to simplify class CountingNot(NOT): def simplify(self): From b618782a0f21af10c0defdc36aadb9225ccfe885 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Tue, 29 Nov 2022 10:25:19 +0100 Subject: [PATCH 3/4] Revert black --- boolean/test_boolean.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/boolean/test_boolean.py b/boolean/test_boolean.py index 7486fb7..9c41d84 100644 --- a/boolean/test_boolean.py +++ b/boolean/test_boolean.py @@ -10,13 +10,26 @@ import unittest from unittest.case import expectedFailure -from boolean import (TOKEN_AND, TOKEN_FALSE, TOKEN_LPAR, TOKEN_NOT, TOKEN_OR, - TOKEN_RPAR, TOKEN_SYMBOL, TOKEN_TRUE, BooleanAlgebra, - ParseError, Symbol) -from boolean.boolean import (PARSE_INVALID_EXPRESSION, PARSE_INVALID_NESTING, - PARSE_INVALID_OPERATOR_SEQUENCE, - PARSE_INVALID_SYMBOL_SEQUENCE, - PARSE_UNKNOWN_TOKEN) +from boolean import ( + TOKEN_AND, + TOKEN_FALSE, + TOKEN_LPAR, + TOKEN_NOT, + TOKEN_OR, + TOKEN_RPAR, + TOKEN_SYMBOL, + TOKEN_TRUE, + BooleanAlgebra, + ParseError, + Symbol, +) +from boolean.boolean import ( + PARSE_INVALID_EXPRESSION, + PARSE_INVALID_NESTING, + PARSE_INVALID_OPERATOR_SEQUENCE, + PARSE_INVALID_SYMBOL_SEQUENCE, + PARSE_UNKNOWN_TOKEN, +) class BooleanAlgebraTestCase(unittest.TestCase): @@ -1214,9 +1227,8 @@ def test_objects_return_set_of_unique_Symbol_objs(self): assert set(["a", "b", "c"]) == exp.objects def test_normalize_blowup(self): - from collections import defaultdict - from boolean import AND, NOT, OR + from collections import defaultdict # Subclasses to count calls to simplify class CountingNot(NOT): From c30ca2c8784c11f56402d52cfc1d19e664541e9d Mon Sep 17 00:00:00 2001 From: Philippe Ombredanne Date: Fri, 15 Nov 2024 20:06:00 +0100 Subject: [PATCH 4/4] Update boolean/test_boolean.py Signed-off-by: Philippe Ombredanne Co-authored-by: Frank Dana --- boolean/test_boolean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean/test_boolean.py b/boolean/test_boolean.py index 9c41d84..93be548 100644 --- a/boolean/test_boolean.py +++ b/boolean/test_boolean.py @@ -763,7 +763,7 @@ def test_simplify(self): def test_absorption_invariant_to_order(self): algebra = BooleanAlgebra() - a, b = algebra.symbols(*"ab") + a, b = algebra.symbols("a", "b") e = (~a | ~b) & b & ~a args = [