vtgl

terminal emulator implemented in OpenGL
git clone anongit@rnpnr.xyz:vtgl.git
Log | Files | Refs | Feed | LICENSE

Commit: 78d8a7d85407d7efc803a8e2598424ae6e76b37c
Parent: 57fc3d43c1091cb2a41d98f9def6765a6c8eee6f
Author: Randy Palamar
Date:   Wed, 22 Jan 2025 21:55:09 -0700

test: completely recreate the term for each test

This eliminates any possiblitity of things accidently passing due
to operations in previous tests.

If I ever reach a large number of tests doing this makes it
trivial to run them in parallel for a massive speedup.

Diffstat:
Mplatform_linux_aarch64.c | 1+
Mplatform_linux_amd64.c | 1+
Mplatform_linux_common.c | 8+++++++-
Mplatform_linux_x11.c | 8++++----
Mtests/test.c | 71+++++++++++++++++++++++++++++++++--------------------------------------
5 files changed, 46 insertions(+), 43 deletions(-)

diff --git a/platform_linux_aarch64.c b/platform_linux_aarch64.c @@ -26,6 +26,7 @@ #define SYS_clock_gettime 113 #define SYS_setsid 157 #define SYS_prctl 167 +#define SYS_munmap 215 #define SYS_clone 220 #define SYS_execve 221 #define SYS_mmap 222 diff --git a/platform_linux_amd64.c b/platform_linux_amd64.c @@ -12,6 +12,7 @@ #define SYS_write 1 #define SYS_close 3 #define SYS_mmap 9 +#define SYS_munmap 11 #define SYS_ioctl 16 #define SYS_pwrite64 18 #define SYS_madvise 28 diff --git a/platform_linux_common.c b/platform_linux_common.c @@ -218,7 +218,7 @@ static PLATFORM_READ_FILE_FN(os_read_file) } static MemoryBlock -linux_block_alloc(size requested_size) +os_block_alloc(size requested_size) { MemoryBlock result = {0}; @@ -237,6 +237,12 @@ linux_block_alloc(size requested_size) return result; } +static void +os_release_memory_block(MemoryBlock memory) +{ + syscall2(SYS_munmap, (iptr)memory.memory, memory.size); +} + static f64 os_get_time(void) { diff --git a/platform_linux_x11.c b/platform_linux_x11.c @@ -366,7 +366,7 @@ linux_render_thread_entry(struct stack_base *stack) i32 main(i32 argc, char *argv[], char *envp[]) { - linux_ctx.platform_memory = arena_from_memory_block(linux_block_alloc(MB(2))); + linux_ctx.platform_memory = arena_from_memory_block(os_block_alloc(MB(2))); linux_ctx.error_stream = stream_alloc(&linux_ctx.platform_memory, KB(256)); iv2 cells = {.x = -1, .y = -1}; @@ -427,11 +427,11 @@ main(i32 argc, char *argv[], char *envp[]) } { - MemoryBlock terminal_memory = linux_block_alloc(MB(32)); + MemoryBlock terminal_memory = os_block_alloc(MB(32)); linux_ctx.memory.memory = terminal_memory.memory; linux_ctx.memory.memory_size = terminal_memory.size; #ifdef _DEBUG - MemoryBlock debug_memory = linux_block_alloc(MB(128)); + MemoryBlock debug_memory = os_block_alloc(MB(128)); linux_ctx.memory.debug_memory = debug_memory.memory; linux_ctx.memory.debug_memory_size = debug_memory.size; #endif @@ -501,7 +501,7 @@ main(i32 argc, char *argv[], char *envp[]) linux_ctx.render_stack->input = &linux_ctx.input; linux_ctx.render_stack->terminal_memory = &linux_ctx.memory; - linux_ctx.render_stack->thread_arena = arena_from_memory_block(linux_block_alloc(MB(8))); + linux_ctx.render_stack->thread_arena = arena_from_memory_block(os_block_alloc(MB(8))); linux_ctx.render_stack->window = linux_ctx.window; syscall3(SYS_futex, (iptr)&linux_ctx.render_stack->work_futex, FUTEX_WAKE, 1); diff --git a/tests/test.c b/tests/test.c @@ -64,20 +64,6 @@ static Test_Fn *tests[] = { static s8 failure_string = s8("\x1B[31mFAILURE\x1B[0m\n"); static s8 success_string = s8("\x1B[32mSUCCESS\x1B[0m\n"); -static void -buffer_reset(Term *t) -{ - t->views[0].log.widx = 0; - t->views[0].log.filled = 0; - t->views[1].log.widx = 0; - t->views[1].log.filled = 0; - t->unprocessed_bytes = 0; - t->views[0].lines.widx = 0; - t->views[0].lines.filled = 0; - t->views[1].lines.widx = 0; - t->views[1].lines.filled = 0; -} - static size copy_into_ringbuf(RingBuf *rb, s8 raw) { @@ -328,33 +314,39 @@ static TEST_FN(working_ringbuffer) return result; } -int -main(void) +static Term * +place_term_into_memory(MemoryBlock memory, i32 rows, i32 columns) { - Arena memory = arena_from_memory_block(linux_block_alloc(MB(32))); - Term term = {0}; - u32 failure_count = 0; + Term *t = memory.memory; + t->arena_for_frame = arena_from_memory_block(memory); + t->arena_for_frame.beg += sizeof(*t); + t->size = (iv2){.w = 80, .h = 24}; - Stream log = stream_alloc(&memory, MB(4)); - for (u32 i = 0; i < ARRAY_COUNT(term.saved_cursors); i++) { - cursor_reset(&term); - cursor_move_to(&term, 0, 0); - cursor_alt(&term, 1); - } - - os_allocate_ring_buffer(&term.views[0].log, BACKLOG_SIZE); - line_buf_alloc(&term.views[0].lines, &memory, term.views[0].log.buf, term.cursor.style, + os_allocate_ring_buffer(&t->views[0].log, MB(2)); + line_buf_alloc(&t->views[0].lines, &t->arena_for_frame, t->views[0].log.buf, t->cursor.style, BACKLOG_LINES); - os_allocate_ring_buffer(&term.views[1].log, ALT_BACKLOG_SIZE); - line_buf_alloc(&term.views[1].lines, &memory, term.views[1].log.buf, term.cursor.style, + + os_allocate_ring_buffer(&t->views[1].log, MB(2)); + line_buf_alloc(&t->views[1].lines, &t->arena_for_frame, t->views[1].log.buf, t->cursor.style, ALT_BACKLOG_LINES); + t->views[0].fb.backing_store = memory_block_from_arena(&t->arena_for_frame, MB(1)); + t->views[1].fb.backing_store = memory_block_from_arena(&t->arena_for_frame, MB(1)); + initialize_framebuffer(&t->views[0].fb, t->size); + initialize_framebuffer(&t->views[1].fb, t->size); + + term_reset(t); + + return t; +} + +int +main(void) +{ + Arena log_backing = arena_from_memory_block(os_block_alloc(MB(16))); + Stream log = arena_stream(log_backing); + /* TODO: should probably be some odd size */ - term.size = (iv2){.w = 80, .h = 24}; - term.views[0].fb.backing_store = memory_block_from_arena(&memory, MB(2)); - term.views[1].fb.backing_store = memory_block_from_arena(&memory, MB(2)); - initialize_framebuffer(&term.views[0].fb, term.size); - initialize_framebuffer(&term.views[1].fb, term.size); u32 max_name_len = 0; #define X(name) if (sizeof(#name) - 1 > max_name_len) max_name_len = sizeof(#name) - 1; @@ -362,10 +354,12 @@ main(void) #undef X max_name_len += 1; + u32 failure_count = 0; for (u32 i = 0; i < ARRAY_COUNT(tests); i++) { - buffer_reset(&term); - term_reset(&term); - struct test_result result = tests[i](&term, memory); + MemoryBlock term_backing = os_block_alloc(MB(4)); + /* TODO(rnp): term sizes as part of the tests */ + Term *term = place_term_into_memory(term_backing, 24, 80); + struct test_result result = tests[i](term, term->arena_for_frame); s8 fn = c_str_to_s8((char *)result.info); stream_push_s8(&log, fn); stream_push_s8(&log, s8(":")); @@ -377,6 +371,7 @@ main(void) } else { stream_push_s8(&log, success_string); } + os_release_memory_block(term_backing); } stream_push_s8(&log, s8("FINISHED: [")); stream_push_u64(&log, ARRAY_COUNT(tests) - failure_count);