Skip to content

Commit

Permalink
Merge pull request sysprog21#127 from yanjiew1/merge_ih_it
Browse files Browse the repository at this point in the history
Merge `do_i(h|t)` into a single function
  • Loading branch information
eecheng87 authored Feb 22, 2023
2 parents 976eb71 + 3114f08 commit 76ef328
Showing 1 changed file with 35 additions and 87 deletions.
122 changes: 35 additions & 87 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ static int string_length = MAXSTRING;
#define MIN_RANDSTR_LEN 5
#define MAX_RANDSTR_LEN 10
static const char charset[] = "abcdefghijklmnopqrstuvwxyz";

/* For queue_insert and queue_remove */
typedef enum {
POS_TAIL,
POS_HEAD,
} position_t;
/* Forward declarations */
static bool q_show(int vlevel);

Expand Down Expand Up @@ -173,15 +177,16 @@ static void fill_rand_string(char *buf, size_t buf_size)
buf[len] = '\0';
}

/* insert head */
static bool do_ih(int argc, char *argv[])
/* insertion */
static bool queue_insert(position_t pos, int argc, char *argv[])
{
if (simulation) {
if (argc != 1) {
report(1, "%s does not need arguments in simulation mode", argv[0]);
return false;
}
bool ok = is_insert_head_const();
bool ok =
pos == POS_TAIL ? is_insert_tail_const() : is_insert_head_const();
if (!ok) {
report(1,
"ERROR: Probably not constant time or wrong implementation");
Expand Down Expand Up @@ -214,18 +219,23 @@ static bool do_ih(int argc, char *argv[])
}

if (!current || !current->q)
report(3, "Warning: Calling insert head on null queue");
report(3, "Warning: Calling insert %s on null queue",
pos == POS_TAIL ? "tail" : "head");
error_check();

if (current && exception_setup(true)) {
for (int r = 0; ok && r < reps; r++) {
if (need_rand)
fill_rand_string(randstr_buf, sizeof(randstr_buf));
bool rval = q_insert_head(current->q, inserts);
bool rval = pos == POS_TAIL ? q_insert_tail(current->q, inserts)
: q_insert_head(current->q, inserts);
if (rval) {
current->size++;
char *cur_inserts =
list_entry(current->q->next, element_t, list)->value;
element_t *entry =
pos == POS_TAIL
? list_last_entry(current->q, element_t, list)
: list_first_entry(current->q, element_t, list);
char *cur_inserts = entry->value;
if (!cur_inserts) {
report(1, "ERROR: Failed to save copy of string in queue");
ok = false;
Expand Down Expand Up @@ -263,85 +273,20 @@ static bool do_ih(int argc, char *argv[])
return ok;
}

/* insert head */
static bool do_ih(int argc, char *argv[])
{
return queue_insert(POS_HEAD, argc, argv);
}

/* insert tail */
static bool do_it(int argc, char *argv[])
{
if (simulation) {
if (argc != 1) {
report(1, "%s does not need arguments in simulation mode", argv[0]);
return false;
}
bool ok = is_insert_tail_const();
if (!ok) {
report(1,
"ERROR: Probably not constant time or wrong implementation");
return false;
}
report(1, "Probably constant time");
return ok;
}

char randstr_buf[MAX_RANDSTR_LEN];
int reps = 1;
bool ok = true, need_rand = false;
if (argc != 2 && argc != 3) {
report(1, "%s needs 1-2 arguments", argv[0]);
return false;
}

char *inserts = argv[1];
if (argc == 3) {
if (!get_int(argv[2], &reps)) {
report(1, "Invalid number of insertions '%s'", argv[2]);
return false;
}
}

if (!strcmp(inserts, "RAND")) {
need_rand = true;
inserts = randstr_buf;
}

if (!current || !current->q)
report(3, "Warning: Calling insert tail on null queue");
error_check();

if (current && exception_setup(true)) {
for (int r = 0; ok && r < reps; r++) {
if (need_rand)
fill_rand_string(randstr_buf, sizeof(randstr_buf));
bool rval = q_insert_tail(current->q, inserts);
if (rval) {
current->size++;
char *cur_inserts =
list_entry(current->q->prev, element_t, list)->value;
if (!cur_inserts) {
report(1, "ERROR: Failed to save copy of string in queue");
ok = false;
}
} else {
fail_count++;
if (fail_count < fail_limit)
report(2, "Insertion of %s failed", inserts);
else {
report(1,
"ERROR: Insertion of %s failed (%d failures total)",
inserts, fail_count);
ok = false;
}
}
ok = ok && !error_check();
}
}
exception_cancel();
q_show(3);
return ok;
return queue_insert(POS_TAIL, argc, argv);
}

static bool do_remove(int option, int argc, char *argv[])
static bool queue_remove(position_t pos, int argc, char *argv[])
{
// option 0 is for remove head; option 1 is for remove tail

/* FIXME: It is known that both functions is_remove_tail_const() and
* is_remove_head_const() can not pass dudect on Apple M1 (based on Arm64).
* We shall figure out the exact reasons and resolve later.
Expand All @@ -352,7 +297,8 @@ static bool do_remove(int option, int argc, char *argv[])
report(1, "%s does not need arguments in simulation mode", argv[0]);
return false;
}
bool ok = option ? is_remove_tail_const() : is_remove_head_const();
bool ok =
pos == POS_TAIL ? is_remove_tail_const() : is_remove_head_const();
if (!ok) {
report(1,
"ERROR: Probably not constant time or wrong implementation");
Expand Down Expand Up @@ -395,13 +341,15 @@ static bool do_remove(int option, int argc, char *argv[])
removes[string_length + STRINGPAD] = '\0';

if (!current || !current->size)
report(3, "Warning: Calling remove head on empty queue");
report(3, "Warning: Calling remove %s on empty queue",
pos == POS_TAIL ? "tail" : "head");
error_check();

element_t *re = NULL;
if (current && exception_setup(true))
re = option ? q_remove_tail(current->q, removes, string_length + 1)
: q_remove_head(current->q, removes, string_length + 1);
re = pos == POS_TAIL
? q_remove_tail(current->q, removes, string_length + 1)
: q_remove_head(current->q, removes, string_length + 1);
exception_cancel();

bool is_null = re ? false : true;
Expand Down Expand Up @@ -458,12 +406,12 @@ static bool do_remove(int option, int argc, char *argv[])

static inline bool do_rh(int argc, char *argv[])
{
return do_remove(0, argc, argv);
return queue_remove(POS_HEAD, argc, argv);
}

static inline bool do_rt(int argc, char *argv[])
{
return do_remove(1, argc, argv);
return queue_remove(POS_TAIL, argc, argv);
}

static bool do_dedup(int argc, char *argv[])
Expand Down

0 comments on commit 76ef328

Please sign in to comment.