summaryrefslogtreecommitdiff
path: root/source/tests/input_buffer.test.c
blob: 1ec1ad4937c1cf747c1e0f47baf416f354a07fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "../kit/input_buffer.h"

#define KIT_TEST_FILE input_buffer
#include "../kit/test.h"

TEST("input buffer read once") {
  str_t          text = { .size = 3, .values = "foo" };
  is_handle_t    in   = IS_WRAP_STRING(text);
  input_buffer_t buf  = ib_wrap(in, NULL);

  ib_token_t tok = ib_read(ib_token(&buf), 3);

  REQUIRE_EQ(tok.status, KIT_OK);
  REQUIRE_EQ(tok.size, 3);
  REQUIRE(AR_EQUAL(text, ib_str(tok)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer read again") {
  str_t          text = { .size = 6, .values = "foobar" };
  str_t          foo  = { .size = 3, .values = "foo" };
  is_handle_t    in   = IS_WRAP_STRING(text);
  input_buffer_t buf  = ib_wrap(in, NULL);

  ib_token_t first  = ib_read(ib_token(&buf), 3);
  ib_token_t second = ib_read(ib_token(&buf), 3);

  REQUIRE(AR_EQUAL(foo, ib_str(first)));
  REQUIRE(AR_EQUAL(foo, ib_str(second)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer read twice") {
  str_t          text = { .size = 6, .values = "foobar" };
  str_t          foo  = { .size = 3, .values = "foo" };
  str_t          bar  = { .size = 3, .values = "bar" };
  is_handle_t    in   = IS_WRAP_STRING(text);
  input_buffer_t buf  = ib_wrap(in, NULL);

  ib_token_t first  = ib_read(ib_token(&buf), 3);
  ib_token_t second = ib_read(first, 3);

  REQUIRE(AR_EQUAL(foo, ib_str(first)));
  REQUIRE(AR_EQUAL(bar, ib_str(second)));

  ib_destroy(&buf);
  is_destroy(in);
}

static b8 is_integer_(str_t const data, void *_) {
  for (ptrdiff_t i = 0; i < data.size; i++)
    if (data.values[i] < '0' || data.values[i] > '9')
      return 0;
  return 1;
}

TEST("input buffer read integer once") {
  str_t          text = { .size = 9, .values = "31415 foo" };
  str_t          num  = { .size = 5, .values = "31415" };
  is_handle_t    in   = IS_WRAP_STRING(text);
  input_buffer_t buf  = ib_wrap(in, NULL);

  ib_token_t tok = ib_while(ib_token(&buf), is_integer_, NULL);

  REQUIRE_EQ(tok.status, KIT_OK);
  REQUIRE_EQ(tok.size, 5);
  REQUIRE(AR_EQUAL(num, ib_str(tok)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer read integer twice") {
  str_t          text  = { .size = 6, .values = "314 15" };
  str_t          num_0 = { .size = 3, .values = "314" };
  str_t          num_1 = { .size = 2, .values = "15" };
  is_handle_t    in    = IS_WRAP_STRING(text);
  input_buffer_t buf   = ib_wrap(in, NULL);

  ib_token_t first  = ib_while(ib_token(&buf), is_integer_, NULL);
  ib_token_t second = ib_read(first, 1);
  ib_token_t third  = ib_while(second, is_integer_, NULL);

  REQUIRE_EQ(third.status, KIT_OK);
  REQUIRE_EQ(first.size, 3);
  REQUIRE_EQ(third.size, 2);
  REQUIRE(AR_EQUAL(num_0, ib_str(first)));
  REQUIRE(AR_EQUAL(num_1, ib_str(third)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer any") {
  str_t          text   = SZ("01234bbdac");
  str_t          expect = SZ("01234");
  is_handle_t    in     = IS_WRAP_STRING(text);
  input_buffer_t buf    = ib_wrap(in, NULL);

  ib_token_t tok = ib_any(ib_token(&buf), SZ("01234"));

  REQUIRE_EQ(tok.status, KIT_OK);
  REQUIRE(AR_EQUAL(expect, ib_str(tok)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer none") {
  str_t          text   = SZ("01234bbdac");
  str_t          expect = SZ("01234");
  is_handle_t    in     = IS_WRAP_STRING(text);
  input_buffer_t buf    = ib_wrap(in, NULL);

  ib_token_t tok = ib_none(ib_token(&buf), SZ("abcd"));

  REQUIRE_EQ(tok.status, KIT_OK);
  REQUIRE(AR_EQUAL(expect, ib_str(tok)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer until") {
  str_t          text   = SZ("01234bbdac");
  str_t          expect = SZ("01234");
  is_handle_t    in     = IS_WRAP_STRING(text);
  input_buffer_t buf    = ib_wrap(in, NULL);

  ib_token_t tok = ib_none(ib_token(&buf), SZ("bbdac"));

  REQUIRE_EQ(tok.status, KIT_OK);
  REQUIRE(AR_EQUAL(expect, ib_str(tok)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer exact success") {
  str_t          text   = SZ("01234bbdac");
  str_t          expect = SZ("01234");
  is_handle_t    in     = IS_WRAP_STRING(text);
  input_buffer_t buf    = ib_wrap(in, NULL);

  ib_token_t tok = ib_exact(ib_token(&buf), SZ("01234"));

  REQUIRE_EQ(tok.status, KIT_OK);
  REQUIRE(AR_EQUAL(expect, ib_str(tok)));

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer exact fail") {
  str_t          text = SZ("01234bbdac");
  is_handle_t    in   = IS_WRAP_STRING(text);
  input_buffer_t buf  = ib_wrap(in, NULL);

  ib_token_t tok = ib_exact(ib_token(&buf), SZ("bbdac"));

  REQUIRE_EQ(tok.status, KIT_PARSING_FAILED);

  ib_destroy(&buf);
  is_destroy(in);
}

TEST("input buffer use after free") {
  str_t          text = SZ("foobarfoobar");
  is_handle_t    in   = IS_WRAP_STRING(text);
  input_buffer_t buf  = ib_wrap(in, NULL);

  ib_token_t first  = ib_exact(ib_token(&buf), SZ("foobar"));
  ib_token_t second = ib_exact(first, ib_str(first));

  REQUIRE_EQ(second.status, KIT_OK);

  ib_destroy(&buf);
  is_destroy(in);
}

#undef KIT_TEST_FILE