repos / gbc

GBC - Go B Compiler
git clone https://github.com/xplshn/gbc.git

gbc / tests
xplshn  ·  2025-09-10

escapes.bx

Bx
  1extrn printf, sprintf;
  2
  3main() {
  4    byte buffer[200];
  5    auto passed = 0;
  6    auto total = 0;
  7
  8    printf("Escape sequence tests\n");
  9
 10    printf("\nBasic escapes:\n");
 11    total++;
 12    sprintf(buffer, "newline:\n tab:\t backslash:\\ quote:\" apostrophe:'");
 13    if (buffer[8] == '\n' && buffer[14] == '\t' && buffer[26] == '\\' && buffer[34] == '"' && buffer[47] == '\'') {
 14        printf("PASS: Basic escapes\n");
 15        passed++;
 16    } else {
 17        printf("FAIL: Basic escapes\n");
 18    }
 19
 20    total++;
 21    sprintf(buffer, "null:\000test");
 22    if (buffer[5] == 0) {
 23        printf("PASS: Octal \\000 (NULL)\n");
 24        passed++;
 25    } else {
 26        printf("FAIL: Octal \\000\n");
 27    }
 28
 29    total++;
 30    sprintf(buffer, "esc:\033test");
 31    if (buffer[4] == 27) {
 32        printf("PASS: Octal \\033 (ESC)\n");
 33        passed++;
 34    } else {
 35        printf("FAIL: Octal \\033 (got %d, expected 27)\n", buffer[4]);
 36    }
 37
 38    total++;
 39    sprintf(buffer, "A:\101test");
 40    if (buffer[2] == 65) {
 41        printf("PASS: Octal \\101 (ASCII A)\n");
 42        passed++;
 43    } else {
 44        printf("FAIL: Octal \\101 (got %d, expected 65)\n", buffer[2]);
 45    }
 46
 47    total++;
 48    sprintf(buffer, "max:\377test");
 49    if (buffer[4] == 255) {
 50        printf("PASS: Octal \\377 (255)\n");
 51        passed++;
 52    } else {
 53        printf("FAIL: Octal \\377 (got %d, expected 255) - UTF-8 issue\n", buffer[4]);
 54    }
 55
 56    total++;
 57    sprintf(buffer, "bel:\007test");
 58    if (buffer[4] == 7) {
 59        printf("PASS: Go-style \\007 (BEL)\n");
 60        passed++;
 61    } else {
 62        printf("FAIL: Go-style \\007 (got %d, expected 7)\n", buffer[4]);
 63    }
 64
 65    total++;
 66    sprintf(buffer, "lf:\012test");
 67    if (buffer[3] == 10) {
 68        printf("PASS: Go-style \\012 (LF)\n");
 69        passed++;
 70    } else {
 71        printf("FAIL: Go-style \\012 (got %d, expected 10)\n", buffer[3]);
 72    }
 73
 74    printf("\nANSI color:\n");
 75    total++;
 76    sprintf(buffer, "\033[31mRED\033[0m");
 77    if (buffer[0] == 27 && buffer[1] == '[' && buffer[2] == '3' && buffer[3] == '1' && buffer[4] == 'm') {
 78        printf("PASS: ANSI color sequence: ");
 79        printf(buffer);
 80        printf("\n");
 81        passed++;
 82    } else {
 83        printf("FAIL: ANSI color sequence\n");
 84    }
 85
 86    total++;
 87    sprintf(buffer, "Line1\nLine2\t\033[32mGreen\033[0m\\\042End\042");
 88    if (buffer[5] == '\n' && buffer[11] == '\t' && buffer[12] == 27 && buffer[26] == '\\' && buffer[27] == '"') {
 89        printf("PASS: Mixed escapes\n");
 90        passed++;
 91    } else {
 92        printf("FAIL: Mixed escapes\n");
 93    }
 94
 95    total++;
 96    auto esc_char = '\033';
 97    auto null_char = '\000';
 98    if (esc_char == 27 && null_char == 0) {
 99        printf("PASS: Character literals with Go-style octal\n");
100        passed++;
101    } else {
102        printf("FAIL: Character literals with Go-style octal\n");
103    }
104
105    total++;
106    sprintf(buffer, "a\000b\000c\000\060d");
107    if (buffer[1] == 0 && buffer[3] == 0 && buffer[5] == 0 && buffer[6] == '0') {
108        printf("PASS: Go-style 3-digit octal\n");
109        passed++;
110    } else {
111        printf("FAIL: Go-style 3-digit octal\n");
112    }
113
114    total++;
115    sprintf(buffer, "\001\010\077\100\177");
116    if (buffer[0] == 1 && buffer[1] == 8 && buffer[2] == 63 && buffer[3] == 64 && buffer[4] == 127) {
117        printf("PASS: Boundary octal values\n");
118        passed++;
119    } else {
120        printf("FAIL: Boundary octal values\n");
121    }
122
123    printf("\nResults: %d/%d tests passed\n", passed, total);
124    if (passed == total) {
125        printf("All tests passed!\n");
126        return (0);
127    } else {
128        printf("Some tests failed.\n");
129        return (1);
130    }
131}