diff options
| author | WormHeamer | 2025-12-29 03:47:03 -0500 |
|---|---|---|
| committer | WormHeamer | 2025-12-29 03:47:03 -0500 |
| commit | ae29fc07f0eae5fba0cfc6e028afb6025c0065bc (patch) | |
| tree | f3d64d83c699434d73769f9f19ff8ececd8effe3 /main.c | |
| parent | 4c5442ccd9428c4bb4e73939d3eff50911b574c0 (diff) | |
try and prevent 0-length splits occurring
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 57 |
1 files changed, 41 insertions, 16 deletions
@@ -143,16 +143,6 @@ int shell_replace(TxtLoc start, TxtLoc end, const char *cmd) { int p = popen2(cmd, &in, &out); if (p < 0) return -1; - TxtLoc l = start; - while (txt_before(l, end)) { - char c = txt_byte(l); - if (write(in, &c, 1) != 1) { - close(in); - close(out); - return -1; - } - l = bnext(l); - } txt_write_range(in, start, end); close(in); @@ -336,15 +326,48 @@ loop: return 1; } +#include <time.h> + +void test_edits(void) { + for (u32 iter = 0; iter < 8192; iter++) { + ASSERT(iter != 385); + u32 n = rand() % 100; + cur = txt_at(&txt, rand() % txt.len); + if (rand() & 1) { + cur = txt_delete(cur, n); + } else { + for (u32 j = 0; j < n; j++) { + const char ch[] = { + 'a','b','c','d','e','f','g','h','j','k','l','i', + 'm','o','p','q','r','s','t','u','v','w','x','y','z','\n', + 'A','B','C','D','E','F','G','H','J','K','L','I', + 'M','O','P','Q','R','S','T','U','V','W','X','Y','Z', + }; + cur = txt_insert_c(cur, ch[rand() % COUNTOF(ch)]); + } + } + if (txt.ptbl.n > 1) { + for (u32 i = 0; i < txt.ptbl.n; i++) { + ASSERT(txt.ptbl.v[i].n > 0); + } + } + } +} + int main(int argc, const char **argv) { scratch = arena_init(1L << 20); vui_init(); vui_curs_vis(1); vui_redraw_fn(draw); const char *path = "test.txt"; + //const char *path = "/usr/share/dict/words"; if (argc > 1) path = argv[1]; if (txt_load(&txt, path)) err(1, "couldn't open file"); cur = txt_end(&txt); + + //srand(clock()); + srand(123); + for (;;) { scratch.beg = scratch.start; draw(NULL); @@ -464,17 +487,14 @@ int main(int argc, const char **argv) { break; case '\r': { ins_newline:; - /* u32 tabs = 0; TxtLoc start = start_of_line(cur); for (TxtLoc l = start; txt_byte(l) == '\t'; l = bnext(l)) tabs++; - while (txt_byte(txt_at(&txt, cur - 1)) == '\t') { - cur = txt_ofs(txt_delete(cur, 1)); - } - */ + while (txt_byte(cprev(cur)) == '\t') + cur = txt_delete(cur, 1); cur = txt_insert_c(cur, '\n'); - //while (tabs--) cur = txt_ofs(txt_insert_c(cur, '\t')); + while (tabs--) cur = txt_insert_c(cur, '\t'); } break; default: @@ -485,6 +505,11 @@ ins_newline:; } if (mode == 1) count = 0; ASSERT(txt_valid_loc(cur)); + if (txt.ptbl.n > 1) { + for (u32 i = 0; i < txt.ptbl.n; i++) { + ASSERT(txt.ptbl.v[i].n > 0); + } + } } brk: vui_fini(); |
