Skip to content

Commit

Permalink
fix windows compilation error
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 18, 2024
1 parent 57343de commit 4b47062
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 72 deletions.
86 changes: 43 additions & 43 deletions cplusplus/src/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,29 @@ std::string get_data_file(const std::string &name) {
}

typedef enum {
ERR,
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;
Expand Down Expand Up @@ -131,11 +131,11 @@ Answer get_answer(const uint16_t id) {
continue;

if (token == "uint") {
answer.type = UINT8; // will adjust size later
answer.type = UINT8T; // will adjust size later
} else if (token == "int") {
answer.type = INT8; // will adjust size later
answer.type = INT8T; // will adjust size later
} else if (token == "str") {
answer.type = STR;
answer.type = STRINGT;
} else {
std::cerr << "Error: Unknown type '" << token << "'\n";
return answer;
Expand All @@ -149,57 +149,57 @@ Answer get_answer(const uint16_t id) {
continue;

switch (answer.type) {
case UINT8:
case UINT8T:
switch (size) {
case 8:
answer.value.UINT8 = (uint8_t)strtoul(token.c_str(), NULL, 10);
answer.value.uint8 = (uint8_t)strtoul(token.c_str(), NULL, 10);
break;
case 16:
answer.value.UINT16 = (uint16_t)strtoul(token.c_str(), NULL, 10);
answer.type = UINT16;
answer.value.uint16 = (uint16_t)strtoul(token.c_str(), NULL, 10);
answer.type = UINT16T;
break;
case 32:
answer.value.UINT32 = strtoul(token.c_str(), NULL, 10);
answer.type = UINT32;
answer.value.uint32 = strtoul(token.c_str(), NULL, 10);
answer.type = UINT32T;
break;
case 64:
answer.value.UINT64 = strtoull(token.c_str(), NULL, 10);
answer.type = UINT64;
answer.value.uint64 = strtoull(token.c_str(), NULL, 10);
answer.type = UINT64T;
break;
default:
std::cerr << "Error: Unsupported int size " << size << "'\n";
Answer err = {{0}};
return err;
}
break;
case INT8:
case INT8T:
switch (size) {
case 8:
answer.value.INT8 = (int8_t)strtol(token.c_str(), NULL, 10);
answer.value.int8 = (int8_t)strtol(token.c_str(), NULL, 10);
break;
case 16:
answer.value.INT16 = (int16_t)strtol(token.c_str(), NULL, 10);
answer.type = INT16;
answer.value.int16 = (int16_t)strtol(token.c_str(), NULL, 10);
answer.type = INT16T;
break;
case 32:
answer.value.INT32 = strtol(token.c_str(), NULL, 10);
answer.type = INT32;
answer.value.int32 = strtol(token.c_str(), NULL, 10);
answer.type = INT32T;
break;
case 64:
answer.value.INT64 = strtoll(token.c_str(), NULL, 10);
answer.type = INT64;
answer.value.int64 = strtoll(token.c_str(), NULL, 10);
answer.type = INT64T;
break;
default:
std::cerr << "Error: Unsupported uint size " << size << "'\n";
Answer err = {{0}};
return err;
}
break;
case STR:
answer.value.STR = (char *)malloc(size + 1);
if (answer.value.STR) {
strncpy(answer.value.STR, token.c_str(), size);
answer.value.STR[size] = 0;
case STRINGT:
answer.value.string = (char *)malloc(size + 1);
if (answer.value.string) {
strncpy(answer.value.string, token.c_str(), size);
answer.value.string[size] = 0;
} else {
std::cerr << "Error: Memory allocation failed for string\n";
Answer err = {{0}};
Expand Down
58 changes: 29 additions & 29 deletions cplusplus/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct {
void *(*func)();
} ProblemRef;

ProblemRef answers[] = {
ProblemRef answers[] = {
{ 1, (void *(*)())p0001 },
{ 2, (void *(*)())p0002 },
// { 3, (void *(*)())p0003 },
Expand Down Expand Up @@ -76,52 +76,52 @@ void test_euler_answer() {
std::string sresult;
std::ostringstream oss;
switch (answer.type) {
case ERR:
case ERRORT:
TEST_FAIL_MESSAGE("Unknown answer type. This should be unreachable.");
case INT8:
case INT8T:
iresult = ((int8_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.INT8 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.INT8, (int8_t)iresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.int8 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.int8, (int8_t)iresult, oss.str().c_str());
break;
case INT16:
case INT16T:
iresult = ((int16_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.INT16 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.INT16, (int16_t)iresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.int16 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.int16, (int16_t)iresult, oss.str().c_str());
break;
case INT32:
case INT32T:
iresult = ((int32_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.INT32 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.INT32, (int32_t)iresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.int32 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT8_MESSAGE(answer.value.int32, (int32_t)iresult, oss.str().c_str());
break;
case INT64:
case INT64T:
iresult = ((int64_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.INT64 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT64_MESSAGE(answer.value.INT64, iresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.int64 << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT64_MESSAGE(answer.value.int64, iresult, oss.str().c_str());
break;
case UINT8:
case UINT8T:
uresult = ((uint8_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.UINT8 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT8_MESSAGE(answer.value.UINT8, (uint8_t) uresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.uint8 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT8_MESSAGE(answer.value.uint8, (uint8_t) uresult, oss.str().c_str());
break;
case UINT16:
case UINT16T:
uresult = ((uint16_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.UINT16 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT16_MESSAGE(answer.value.UINT16, (uint16_t) uresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.uint16 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT16_MESSAGE(answer.value.uint16, (uint16_t) uresult, oss.str().c_str());
break;
case UINT32:
case UINT32T:
uresult = ((uint32_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.UINT32 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT32_MESSAGE(answer.value.UINT32, (uint32_t) uresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.uint32 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT32_MESSAGE(answer.value.uint32, (uint32_t) uresult, oss.str().c_str());
break;
case UINT64:
case UINT64T:
uresult = ((uint64_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.UINT64 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT64_MESSAGE(answer.value.UINT64, uresult, oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.uint64 << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT64_MESSAGE(answer.value.uint64, uresult, oss.str().c_str());
break;
case STR:
case STRINGT:
sresult = ((std::string (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.STR << ", but we actually got " << sresult;
TEST_ASSERT_EQUAL_STRING_MESSAGE(answer.value.STR, sresult.c_str(), oss.str().c_str());
oss << "Euler problem " << key.id << " should have an answer of " << answer.value.string << ", but we actually got " << sresult;
TEST_ASSERT_EQUAL_STRING_MESSAGE(answer.value.string, sresult.c_str(), oss.str().c_str());
}
}

Expand Down

0 comments on commit 4b47062

Please sign in to comment.