diff --git a/c/src/include/utils.h b/c/src/include/utils.h index c8db2b57..d3c75073 100644 --- a/c/src/include/utils.h +++ b/c/src/include/utils.h @@ -103,29 +103,29 @@ char *get_data_file(const char *name) { } typedef enum { - ERROR, - INT8, - INT16, - INT32, - INT64, - UINT8, - UINT16, - UINT32, - UINT64, - STR, + ERRORT, + INT8T, + INT16T, + INT32T, + INT64T, + UINT8T, + UINT16T, + UINT32T, + UINT64T, + STRINGT, } AnswerType; typedef struct { union { - uint8_t UINT8; - uint16_t UINT16; - uint32_t UINT32; - uint64_t UINT64; - int8_t INT8; - int16_t INT16; - int32_t INT32; - int64_t INT64; - char *STR; + uint8_t uint8; + uint16_t uint16; + uint32_t uint32; + uint64_t uint64; + int8_t int8; + int16_t int16; + int32_t int32; + int64_t int64; + char *string; } value; uint16_t id : 12; AnswerType type : 4; @@ -138,7 +138,7 @@ Answer get_answer(uint16_t id) { .id = id, }; char *answers = get_data_file("answers.tsv"); - + if (!answers) { fprintf(stderr, "Error: Unable to get data from file\n"); return ret; @@ -158,11 +158,11 @@ Answer get_answer(uint16_t id) { continue; if (strcmp(token, "uint") == 0) { - ret.type = UINT8; // will adjust size later + ret.type = UINT8T; // will adjust size later } else if (strcmp(token, "int") == 0) { - ret.type = INT8; // will adjust size later + ret.type = INT8T; // will adjust size later } else if (strcmp(token, "str") == 0) { - ret.type = STR; + ret.type = STRINGT; } else { fprintf(stderr, "Error: Unknown type '%s'\n", token); return ret; @@ -178,25 +178,25 @@ Answer get_answer(uint16_t id) { continue; switch (ret.type) { - case UINT8: - case UINT16: - case UINT32: - case UINT64: + case UINT8T: + case UINT16T: + case UINT32T: + case UINT64T: switch (size) { case 8: - ret.value.UINT8 = (uint8_t)strtoul(token, NULL, 10); + ret.value.uint8 = (uint8_t)strtoul(token, NULL, 10); break; case 16: - ret.value.UINT16 = (uint16_t)strtoul(token, NULL, 10); - ret.type = UINT16; + ret.value.uint16 = (uint16_t)strtoul(token, NULL, 10); + ret.type = UINT16T; break; case 32: - ret.value.UINT32 = strtoul(token, NULL, 10); - ret.type = UINT32; + ret.value.uint32 = strtoul(token, NULL, 10); + ret.type = UINT32T; break; case 64: - ret.value.UINT64 = strtoull(token, NULL, 10); - ret.type = UINT64; + ret.value.uint64 = strtoull(token, NULL, 10); + ret.type = UINT64T; break; default: fprintf(stderr, "Error: Unsupported uint size %" PRIu64 "\n", (uint64_t)size); @@ -204,25 +204,25 @@ Answer get_answer(uint16_t id) { return err; } break; - case INT8: - case INT16: - case INT32: - case INT64: + case INT8T: + case INT16T: + case INT32T: + case INT64T: switch (size) { case 8: - ret.value.INT8 = (int8_t)strtol(token, NULL, 10); + ret.value.int8 = (int8_t)strtol(token, NULL, 10); break; case 16: - ret.value.INT16 = (int16_t)strtol(token, NULL, 10); - ret.type = INT16; + ret.value.int16 = (int16_t)strtol(token, NULL, 10); + ret.type = INT16T; break; case 32: - ret.value.INT32 = strtol(token, NULL, 10); - ret.type = INT32; + ret.value.int32 = strtol(token, NULL, 10); + ret.type = INT32T; break; case 64: - ret.value.INT64 = strtoll(token, NULL, 10); - ret.type = INT64; + ret.value.int64 = strtoll(token, NULL, 10); + ret.type = INT64T; break; default: fprintf(stderr, "Error: Unsupported int size %" PRIu64 "\n", (uint64_t)size); @@ -230,18 +230,18 @@ Answer get_answer(uint16_t id) { return err; } break; - case STR: - ret.value.STR = (char *)malloc(size + 1); - if (ret.value.STR) { - strncpy(ret.value.STR, token, size); - ret.value.STR[size] = 0; + case STRINGT: + ret.value.string = (char *)malloc(size + 1); + if (ret.value.string) { + strncpy(ret.value.string, token, size); + ret.value.string[size] = 0; } else { fprintf(stderr, "Error: Memory allocation failed for string\n"); Answer err = {0}; return err; } break; - case ERROR: + case ERRORT: fprintf(stderr, "Error: Unknown type (should be unreachable)\n"); Answer err = {0}; return err; diff --git a/c/test.c b/c/test.c index 5a416eaa..8618b9dd 100644 --- a/c/test.c +++ b/c/test.c @@ -34,7 +34,7 @@ typedef struct { void *(*func)(); } ProblemRef; -ProblemRef answers[] = { +ProblemRef answers[] = { { 1, (void *(*)())p0001 }, { 2, (void *(*)())p0002 }, { 3, (void *(*)())p0003 }, @@ -46,7 +46,6 @@ ProblemRef answers[] = { { 9, (void *(*)())p0009 }, { 10, (void *(*)())p0010 }, { 11, (void *(*)())p0011 }, - // { 12, (void *(*)())p0012 }, { 13, (void *(*)())p0013 }, { 14, (void *(*)())p0014 }, { 15, (void *(*)())p0015 }, @@ -60,6 +59,7 @@ ProblemRef answers[] = { { 34, (void *(*)())p0034 }, { 76, (void *(*)())p0076 }, { 836, (void *(*)())p0836 }, + { 12, (void *(*)())p0012 }, }; const size_t ANSWERS_LEN = sizeof(answers) / sizeof(answers[0]); static size_t current_index = 0; @@ -75,52 +75,52 @@ void test_euler_answer() { int64_t iresult; uint64_t uresult; switch (answer.type) { - case ERROR: + case ERRORT: TEST_FAIL_MESSAGE("Unknown answer type. This should be unreachable."); - case INT8: + case INT8T: iresult = ((int8_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId8 ", but we actually got %" PRId8, key.id, answer.value.INT8, (int8_t)iresult); - TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.INT8, iresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId8 ", but we actually got %" PRId8, key.id, answer.value.int8, (int8_t)iresult); + TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.int8, iresult, msg); break; - case INT16: + case INT16T: iresult = ((int16_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId16 ", but we actually got %" PRId16, key.id, answer.value.INT16, (int16_t)iresult); - TEST_ASSERT_EQUAL_INT16_MESSAGE(answer.value.INT16, iresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId16 ", but we actually got %" PRId16, key.id, answer.value.int16, (int16_t)iresult); + TEST_ASSERT_EQUAL_INT16_MESSAGE(answer.value.int16, iresult, msg); break; - case INT32: + case INT32T: iresult = ((int32_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId32 ", but we actually got %" PRId32, key.id, answer.value.INT32, (int32_t)iresult); - TEST_ASSERT_EQUAL_INT32_MESSAGE(answer.value.INT32, iresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId32 ", but we actually got %" PRId32, key.id, answer.value.int32, (int32_t)iresult); + TEST_ASSERT_EQUAL_INT32_MESSAGE(answer.value.int32, iresult, msg); break; - case INT64: + case INT64T: iresult = ((int64_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId64 ", but we actually got %" PRId64, key.id, answer.value.UINT64, iresult); - TEST_ASSERT_EQUAL_INT64_MESSAGE(answer.value.INT64, iresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRId64 ", but we actually got %" PRId64, key.id, answer.value.int64, iresult); + TEST_ASSERT_EQUAL_INT64_MESSAGE(answer.value.int64, iresult, msg); break; - case UINT8: + case UINT8T: uresult = ((uint8_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu8 ", but we actually got %" PRIu8, key.id, answer.value.UINT8, (uint8_t)uresult); - TEST_ASSERT_EQUAL_UINT8_MESSAGE(answer.value.UINT8, uresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu8 ", but we actually got %" PRIu8, key.id, answer.value.uint8, (uint8_t)uresult); + TEST_ASSERT_EQUAL_UINT8_MESSAGE(answer.value.uint8, uresult, msg); break; - case UINT16: + case UINT16T: uresult = ((uint16_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu16 ", but we actually got %" PRIu16, key.id, answer.value.UINT16, (uint16_t)uresult); - TEST_ASSERT_EQUAL_UINT16_MESSAGE(answer.value.UINT16, uresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu16 ", but we actually got %" PRIu16, key.id, answer.value.uint16, (uint16_t)uresult); + TEST_ASSERT_EQUAL_UINT16_MESSAGE(answer.value.uint16, uresult, msg); break; - case UINT32: + case UINT32T: uresult = ((uint32_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu32 ", but we actually got %" PRIu32, key.id, answer.value.UINT32, (uint32_t)uresult); - TEST_ASSERT_EQUAL_UINT32_MESSAGE(answer.value.UINT32, uresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu32 ", but we actually got %" PRIu32, key.id, answer.value.uint32, (uint32_t)uresult); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(answer.value.uint32, uresult, msg); break; - case UINT64: + case UINT64T: uresult = ((uint64_t (*)()) key.func)(); - snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu64 ", but we actually got %" PRIu64, key.id, answer.value.UINT64, uresult); - TEST_ASSERT_EQUAL_UINT64_MESSAGE(answer.value.UINT64, uresult, msg); + snprintf(msg, 256, "Euler problem %" PRIu16 " should have an answer of %" PRIu64 ", but we actually got %" PRIu64, key.id, answer.value.uint64, uresult); + TEST_ASSERT_EQUAL_UINT64_MESSAGE(answer.value.uint64, uresult, msg); break; - case STR: + case STRINGT: sresult = ((char *(*)()) key.func)(); - snprintf(msg, 256, "Euler problem %u should have an answer of %s, but we actually got %s", key.id, answer.value.STR, sresult); - TEST_ASSERT_EQUAL_STRING_MESSAGE(answer.value.STR, sresult, msg); + snprintf(msg, 256, "Euler problem %u should have an answer of %s, but we actually got %s", key.id, answer.value.string, sresult); + TEST_ASSERT_EQUAL_STRING_MESSAGE(answer.value.string, sresult, msg); } }