TOFIX historycopy

This commit is contained in:
Théophyl Hoguet committed 2021-12-16 11:25:48 +01:00
1 parent 9c60c774a5
commit fcfee786d7
4 files changed
+66

No files matched your search

+27
View File
@@ -55,6 +55,33 @@
} }
], ],
"preLaunchTask": "make_build" "preLaunchTask": "make_build"
},
{
"name": "(gdb) debug tests histo",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/history_test",
"args": [
"history_copy"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "make_build"
} }
] ]
} }
+1
View File
@@ -78,3 +78,4 @@ add_test(test_history_delete_before ./history_test history_delete_before)
add_test(test_history_last ./history_test history_last) add_test(test_history_last ./history_test history_last)
add_test(test_history_first ./history_test history_first) add_test(test_history_first ./history_test history_first)
add_test(test_history_append ./history_test history_append) add_test(test_history_append ./history_test history_append)
add_test(test_history_copy ./history_test history_copy)
+24
View File
@@ -189,3 +189,27 @@ void history_delete_entire_history(history h) {
return history_free(history_delete_all_after(h, h)); return history_free(history_delete_all_after(h, h));
return history_delete_entire_history(history_prev(h)); return history_delete_entire_history(history_prev(h));
} }
history history_copy(history h) {
history tmp = h;
if (history_is_empty(h)) {
return h;
}
h = history_first(h);
uint cpt = 0;
while (h != tmp) {
h = history_next(h);
cpt++;
}
history h2 = history_insert_first(history_create_empty(), h->state, h->i, h->j);
while (!history_is_empty(history_next(h))) {
h = history_next(h);
h2 = history_insert_after(h2, h2, h->state, h->i, h->j);
h2 = history_next(h2);
}
h2 = history_first(h2);
for (uint i = 0; i < cpt; i++) {
h2 = history_next(h2);
}
return h2;
}
+14
View File
@@ -165,6 +165,18 @@ bool test_history_append(void) {
return true; return true;
} }
bool test_history_copy(void) {
history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
assert(new);
new = history_insert_after(new, new, S_BLANK, 5, 8);
history h2 = history_copy(new);
assert(history_state(h2) != history_state(new));
assert(history_state(history_next(h2)) == history_state(history_next(new)));
history_delete_entire_history(h2);
history_delete_entire_history(new);
return true;
}
int usage(int argc, char* argv[]) { int usage(int argc, char* argv[]) {
fprintf(stderr, "Usage: %s <testname> [<...>]\n", argv[0]); fprintf(stderr, "Usage: %s <testname> [<...>]\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -206,6 +218,8 @@ int main(int argc, char* argv[]) {
ok = test_history_first(); ok = test_history_first();
else if (strcmp("history_append", argv[1]) == 0) else if (strcmp("history_append", argv[1]) == 0)
ok = test_history_append(); ok = test_history_append();
else if (strcmp("history_copy", argv[1]) == 0)
ok = test_history_copy();
else { else {
fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]); fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);