-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_test.cpp
More file actions
149 lines (128 loc) · 3.69 KB
/
stress_test.cpp
File metadata and controls
149 lines (128 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 1. DEEP RECURSION
// Pushes the call stack near the default limit of 256.
int recursive_sum(int n) {
if (n <= 1) {
return 1;
}
return n + recursive_sum(n - 1);
}
// Helper for Cellular Automaton
bool xor(bool a, bool b) {
return (a && !b) || (!a && b);
}
int main() {
// TEST 1: Call Stack & Recursion
int sum = recursive_sum(250);
// sum should be 250 * 251 / 2 = 31375
if (sum != 31375) {
return 1; // Fail
}
// TEST 2: Sieve of Eratosthenes (Array & Loop Stress)
// Arrays must be local and cannot be initialized with lists.
bool is_prime[100];
for (int i = 0; i < 100; i += 1) {
is_prime[i] = true;
}
is_prime[0] = false;
is_prime[1] = false;
for (int p = 2; p * p < 100; p += 1) {
if (is_prime[p]) {
for (int i = p * p; i < 100; i += p) {
is_prime[i] = false;
}
}
}
int prime_count = 0;
for (int i = 0; i < 100; i += 1) {
if (is_prime[i]) {
prime_count += 1;
}
}
// There are exactly 25 primes under 100.
if (prime_count != 25) {
return 2; // Fail
}
// TEST 3: Rule 30 Cellular Automaton (Logic & State Swapping)
bool state[31];
bool next_state[31];
for (int i = 0; i < 31; i += 1) {
state[i] = false;
next_state[i] = false;
}
state[15] = true; // Center cell
for (int gen = 0; gen < 15; gen += 1) {
for (int i = 1; i < 30; i += 1) {
bool left = state[i - 1];
bool center = state[i];
bool right = state[i + 1];
// Rule 30: left XOR (center OR right)
next_state[i] = xor(left, center || right);
}
for (int i = 1; i < 30; i += 1) {
state[i] = next_state[i];
}
}
// TEST 4: Complex LValues & Short-Circuiting
int complex_arr[10];
for (int i = 0; i < 10; i += 1) {
complex_arr[i] = i;
}
int idx = 2;
// Evaluates left-to-right:
// 1. idx becomes 3.
// 2. complex_arr[4] becomes 4 * 2 = 8.
// 3. complex_arr[3] becomes 3 + 8 = 11.
(((complex_arr[((idx += 1))])) += (((complex_arr[idx + 1]) *= 2)));
if (complex_arr[3] != 11 || complex_arr[4] != 8 || idx != 3) {
return 3; // Fail
}
// Short-circuiting test: out-of-bounds array access should NOT trigger a runtime error
bool safe = true;
if (safe || (complex_arr[999] == 0)) {
safe = true;
} else {
return 4; // Fail
}
if (!safe && (complex_arr[-1] == 0)) {
return 5; // Fail
}
// TEST 5: Scope Shadowing & Loop Control
int shadow = 100;
int loop_executions = 0;
{
bool shadow = true; // Shadows outer 'int shadow'
if (shadow) {
int shadow = 42; // Shadows inner 'bool shadow'
for (int i = 0; i < 10; i += 1) {
if (i % 2 == 0) {
continue;
}
if (i == 7) {
break;
}
shadow += i; // Adds 1, 3, 5 -> 42 + 9 = 51
loop_executions += 1;
}
if (shadow != 51) {
return 6; // Fail
}
}
}
if (shadow != 100 || loop_executions != 3) {
return 7; // Fail
}
// TEST 6: Heavy Arithmetic & Cascading Assignments
int a = 0;
int b = 0;
int c = 0;
// Right-associative assignment
a = b = c = 5;
// Complex precedence: a = 5 + (5 * 5) - (5 / 5) % 5 = 5 + 25 - 1 = 29
a = b + c * a - b / c % a;
if (a != 29) {
return 8; // Fail
}
// If we made it here, the interpreter survived the gauntlet!
print(1337);
return 0;
}