5
5
6
6
import timeit
7
7
import unittest
8
+ from copy import deepcopy
8
9
from typing import Any , Dict , List
9
10
10
11
from constrainedrandom import RandObj , RandomizationError
@@ -72,7 +73,7 @@ def tmp_check(self, results) -> None:
72
73
'''
73
74
pass
74
75
75
- def randomize_and_time (self , randobj , iterations , tmp_constraints = None , tmp_values = None ) -> Dict [str , Any ]:
76
+ def randomize_and_time (self , randobj , iterations , tmp_constraints = None , tmp_values = None ) -> List [ Dict [str , Any ] ]:
76
77
'''
77
78
Call randobj.randomize() iterations times, time it, print performance stats,
78
79
return the results.
@@ -93,7 +94,6 @@ def randomize_and_time(self, randobj, iterations, tmp_constraints=None, tmp_valu
93
94
print (f'{ self .get_full_test_name ()} took { time_taken :.4g} s for { iterations } iterations ({ hz :.1f} Hz)' )
94
95
return results
95
96
96
-
97
97
def test_randobj (self ) -> None :
98
98
'''
99
99
Reusable test function to randomize a RandObj for a number of iterations and perform checks.
@@ -109,6 +109,8 @@ def test_randobj(self) -> None:
109
109
110
110
# Test with seed 0
111
111
randobj = self .get_randobj (0 )
112
+ # Take a copy of the randobj for use later
113
+ randobj_copy = deepcopy (randobj )
112
114
if self .EXPECT_FAILURE :
113
115
self .assertRaises (RandomizationError , randobj .randomize )
114
116
else :
@@ -128,41 +130,43 @@ def test_randobj(self) -> None:
128
130
add_results = self .randomize_and_time (randobj , self .iterations , tmp_values = tmp_values )
129
131
self .tmp_check (add_results )
130
132
131
- # Test again with seed 0, ensuring results are the same
133
+ # Test again with seed 0, ensuring results are the same.
134
+ # Also test the copy we took earlier.
132
135
randobj0 = self .get_randobj (0 )
133
- if self .EXPECT_FAILURE :
134
- self .assertRaises (RandomizationError , randobj0 .randomize )
135
- else :
136
- results0 = self .randomize_and_time (randobj0 , self .iterations )
137
- assertListOfDictsEqual (self , results , results0 , "Non-determinism detected, results were not equal" )
138
- if do_tmp_checks :
139
- # Check applying temporary constraints is also deterministic
140
- tmp_results0 = self .randomize_and_time (randobj0 , self .iterations , tmp_constraints , tmp_values )
141
- assertListOfDictsEqual (
142
- self ,
143
- tmp_results ,
144
- tmp_results0 ,
145
- "Non-determinism detected, results were not equal with temp constraints"
146
- )
147
- # Check temporary constraints don't break base randomization determinism
148
- post_tmp_results0 = self .randomize_and_time (randobj0 , self .iterations )
149
- assertListOfDictsEqual (
150
- self ,
151
- post_tmp_results ,
152
- post_tmp_results0 ,
153
- "Non-determinism detected, results were not equal after temp constraints"
154
- )
155
- # Add temporary constraints permanently, see what happens
156
- if tmp_constraints is not None :
157
- for constr , vars in tmp_constraints :
158
- randobj0 .add_constraint (constr , vars )
159
- add_results0 = self .randomize_and_time (randobj0 , self .iterations , tmp_values = tmp_values )
136
+ for tmp_randobj in [randobj0 , randobj_copy ]:
137
+ if self .EXPECT_FAILURE :
138
+ self .assertRaises (RandomizationError , tmp_randobj .randomize )
139
+ else :
140
+ results0 = self .randomize_and_time (tmp_randobj , self .iterations )
141
+ assertListOfDictsEqual (self , results , results0 , "Non-determinism detected, results were not equal" )
142
+ if do_tmp_checks :
143
+ # Check applying temporary constraints is also deterministic
144
+ tmp_results0 = self .randomize_and_time (tmp_randobj , self .iterations , tmp_constraints , tmp_values )
145
+ assertListOfDictsEqual (
146
+ self ,
147
+ tmp_results ,
148
+ tmp_results0 ,
149
+ "Non-determinism detected, results were not equal with temp constraints"
150
+ )
151
+ # Check temporary constraints don't break base randomization determinism
152
+ post_tmp_results0 = self .randomize_and_time (tmp_randobj , self .iterations )
160
153
assertListOfDictsEqual (
161
154
self ,
162
- add_results ,
163
- add_results0 ,
164
- "Non-determinism detected, results were not equal after constraints added "
155
+ post_tmp_results ,
156
+ post_tmp_results0 ,
157
+ "Non-determinism detected, results were not equal after temp constraints "
165
158
)
159
+ # Add temporary constraints permanently, see what happens
160
+ if tmp_constraints is not None :
161
+ for constr , vars in tmp_constraints :
162
+ tmp_randobj .add_constraint (constr , vars )
163
+ add_results0 = self .randomize_and_time (tmp_randobj , self .iterations , tmp_values = tmp_values )
164
+ assertListOfDictsEqual (
165
+ self ,
166
+ add_results ,
167
+ add_results0 ,
168
+ "Non-determinism detected, results were not equal after constraints added"
169
+ )
166
170
167
171
# Test with seed 1, ensuring results are different
168
172
randobj1 = self .get_randobj (1 )
0 commit comments