This commit is contained in:
Chu'vok
2024-09-13 13:45:43 +02:00
commit 4dfcf8fe85
9 changed files with 2039 additions and 0 deletions

30
test/color.c Normal file
View File

@@ -0,0 +1,30 @@
// Shows xterm-256 color palette.
#include "../tim.h"
static void foo(int x, int y, int c) {
char buf[16] = {0};
sprintf(buf, " %02x ", c);
draw_str(buf, x * 4, y, 4, 0, c);
}
int main(void) {
while (tim_run(0)) {
for (int i = 0; i < 16; i++) {
foo(i % 8, i / 8, i);
}
for (int i = 0; i < 108; i++) {
foo(i % 6, i / 6 + 3, i + 16);
}
for (int i = 0; i < 108; i++) {
foo(i % 6 + 7, i / 6 + 3, i + 124);
}
for (int i = 0; i < 24; i++) {
foo(i % 12, i / 12 + 22, i + 232);
}
if (is_key_press('q') || is_key_press(ESCAPE_KEY)) {
exit(1);
}
}
}

70
test/string.c Normal file
View File

@@ -0,0 +1,70 @@
// Test string functions.
#include "../tim.h"
#define U(s) (uint8_t*)(""s), (sizeof(s) - 1)
#define TEST(t) printf("\33[3%s\33[0m %s\n", (t) ? "2mpass" : "1mfail", #t)
int main(void) {
(void)tim_run;
TEST(ztrlen(NULL) == 0);
TEST(ztrlen("") == 0);
TEST(ztrlen("$") == 1);
TEST(ztrlen("£") == 2);
TEST(ztrlen("") == 3);
TEST(ztrlen("𐍈") == 4);
TEST(bsr8(128) == 0);
TEST(bsr8(64) == 1);
TEST(bsr8(1) == 7);
TEST(bsr8(0) == 8);
TEST(utfchr(NULL) == 0);
TEST(utfchr("") == 0);
TEST(utfchr("$") == 0x24);
TEST(utfchr("£") == 0xA3);
TEST(utfchr("И") == 0x418);
TEST(utfchr("") == 0x939);
TEST(utfchr("") == 0x20AC);
TEST(utfchr("") == 0xD55C);
TEST(utfchr("𐍈") == 0x10348);
TEST(utflen(NULL) == 0);
TEST(utflen("") == 0);
TEST(utflen("$") == 1);
TEST(utflen("$$") == 2);
TEST(utflen("") == 2);
TEST(utflen("$€𐍈") == 3);
TEST(utfpos(NULL, 0) == 0);
TEST(utfpos("äbc", 0) == 0);
TEST(utfpos("äbc", 1) == 2);
TEST(utfpos("äbc", 2) == 3);
TEST(utfpos("äbc", 9) == 4);
TEST(scan_str(NULL).lines == 0);
TEST(scan_str("").lines == 0);
TEST(scan_str("abc").lines == 1);
TEST(scan_str("a\no").lines == 2);
TEST(scan_str("a").width == 1);
TEST(scan_str("äß\no").width == 2);
struct line ln = {.str = "foo\nbar"};
TEST(next_line(&ln) == true);
TEST(!memcmp(ln.line, "foo", ln.size));
TEST(next_line(&ln) == true);
TEST(!memcmp(ln.line, "bar", ln.size));
TEST(next_line(&ln) == false);
TEST(is_wide_perhaps(NULL, 0) == false);
TEST(is_wide_perhaps(U("")) == false);
TEST(is_wide_perhaps(U("$")) == false);
TEST(is_wide_perhaps(U("£")) == false);
TEST(is_wide_perhaps(U("")) == false);
TEST(is_wide_perhaps(U("")) == true);
TEST(is_wide_perhaps(U("")) == false);
TEST(is_wide_perhaps(U("")) == true);
TEST(is_wide_perhaps(U("𐍈")) == true);
}

119
test/test.c Normal file
View File

@@ -0,0 +1,119 @@
#include "../tim.h"
static inline void test_screen(struct event* e) {
static struct event me;
static struct event ke;
static int render_us;
char buf[64];
ke = (e->type == KEY_EVENT) ? *e : ke;
me = (e->type == MOUSE_EVENT) ? *e : me;
// positioning
label("+", 0, 0, A, A, 0xf);
label("+", ~0, 0, A, A, 0xf);
label("+", 0, ~0, A, A, 0xf);
label("+", ~0, ~0, A, A, 0xf);
label("+", A, A, A, A, 0xf);
label("-", 0, A, A, A, 0xf);
label("-", ~0, A, A, A, 0xf);
label("|", A, 0, A, A, 0xf);
label("|", A, ~0, A, A, 0xf);
// some information
sprintf(buf, "screen: %dx%d", tim.w, tim.h);
label(buf, 2, 0, A, A, 0xf);
sprintf(buf, "frame : [%c] %d", ": "[tim.frame & 1], tim.frame);
label(buf, 2, 1, A, A, 0xf);
sprintf(buf, "key : [%d] %s", ke.key, ke.str + (ke.key < 32));
label(buf, 2, 2, A, A, 0xf);
sprintf(buf, "mouse : [%d] %d:%d", me.key, me.x, me.y);
label(buf, 2, 3, A, A, 0xf);
sprintf(buf, "input : %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
e->str[0], e->str[1], e->str[2], e->str[3], e->str[4], e->str[5], e->str[6], e->str[7]);
label(buf, 2, 4, A, A, 0xf);
// lower right
render_us += tim.render_us;
sprintf(buf, "%d µs (Ø %d µs)", tim.render_us, render_us / MAX(tim.frame, 1));
label(buf, ~2, ~2, A, A, 0xf);
sprintf(buf, "%d cells (%.0f %%)", tim.w * tim.h, 100.0 * tim.w * tim.h / MAX_CELLS);
label(buf, ~2, ~1, A, A, 0xf);
sprintf(buf, "%d bytes (%.0f %%)", tim.buf_size, 100.0 * tim.buf_size / MAX_BUF);
label(buf, ~2, ~0, A, A, 0xf);
// multi line label
label("multi\nliñe\nlabël", 24, 1, A, A, 0xf);
// colors
scope (1, 5, 16, 5) {
frame(0, 0, ~0, ~0, 0xf);
label(" Red ", 1, 1, 7, A, 0x0900);
label(" ", 8, 1, 7, A, 0xc400);
label(" Green ", 1, 2, 7, A, 0x0a00);
label(" ", 8, 2, 7, A, 0x2e00);
label(" Blue ", 1, 3, 7, A, 0x0c00);
label(" ", 8, 3, 7, A, 0x1500);
}
// button
static uint64_t bc = 0x100;
if (button("Click Me", 17, 5, 16, 5, bc)) {
bc = (bc + 0x100) & 0xff00;
}
// edit
static struct edit ed1 = {.str = "Edit 1"};
static struct edit ed2 = {};
edit(&ed1, 1, 10, 32, 0xff00ff);
sprintf(buf, "cursor: %d length: %d", ed1.cursor, ed1.length);
label(buf, 2, 13, A, A, 0xf);
edit(&ed2, 1, 14, 32, 0xff00ff);
label(ed2.str, 2, 17, A, A, 0xf);
// checkbox
static int chk[2] = {-1, 1};
check("Check 1", &chk[0], 1, 18, A, 0xa000f);
check("Check 2", &chk[1], 14, 18, A, 0xa000f);
// radiobox
static int rad = 0;
radio("Radio 1", &rad, 1, 1, 19, A, 0xa000f);
radio("Radio 2", &rad, 2, 14, 19, A, 0xa000f);
radio("Radio 3", &rad, 3, 1, 20, A, 0xa000f);
radio("Radio 4", &rad, 4, 14, 20, A, 0xa000f);
// scope nesting
scope(~1, 1, 20, 10) {
scope(0, 0, 10, 5) {
frame(0, 0, ~0, ~0, 0x9);
}
scope(~0, 0, 10, 5) {
frame(0, 0, ~0, ~0, 0xa);
}
scope(~0, ~0, 10, 5) {
frame(0, 0, ~0, ~0, 0xb);
}
scope(0, ~0, 10, 5) {
frame(0, 0, ~0, ~0, 0xc);
}
}
// funny characters
scope (~1, ~3, 11, 5) {
frame(0, 0, ~0, ~0, 0xf);
label("123456789", 1, 1, 9, A, 0x0f05);
label("$£ह€𐍈6789", 1, 2, A, A, 0x0f05);
label("圍棋56789", 1, 3, A, A, 0x0f05);
}
};
int main(void) {
while (tim_run(1.5)) {
test_screen(&tim.event);
if (is_key_press('q') || is_key_press(ESCAPE_KEY)) {
break;
}
}
}

69
test/width.c Normal file
View File

@@ -0,0 +1,69 @@
// Test character width.
#include <assert.h>
#include "../tim.h"
static int cp_to_utf8(int32_t cp, char* s) {
assert(cp > 0 && cp < 0x110000);
if (cp < 0x80) {
s[0] = cp;
return 1;
} else if (cp < 0x800) {
s[0] = (cp >> 6) | 0xc0;
s[1] = (cp & 0x3f) | 0x80;
return 2;
} else if (cp < 0x10000) {
s[0] = (cp >> 12) | 0xe0;
s[1] = ((cp >> 6) & 0x3f) | 0x80;
s[2] = (cp & 0x3f) | 0x80;
return 3;
} else {
s[0] = (cp >> 18) | 0xf0;
s[1] = ((cp >> 12) & 0x3f) | 0x80;
s[2] = ((cp >> 6) & 0x3f) | 0x80;
s[3] = (cp & 0x3f) | 0x80;
return 4;
}
return -1;
}
static int cursor_pos() {
write(STDOUT_FILENO, S("\33[6n"));
char buf[64] = {0};
int n = read(STDIN_FILENO, buf, 64);
if (n < 6 || buf[0] != '\33' || buf[n - 1] != 'R') {
return -1;
}
int r = atoi(buf + 2);
int c = atoi(buf + 4 + (r > 9));
return c;
}
int main(int argc, char** argv) {
assert(argc == 2);
(void)tim_run;
FILE* f = fopen(argv[1], "w");
assert(f);
init_terminal();
for (int i = 32; i < 0x110000; i++) {
write(STDOUT_FILENO, S("\33[0;0H"));
char buf[5] = {0};
int n = cp_to_utf8(i, buf);
write(STDOUT_FILENO, buf, n);
int w = cursor_pos() - 1;
if (w) {
fprintf(f, "u+%06x %d %s\n", i, w, buf);
} else {
fprintf(f, "u+%06x %d\n", i, w);
}
}
reset_terminal();
fclose(f);
}