@@ -27,10 +27,7 @@ def generate_exception_file(destructor_percent: int, depth: int) -> str:
2727#include <cstdint>
2828#include <string_view>
2929
30- // External functions
31- extern void start();
32- extern void end();
33- extern void log_start(std::string_view);
30+ #include <platform.hpp>
3431
3532// Global side effect to prevent optimization
3633std::int32_t volatile side_effect = 0;
@@ -84,10 +81,24 @@ class destructor_object {{
8481
8582 # Generate function implementations
8683 for i in range (depth , 0 , - 1 ):
87- # Determine if this function should have a destructor
88- has_destructor = i <= destructor_count
89- obj_type = "destructor_object" if has_destructor else "simple_object"
84+ # Calculate which functions should have destructors - evenly distributed
85+ function_index = depth - i # 0-based index for this function
86+
87+ # Calculate total destructor count and distribute evenly
88+ destructor_count = int ((destructor_percent / 100.0 ) * depth )
89+
90+ if destructor_count == 0 :
91+ has_destructor = False
92+ elif destructor_count >= depth :
93+ has_destructor = True
94+ else :
95+ # Even distribution using integer arithmetic
96+ current_position = function_index * destructor_count // depth
97+ next_position = (function_index + 1 ) * destructor_count // depth
98+ has_destructor = next_position > current_position
9099
100+ obj_type = "destructor_object" if has_destructor else "simple_object"
101+ obj_type = "destructor_object" if has_destructor else "simple_object"
91102 content += f'''[[gnu::noinline]]
92103int depth_{ i :02d} () {{
93104 { obj_type } obj(side_effect >> 8);
@@ -133,11 +144,38 @@ class destructor_object {{
133144 side_effect = 1; // Ensure we will throw
134145
135146 try {{
136- depth_{ depth :02d} ();
137- }} catch (const test_error& e) {{
147+ depth_70();
148+ }} catch (test_error const& e) {{
149+ end();
150+ }}
151+
152+ pause();
153+
154+ try {{
155+ depth_50();
156+ }} catch (test_error const& e) {{
157+ end();
158+ }}
159+
160+ pause();
161+
162+ try {{
163+ depth_30();
164+ }} catch (test_error const& e) {{
138165 end();
139166 }}
167+
168+ pause();
169+
170+ try {{
171+ depth_10();
172+ }} catch (test_error const& e) {{
173+ end();
174+ }}
175+
176+ pause();
140177}}
178+
141179'''
142180
143181 return content
@@ -154,10 +192,7 @@ def generate_result_file(destructor_percent: int, depth: int) -> str:
154192#include <expected>
155193#include <string_view>
156194
157- // External functions
158- extern void start();
159- extern void end();
160- extern void log_start(std::string_view);
195+ #include <platform.hpp>
161196
162197// Global side effect to prevent optimization
163198std::int32_t volatile side_effect = 0;
@@ -261,11 +296,23 @@ class destructor_object {{
261296 # Generate test runner
262297 content += f'''// Test runner
263298void run_test() {{
264- log_start("RESULT_ { destructor_percent } PCT_DEPTH { depth } ");
299+ log_start("RESULT- { destructor_percent } %-DEPTH { depth } ");
265300 side_effect = 1; // Ensure we will return error
266301
267- auto result = depth_{ depth :02d} ();
268- if (!result) {{
302+ auto result_70 = depth_70();
303+ if (!result_70) {{
304+ end();
305+ }}
306+ auto result_50 = depth_50();
307+ if (!result_50) {{
308+ end();
309+ }}
310+ auto result_30 = depth_30();
311+ if (!result_30) {{
312+ end();
313+ }}
314+ auto result_10 = depth_10();
315+ if (!result_10) {{
269316 end();
270317 }}
271318}}
@@ -303,15 +350,14 @@ def main():
303350 """Generate test files for all combinations."""
304351
305352 destructor_percentages = [0 , 25 , 50 , 75 , 100 ]
306- depths = [ 10 , 30 , 50 , 70 ]
353+ max_depth = 70
307354 output_dir = Path ("generated_tests" )
308355
309356 for destructor_pct in destructor_percentages :
310- for depth in depths :
311- generate_test_files (destructor_pct , depth , output_dir )
357+ generate_test_files (destructor_pct , max_depth , output_dir )
312358
313359 print (
314- f"\n Generated { len (destructor_percentages ) * len ( depths ) * 2 } test files in { output_dir } /" )
360+ f"\n Generated { len (destructor_percentages ) * 2 } test files in { output_dir } /" )
315361
316362
317363if __name__ == "__main__" :
0 commit comments