7
7
SPDX-License-Identifier: BSD-2-Clause
8
8
"""
9
9
10
- import time
11
10
import unittest
12
11
from unittest .case import expectedFailure
13
12
@@ -140,7 +139,6 @@ def tokenize(self, s):
140
139
141
140
def test_parse_with_advanced_tokenizer_example (self ):
142
141
import tokenize
143
-
144
142
from io import StringIO
145
143
146
144
class PlainVar (Symbol ):
@@ -1207,6 +1205,27 @@ def test_objects_return_set_of_unique_Symbol_objs(self):
1207
1205
assert set (["a" , "b" , "c" ]) == exp .objects
1208
1206
1209
1207
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
+
1210
1229
# Real-world example of a complex expression with simple CNF/DNF form.
1211
1230
# Note this is a more reduced, milder version of the problem, for rapid
1212
1231
# testing.
@@ -1221,16 +1240,18 @@ def test_normalize_blowup(self):
1221
1240
| (c & f & g & ~t & ~(b & c & d & e & f & g))
1222
1241
)
1223
1242
"""
1224
- algebra = BooleanAlgebra ()
1243
+ algebra = BooleanAlgebra (
1244
+ NOT_class = CountingNot ,
1245
+ AND_class = CountingAnd ,
1246
+ OR_class = CountingOr ,
1247
+ )
1248
+
1225
1249
expr = algebra .parse (formula )
1226
- t0 = time .time ()
1227
1250
cnf = algebra .cnf (expr )
1228
- t1 = time .time ()
1229
-
1230
1251
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 }
1234
1255
1235
1256
1236
1257
class BooleanBoolTestCase (unittest .TestCase ):
0 commit comments