Skip to content

Commit

Permalink
Merge pull request #177 from yenslife/stable-sort-test
Browse files Browse the repository at this point in the history
Check if sorting implementation is stable
  • Loading branch information
jserv authored Apr 1, 2024
2 parents 9bda828 + b0aa080 commit 017ec7d
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,23 @@ bool do_sort(int argc, char *argv[])
error_check();

set_noallocate_mode(true);

/* If the number of elements is too large, it may take a long time to check the
* stability of the sort. So, MAX_NODES is used to limit the number of elements
* to check the stability of the sort. */
#define MAX_NODES 100000
struct list_head *nodes[MAX_NODES];
unsigned no = 0;
if (current && current->size && current->size <= MAX_NODES) {
element_t *entry;
list_for_each_entry (entry, current->q, list)
nodes[no++] = &entry->list;
} else if (current && current->size > MAX_NODES)
report(1,
"Warning: Skip checking the stability of the sort because the "
"number of elements %d is too large, exceeds the limit %d.",
current->size, MAX_NODES);

if (current && exception_setup(true))
q_sort(current->q, descend);
exception_cancel();
Expand All @@ -619,8 +636,32 @@ bool do_sort(int argc, char *argv[])
ok = false;
break;
}
/* Ensure the stability of the sort */
if (current->size <= MAX_NODES &&
!strcmp(item->value, next_item->value)) {
bool unstable = false;
for (unsigned i = 0; i < MAX_NODES; i++) {
if (nodes[i] == cur_l->next) {
unstable = true;
break;
}
if (nodes[i] == cur_l) {
break;
}
}
if (unstable) {
report(
1,
"ERROR: Not stable sort. The duplicate strings \"%s\" "
"are not in the same order.",
item->value);
ok = false;
break;
}
}
}
}
#undef MAX_NODES

q_show(3);
return ok && !error_check();
Expand Down

0 comments on commit 017ec7d

Please sign in to comment.