test-common.c (3096B)
1 /* See LICENSE for copyright details */ 2 #include "vtgl.h" 3 #include "config.h" 4 5 /* NOTE: stubs for stuff we aren't testing */ 6 static void get_gpu_glyph_index(Arena, void *, void *, u32, u32, u32, CachedGlyph **); 7 8 KEYBIND_FN(copy) { return 0; } 9 KEYBIND_FN(paste) { return 0; } 10 KEYBIND_FN(scroll) { return 0; } 11 KEYBIND_FN(zoom) { return 0; } 12 13 #include "font.c" 14 #include "terminal.c" 15 16 static PLATFORM_WRITE_FN(test_write) 17 { 18 /* NOTE(rnp): for testing the caller will provide a stream via the platform 19 * child handle. Then this function writes into it and the caller can compare 20 * with the expected results */ 21 Stream *s = (Stream *)file; 22 stream_push_s8(s, raw); 23 return !s->errors; 24 } 25 26 static PLATFORM_WINDOW_TITLE_FN(test_get_window_title) 27 { 28 ASSERT(buffer); 29 stream_push_s8(buffer, s8("test_title")); 30 } 31 32 static PLATFORM_WINDOW_TITLE_FN(test_set_window_title) 33 { 34 ASSERT(buffer); 35 stream_push_byte(buffer, 0); 36 } 37 38 static size 39 copy_into_ringbuf(RingBuf *rb, s8 raw) 40 { 41 ASSERT(raw.len < rb->cap); 42 for (size i = 0; i < raw.len; i++) 43 rb->buf[rb->widx + i] = raw.data[i]; 44 45 rb->widx += raw.len; 46 rb->filled += raw.len; 47 48 CLAMP(rb->filled, 0, rb->cap); 49 if (rb->widx >= rb->cap) 50 rb->widx -= rb->cap; 51 52 ASSERT(rb->filled >= 0); 53 ASSERT(rb->widx >= 0 && rb->widx < rb->cap); 54 return raw.len; 55 } 56 57 static s8 58 launder_static_string(Term *term, s8 static_str) 59 { 60 RingBuf *rb = &term->views[term->view_idx].log; 61 term->unprocessed_bytes += copy_into_ringbuf(rb, static_str); 62 s8 raw = { 63 .len = term->unprocessed_bytes, 64 .data = rb->buf + (rb->widx - term->unprocessed_bytes) 65 }; 66 return raw; 67 } 68 69 static Term * 70 place_term_into_memory(MemoryBlock memory, i32 rows, i32 columns) 71 { 72 Arena tmp = arena_from_memory_block(memory); 73 Term *t = push_struct(&tmp, Term); 74 t->size = (iv2){.w = 80, .h = 24}; 75 76 t->platform = push_struct(&tmp, typeof(*t->platform)); 77 t->platform->set_window_title = test_set_window_title; 78 t->platform->get_window_title = test_get_window_title; 79 t->platform->write = test_write; 80 81 t->arena_for_frame = tmp; 82 83 os_allocate_ring_buffer(&t->views[0].log, MB(2)); 84 line_buf_alloc(&t->views[0].lines, &t->arena_for_frame, t->views[0].log.buf, t->cursor.style, 85 BACKLOG_LINES); 86 87 os_allocate_ring_buffer(&t->views[1].log, MB(2)); 88 line_buf_alloc(&t->views[1].lines, &t->arena_for_frame, t->views[1].log.buf, t->cursor.style, 89 ALT_BACKLOG_LINES); 90 91 t->views[0].fb.backing_store = memory_block_from_arena(&t->arena_for_frame, MB(1)); 92 t->views[1].fb.backing_store = memory_block_from_arena(&t->arena_for_frame, MB(1)); 93 initialize_framebuffer(&t->views[0].fb, t->size); 94 initialize_framebuffer(&t->views[1].fb, t->size); 95 96 term_reset(t); 97 98 return t; 99 } 100 101 /* NOTE(rnp): these work better with ASAN for testing */ 102 /* TODO(rnp): add appropriate poison to normal os_block_allocator when compiling with ASAN */ 103 void *malloc(size_t); 104 void free(void *); 105 106 static void 107 release_term_memory(MemoryBlock backing) 108 { 109 Term *t = backing.memory; 110 os_release_ring_buffer(&t->views[0].log); 111 os_release_ring_buffer(&t->views[1].log); 112 free(backing.memory); 113 }