Skip to content

Commit 5f93c8b

Browse files
committed
Make test independent of CPU speed
Reference: #105 (comment) Reported-by: @prodhype Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 1305cbf commit 5f93c8b

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

boolean/test_boolean.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
SPDX-License-Identifier: BSD-2-Clause
88
"""
99

10-
import time
1110
import unittest
1211
from unittest.case import expectedFailure
1312

@@ -140,7 +139,6 @@ def tokenize(self, s):
140139

141140
def test_parse_with_advanced_tokenizer_example(self):
142141
import tokenize
143-
144142
from io import StringIO
145143

146144
class PlainVar(Symbol):
@@ -1207,6 +1205,27 @@ def test_objects_return_set_of_unique_Symbol_objs(self):
12071205
assert set(["a", "b", "c"]) == exp.objects
12081206

12091207
def test_normalize_blowup(self):
1208+
from boolean import AND, NOT, OR
1209+
from collections import defaultdict
1210+
1211+
# Subclasses to count calls to simplify
1212+
class CountingNot(NOT):
1213+
def simplify(self):
1214+
counts["CountingNot"] += 1
1215+
return super().simplify()
1216+
1217+
class CountingAnd(AND):
1218+
def simplify(self, sort=True):
1219+
counts["CountingAnd"] += 1
1220+
return super().simplify(sort=sort)
1221+
1222+
class CountingOr(OR):
1223+
def simplify(self, sort=True):
1224+
counts["CountingOr"] += 1
1225+
return super().simplify(sort=sort)
1226+
1227+
counts = defaultdict(int)
1228+
12101229
# Real-world example of a complex expression with simple CNF/DNF form.
12111230
# Note this is a more reduced, milder version of the problem, for rapid
12121231
# testing.
@@ -1221,16 +1240,18 @@ def test_normalize_blowup(self):
12211240
| (c & f & g & ~t & ~(b & c & d & e & f & g))
12221241
)
12231242
"""
1224-
algebra = BooleanAlgebra()
1243+
algebra = BooleanAlgebra(
1244+
NOT_class=CountingNot,
1245+
AND_class=CountingAnd,
1246+
OR_class=CountingOr,
1247+
)
1248+
12251249
expr = algebra.parse(formula)
1226-
t0 = time.time()
12271250
cnf = algebra.cnf(expr)
1228-
t1 = time.time()
1229-
12301251
assert str(cnf) == "a&c&f&g"
1231-
# Locally, this test takes 0.4s, previously it was 500s.
1232-
# We allow 30s because of the wide range of possible CPUs.
1233-
assert t1 - t0 < 30, "Normalizing took too long"
1252+
# We should get exactly this count of calls.
1253+
# before we had a combinatorial explosion
1254+
assert counts == {"CountingAnd": 44, "CountingNot": 193, "CountingOr": 2490}
12341255

12351256

12361257
class BooleanBoolTestCase(unittest.TestCase):

0 commit comments

Comments
 (0)