Skip to content

Commit

Permalink
Enhance stability checks in sort function
Browse files Browse the repository at this point in the history
This version improves stability testing in the do_sort function by
tracking node pointers and their original order to verify stable
sorting. Unlike the previous version, which modified element_t, this
iteration avoids altering the structure and instead relies on
auxiliary data structures. However, the stability test is limited
to a maximum number of elements, currently set as MAX_NO, to mitigate
potential performance issues. Note: If the dataset is too large, it
may lead to excessively long test times, hence the use of MAX_NO.
  • Loading branch information
yenslife committed Mar 23, 2024
1 parent 20631f6 commit 5a51857
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
62 changes: 50 additions & 12 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,25 @@ bool do_sort(int argc, char *argv[])
error_check();

set_noallocate_mode(true);

#define MAX_NO 100000
struct list_head *element_pointers[MAX_NO];
unsigned no = 0;
if (current && exception_setup(true)) {
if (current && current->size) {
element_t *entry;
list_for_each_entry (entry, current->q, list) {
entry->no = no++;
element_pointers[no++] = &entry->list;
if (no >= MAX_NO) {
report(1,
"Skip the stability check because the number of "
"elements is too large.");
break;
}
}
q_sort(current->q, descend);
}

if (current && exception_setup(true))
q_sort(current->q, descend);
exception_cancel();
set_noallocate_mode(false);

Expand All @@ -630,15 +641,42 @@ bool do_sort(int argc, char *argv[])
ok = false;
break;
}

if (!strcmp(item->value, next_item->value) &&
item->no > next_item->no) {
report(1,
"ERROR: Not stable sort. The duplicate string \"%s\" is "
"not in the same order",
item->value);
ok = false;
break;
/* Ensure the stability of the sort
* If two elements are equal, we check the order of the two elements
* in the original list.
*
* If the number of elements is too large, it may take a long time.
* So, the number of elements is limited to MAX_NO.
*/
if (no < MAX_NO && !strcmp(item->value, next_item->value)) {
int cur_no = 0, next_no = 0;
for (unsigned i = 0; i < MAX_NO; i++) {
if (element_pointers[i] == cur_l) {
cur_no = i;
break;
}
/* In some cases like the number of elements is too large,
* the element pointer may not be found in the array.
* In this case, the stability check is skipped.
*/
cur_no = 0;
}
for (unsigned i = 0; i < MAX_NO; i++) {
if (element_pointers[i] == cur_l->next) {
next_no = i;
break;
}
cur_no = 1;
}
if (cur_no > next_no) {
report(
1,
"ERROR: Not stable sort. The duplicate strings \"%s\" "
"are not in the same order.",
item->value);
ok = false;
break;
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
typedef struct {
char *value;
struct list_head list;
unsigned no;
} element_t;

/**
Expand Down

0 comments on commit 5a51857

Please sign in to comment.