From bbdc49b2293a293cbe31a7197898e155f2965947 Mon Sep 17 00:00:00 2001 From: tracer-ysyx Date: Sat, 13 Jan 2024 10:08:10 +0800 Subject: [PATCH] =?UTF-8?q?>=20compile=20NEMU=20ysyx=5F22040000=20?= =?UTF-8?q?=E6=9D=8E=E5=BF=83=E6=9D=A8=20Linux=20calcite=206.1.69=20#1-Nix?= =?UTF-8?q?OS=20SMP=20PREEMPT=5FDYNAMIC=20Wed=20Dec=2020=2016:00:29=20UTC?= =?UTF-8?q?=202023=20x86=5F64=20GNU/Linux=20=2010:08:10=20=20up=201=20day?= =?UTF-8?q?=2010:24,=20=202=20users,=20=20load=20average:=200.92,=200.71,?= =?UTF-8?q?=200.58?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nemu/tests/expr_test.c | 167 +++++++++++++++++++++++++---------------- 1 file changed, 104 insertions(+), 63 deletions(-) diff --git a/nemu/tests/expr_test.c b/nemu/tests/expr_test.c index c2923f0..d87855a 100644 --- a/nemu/tests/expr_test.c +++ b/nemu/tests/expr_test.c @@ -1,27 +1,26 @@ #include "sys/types.h" #include "unistd.h" -#include -#include -#include -#include #include #include #include +#include +#include +#include +#include +#include #include char buf[65536] = {}, ref_buf[65536] = {}; static char code_buf[65536 + 128] = {}; // a little larger than `buf` const int buf_start_pos = 0; char *buf_ptr = buf + buf_start_pos, *ref_buf_ptr = ref_buf; -static char *code_format = -"#include \n" -"#include \n" -"int main() { " -" uint32_t result = %s; " -" printf(\"%%u\", result); " -" return 0; " -"}"; - +static char *code_format = "#include \n" + "#include \n" + "int main() { " + " uint32_t result = %s; " + " printf(\"%%u\", result); " + " return 0; " + "}"; void gen(char c) { *(buf_ptr++) = c; @@ -31,31 +30,40 @@ void gen(char c) { void gen_num(void) { uint32_t num = rand(); int len = 0, ref_len = 0; - switch(rand() % 3) { - case 0: - len = snprintf(buf_ptr, 100, "%u", num); - ref_len = snprintf(ref_buf_ptr, 100, "%uU", num); - break; - case 1: - len = snprintf(buf_ptr, 100, "0x%x", num); - ref_len = snprintf(ref_buf_ptr, 100, "%uU", num); - break; - case 2: - len = snprintf(buf_ptr, 100, "%d", num); - ref_len = snprintf(ref_buf_ptr, 100, "%d", num); - break; - default: assert(0); + switch (rand() % 3) { + case 0: + len = snprintf(buf_ptr, 100, "%u", num); + ref_len = snprintf(ref_buf_ptr, 100, "%uU", num); + break; + case 1: + len = snprintf(buf_ptr, 100, "0x%x", num); + ref_len = snprintf(ref_buf_ptr, 100, "%uU", num); + break; + case 2: + len = snprintf(buf_ptr, 100, "%d", num); + ref_len = snprintf(ref_buf_ptr, 100, "%d", num); + break; + default: + assert(0); } buf_ptr += len; ref_buf_ptr += ref_len; } void gen_rand_op(void) { - switch(rand() % 4) { - case 0: gen('+'); break; - case 1: gen('-'); break; - case 2: gen('*'); break; - case 3: gen('/'); break; + switch (rand() % 4) { + case 0: + gen('+'); + break; + case 1: + gen('-'); + break; + case 2: + gen('*'); + break; + case 3: + gen('/'); + break; } } @@ -63,11 +71,23 @@ void gen_rand_expr(void) { int choice = rand() % 3; if (buf_ptr - buf > 2000) { choice = 0; - } + } switch (choice) { - case 0: gen_num(); break; - case 1: gen('('); gen_rand_expr(); gen(')'); break; - default: gen_rand_expr(); gen(' '); gen_rand_op(); gen(' '); gen_rand_expr(); break; + case 0: + gen_num(); + break; + case 1: + gen('('); + gen_rand_expr(); + gen(')'); + break; + default: + gen_rand_expr(); + gen(' '); + gen_rand_op(); + gen(' '); + gen_rand_expr(); + break; } } @@ -82,8 +102,9 @@ START_TEST(test_expr_random_100) { fputs(code_buf, fp); fclose(fp); - int ret = system("gcc /tmp/.code.c -Werror=div-by-zero -o /tmp/.expr 2>/dev/null"); - if(ret == 256) { + int ret = + system("gcc /tmp/.code.c -Werror=div-by-zero -o /tmp/.expr 2>/dev/null"); + if (ret == 256) { // Probably devide by zero. Skip goto clean_up; } @@ -96,53 +117,73 @@ START_TEST(test_expr_random_100) { ret = fscanf(fp, "%u", &reference); ck_assert(ret == 1); pclose(fp); -// fprintf(stderr, "\n\tbuf = %s\n\taddr = %u, reference = %u", buf, addr, reference); + // fprintf(stderr, "\n\tbuf = %s\n\taddr = %u, reference = %u", buf, addr, + // reference); yy_scan_string(buf + buf_start_pos); uint32_t addr; ck_assert(!yyparse(&addr)); yylex_destroy(); - ck_assert_msg(addr == reference, "\n\tbuf = %s\n\taddr = %u, reference = %u\n", buf, addr, reference); + ck_assert_msg(addr == reference, + "\n\tbuf = %s\n\t(addr = %u) != (reference = %u)\n", buf, addr, + reference); clean_up: - while(buf_ptr != buf + buf_start_pos) { + while (buf_ptr != buf + buf_start_pos) { *(--buf_ptr) = '\0'; } - while(ref_buf_ptr != ref_buf) { + while (ref_buf_ptr != ref_buf) { *(--ref_buf_ptr) = '\0'; } -} END_TEST +} +END_TEST -START_TEST(test_test2) { - ; -} END_TEST +struct { + const char *expr; + uint32_t reference; +} exprs[] = { + {"-1", 0xFFFFFFFFU}, + {"--1", 0x1}, +}; +START_TEST(test_expr_negative_operand) { + yy_scan_string(exprs[_i].expr); + uint32_t addr; + ck_assert(!yyparse(&addr)); + yylex_destroy(); + + ck_assert_msg(addr == exprs[_i].reference, + "\n\texpr = %s\n\t(addr = %u) != (reference = %u)\n", exprs[_i].expr, + addr, exprs[_i].reference); +} +END_TEST Suite *expr_suite(void) { - Suite *s; - TCase *tc_core; + Suite *s; + TCase *tc_core; - s = suite_create("Expr test"); - tc_core = tcase_create("Core"); + s = suite_create("Expr test"); + tc_core = tcase_create("Core"); - tcase_add_loop_test(tc_core, test_expr_random_100, 0, 100); - tcase_add_test(tc_core, test_test2); - suite_add_tcase(s, tc_core); + tcase_add_loop_test(tc_core, test_expr_random_100, 0, 100); + tcase_add_loop_test(tc_core, test_expr_negative_operand, 0, + sizeof(exprs) / sizeof(exprs[0])); + suite_add_tcase(s, tc_core); - return s; + return s; } int main(void) { - int number_failed; - Suite *s; - SRunner *sr; + int number_failed; + Suite *s; + SRunner *sr; - s = expr_suite(); - sr = srunner_create(s); + s = expr_suite(); + sr = srunner_create(s); - srunner_run_all(sr, CK_NORMAL); - number_failed = srunner_ntests_failed(sr); - srunner_free(sr); + srunner_run_all(sr, CK_NORMAL); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); - return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }