-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_state_persistence.cpp
More file actions
262 lines (202 loc) · 8.52 KB
/
test_state_persistence.cpp
File metadata and controls
262 lines (202 loc) · 8.52 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <iostream>
#include <cassert>
// Test state persistence (AC: 6)
// Validate that both effect states are properly saved and restored
// Mock SD Settings for testing
template<typename T>
class MockSDSettings {
public:
enum class Result {
OK,
ERR_NO_SD_CARD,
ERR_FILE_ERROR,
ERR_MOUNT_FAILED
};
private:
T settings_;
bool initialized_ = false;
public:
Result Init(const T& defaults, const char* filename) {
settings_ = defaults;
initialized_ = true;
return Result::OK;
}
T& GetSettings() { return settings_; }
Result Save() {
if (!initialized_) return Result::ERR_FILE_ERROR;
// Simulate successful save
return Result::OK;
}
};
// Settings struct from main.cpp
struct Settings {
int version = 1;
bool is_overdrive_enabled = false;
bool is_filter_modulation_enabled = false;
bool operator!=(const Settings& other) const {
return version != other.version
|| is_overdrive_enabled != other.is_overdrive_enabled
|| is_filter_modulation_enabled != other.is_filter_modulation_enabled;
}
};
namespace Test_State_Persistence {
void test_settings_structure() {
std::cout << "Testing Settings structure..." << std::endl;
Settings settings;
// Verify default values
assert(settings.version == 1);
assert(settings.is_overdrive_enabled == false);
assert(settings.is_filter_modulation_enabled == false);
// Test modification
settings.is_overdrive_enabled = true;
settings.is_filter_modulation_enabled = true;
assert(settings.is_overdrive_enabled == true);
assert(settings.is_filter_modulation_enabled == true);
std::cout << "✅ Settings structure: PASSED" << std::endl;
}
void test_settings_comparison() {
std::cout << "Testing Settings comparison..." << std::endl;
Settings s1, s2;
// Initially equal
assert(!(s1 != s2));
// Change overdrive state
s1.is_overdrive_enabled = true;
assert(s1 != s2);
// Make equal again
s2.is_overdrive_enabled = true;
assert(!(s1 != s2));
// Change modulation state
s1.is_filter_modulation_enabled = true;
assert(s1 != s2);
std::cout << "✅ Settings comparison: PASSED" << std::endl;
}
void test_state_save_scenarios() {
std::cout << "Testing state save scenarios..." << std::endl;
MockSDSettings<Settings> storage;
Settings defaults = {1, false, false};
// Initialize storage
auto result = storage.Init(defaults, "test_settings.bin");
assert(result == MockSDSettings<Settings>::Result::OK);
// Test scenario 1: Save overdrive only
Settings& settings = storage.GetSettings();
settings.is_overdrive_enabled = true;
settings.is_filter_modulation_enabled = false;
result = storage.Save();
assert(result == MockSDSettings<Settings>::Result::OK);
assert(settings.is_overdrive_enabled == true);
assert(settings.is_filter_modulation_enabled == false);
// Test scenario 2: Save modulation only
settings.is_overdrive_enabled = false;
settings.is_filter_modulation_enabled = true;
result = storage.Save();
assert(result == MockSDSettings<Settings>::Result::OK);
assert(settings.is_overdrive_enabled == false);
assert(settings.is_filter_modulation_enabled == true);
// Test scenario 3: Save both enabled
settings.is_overdrive_enabled = true;
settings.is_filter_modulation_enabled = true;
result = storage.Save();
assert(result == MockSDSettings<Settings>::Result::OK);
assert(settings.is_overdrive_enabled == true);
assert(settings.is_filter_modulation_enabled == true);
// Test scenario 4: Save both disabled
settings.is_overdrive_enabled = false;
settings.is_filter_modulation_enabled = false;
result = storage.Save();
assert(result == MockSDSettings<Settings>::Result::OK);
assert(settings.is_overdrive_enabled == false);
assert(settings.is_filter_modulation_enabled == false);
std::cout << "✅ State save scenarios: PASSED" << std::endl;
}
void test_boot_recovery_simulation() {
std::cout << "Testing boot recovery simulation..." << std::endl;
// Simulate various saved states and recovery
struct RecoveryTest {
bool saved_overdrive;
bool saved_modulation;
};
RecoveryTest tests[] = {
{false, false},
{true, false},
{false, true},
{true, true}
};
for (auto& test : tests) {
// Simulate saved settings
Settings saved_settings = {1, test.saved_overdrive, test.saved_modulation};
// Simulate runtime variables (would be set during boot)
bool enable_overdrive = saved_settings.is_overdrive_enabled;
bool filterModulationEnabled = saved_settings.is_filter_modulation_enabled;
// Verify recovery
assert(enable_overdrive == test.saved_overdrive);
assert(filterModulationEnabled == test.saved_modulation);
std::cout << "Recovery test - Overdrive: " << test.saved_overdrive
<< ", Modulation: " << test.saved_modulation << " ✅" << std::endl;
}
std::cout << "✅ Boot recovery simulation: PASSED" << std::endl;
}
void test_version_handling() {
std::cout << "Testing version handling..." << std::endl;
const int SETTINGS_VERSION = 1;
// Test matching version (normal case)
Settings loaded_settings = {SETTINGS_VERSION, true, true};
if (loaded_settings.version == SETTINGS_VERSION) {
// Settings should be used as-is
assert(loaded_settings.is_overdrive_enabled == true);
assert(loaded_settings.is_filter_modulation_enabled == true);
}
// Test version mismatch (fallback to defaults)
Settings old_settings = {0, true, true}; // Old version
bool use_defaults = false;
if (old_settings.version != SETTINGS_VERSION) {
// Should fall back to safe defaults
use_defaults = true;
}
assert(use_defaults == true);
std::cout << "✅ Version handling: PASSED" << std::endl;
}
void test_power_cycle_combinations() {
std::cout << "Testing power cycle state combinations..." << std::endl;
// Test matrix of all possible state combinations across power cycles
bool overdrive_states[] = {false, true};
bool modulation_states[] = {false, true};
for (bool od_state : overdrive_states) {
for (bool mod_state : modulation_states) {
// Simulate: Set states -> Save -> Power cycle -> Restore
// 1. Set states
Settings settings = {1, od_state, mod_state};
// 2. Save (simulated)
MockSDSettings<Settings> storage;
storage.Init({1, false, false}, "test.bin");
Settings& stored = storage.GetSettings();
stored.is_overdrive_enabled = od_state;
stored.is_filter_modulation_enabled = mod_state;
auto save_result = storage.Save();
assert(save_result == MockSDSettings<Settings>::Result::OK);
// 3. Power cycle simulation - restore from saved
bool recovered_overdrive = stored.is_overdrive_enabled;
bool recovered_modulation = stored.is_filter_modulation_enabled;
// 4. Verify restoration
assert(recovered_overdrive == od_state);
assert(recovered_modulation == mod_state);
std::cout << "Power cycle test - OD:" << od_state << " MOD:" << mod_state << " ✅" << std::endl;
}
}
std::cout << "✅ Power cycle combinations: PASSED" << std::endl;
}
void run_all_tests() {
std::cout << "=== Story 2.4 State Persistence Tests ===" << std::endl;
test_settings_structure();
test_settings_comparison();
test_state_save_scenarios();
test_boot_recovery_simulation();
test_version_handling();
test_power_cycle_combinations();
std::cout << "=== All State Persistence Tests PASSED! ===" << std::endl;
std::cout << std::endl;
}
}
int main() {
Test_State_Persistence::run_all_tests();
return 0;
}