From: Florian Krohm Date: Fri, 25 Jul 2025 21:11:57 +0000 (+0000) Subject: iropt-test: Refactor code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a159cf245ff4fec2fd148c899ce7a0380ca3d04;p=thirdparty%2Fvalgrind.git iropt-test: Refactor code Refactor code in preparation for running each testcase twice: once with constant folding and once without. - remove function print_opnd - remove function complain - factor out function get_expected_value - checking the result moved to valgrind_execute_test - make IRICB a static global in valgrind.c - new_iricb now returns a pointer to it --- diff --git a/none/tests/iropt-test/binary.c b/none/tests/iropt-test/binary.c index 62c9faeb3..0d40a4da3 100644 --- a/none/tests/iropt-test/binary.c +++ b/none/tests/iropt-test/binary.c @@ -26,7 +26,7 @@ #include // UINT64_MAX #include "vtest.h" -static void check_result(const irop_t *, const test_data_t *); +static uint64_t get_expected_value(const irop_t *, const test_data_t *); static void run_tests(const irop_t *, test_data_t *); static void run_shift_tests(const irop_t *, test_data_t *); static int is_shift_op(IROp); @@ -59,8 +59,7 @@ run_selected_tests(const irop_t *op, test_data_t *data) if (is_division_op(op->op) && opnd_r->value == 0) continue; - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } } } @@ -79,8 +78,7 @@ run_random_tests(const irop_t *op, test_data_t *data) if (is_division_op(op->op) && opnd_r->value == 0) continue; - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } } @@ -101,8 +99,7 @@ run_shift_tests(const irop_t *op, test_data_t *data) for (unsigned j = 0; j < max_shift_amount; ++j) { opnd_r->value = j; - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } } @@ -111,8 +108,7 @@ run_shift_tests(const irop_t *op, test_data_t *data) opnd_l->value = get_random_value(opnd_l->type); opnd_r->value = get_random_value(opnd_r->type) & max_shift_amount; - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } } @@ -125,11 +121,10 @@ run_tests(const irop_t *op, test_data_t *data) } -/* Check the result of a binary operation. */ -static void -check_result(const irop_t *op, const test_data_t *data) +/* Compute the expected result of a binary operation. */ +static uint64_t +get_expected_value(const irop_t *op, const test_data_t *data) { - uint64_t result = data->result.value; uint64_t opnd_l = data->opnds[0].value; uint64_t opnd_r = data->opnds[1].value; uint64_t expected; @@ -384,25 +379,7 @@ check_result(const irop_t *op, const test_data_t *data) panic(__func__); } - if (verbose > 1) { - printf("expected: value = "); - print_value(stdout, expected, bitsof_irtype(data->result.type)); - printf("\n"); - } - - int ok = 1; - switch (data->result.type) { - case Ity_I1: ok = result == expected; break; - case Ity_I8: ok = result == expected; break; - case Ity_I16: ok = result == expected; break; - case Ity_I32: ok = result == expected; break; - case Ity_I64: ok = result == expected; break; - default: - panic(__func__); - } - - if (! ok) - complain(op, data, expected); + return expected; } diff --git a/none/tests/iropt-test/main.c b/none/tests/iropt-test/main.c index 9f1cc5083..8fcf9fb45 100644 --- a/none/tests/iropt-test/main.c +++ b/none/tests/iropt-test/main.c @@ -81,9 +81,9 @@ main(int argc, char *argv[]) test_data_t *data = new_test_data(op); - IRICB iricb = new_iricb(op, data); + IRICB *iricb = new_iricb(op, data); - valgrind_vex_init_for_iri(&iricb); + valgrind_vex_init_for_iri(iricb); switch (op->num_opnds) { case 1: diff --git a/none/tests/iropt-test/unary.c b/none/tests/iropt-test/unary.c index e6554cef6..fa961f28a 100644 --- a/none/tests/iropt-test/unary.c +++ b/none/tests/iropt-test/unary.c @@ -27,9 +27,9 @@ #include // UINT64_MAX #include "vtest.h" -static void check_result(const irop_t *, const test_data_t *); static void run_selected_tests(const irop_t *, test_data_t *); static void run_random_tests(const irop_t *, test_data_t *); +static uint64_t get_expected_value(const irop_t *, const test_data_t *); static uint64_t left(uint64_t, unsigned); static uint32_t popcount(uint64_t); static uint32_t clz(uint64_t, unsigned); @@ -49,8 +49,7 @@ test_unary_op(const irop_t *op, test_data_t *data) for (unsigned i = 0; i <= max; ++i) { opnd->value = i; - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } break; } @@ -78,8 +77,7 @@ run_selected_tests(const irop_t *op, test_data_t *data) for (unsigned i = 0; i < num_val; ++i) { opnd->value = values[i]; - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } } @@ -93,17 +91,15 @@ run_random_tests(const irop_t *op, test_data_t *data) for (unsigned i = 0; i < num_random_tests; ++i) { opnd->value = get_random_value(opnd->type); - valgrind_execute_test(op, data); - check_result(op, data); + valgrind_execute_test(op, data, get_expected_value(op, data)); } } -/* Check the result of a unary operation. */ -static void -check_result(const irop_t *op, const test_data_t *data) +/* Compute the expected result of a unary operation. */ +static uint64_t +get_expected_value(const irop_t *op, const test_data_t *data) { - uint64_t result = data->result.value; uint64_t opnd = data->opnds[0].value; uint64_t expected; @@ -209,25 +205,7 @@ check_result(const irop_t *op, const test_data_t *data) panic("%s: operator %s not handled\n", __func__, op->name); } - if (verbose > 1) { - printf("expected: value = "); - print_value(stdout, expected, bitsof_irtype(data->result.type)); - printf("\n"); - } - - int ok = 1; - switch (data->result.type) { - case Ity_I1: ok = result == expected; break; - case Ity_I8: ok = result == expected; break; - case Ity_I16: ok = result == expected; break; - case Ity_I32: ok = result == expected; break; - case Ity_I64: ok = result == expected; break; - default: - panic(__func__); - } - - if (! ok) - complain(op, data, expected); + return expected; } diff --git a/none/tests/iropt-test/util.c b/none/tests/iropt-test/util.c index 18b671114..f34936611 100644 --- a/none/tests/iropt-test/util.c +++ b/none/tests/iropt-test/util.c @@ -44,27 +44,6 @@ panic(const char *fmt, ...) } -/* Issue a complaint because the result of an operation differs from what - was expected. */ -void -complain(const irop_t *op, const test_data_t *data, uint64_t expected) -{ - fprintf(stderr, "*** Incorrect result for operator %s\n", op->name); - - for (unsigned i = 0; i < op->num_opnds; ++i) { - fprintf(stderr, " opnd %u: ", i); - print_opnd(stderr, &data->opnds[i]); - fprintf(stderr, "\n"); - } - fprintf(stderr, " result: "); - print_opnd(stderr, &data->result); - fprintf(stderr, "\n"); - fprintf(stderr, " expect: "); - print_value(stderr, expected, bitsof_irtype(op->result_type)); - fprintf(stderr, "\n"); -} - - void print_value(FILE *fp, uint64_t val, unsigned num_bits) { @@ -83,14 +62,6 @@ print_value(FILE *fp, uint64_t val, unsigned num_bits) } -void -print_opnd(FILE *fp, const opnd_t *opnd) -{ - fprintf(fp, "value = "); - print_value(fp, opnd->value, bitsof_irtype(opnd->type)); -} - - unsigned bitsof_irtype(IRType ty) { diff --git a/none/tests/iropt-test/valgrind.c b/none/tests/iropt-test/valgrind.c index e482909b1..004c8a09a 100644 --- a/none/tests/iropt-test/valgrind.c +++ b/none/tests/iropt-test/valgrind.c @@ -26,9 +26,10 @@ #include "valgrind.h" // VALGRIND_VEX_INJECT_IR #include "vtest.h" +static IRICB iricb; /* Return a completely initialised control block */ -IRICB +IRICB * new_iricb(const irop_t *op, test_data_t *data) { IRICB_iropt_payload cb; @@ -45,7 +46,10 @@ new_iricb(const irop_t *op, test_data_t *data) cb.num_operands = op->num_opnds; - return (IRICB) { .kind = IRICB_iropt, .iropt = cb }; + iricb.kind = IRICB_iropt; + iricb.iropt = cb; + + return &iricb; } @@ -69,24 +73,47 @@ valgrind_vex_inject_ir(void) /* Execute the test under valgrind. Well, yes, we're not really executing it here, just preparing for it... */ void -valgrind_execute_test(const irop_t *op, test_data_t *data) +valgrind_execute_test(const irop_t *op, test_data_t *data, uint64_t expected) { - if (verbose > 1) + if (verbose > 1) { printf("---------- Running a test\n"); - for (unsigned i = 0; i < op->num_opnds; ++i) { - if (verbose > 1) { - printf("opnd #%u: ", i); - print_opnd(stdout, &data->opnds[i]); + for (unsigned i = 0; i < op->num_opnds; ++i) { + const opnd_t *opnd = data->opnds + i; + printf("opnd %u: value = ", i); + print_value(stdout, opnd->value, bitsof_irtype(opnd->type)); printf("\n"); } } valgrind_vex_inject_ir(); + uint64_t result = data->result.value; + unsigned num_result_bits = bitsof_irtype(data->result.type); if (verbose > 1) { - printf("result: "); - print_opnd(stdout, &data->result); + printf("result: value = "); + print_value(stdout, result, num_result_bits); printf("\n"); + printf("expected: value = "); + print_value(stdout, expected, num_result_bits); + printf("\n"); + } + + /* Check result */ + if (result != expected) { + fprintf(stderr, "*** Incorrect result for operator %s\n", op->name); + + for (unsigned i = 0; i < op->num_opnds; ++i) { + const opnd_t *opnd = data->opnds + i; + fprintf(stderr, " opnd %u: ", i); + print_value(stderr, opnd->value, bitsof_irtype(opnd->type)); + fprintf(stderr, "\n"); + } + fprintf(stderr, " result: "); + print_value(stderr, result, num_result_bits); + fprintf(stderr, "\n"); + fprintf(stderr, " expect: "); + print_value(stderr, expected, num_result_bits); + fprintf(stderr, "\n"); } } diff --git a/none/tests/iropt-test/vtest.h b/none/tests/iropt-test/vtest.h index 47b397f68..f31f3c3ce 100644 --- a/none/tests/iropt-test/vtest.h +++ b/none/tests/iropt-test/vtest.h @@ -69,19 +69,17 @@ typedef struct { /* Function prototypes */ -void print_opnd(FILE *, const opnd_t *); void print_value(FILE *, uint64_t, unsigned); void test_unary_op(const irop_t *, test_data_t *); void test_binary_op(const irop_t *, test_data_t *); void valgrind_vex_init_for_iri(IRICB *); -void valgrind_execute_test(const irop_t *, test_data_t *); +void valgrind_execute_test(const irop_t *, test_data_t *, uint64_t); -IRICB new_iricb(const irop_t *, test_data_t *); +IRICB *new_iricb(const irop_t *, test_data_t *); void panic(const char *, ...) __attribute__((noreturn)); -void complain(const irop_t *, const test_data_t *, uint64_t expected); unsigned bitsof_irtype(IRType); uint64_t get_random_value(IRType);