feat: add jest configuration and sample unit test for utils module

This commit is contained in:
2025-11-23 22:46:02 +00:00
parent 76ffc91546
commit b8b990785b
14 changed files with 954 additions and 93 deletions
+112 -93
View File
@@ -5,208 +5,227 @@ class TestCase {
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 init(int self, char* test_name) {
self.name = test_name;
self.passed = 0;
self.failed = 0;
self.verbose = 0;
self.failfast = 0;
}
void setUp(int this) {
void setUp(int self) {
}
void tearDown(int this) {
void tearDown(int self) {
}
void assertEqual(int this, int actual, int expected, char* msg) {
void assertEqual(int self, int actual, int expected, char* msg) {
if (actual == expected) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertNotEqual(int this, int actual, int expected, char* msg) {
void assertNotEqual(int self, int actual, int expected, char* msg) {
if (actual != expected) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected not equal to %d)\n", msg, expected);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertTrue(int this, int value, char* msg) {
void assertTrue(int self, int value, char* msg) {
if (value != 0) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected true, got false)\n", msg);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertFalse(int this, int value, char* msg) {
void assertFalse(int self, int value, char* msg) {
if (value == 0) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected false, got true)\n", msg);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertGreater(int this, int actual, int threshold, char* msg) {
void assertGreater(int self, int actual, int threshold, char* msg) {
if (actual > threshold) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected > %d, got %d)\n", msg, threshold, actual);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertLess(int this, int actual, int threshold, char* msg) {
void assertLess(int self, int actual, int threshold, char* msg) {
if (actual < threshold) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected < %d, got %d)\n", msg, threshold, actual);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertGreaterEqual(int this, int actual, int threshold, char* msg) {
void assertGreaterEqual(int self, int actual, int threshold, char* msg) {
if (actual >= threshold) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected >= %d, got %d)\n", msg, threshold, actual);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertLessEqual(int this, int actual, int threshold, char* msg) {
void assertLessEqual(int self, int actual, int threshold, char* msg) {
if (actual <= threshold) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected <= %d, got %d)\n", msg, threshold, actual);
if (this.failfast == 1) {
if (self.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) {
void assertStringEqual(int self, char* actual, char* expected, char* msg) {
int len_a = strlen(actual);
int len_b = strlen(expected);
int equal = 0;
if (len_a == len_b) {
equal = 1;
int i = 0;
while (i < len_a) {
char* char_a = actual[i:i+1];
char* char_b = expected[i:i+1];
int pos = strpos(char_a, char_b);
if (pos != 0) {
equal = 0;
}
i = i + 1;
}
}
if (equal == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected '%s', got '%s')\n", msg, expected, actual);
if (this.failfast == 1) {
if (self.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) {
void assertStringContains(int self, char* haystack, char* needle, char* msg) {
int pos = strpos(haystack, needle);
if (pos >= 0) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected '%s' to contain '%s')\n", msg, haystack, needle);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertNull(int this, int value, char* msg) {
void assertNull(int self, int value, char* msg) {
if (value == null) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected null, got %d)\n", msg, value);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
}
}
void assertNotNull(int this, int value, char* msg) {
void assertNotNull(int self, int value, char* msg) {
if (value != null) {
this.passed = this.passed + 1;
if (this.verbose == 1) {
self.passed = self.passed + 1;
if (self.verbose == 1) {
printf(" PASS: %s\n", msg);
}
} else {
this.failed = this.failed + 1;
self.failed = self.failed + 1;
printf(" FAIL: %s (expected not null)\n", msg);
if (this.failfast == 1) {
if (self.failfast == 1) {
printf("\nFailing fast due to -f flag\n");
return;
}
@@ -221,40 +240,40 @@ class TestRunner {
int quiet = 0;
int failfast = 0;
void init(int this, int verbosity_mode) {
this.total_passed = 0;
this.total_failed = 0;
void init(int self, int verbosity_mode) {
self.total_passed = 0;
self.total_failed = 0;
if (verbosity_mode == 1) {
this.verbose = 1;
this.quiet = 0;
self.verbose = 1;
self.quiet = 0;
} else if (verbosity_mode == 2) {
this.verbose = 0;
this.quiet = 1;
self.verbose = 0;
self.quiet = 1;
} else if (verbosity_mode == 3) {
this.verbose = 0;
this.quiet = 0;
this.failfast = 1;
self.verbose = 0;
self.quiet = 0;
self.failfast = 1;
} else {
this.verbose = 0;
this.quiet = 0;
self.verbose = 0;
self.quiet = 0;
}
}
void runTest(int this, int test_case, char* test_name) {
void runTest(int self, int test_case, char* test_name) {
int tc = test_case;
tc.verbose = this.verbose;
tc.failfast = this.failfast;
tc.verbose = self.verbose;
tc.failfast = self.failfast;
if (this.quiet == 0) {
if (self.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;
self.total_passed = self.total_passed + tc.passed;
self.total_failed = self.total_failed + tc.failed;
if (this.quiet == 0) {
if (self.quiet == 0) {
if (tc.failed == 0) {
printf("OK (%d tests)\n", tc.passed);
} else {
@@ -263,16 +282,16 @@ class TestRunner {
}
}
void printSummary(int this) {
void printSummary(int self) {
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("Total Passed: %d\n", self.total_passed);
printf("Total Failed: %d\n", self.total_failed);
printf("======================\n");
if (this.total_failed == 0) {
if (self.total_failed == 0) {
printf("All tests PASSED!\n");
} else {
printf("Some tests FAILED\n");