|
6 | 6 | #include "smlr.h" |
7 | 7 | #include "smlr_internal.h" |
8 | 8 |
|
| 9 | +void dump_smlr(smlr_t* p_smlr) { |
| 10 | + if (!p_smlr) return; |
| 11 | + char buf[p_smlr->buf_bytes + 1]; |
| 12 | + memcpy(buf, p_smlr->p_buf, p_smlr->buf_bytes); |
| 13 | + buf[p_smlr->buf_bytes] = '\0'; |
| 14 | + printf("\tp_buf:\t\t%s\n", buf); |
| 15 | + printf("\tindex:\t\t%zu\n", p_smlr->index); |
| 16 | + printf("\tinput_bytes:\t%zu\n", p_smlr->input_bytes); |
| 17 | + printf("\tbuf_bytes:\t%zu\n", p_smlr->buf_bytes); |
| 18 | + printf("\tleft_bytes:\t%zu\n", p_smlr->left_bytes); |
| 19 | + printf("\tright_bytes:\t%zu\n", p_smlr->right_bytes); |
| 20 | + printf("\tbuf_base:\t%zu\n", p_smlr->buf_base); |
| 21 | + printf("\talign:\t\t%u\n", p_smlr->align); |
| 22 | +} |
| 23 | + |
9 | 24 | TEST(SmlrTest, valid_new_delete_test) { |
10 | 25 | smlr_t *p_smlr; |
11 | 26 | EXPECT_NE(nullptr, p_smlr = smlr_new(20, ALIGN_LEFT)); |
| 27 | + if (HasFailure()) dump_smlr(p_smlr); |
12 | 28 | EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 29 | + |
13 | 30 | } |
14 | 31 |
|
15 | 32 | TEST(SmlrTest, invalid_ptr_new_delete_test) { |
16 | 33 | smlr_t *p_smlr; |
17 | 34 | EXPECT_EQ(nullptr, p_smlr = smlr_new(0, ALIGN_LEFT)); |
| 35 | + if (HasFailure()) dump_smlr(p_smlr); |
18 | 36 | EXPECT_NE(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 37 | + |
19 | 38 | } |
20 | 39 |
|
21 | 40 | TEST(SmlrTest, invalid_alignment_new_delete_test) { |
22 | 41 | smlr_t *p_smlr; |
23 | 42 | EXPECT_EQ(nullptr, p_smlr = smlr_new(10, (smlr_alignment_t)10)); |
| 43 | + if (HasFailure()) dump_smlr(p_smlr); |
24 | 44 | EXPECT_NE(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 45 | + |
25 | 46 | } |
26 | 47 |
|
27 | 48 | TEST(SmlrTest, smlr_push) { |
28 | 49 | smlr_t *p_smlr; |
29 | | - size_t prefix_sz; |
| 50 | + uint8_t is_end = 0; |
30 | 51 | EXPECT_NE(nullptr, p_smlr = smlr_new(7, ALIGN_LEFT)); |
| 52 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '1', &is_end)); |
| 53 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '2', &is_end)); |
| 54 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '3', &is_end)); |
| 55 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '4', &is_end)); |
| 56 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '5', &is_end)); |
| 57 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '6', &is_end)); |
| 58 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '7', &is_end)); |
| 59 | + |
| 60 | + if (HasFailure()) dump_smlr(p_smlr); |
| 61 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +TEST(SmlrTest, smlr_push_RIGHT) { |
| 66 | + smlr_t *p_smlr; |
| 67 | + size_t buf_sz = 5; |
| 68 | + uint8_t is_end = 0; |
| 69 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_RIGHT)); |
| 70 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '1', &is_end)); |
| 71 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '2', &is_end)); |
| 72 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '3', &is_end)); |
| 73 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '4', &is_end)); |
| 74 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '5', &is_end)); |
| 75 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '6', &is_end)); |
| 76 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '7', &is_end)); |
| 77 | + char truth[] = "67345"; |
31 | 78 |
|
32 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '1')); |
33 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '2')); |
34 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '3')); |
35 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '4')); |
36 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '5')); |
37 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '6')); |
38 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '7')); |
| 79 | + EXPECT_EQ(0, memcmp(p_smlr->p_buf, truth, buf_sz)); |
39 | 80 |
|
| 81 | + if (HasFailure()) dump_smlr(p_smlr); |
40 | 82 | EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
41 | 83 | } |
42 | 84 |
|
43 | | -TEST(SmlrTest, smlr_push_wrap) { |
| 85 | +TEST(SmlrTest, smlr_push_MIDDLE) { |
44 | 86 | smlr_t *p_smlr; |
45 | | - size_t prefix_sz; |
46 | | - EXPECT_NE(nullptr, p_smlr = smlr_new(5, ALIGN_LEFT)); |
| 87 | + size_t buf_sz = 5; |
| 88 | + uint8_t is_end = 0; |
| 89 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_MIDDLE)); |
| 90 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '1', &is_end)); |
| 91 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '2', &is_end)); |
| 92 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '3', &is_end)); |
| 93 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '4', &is_end)); |
| 94 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '5', &is_end)); |
| 95 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '6', &is_end)); |
| 96 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '7', &is_end)); |
| 97 | + char truth[] = "16745"; |
| 98 | + |
| 99 | + EXPECT_EQ(0, memcmp(p_smlr->p_buf, truth, buf_sz)); |
| 100 | + |
| 101 | + if (HasFailure()) dump_smlr(p_smlr); |
| 102 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 103 | + |
| 104 | +} |
47 | 105 |
|
48 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '1')); |
49 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '2')); |
50 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '3')); |
51 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '4')); |
52 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '5')); |
53 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '6')); |
54 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '7')); |
| 106 | +TEST(SmlrTest, smlr_push_LEFT) { |
| 107 | + smlr_t *p_smlr; |
| 108 | + size_t buf_sz = 5; |
| 109 | + uint8_t is_end = 0; |
| 110 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_LEFT)); |
| 111 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '1', &is_end)); |
| 112 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '2', &is_end)); |
| 113 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '3', &is_end)); |
| 114 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '4', &is_end)); |
| 115 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '5', &is_end)); |
| 116 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '6', &is_end)); |
| 117 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, '7', &is_end)); |
| 118 | + char truth[] = "12345"; |
55 | 119 |
|
| 120 | + EXPECT_EQ(0, memcmp(p_smlr->p_buf, truth, buf_sz)); |
| 121 | + if (HasFailure()) dump_smlr(p_smlr); |
56 | 122 | EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 123 | + |
57 | 124 | } |
58 | 125 |
|
59 | 126 | TEST(SmlrTest, smlr_push_invalid) { |
| 127 | + uint8_t is_end = 0; |
| 128 | + EXPECT_NE(EXIT_SUCCESS, smlr_push(NULL, '1', &is_end)); |
| 129 | +} |
| 130 | + |
| 131 | +TEST(SmlrTest, smlr_push_eof) { |
| 132 | + smlr_t *p_smlr; |
| 133 | + uint8_t is_end = 0; |
| 134 | + EXPECT_NE(nullptr, p_smlr = smlr_new(5, ALIGN_LEFT)); |
| 135 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, EOF, &is_end)); |
| 136 | + EXPECT_NE(0, is_end); |
| 137 | + if (HasFailure()) dump_smlr(p_smlr); |
| 138 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +TEST(SmlrTest, smlr_push_not_eof) { |
60 | 143 | smlr_t *p_smlr; |
61 | | - size_t prefix_sz; |
| 144 | + uint8_t is_end = 0; |
62 | 145 | EXPECT_NE(nullptr, p_smlr = smlr_new(5, ALIGN_LEFT)); |
63 | | - EXPECT_NE(EXIT_SUCCESS, smlr_push(NULL, '1')); |
| 146 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'a', &is_end)); |
| 147 | + EXPECT_EQ(0, is_end); |
| 148 | + if (HasFailure()) dump_smlr(p_smlr); |
64 | 149 | EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 150 | + |
65 | 151 | } |
66 | 152 |
|
67 | 153 | TEST(SmlrTest, smlr_get_buf_left_nominal_short) { |
68 | 154 | smlr_t *p_smlr; |
69 | 155 | const size_t buf_sz = 10; |
| 156 | + uint8_t is_end = 0; |
70 | 157 | char buf[buf_sz]; |
71 | | - |
72 | 158 | EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_LEFT)); |
73 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'a')); |
74 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'b')); |
75 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'c')); |
76 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'd')); |
77 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'e')); |
78 | | - EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'f')); |
| 159 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'a', &is_end)); |
| 160 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'b', &is_end)); |
| 161 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'c', &is_end)); |
| 162 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'd', &is_end)); |
| 163 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'e', &is_end)); |
| 164 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'f', &is_end)); |
79 | 165 | EXPECT_EQ(EXIT_SUCCESS, smlr_get_buf(p_smlr, buf, buf_sz)); |
80 | 166 |
|
81 | | - EXPECT_EQ(0, strcmp(buf, "abcdef")); |
| 167 | + EXPECT_STREQ(buf, "abcdef"); |
82 | 168 |
|
| 169 | + if (HasFailure()) dump_smlr(p_smlr); |
83 | 170 | EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +TEST(SmlrTest, smlr_internals_5) { |
| 175 | + smlr_t *p_smlr; |
| 176 | + size_t buf_sz = 5; |
| 177 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_MIDDLE)); |
| 178 | + |
| 179 | + EXPECT_EQ(0, p_smlr->index); |
| 180 | + EXPECT_EQ(0, p_smlr->input_bytes); |
| 181 | + EXPECT_EQ(5, p_smlr->buf_bytes); |
| 182 | + EXPECT_EQ(1, p_smlr->left_bytes); |
| 183 | + EXPECT_EQ(1, p_smlr->right_bytes); |
| 184 | + EXPECT_EQ(0, p_smlr->buf_base); |
| 185 | + |
| 186 | + if (HasFailure()) dump_smlr(p_smlr); |
| 187 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 188 | + |
| 189 | +} |
| 190 | + |
| 191 | +TEST(SmlrTest, smlr_internals_10) { |
| 192 | + smlr_t *p_smlr; |
| 193 | + size_t buf_sz = 10; |
| 194 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_MIDDLE)); |
| 195 | + |
| 196 | + EXPECT_EQ(0, p_smlr->index); |
| 197 | + EXPECT_EQ(0, p_smlr->input_bytes); |
| 198 | + EXPECT_EQ(10, p_smlr->buf_bytes); |
| 199 | + EXPECT_EQ(4, p_smlr->left_bytes); |
| 200 | + EXPECT_EQ(3, p_smlr->right_bytes); |
| 201 | + EXPECT_EQ(0, p_smlr->buf_base); |
| 202 | + |
| 203 | + if (HasFailure()) dump_smlr(p_smlr); |
| 204 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 205 | + |
| 206 | +} |
| 207 | + |
| 208 | +TEST(SmlrTest, smlr_left_alphabet) { |
| 209 | + smlr_t *p_smlr; |
| 210 | + const size_t buf_sz = 12; |
| 211 | + uint8_t is_end = 0; |
| 212 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_MIDDLE)); |
| 213 | + |
| 214 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'a', &is_end)); |
| 215 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'b', &is_end)); |
| 216 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'c', &is_end)); |
| 217 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'd', &is_end)); |
| 218 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'e', &is_end)); |
| 219 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'f', &is_end)); |
| 220 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'g', &is_end)); |
| 221 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'h', &is_end)); |
| 222 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'i', &is_end)); |
| 223 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'j', &is_end)); |
| 224 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'k', &is_end)); |
| 225 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'l', &is_end)); |
| 226 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, EOF, &is_end)); |
| 227 | + |
| 228 | + EXPECT_EQ(12, p_smlr->index); |
| 229 | + EXPECT_EQ(12, p_smlr->input_bytes); |
| 230 | + EXPECT_EQ(12, p_smlr->buf_bytes); |
| 231 | + EXPECT_EQ(5, p_smlr->left_bytes); |
| 232 | + EXPECT_EQ(4, p_smlr->right_bytes); |
| 233 | + EXPECT_EQ(0, p_smlr->buf_base); |
| 234 | + EXPECT_NE(0, is_end); |
| 235 | + |
| 236 | + char buf_true[buf_sz+1] = "abcdefghijkl"; |
| 237 | + char buf_out[buf_sz+1]; |
| 238 | + EXPECT_EQ(EXIT_SUCCESS, smlr_get_buf(p_smlr, buf_out, buf_sz+1)); |
| 239 | + EXPECT_STREQ(buf_true, buf_out); |
| 240 | + |
| 241 | + if (HasFailure()) dump_smlr(p_smlr); |
| 242 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 243 | + |
| 244 | +} |
| 245 | + |
| 246 | +TEST(SmlrTest, smlr_left_alphabet_trunc) { |
| 247 | + smlr_t *p_smlr; |
| 248 | + const size_t buf_sz = 11; |
| 249 | + uint8_t is_end = 0; |
| 250 | + EXPECT_NE(nullptr, p_smlr = smlr_new(buf_sz, ALIGN_MIDDLE)); |
| 251 | + |
| 252 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'a', &is_end)); |
| 253 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'b', &is_end)); |
| 254 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'c', &is_end)); |
| 255 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'd', &is_end)); |
| 256 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'e', &is_end)); |
| 257 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'f', &is_end)); |
| 258 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'g', &is_end)); |
| 259 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'h', &is_end)); |
| 260 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'i', &is_end)); |
| 261 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'j', &is_end)); |
| 262 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'k', &is_end)); |
| 263 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, 'l', &is_end)); |
| 264 | + EXPECT_EQ(EXIT_SUCCESS, smlr_push(p_smlr, EOF, &is_end)); |
| 265 | + |
| 266 | + EXPECT_EQ(5, p_smlr->index); |
| 267 | + EXPECT_EQ(12, p_smlr->input_bytes); |
| 268 | + EXPECT_EQ(11, p_smlr->buf_bytes); |
| 269 | + EXPECT_EQ(4, p_smlr->left_bytes); |
| 270 | + EXPECT_EQ(4, p_smlr->right_bytes); |
| 271 | + EXPECT_EQ(4, p_smlr->buf_base); |
| 272 | + EXPECT_NE(0, is_end); |
| 273 | + |
| 274 | + char buf_true[buf_sz+1] = "abcd...ijkl"; |
| 275 | + char buf_out[buf_sz+1]; |
| 276 | + EXPECT_EQ(EXIT_SUCCESS, smlr_get_buf(p_smlr, buf_out, buf_sz+1)); |
| 277 | + EXPECT_STREQ(buf_true, buf_out); |
| 278 | + |
| 279 | + if (HasFailure()) dump_smlr(p_smlr); |
| 280 | + EXPECT_EQ(EXIT_SUCCESS, smlr_delete(p_smlr)); |
| 281 | + |
84 | 282 | } |
0 commit comments