summaryrefslogtreecommitdiff
path: root/tests/getc.c
diff options
context:
space:
mode:
authorDavid Moc <personal@cdatgoose.org>2026-03-08 15:02:30 +0100
committerDavid Moc <personal@cdatgoose.org>2026-03-08 15:02:30 +0100
commitad035ac5d942c6448a6a0464b995c2868a8378db (patch)
tree9c2c779d544a45b4234a6a3c311aa7b612ea975e /tests/getc.c
parent0385817bb1301a778bb33f8405a435293b9f8905 (diff)
Gosh.\nFixed bugs in offsetting. Added syscalls. Cleaned up the previous commenting (used to pass the project thu claude to add comments but LLMs are dumb). Removed the LLM made test runner cuz fuck AI.HEADmaster
Diffstat (limited to 'tests/getc.c')
-rw-r--r--tests/getc.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/tests/getc.c b/tests/getc.c
new file mode 100644
index 0000000..ef3b860
--- /dev/null
+++ b/tests/getc.c
@@ -0,0 +1,131 @@
+int g_passed = 0;
+int g_failed = 0;
+
+int getc() {
+ char c;
+ int n = syscall(0, 0, &c, 1);
+ if (n <= 0) return -1;
+ return (int)c;
+}
+
+int write_char(int c) {
+ char b;
+ b = (char)c;
+ syscall(1, 1, &b, 1);
+ return 1;
+}
+
+int write_str(char *s) {
+ int n = 0;
+ while (s[n] != 0) n++;
+ syscall(1, 1, s, n);
+ return n;
+}
+
+int write_int(int n) {
+ char tmp[24];
+ int len = 0;
+ int neg = n < 0;
+ if (neg) { write_char('-'); n = -n; }
+ if (n == 0) { write_char('0'); return 1; }
+ while (n > 0) { tmp[len] = (char)('0' + n % 10); n = n / 10; len++; }
+ int i = len - 1;
+ while (i >= 0) { write_char(tmp[i]); i--; }
+ return neg + len;
+}
+
+int check(char *name, int got, int want) {
+ if (got == want) {
+ write_str("PASS "); write_str(name); write_char('\n');
+ g_passed++;
+ return 1;
+ }
+ write_str("FAIL "); write_str(name);
+ write_str(": got "); write_int(got);
+ write_str(" want "); write_int(want);
+ write_char('\n');
+ g_failed++;
+ return 0;
+}
+
+int read_line(char *buf, int cap) {
+ int i = 0;
+ int c;
+ while (i < cap - 1) {
+ c = getc();
+ if (c == -1 || c == '\n') break;
+ buf[i] = (char)c;
+ i++;
+ }
+ buf[i] = 0;
+ return i;
+}
+
+int test1() {
+ int c = getc();
+ int result = c;
+ while (c != '\n' && c != -1) c = getc();
+ return check("single_char_is_65", result, 65);
+}
+
+int test2() {
+ char buf[64];
+ int n = read_line(buf, 64);
+ write_str(buf); write_char('\n');
+ return check("readline_length", n, 5);
+}
+
+int test3() {
+ int newlines = 0;
+ int c;
+ while (newlines < 2) {
+ c = getc();
+ if (c == -1) break;
+ if (c == '\n') newlines++;
+ }
+ return check("count_lines", newlines, 2);
+}
+
+int test4() {
+ int value = 0;
+ int c;
+ while (1) {
+ c = getc();
+ if (c < '0' || c > '9') break;
+ value = value * 10 + (c - '0');
+ }
+ return check("parse_int", value, 42);
+}
+
+int test5() {
+ char buf[64];
+ int n = read_line(buf, 64);
+ int i = 0;
+ while (i < n) {
+ char ch = buf[i];
+ if (ch >= 'a' && ch <= 'z') buf[i] = ch - 'a' + 'A';
+ i++;
+ }
+ write_str(buf); write_char('\n');
+ return check("toupper_length", n, 5);
+}
+
+int test6() {
+ int c = getc();
+ return check("eof_is_minus1", c, -1);
+}
+
+int main() {
+ test1();
+ test2();
+ test3();
+ test4();
+ test5();
+ test6();
+
+ write_char('\n');
+ write_int(g_passed); write_str(" passed, ");
+ write_int(g_failed); write_str(" failed\n");
+
+ return g_failed == 0 ? 0 : 1;
+}