vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
buffer-test.c
(2696B)
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "tap.h"
7 #include "buffer.c"
8
9 static bool compare(Buffer *buf, const char *data, size_t len) {
10 return buf->len == len && (len == 0 || memcmp(buf->data, data, buf->len) == 0);
11 }
12
13 static bool compare0(Buffer *buf, const char *data) {
14 return buf->len == strlen(data)+1 && memcmp(buf->data, data, buf->len) == 0;
15 }
16
17 int main(int argc, char *argv[]) {
18 Buffer buf = {0};
19
20 plan_no_plan();
21
22 ok(buf.data == NULL && buf.len == 0 && buf.size == 0, "Initialization");
23 ok(buffer_insert(&buf, 0, "foo", 0) && buf.data == NULL &&
24 buf.len == 0 && buf.size == 0, "Insert zero length data");
25 ok(!buffer_insert0(&buf, 1, "foo"), "Insert string at invalid position");
26
27 ok(buffer_insert0(&buf, 0, "") && compare0(&buf, ""), "Insert empty string");
28 ok(buffer_insert0(&buf, 0, "foo") && compare0(&buf, "foo"), "Insert string at start");
29 ok(buffer_insert0(&buf, 1, "l") && compare0(&buf, "floo"), "Insert string in middle");
30 ok(buffer_insert0(&buf, 4, "r") && compare0(&buf, "floor"), "Insert string at end");
31
32 ok(buffer_put0(&buf, "") && compare0(&buf, ""), "Put empty string");
33 ok(buffer_put0(&buf, "bar") && compare0(&buf, "bar"), "Put string");
34
35 ok(buffer_append0(&buf, "baz") && compare0(&buf, "barbaz"), "Append string");
36
37 buffer_release(&buf);
38 ok(buf.data == NULL && buf.len == 0 && buf.size == 0, "Release");
39
40 ok(buffer_insert(&buf, 0, "foo", 0) && compare(&buf, "", 0), "Insert zero length data");
41 ok(buffer_insert(&buf, 0, "foo", 3) && compare(&buf, "foo", 3), "Insert data at start");
42 ok(buffer_insert(&buf, 1, "l", 1) && compare(&buf, "floo", 4), "Insert data in middle");
43 ok(buffer_insert(&buf, 4, "r", 1) && compare(&buf, "floor", 5), "Insert data at end");
44
45 size_t cap = buf.size;
46 buf.len = 0;
47 ok(buf.data && buf.len == 0 && buf.size == cap, "Clear");
48
49 ok(buffer_put(&buf, "foo", 0) && compare(&buf, "", 0), "Put zero length data");
50 ok(buffer_put(&buf, "bar", 3) && compare(&buf, "bar", 3), "Put data");
51
52 ok(buffer_append(&buf, "\0baz", 4) && compare(&buf, "bar\0baz", 7), "Append data");
53
54 ok(buffer_grow(&buf, cap+1) && compare(&buf, "bar\0baz", 7) && buf.size >= cap+1, "Grow");
55 buf.len = 0;
56
57 skip_if(TIS_INTERPRETER, 1, "vsnprintf not supported") {
58 bool append = true;
59 for (int i = 1; i <= 10; i++)
60 append &= buffer_appendf(&buf, "%d", i);
61 ok(append && compare0(&buf, "12345678910"), "Append formatted");
62 buf.len = 0;
63
64 append = true;
65 for (int i = 1; i <= 10; i++)
66 append &= buffer_appendf(&buf, "%s", "");
67 ok(append && compare0(&buf, ""), "Append formatted empty string");
68 buf.len = 0;
69 }
70
71 buffer_release(&buf);
72
73 return exit_status();
74 }