Skip to content

Commit

Permalink
Update test files to support multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 17, 2024
1 parent 2cae621 commit f5955f1
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 62 deletions.
2 changes: 1 addition & 1 deletion c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ lint:

.PHONY: native
native: ../LICENSE Unity/src/unity.c
@$(CC) test.c Unity/src/unity.c -O2 -lm -lc -Wall -Werror -march=native -flto -DAMD_COMPILER=0
@$(CC) test.c Unity/src/unity.c -O2 -g -lm -lc -Wall -Werror -std=gnu99 -march=native -flto

.PHONY: clean
clean:
Expand Down
98 changes: 68 additions & 30 deletions c/test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#define UNITY_SUPPORT_TEST_CASES
#include <stdint.h>
#include <inttypes.h>
#include "Unity/src/unity.h"
#include "src/p0001.c"
#include "src/p0002.c"
Expand Down Expand Up @@ -26,36 +28,51 @@
#include "src/p0076.c"
#include "src/p0836.c"

typedef enum {
INT8,
INT16,
INT32,
INT64,
UINT8,
UINT16,
UINT32,
UINT64,
STR,
} AnswerType;

typedef struct {
uint32_t id;
uint64_t answer;
uint64_t (*func)();
AnswerType type;
void *answer;
void *(*func)();
} Answer;

const Answer answers[] = {
{1, 233168, (uint64_t (*)())p0001},
{2, 4613732, (uint64_t (*)())p0002},
{3, 6857, (uint64_t (*)())p0003},
{4, 906609, (uint64_t (*)()) p0004},
{5, 232792560, (uint64_t (*)())p0005},
{6, 25164150, (uint64_t (*)())p0006},
{7, 104743, (uint64_t (*)())p0007},
{8, 23514624000, p0008},
{9, 31875000, (uint64_t (*)())p0009},
{10, 142913828922, p0010},
{11, 70600674, p0011},
{13, 5537376230, p0013},
{14, 837799, p0014},
{15, 137846528820, p0015},
{16, 1366, p0016},
{17, 21124, p0017},
{19, 171, (uint64_t (*)()) p0019},
{20, 648, p0020},
{22, 871198282, p0022},
{25, 4782, p0025},
{30, 443839, p0030},
{34, 40730, p0034},
{76, 190569291, (uint64_t (*)()) p0076},
const Answer answers[] = {
{ 1, INT32, (void *)233168, (void *(*)())p0001 },
{ 2, INT32, (void *)4613732, (void *(*)())p0002 },
{ 3, INT16, (void *)6857, (void *(*)())p0003 },
{ 4, INT32, (void *)906609, (void *(*)())p0004 },
{ 5, INT32, (void *)232792560, (void *(*)())p0005 },
{ 6, INT32, (void *)25164150, (void *(*)())p0006 },
{ 7, INT32, (void *)104743, (void *(*)())p0007 },
{ 8, INT64, (void *)23514624000, (void *(*)())p0008 },
{ 9, INT32, (void *)31875000, (void *(*)())p0009 },
{ 10, INT64, (void *)142913828922, (void *(*)())p0010 },
{ 11, INT32, (void *)70600674, (void *(*)())p0011 },
// { 12, INT32, (void *)76576500, (void *(*)())p0012 },
{ 13, INT64, (void *)5537376230, (void *(*)())p0013 },
{ 14, INT32, (void *)837799, (void *(*)())p0014 },
{ 15, INT64, (void *)137846528820, (void *(*)())p0015 },
{ 16, INT16, (void *)1366, (void *(*)())p0016 },
{ 17, INT16, (void *)21124, (void *(*)())p0017 },
{ 19, UINT8, (void *)171, (void *(*)())p0019 },
{ 20, INT16, (void *)648, (void *(*)())p0020 },
{ 22, INT32, (void *)871198282, (void *(*)())p0022 },
{ 25, INT16, (void *)4782, (void *(*)())p0025 },
{ 30, INT32, (void *)443839, (void *(*)())p0030 },
{ 34, UINT16, (void *)40730, (void *(*)())p0034 },
{ 76, INT32, (void *)190569291, (void *(*)())p0076 },
{ 836, STR, (void *)"aprilfoolsjoke", (void *(*)())p0836 },
};
const size_t ANSWERS_LEN = sizeof(answers) / sizeof(answers[0]);
static size_t current_index = 0;
Expand All @@ -66,10 +83,31 @@ void tearDown(void) {}

void test_euler_answer() {
Answer key = answers[current_index];
uint64_t result = key.func();
char *msg = (char*)malloc(256 * sizeof(char));
snprintf(msg, 256, "Euler problem %u should have an answer of %lu, but we actually got %lu", key.id, key.answer, result);
TEST_ASSERT_EQUAL_INT64_MESSAGE(key.answer, result, msg);
char *msg = (char*)malloc(256 * sizeof(char)), *sresult;
int64_t iresult;
uint64_t uresult;
switch (key.type) {
case INT8:
case INT16:
case INT32:
case INT64:
iresult = ((uint64_t (*)()) key.func)();
snprintf(msg, 256, "Euler problem %u should have an answer of %" PRId64 ", but we actually got %" PRId64, key.id, (int64_t) key.answer, iresult);
TEST_ASSERT_EQUAL_INT64_MESSAGE(key.answer, iresult, msg);
break;
case UINT8:
case UINT16:
case UINT32:
case UINT64:
uresult = ((uint64_t (*)()) key.func)();
snprintf(msg, 256, "Euler problem %u should have an answer of %" PRIu64 ", but we actually got %" PRIu64, key.id, (uint64_t) key.answer, uresult);
TEST_ASSERT_EQUAL_UINT64_MESSAGE(key.answer, uresult, msg);
break;
case STR:
sresult = ((char *(*)()) key.func)();
snprintf(msg, 256, "Euler problem %u should have an answer of %s, but we actually got %s", key.id, (char *) key.answer, sresult);
TEST_ASSERT_EQUAL_STRING_MESSAGE(key.answer, sresult, msg);
}
free(msg);
}

Expand Down
2 changes: 1 addition & 1 deletion cplusplus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ lint:

.PHONY: native
native: ../LICENSE Unity/src/unity.c
@$(CXX) test.cpp Unity/src/unity.c -O2 -lm -Wall -Werror -std=c++11 -march=native -flto -DAMD_COMPILER=0 -Wno-deprecated
@$(CXX) test.cpp Unity/src/unity.c -O2 -g -lm -lstdc++ -Wall -Werror -std=gnu++98 -march=native -flto

.PHONY: clean
clean:
Expand Down
102 changes: 72 additions & 30 deletions cplusplus/test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#define UNITY_SUPPORT_TEST_CASES
#include <stdint.h>
#include <inttypes.h>
#include "Unity/src/unity.h"
#include "src/p0001.cpp"
#include "src/p0002.cpp"
Expand All @@ -20,40 +22,59 @@
#include "src/p0019.cpp"
#include "src/p0020.cpp"
#include "src/p0022.cpp"
// #include "src/p0025.cpp"
// #include "src/p0030.cpp"
#include "src/p0034.cpp"
#include "src/p0076.cpp"
#include "src/p0836.cpp"

typedef enum {
INT8,
INT16,
INT32,
INT64,
UINT8,
UINT16,
UINT32,
UINT64,
STR,
} AnswerType;

typedef struct {
uint32_t id;
uint64_t answer;
uint64_t (*func)();
AnswerType type;
void *answer;
void *(*func)();
} Answer;

#define ANSWERS_LEN (sizeof(answers) / sizeof(answers[0]))
static const Answer answers[] = {
{1, 233168, p0001},
{2, 4613732, p0002},
// {3, 6857, p0003},
{4, 906609, (uint64_t (*)()) p0004},
// {5, 232792560, p0005},
{6, 25164150, p0006},
// {7, 104743, p0007},
{8, 23514624000, p0008},
{9, 31875000, p0009},
// {10, 142913828922, p0010},
{11, 70600674, p0011},
{13, 5537376230, p0013},
{14, 837799, p0014},
{15, 137846528820, p0015},
{16, 1366, p0016},
{17, 21124, p0017},
{19, 171, (uint64_t (*)()) p0019},
{20, 648, p0020},
{22, 871198282, p0022},
{34, 40730, p0034},
{76, 190569291, (uint64_t (*)()) p0076},
const Answer answers[] = {
{ 1, INT32, (void *)233168, (void *(*)())p0001 },
{ 2, INT32, (void *)4613732, (void *(*)())p0002 },
// { 3, INT16, (void *)6857, (void *(*)())p0003 },
{ 4, INT32, (void *)906609, (void *(*)())p0004 },
// { 5, INT32, (void *)232792560, (void *(*)())p0005 },
{ 6, INT32, (void *)25164150, (void *(*)())p0006 },
// { 7, INT32, (void *)104743, (void *(*)())p0007 },
{ 8, INT64, (void *)23514624000, (void *(*)())p0008 },
{ 9, INT32, (void *)31875000, (void *(*)())p0009 },
// { 10, INT64, (void *)142913828922, (void *(*)())p0010 },
{ 11, INT32, (void *)70600674, (void *(*)())p0011 },
// { 12, INT32, (void *)76576500, (void *(*)())p0012 },
{ 13, INT64, (void *)5537376230, (void *(*)())p0013 },
{ 14, INT32, (void *)837799, (void *(*)())p0014 },
{ 15, INT64, (void *)137846528820, (void *(*)())p0015 },
{ 16, INT16, (void *)1366, (void *(*)())p0016 },
{ 17, INT16, (void *)21124, (void *(*)())p0017 },
{ 19, UINT8, (void *)171, (void *(*)())p0019 },
{ 20, INT16, (void *)648, (void *(*)())p0020 },
{ 22, INT32, (void *)871198282, (void *(*)())p0022 },
// { 25, INT16, (void *)4782, (void *(*)())p0025 },
// { 30, INT32, (void *)443839, (void *(*)())p0030 },
{ 34, UINT16, (void *)40730, (void *(*)())p0034 },
{ 76, INT32, (void *)190569291, (void *(*)())p0076 },
{ 836, STR, (void *)"aprilfoolsjoke", (void *(*)())p0836 },
};
const size_t ANSWERS_LEN = sizeof(answers) / sizeof(answers[0]);
static uint64_t current_index = 0;

void setUp(void) {}
Expand All @@ -62,11 +83,32 @@ void tearDown(void) {}

void test_euler_answer() {
Answer key = answers[current_index];
uint64_t result = key.func();
char *msg = (char*)malloc(256 * sizeof(char));
snprintf(msg, 256, "Euler problem %u should have an answer of %lu, but we actually got %lu", key.id, key.answer, result);
TEST_ASSERT_EQUAL_INT64_MESSAGE(key.answer, result, msg);
free(msg);
int64_t iresult;
uint64_t uresult;
std::string sresult;
std::ostringstream oss;
switch (key.type) {
case INT8:
case INT16:
case INT32:
case INT64:
iresult = ((int64_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << (int64_t) key.answer << ", but we actually got " << iresult;
TEST_ASSERT_EQUAL_INT64_MESSAGE(key.answer, iresult, oss.str().c_str());
break;
case UINT8:
case UINT16:
case UINT32:
case UINT64:
uresult = ((uint64_t (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << (uint64_t) key.answer << ", but we actually got " << uresult;
TEST_ASSERT_EQUAL_UINT64_MESSAGE(key.answer, uresult, oss.str().c_str());
break;
case STR:
sresult = ((std::string (*)()) key.func)();
oss << "Euler problem " << key.id << " should have an answer of " << (char *) key.answer << ", but we actually got " << sresult;
TEST_ASSERT_EQUAL_STRING_MESSAGE((const char*) key.answer, sresult.c_str(), oss.str().c_str());
}
}

int main(int argc, char const *argv[]) {
Expand Down

0 comments on commit f5955f1

Please sign in to comment.