chore: extract async, io, math, string, and constants into modular stdlib subdirectories
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
class TestCase {
|
||||
int counter = 0;
|
||||
|
||||
void report(int obj) {
|
||||
printf("Counter: %d\n", obj.counter);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
|
||||
printf("Initial value:\n");
|
||||
tc.report();
|
||||
|
||||
printf("Incrementing from main...\n");
|
||||
tc.counter = tc.counter + 1;
|
||||
|
||||
printf("After increment:\n");
|
||||
tc.report();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
include "tests/unittest.rc"
|
||||
|
||||
void test_basic_math(int tc) {
|
||||
tc.assertEqual(2 + 2, 4, "2 + 2 should equal 4");
|
||||
tc.assertEqual(10 - 5, 5, "10 - 5 should equal 5");
|
||||
tc.assertEqual(3 * 4, 12, "3 * 4 should equal 12");
|
||||
tc.assertEqual(20 / 4, 5, "20 / 4 should equal 5");
|
||||
}
|
||||
|
||||
void test_comparisons(int tc) {
|
||||
tc.assertTrue(5 > 3, "5 should be greater than 3");
|
||||
tc.assertFalse(2 > 10, "2 should not be greater than 10");
|
||||
tc.assertGreater(100, 50, "100 should be greater than 50");
|
||||
tc.assertLess(25, 30, "25 should be less than 30");
|
||||
}
|
||||
|
||||
void test_strings(int tc) {
|
||||
char* hello = "Hello";
|
||||
char* world = "World";
|
||||
|
||||
tc.assertStringEqual(hello, "Hello", "String should equal Hello");
|
||||
tc.assertStringContains("Hello World", "World", "Should contain World");
|
||||
}
|
||||
|
||||
void test_null_checks(int tc) {
|
||||
int obj = null;
|
||||
int obj2 = new TestCase();
|
||||
|
||||
tc.assertNull(obj, "Should be null");
|
||||
tc.assertNotNull(obj2, "Should not be null");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int runner = new TestRunner();
|
||||
runner.init(0);
|
||||
|
||||
int tc1 = new TestCase();
|
||||
tc1.init("Basic Math Tests");
|
||||
test_basic_math(tc1);
|
||||
runner.runTest(tc1, "Basic Math Tests");
|
||||
|
||||
int tc2 = new TestCase();
|
||||
tc2.init("Comparison Tests");
|
||||
test_comparisons(tc2);
|
||||
runner.runTest(tc2, "Comparison Tests");
|
||||
|
||||
int tc3 = new TestCase();
|
||||
tc3.init("String Tests");
|
||||
test_strings(tc3);
|
||||
runner.runTest(tc3, "String Tests");
|
||||
|
||||
int tc4 = new TestCase();
|
||||
tc4.init("Null Check Tests");
|
||||
test_null_checks(tc4);
|
||||
runner.runTest(tc4, "Null Check Tests");
|
||||
|
||||
runner.printSummary();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
class TestCase {
|
||||
char* name = null;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
void init(int obj, char* test_name) {
|
||||
obj.name = test_name;
|
||||
obj.passed = 0;
|
||||
obj.failed = 0;
|
||||
}
|
||||
|
||||
void assertEqual(int obj, int actual, int expected, char* msg) {
|
||||
if (actual == expected) {
|
||||
obj.passed = obj.passed + 1;
|
||||
printf(" PASS: %s\n", msg);
|
||||
} else {
|
||||
obj.failed = obj.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
printf("Created: %d\n", tc);
|
||||
|
||||
tc.init("Test1");
|
||||
tc.assertEqual(5, 5, "5 equals 5");
|
||||
|
||||
printf("Passed: %d, Failed: %d\n", tc.passed, tc.failed);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
class TestCase {
|
||||
int counter = 0;
|
||||
|
||||
void increment(int obj) {
|
||||
obj.counter = obj.counter + 1;
|
||||
}
|
||||
|
||||
void report(int obj) {
|
||||
printf("Counter: %d\n", obj.counter);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
|
||||
printf("Initial value:\n");
|
||||
tc.report();
|
||||
|
||||
printf("Incrementing...\n");
|
||||
tc.increment();
|
||||
|
||||
printf("After increment:\n");
|
||||
tc.report();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class TestCase {
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
void report(int obj) {
|
||||
printf("Passed: %d, Failed: %d\n", obj.passed, obj.failed);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
printf("Created: %d\n", tc);
|
||||
|
||||
printf("Calling report...\n");
|
||||
tc.report();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
include "tests/unittest.rc"
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase object...\n");
|
||||
int tc = new TestCase();
|
||||
printf("TestCase created: %d\n", tc);
|
||||
|
||||
printf("Initializing TestCase...\n");
|
||||
tc.init("Simple Test");
|
||||
printf("TestCase initialized\n");
|
||||
|
||||
printf("Running assertion...\n");
|
||||
tc.assertEqual(5, 5, "5 should equal 5");
|
||||
|
||||
printf("Passed: %d\n", tc.passed);
|
||||
printf("Failed: %d\n", tc.failed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
include "tests/unittest_simple.rc"
|
||||
|
||||
int main() {
|
||||
printf("Creating TestCase...\n");
|
||||
int tc = new TestCase();
|
||||
printf("Created: %d\n", tc);
|
||||
|
||||
tc.init("Test1");
|
||||
tc.assertEqual(5, 5, "5 equals 5");
|
||||
|
||||
printf("Passed: %d, Failed: %d\n", tc.passed, tc.failed);
|
||||
return 0;
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
class TestCase {
|
||||
char* name = null;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
void init(int this, char* test_name) {
|
||||
this.name = test_name;
|
||||
this.passed = 0;
|
||||
this.failed = 0;
|
||||
}
|
||||
|
||||
void assertEqual(int this, int actual, int expected, char* msg) {
|
||||
if (actual == expected) {
|
||||
this.passed = this.passed + 1;
|
||||
printf(" PASS: %s\n", msg);
|
||||
} else {
|
||||
this.failed = this.failed + 1;
|
||||
printf(" FAIL: %s (expected %d, got %d)\n", msg, expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user