chore: restructure project directory layout for improved modularity
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
class TestCase {
|
||||
char* name = null;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
int verbose = 0;
|
||||
int failfast = 0;
|
||||
|
||||
void init(int this, char* test_name) {
|
||||
this.name = test_name;
|
||||
this.passed = 0;
|
||||
this.failed = 0;
|
||||
this.verbose = 0;
|
||||
this.failfast = 0;
|
||||
}
|
||||
|
||||
void setUp(int this) {
|
||||
|
||||
}
|
||||
|
||||
void tearDown(int this) {
|
||||
|
||||
}
|
||||
|
||||
void assertEqual(int this, int actual, int expected, char* msg) {
|
||||
if (actual == expected) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertNotEqual(int this, int actual, int expected, char* msg) {
|
||||
if (actual != expected) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected not equal to %d)\n", msg, expected);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertTrue(int this, int value, char* msg) {
|
||||
if (value != 0) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected true, got false)\n", msg);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertFalse(int this, int value, char* msg) {
|
||||
if (value == 0) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected false, got true)\n", msg);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertGreater(int this, int actual, int threshold, char* msg) {
|
||||
if (actual > threshold) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected > %d, got %d)\n", msg, threshold, actual);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertLess(int this, int actual, int threshold, char* msg) {
|
||||
if (actual < threshold) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected < %d, got %d)\n", msg, threshold, actual);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertGreaterEqual(int this, int actual, int threshold, char* msg) {
|
||||
if (actual >= threshold) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected >= %d, got %d)\n", msg, threshold, actual);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertLessEqual(int this, int actual, int threshold, char* msg) {
|
||||
if (actual <= threshold) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected <= %d, got %d)\n", msg, threshold, actual);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertStringEqual(int this, char* actual, char* expected, char* msg) {
|
||||
if (strcmp(actual, expected) == 0) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected '%s', got '%s')\n", msg, expected, actual);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertStringContains(int this, char* haystack, char* needle, char* msg) {
|
||||
if (strstr(haystack, needle) != null) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected '%s' to contain '%s')\n", msg, haystack, needle);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertNull(int this, int value, char* msg) {
|
||||
if (value == null) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected null, got %d)\n", msg, value);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertNotNull(int this, int value, char* msg) {
|
||||
if (value != null) {
|
||||
this.passed = this.passed + 1;
|
||||
if (this.verbose == 1) {
|
||||
printf(" PASS: %s\n", msg);
|
||||
}
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected not null)\n", msg);
|
||||
if (this.failfast == 1) {
|
||||
printf("\nFailing fast due to -f flag\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestRunner {
|
||||
int total_passed = 0;
|
||||
int total_failed = 0;
|
||||
int verbose = 0;
|
||||
int quiet = 0;
|
||||
int failfast = 0;
|
||||
|
||||
void init(int this, int verbosity_mode) {
|
||||
this.total_passed = 0;
|
||||
this.total_failed = 0;
|
||||
|
||||
if (verbosity_mode == 1) {
|
||||
this.verbose = 1;
|
||||
this.quiet = 0;
|
||||
} else if (verbosity_mode == 2) {
|
||||
this.verbose = 0;
|
||||
this.quiet = 1;
|
||||
} else if (verbosity_mode == 3) {
|
||||
this.verbose = 0;
|
||||
this.quiet = 0;
|
||||
this.failfast = 1;
|
||||
} else {
|
||||
this.verbose = 0;
|
||||
this.quiet = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void runTest(int this, int test_case, char* test_name) {
|
||||
int tc = test_case;
|
||||
|
||||
tc.verbose = this.verbose;
|
||||
tc.failfast = this.failfast;
|
||||
|
||||
if (this.quiet == 0) {
|
||||
printf("\n=== Running %s ===\n", test_name);
|
||||
}
|
||||
|
||||
this.total_passed = this.total_passed + tc.passed;
|
||||
this.total_failed = this.total_failed + tc.failed;
|
||||
|
||||
if (this.quiet == 0) {
|
||||
if (tc.failed == 0) {
|
||||
printf("OK (%d tests)\n", tc.passed);
|
||||
} else {
|
||||
printf("FAILED (passed=%d, failed=%d)\n", tc.passed, tc.failed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printSummary(int this) {
|
||||
printf("\n");
|
||||
printf("======================\n");
|
||||
printf("Test Summary\n");
|
||||
printf("======================\n");
|
||||
printf("Total Passed: %d\n", this.total_passed);
|
||||
printf("Total Failed: %d\n", this.total_failed);
|
||||
printf("======================\n");
|
||||
|
||||
if (this.total_failed == 0) {
|
||||
printf("All tests PASSED!\n");
|
||||
} else {
|
||||
printf("Some tests FAILED\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user