| 1 |
/* |
| 2 |
* boxes - Command line filter to draw/remove ASCII boxes around text |
| 3 |
* Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public |
| 6 |
* License, version 3, as published by the Free Software Foundation. |
| 7 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 8 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 9 |
* details. |
| 10 |
* You should have received a copy of the GNU General Public License along with this program. |
| 11 |
* If not, see <https://www.gnu.org/licenses/>. |
| 12 |
* |
| 13 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 14 |
*/ |
| 15 |
|
| 16 |
/* |
| 17 |
* Unit tests of the 'remove' module |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include <setjmp.h> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <stddef.h> |
| 25 |
#include <unistr.h> |
| 26 |
|
| 27 |
#include <cmocka.h> |
| 28 |
|
| 29 |
#include "boxes.h" |
| 30 |
#include "unicode.h" |
| 31 |
#include "shape.h" |
| 32 |
#include "tools.h" |
| 33 |
#include "remove_test.h" |
| 34 |
#include "global_mock.h" |
| 35 |
#include "utest_tools.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
void test_match_outer_shape_null(void **state) |
| 40 |
{ |
| 41 |
UNUSED(state); |
| 42 |
|
| 43 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 44 |
bxstr_t *shape_line = bxs_from_ascii(" xx "); |
| 45 |
|
| 46 |
match_result_t *actual = match_outer_shape(BLEF, NULL, shape_line); |
| 47 |
assert_null(actual); |
| 48 |
|
| 49 |
actual = match_outer_shape(BLEF, input_line, NULL); |
| 50 |
assert_null(actual); |
| 51 |
|
| 52 |
bxs_free(input_line); |
| 53 |
bxs_free(shape_line); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
void test_match_outer_shape_left_anchored(void **state) |
| 59 |
{ |
| 60 |
UNUSED(state); |
| 61 |
|
| 62 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 63 |
bxstr_t *shape_line = bxs_from_ascii(" xx "); |
| 64 |
|
| 65 |
match_result_t *actual = match_outer_shape(BLEF, input_line, shape_line); |
| 66 |
uint32_t *expected_p = bxs_first_char_ptr(input_line, 3); |
| 67 |
assert_non_null(actual); |
| 68 |
assert_int_equal(3, (int) actual->p_idx); |
| 69 |
assert_int_equal(0, u32_strcmp(expected_p, actual->p)); |
| 70 |
assert_int_equal(4, actual->len); |
| 71 |
assert_int_equal(0, actual->shiftable); |
| 72 |
|
| 73 |
BFREE(actual); |
| 74 |
bxs_free(input_line); |
| 75 |
bxs_free(shape_line); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
void test_match_outer_shape_left_shiftable(void **state) |
| 81 |
{ |
| 82 |
UNUSED(state); |
| 83 |
|
| 84 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 85 |
bxstr_t *shape_line = bxs_from_ascii(" "); |
| 86 |
|
| 87 |
match_result_t *actual = match_outer_shape(BLEF, input_line, shape_line); |
| 88 |
assert_non_null(actual); |
| 89 |
assert_int_equal(0, (int) actual->p_idx); |
| 90 |
assert_int_equal(0, u32_strcmp(input_line->memory, actual->p)); |
| 91 |
assert_int_equal(0, actual->len); |
| 92 |
assert_int_equal(1, actual->shiftable); |
| 93 |
|
| 94 |
BFREE(actual); |
| 95 |
bxs_free(input_line); |
| 96 |
bxs_free(shape_line); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
void test_match_outer_shape_left_shortened(void **state) |
| 102 |
{ |
| 103 |
UNUSED(state); |
| 104 |
|
| 105 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 106 |
bxstr_t *shape_line = bxs_from_ascii(" xx"); /* length 6 */ |
| 107 |
|
| 108 |
match_result_t *actual = match_outer_shape(BLEF, input_line, shape_line); |
| 109 |
assert_non_null(actual); |
| 110 |
assert_int_equal(0, (int) actual->p_idx); |
| 111 |
assert_int_equal(0, u32_strcmp(input_line->memory, actual->p)); |
| 112 |
assert_int_equal(4, actual->len); /* only 4 characters matched */ |
| 113 |
assert_int_equal(0, actual->shiftable); |
| 114 |
|
| 115 |
BFREE(actual); |
| 116 |
bxs_free(input_line); |
| 117 |
bxs_free(shape_line); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
void test_match_outer_shape_left_not_found(void **state) |
| 123 |
{ |
| 124 |
UNUSED(state); |
| 125 |
|
| 126 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 127 |
bxstr_t *shape_line = bxs_from_ascii(" ---"); |
| 128 |
|
| 129 |
match_result_t *actual = match_outer_shape(BLEF, input_line, shape_line); |
| 130 |
assert_null(actual); |
| 131 |
|
| 132 |
bxs_free(input_line); |
| 133 |
bxs_free(shape_line); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
void test_match_outer_shape_left_too_far_in(void **state) |
| 139 |
{ |
| 140 |
UNUSED(state); |
| 141 |
|
| 142 |
bxstr_t *input_line = bxs_from_ascii(" xx M xx "); |
| 143 |
bxstr_t *shape_line = bxs_from_ascii("x M"); |
| 144 |
|
| 145 |
match_result_t *actual = match_outer_shape(BLEF, input_line, shape_line); |
| 146 |
assert_null(actual); |
| 147 |
|
| 148 |
bxs_free(input_line); |
| 149 |
bxs_free(shape_line); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
void test_match_outer_shape_left_edge(void **state) |
| 155 |
{ |
| 156 |
UNUSED(state); |
| 157 |
|
| 158 |
bxstr_t *input_line = bxs_from_ascii(" x yy"); |
| 159 |
bxstr_t *shape_line = bxs_from_ascii("x"); |
| 160 |
|
| 161 |
match_result_t *actual = match_outer_shape(BLEF, input_line, shape_line); |
| 162 |
|
| 163 |
uint32_t *expected_p = bxs_first_char_ptr(input_line, 3); |
| 164 |
assert_non_null(actual); |
| 165 |
assert_int_equal(3, (int) actual->p_idx); |
| 166 |
assert_int_equal(0, u32_strcmp(expected_p, actual->p)); |
| 167 |
assert_int_equal(1, actual->len); |
| 168 |
assert_int_equal(0, actual->shiftable); |
| 169 |
|
| 170 |
BFREE(actual); |
| 171 |
bxs_free(input_line); |
| 172 |
bxs_free(shape_line); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
void test_match_outer_shape_right_anchored(void **state) |
| 178 |
{ |
| 179 |
UNUSED(state); |
| 180 |
|
| 181 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 182 |
bxstr_t *shape_line = bxs_from_ascii(" xx "); |
| 183 |
|
| 184 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 185 |
uint32_t *expected_p = bxs_first_char_ptr(input_line, 8); |
| 186 |
assert_non_null(actual); |
| 187 |
assert_int_equal(8, (int) actual->p_idx); |
| 188 |
assert_int_equal(0, u32_strcmp(expected_p, actual->p)); |
| 189 |
assert_int_equal(4, actual->len); |
| 190 |
assert_int_equal(0, actual->shiftable); |
| 191 |
|
| 192 |
BFREE(actual); |
| 193 |
bxs_free(input_line); |
| 194 |
bxs_free(shape_line); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
void test_match_outer_shape_right_shiftable(void **state) |
| 200 |
{ |
| 201 |
UNUSED(state); |
| 202 |
|
| 203 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 204 |
bxstr_t *shape_line = bxs_from_ascii(" "); |
| 205 |
|
| 206 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 207 |
assert_non_null(actual); |
| 208 |
assert_int_equal(15, (int) actual->p_idx); |
| 209 |
assert_int_equal(1, is_char_at(actual->p, 0, char_nul)); |
| 210 |
assert_int_equal(0, actual->len); |
| 211 |
assert_int_equal(1, actual->shiftable); |
| 212 |
|
| 213 |
BFREE(actual); |
| 214 |
bxs_free(input_line); |
| 215 |
bxs_free(shape_line); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
void test_match_outer_shape_right_shortened(void **state) |
| 221 |
{ |
| 222 |
UNUSED(state); |
| 223 |
|
| 224 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 225 |
bxstr_t *shape_line = bxs_from_ascii("xx "); /* length 6 */ |
| 226 |
|
| 227 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 228 |
|
| 229 |
uint32_t *expected_p = bxs_first_char_ptr(input_line, 7); |
| 230 |
assert_non_null(actual); |
| 231 |
assert_int_equal(7, (int) actual->p_idx); |
| 232 |
assert_int_equal(0, u32_strcmp(expected_p, actual->p)); |
| 233 |
assert_int_equal(4, actual->len); /* only 4 characters matched */ |
| 234 |
assert_int_equal(0, actual->shiftable); |
| 235 |
|
| 236 |
BFREE(actual); |
| 237 |
bxs_free(input_line); |
| 238 |
bxs_free(shape_line); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
void test_match_outer_shape_right_not_found(void **state) |
| 244 |
{ |
| 245 |
UNUSED(state); |
| 246 |
|
| 247 |
bxstr_t *input_line = bxs_from_ascii(" xx x xx "); |
| 248 |
bxstr_t *shape_line = bxs_from_ascii("--- "); |
| 249 |
|
| 250 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 251 |
assert_null(actual); |
| 252 |
|
| 253 |
bxs_free(input_line); |
| 254 |
bxs_free(shape_line); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
void test_match_outer_shape_right_too_far_in(void **state) |
| 260 |
{ |
| 261 |
UNUSED(state); |
| 262 |
|
| 263 |
bxstr_t *input_line = bxs_from_ascii(" xx M xx "); |
| 264 |
bxstr_t *shape_line = bxs_from_ascii("M x"); |
| 265 |
|
| 266 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 267 |
assert_null(actual); |
| 268 |
|
| 269 |
bxs_free(input_line); |
| 270 |
bxs_free(shape_line); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
void test_match_outer_shape_right_shortened2(void **state) |
| 276 |
{ |
| 277 |
UNUSED(state); |
| 278 |
|
| 279 |
bxstr_t *input_line = bxs_from_ascii(" x Y"); |
| 280 |
bxstr_t *shape_line = bxs_from_ascii("Y "); |
| 281 |
|
| 282 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 283 |
|
| 284 |
uint32_t *expected_p = bxs_first_char_ptr(input_line, 4); |
| 285 |
assert_non_null(actual); |
| 286 |
assert_int_equal(4, (int) actual->p_idx); |
| 287 |
assert_int_equal(0, u32_strcmp(expected_p, actual->p)); |
| 288 |
assert_int_equal(1, actual->len); |
| 289 |
assert_int_equal(0, actual->shiftable); |
| 290 |
|
| 291 |
BFREE(actual); |
| 292 |
bxs_free(input_line); |
| 293 |
bxs_free(shape_line); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
void test_match_outer_shape_right_edge(void **state) |
| 299 |
{ |
| 300 |
UNUSED(state); |
| 301 |
|
| 302 |
bxstr_t *input_line = bxs_from_ascii("x"); |
| 303 |
bxstr_t *shape_line = bxs_from_ascii("x"); |
| 304 |
|
| 305 |
match_result_t *actual = match_outer_shape(BRIG, input_line, shape_line); |
| 306 |
|
| 307 |
assert_non_null(actual); |
| 308 |
assert_int_equal(0, (int) actual->p_idx); |
| 309 |
assert_int_equal(0, u32_strcmp(input_line->memory, actual->p)); |
| 310 |
assert_int_equal(1, actual->len); |
| 311 |
assert_int_equal(0, actual->shiftable); |
| 312 |
|
| 313 |
BFREE(actual); |
| 314 |
bxs_free(input_line); |
| 315 |
bxs_free(shape_line); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
void test_shorten_preferleft_allowall(void **state) |
| 321 |
{ |
| 322 |
UNUSED(state); |
| 323 |
|
| 324 |
shape_line_ctx_t *shape_line_ctx = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t)); |
| 325 |
shape_line_ctx->text = bxs_from_ascii(" abc "); |
| 326 |
size_t quality = shape_line_ctx->text->num_chars; |
| 327 |
|
| 328 |
uint32_t *actual = shorten(shape_line_ctx, &quality, 1, 1, 1); |
| 329 |
uint32_t *expected = u32_strconv_from_arg(" abc ", "ASCII"); |
| 330 |
assert_int_equal(6, (int) quality); |
| 331 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 332 |
BFREE(actual); |
| 333 |
BFREE(expected); |
| 334 |
|
| 335 |
actual = shorten(shape_line_ctx, &quality, 1, 1, 1); |
| 336 |
expected = u32_strconv_from_arg("abc ", "ASCII"); |
| 337 |
assert_int_equal(5, (int) quality); |
| 338 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 339 |
BFREE(actual); |
| 340 |
BFREE(expected); |
| 341 |
|
| 342 |
actual = shorten(shape_line_ctx, &quality, 1, 1, 1); |
| 343 |
expected = u32_strconv_from_arg("abc ", "ASCII"); |
| 344 |
assert_int_equal(4, (int) quality); |
| 345 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 346 |
BFREE(actual); |
| 347 |
BFREE(expected); |
| 348 |
|
| 349 |
actual = shorten(shape_line_ctx, &quality, 1, 1, 1); |
| 350 |
expected = u32_strconv_from_arg("abc", "ASCII"); |
| 351 |
assert_int_equal(3, (int) quality); |
| 352 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 353 |
BFREE(actual); |
| 354 |
BFREE(expected); |
| 355 |
|
| 356 |
actual = shorten(shape_line_ctx, &quality, 1, 1, 1); |
| 357 |
assert_int_equal(3, (int) quality); |
| 358 |
assert_null(actual); |
| 359 |
BFREE(actual); |
| 360 |
|
| 361 |
bxs_free(shape_line_ctx->text); |
| 362 |
BFREE(shape_line_ctx); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
void test_shorten_preferleft_allowright(void **state) |
| 368 |
{ |
| 369 |
UNUSED(state); |
| 370 |
|
| 371 |
shape_line_ctx_t *shape_line_ctx = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t)); |
| 372 |
shape_line_ctx->text = bxs_from_ascii(" abc "); |
| 373 |
size_t quality = shape_line_ctx->text->num_chars; |
| 374 |
|
| 375 |
uint32_t *actual = shorten(shape_line_ctx, &quality, 1, 0, 1); |
| 376 |
uint32_t *expected = u32_strconv_from_arg(" abc ", "ASCII"); |
| 377 |
assert_int_equal(6, (int) quality); |
| 378 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 379 |
BFREE(actual); |
| 380 |
BFREE(expected); |
| 381 |
|
| 382 |
actual = shorten(shape_line_ctx, &quality, 1, 0, 1); |
| 383 |
expected = u32_strconv_from_arg(" abc", "ASCII"); |
| 384 |
assert_int_equal(5, (int) quality); |
| 385 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 386 |
BFREE(actual); |
| 387 |
BFREE(expected); |
| 388 |
|
| 389 |
actual = shorten(shape_line_ctx, &quality, 1, 0, 1); |
| 390 |
assert_int_equal(5, (int) quality); |
| 391 |
assert_null(actual); |
| 392 |
BFREE(actual); |
| 393 |
|
| 394 |
bxs_free(shape_line_ctx->text); |
| 395 |
BFREE(shape_line_ctx); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
void test_shorten_preferleft_allowleft(void **state) |
| 401 |
{ |
| 402 |
UNUSED(state); |
| 403 |
|
| 404 |
shape_line_ctx_t *shape_line_ctx = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t)); |
| 405 |
shape_line_ctx->text = bxs_from_ascii(" abc "); |
| 406 |
size_t quality = shape_line_ctx->text->num_chars; |
| 407 |
|
| 408 |
uint32_t *actual = shorten(shape_line_ctx, &quality, 1, 1, 0); |
| 409 |
uint32_t *expected = u32_strconv_from_arg(" abc ", "ASCII"); |
| 410 |
assert_int_equal(6, (int) quality); |
| 411 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 412 |
BFREE(actual); |
| 413 |
BFREE(expected); |
| 414 |
|
| 415 |
actual = shorten(shape_line_ctx, &quality, 1, 1, 0); |
| 416 |
expected = u32_strconv_from_arg("abc ", "ASCII"); |
| 417 |
assert_int_equal(5, (int) quality); |
| 418 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 419 |
BFREE(actual); |
| 420 |
BFREE(expected); |
| 421 |
|
| 422 |
actual = shorten(shape_line_ctx, &quality, 1, 1, 0); |
| 423 |
assert_int_equal(5, (int) quality); |
| 424 |
assert_null(actual); |
| 425 |
BFREE(actual); |
| 426 |
|
| 427 |
bxs_free(shape_line_ctx->text); |
| 428 |
BFREE(shape_line_ctx); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
void test_shorten_preferright_allowall(void **state) |
| 434 |
{ |
| 435 |
UNUSED(state); |
| 436 |
|
| 437 |
shape_line_ctx_t *shape_line_ctx = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t)); |
| 438 |
shape_line_ctx->text = bxs_from_ascii(" abc "); |
| 439 |
size_t quality = shape_line_ctx->text->num_chars; |
| 440 |
|
| 441 |
uint32_t *actual = shorten(shape_line_ctx, &quality, 0, 1, 1); |
| 442 |
uint32_t *expected = u32_strconv_from_arg(" abc ", "ASCII"); |
| 443 |
assert_int_equal(6, (int) quality); |
| 444 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 445 |
BFREE(actual); |
| 446 |
BFREE(expected); |
| 447 |
|
| 448 |
actual = shorten(shape_line_ctx, &quality, 0, 1, 1); |
| 449 |
expected = u32_strconv_from_arg(" abc", "ASCII"); |
| 450 |
assert_int_equal(5, (int) quality); |
| 451 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 452 |
BFREE(actual); |
| 453 |
BFREE(expected); |
| 454 |
|
| 455 |
actual = shorten(shape_line_ctx, &quality, 0, 1, 1); |
| 456 |
expected = u32_strconv_from_arg(" abc", "ASCII"); |
| 457 |
assert_int_equal(4, (int) quality); |
| 458 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 459 |
BFREE(actual); |
| 460 |
BFREE(expected); |
| 461 |
|
| 462 |
actual = shorten(shape_line_ctx, &quality, 0, 1, 1); |
| 463 |
expected = u32_strconv_from_arg("abc", "ASCII"); |
| 464 |
assert_int_equal(3, (int) quality); |
| 465 |
assert_int_equal(0, u32_strcmp(expected, actual)); |
| 466 |
BFREE(actual); |
| 467 |
BFREE(expected); |
| 468 |
|
| 469 |
actual = shorten(shape_line_ctx, &quality, 0, 1, 1); |
| 470 |
assert_int_equal(3, (int) quality); |
| 471 |
assert_null(actual); |
| 472 |
BFREE(actual); |
| 473 |
|
| 474 |
bxs_free(shape_line_ctx->text); |
| 475 |
BFREE(shape_line_ctx); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
void test_shorten_corner_cases(void **state) |
| 481 |
{ |
| 482 |
UNUSED(state); |
| 483 |
|
| 484 |
shape_line_ctx_t *shape_line_ctx = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t)); |
| 485 |
shape_line_ctx->text = bxs_from_ascii(" abc "); |
| 486 |
size_t quality = shape_line_ctx->text->num_chars; |
| 487 |
|
| 488 |
uint32_t *actual = shorten(shape_line_ctx, &quality, 0, 0, 0); |
| 489 |
assert_int_equal(7, (int) quality); |
| 490 |
assert_null(actual); |
| 491 |
|
| 492 |
actual = shorten(NULL, &quality, 1, 1, 0); |
| 493 |
assert_int_equal(7, (int) quality); |
| 494 |
assert_null(actual); |
| 495 |
|
| 496 |
quality = 100; /* > 7 */ |
| 497 |
actual = shorten(shape_line_ctx, &quality, 0, 0, 0); |
| 498 |
assert_int_equal(100, (int) quality); |
| 499 |
assert_null(actual); |
| 500 |
|
| 501 |
actual = shorten(shape_line_ctx, NULL, 0, 0, 1); |
| 502 |
assert_null(actual); /* quality == NULL */ |
| 503 |
|
| 504 |
quality = 7; |
| 505 |
bxs_free(shape_line_ctx->text); |
| 506 |
shape_line_ctx->text = NULL; |
| 507 |
actual = shorten(shape_line_ctx, &quality, 0, 0, 1); |
| 508 |
assert_null(actual); /* text == NULL */ |
| 509 |
|
| 510 |
BFREE(shape_line_ctx); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
static shape_line_ctx_t *build_shapes_relevant_sunny() |
| 516 |
{ |
| 517 |
shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t)); |
| 518 |
shapes_relevant[0].empty = 0; |
| 519 |
shapes_relevant[0].text = bxs_from_ascii("WCORNER"); |
| 520 |
shapes_relevant[0].elastic = 0; |
| 521 |
shapes_relevant[1].empty = 0; |
| 522 |
shapes_relevant[1].text = bxs_from_ascii("-"); |
| 523 |
shapes_relevant[1].elastic = 1; |
| 524 |
shapes_relevant[2].empty = 0; |
| 525 |
shapes_relevant[2].text = bxs_from_ascii("middle"); |
| 526 |
shapes_relevant[2].elastic = 0; |
| 527 |
shapes_relevant[3].empty = 0; |
| 528 |
shapes_relevant[3].text = bxs_from_ascii("-"); |
| 529 |
shapes_relevant[3].elastic = 1; |
| 530 |
shapes_relevant[4].empty = 0; |
| 531 |
shapes_relevant[4].text = bxs_from_ascii("ECORNER"); |
| 532 |
shapes_relevant[4].elastic = 0; |
| 533 |
return shapes_relevant; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
static void free_shapes_relevant(shape_line_ctx_t *shapes_relevant) |
| 539 |
{ |
| 540 |
for (size_t i = 0; i < SHAPES_PER_SIDE; i++) { |
| 541 |
bxs_free(shapes_relevant[i].text); |
| 542 |
} |
| 543 |
BFREE(shapes_relevant); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
void test_hmm_sunny_day(void **state) |
| 549 |
{ |
| 550 |
UNUSED(state); |
| 551 |
|
| 552 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_sunny(); |
| 553 |
|
| 554 |
uint32_t *input_line = u32_strconv_from_arg(" WCORNER-----middle-----ECORNER ", "ASCII"); |
| 555 |
uint32_t *cur_pos = input_line + 9; /* '-' after WCORNER */ |
| 556 |
uint32_t *end_pos = input_line + 25; /* 'E' of ECORNER */ |
| 557 |
|
| 558 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 1); |
| 559 |
|
| 560 |
assert_int_equal(1, actual); |
| 561 |
|
| 562 |
free_shapes_relevant(shapes_relevant); |
| 563 |
BFREE(input_line); |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
void test_hmm_sunny_day_short(void **state) |
| 569 |
{ |
| 570 |
UNUSED(state); |
| 571 |
|
| 572 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_sunny(); |
| 573 |
|
| 574 |
uint32_t *input_line = u32_strconv_from_arg("WCORNER-middle-", "ASCII"); |
| 575 |
uint32_t *cur_pos = input_line + 7; /* '-' after WCORNER */ |
| 576 |
uint32_t *end_pos = input_line + 15; /* NUL */ |
| 577 |
|
| 578 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 0); |
| 579 |
|
| 580 |
assert_int_equal(1, actual); |
| 581 |
|
| 582 |
free_shapes_relevant(shapes_relevant); |
| 583 |
BFREE(input_line); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
void test_hmm_missing_elastic_nne(void **state) |
| 589 |
{ |
| 590 |
UNUSED(state); |
| 591 |
|
| 592 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_sunny(); |
| 593 |
|
| 594 |
uint32_t *input_line = u32_strconv_from_arg("WCORNER---middleECORNER", "ASCII"); |
| 595 |
uint32_t *cur_pos = input_line + 7; /* '-' after WCORNER */ |
| 596 |
uint32_t *end_pos = input_line + 16; /* 'E' of ECORNER */ |
| 597 |
|
| 598 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 1); |
| 599 |
|
| 600 |
assert_int_equal(0, actual); /* should fail because NNE shape is not present */ |
| 601 |
|
| 602 |
free_shapes_relevant(shapes_relevant); |
| 603 |
BFREE(input_line); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
void test_hmm_invalid_input(void **state) |
| 609 |
{ |
| 610 |
UNUSED(state); |
| 611 |
|
| 612 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_sunny(); |
| 613 |
|
| 614 |
uint32_t *input_line = u32_strconv_from_arg("not_used", "ASCII"); |
| 615 |
uint32_t *cur_pos = input_line + 5; |
| 616 |
uint32_t *end_pos = input_line + 2; /* before cur_pos, which is an error */ |
| 617 |
|
| 618 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 1); |
| 619 |
|
| 620 |
assert_int_equal(0, actual); /* should fail because cur_pos > end_pos */ |
| 621 |
|
| 622 |
free_shapes_relevant(shapes_relevant); |
| 623 |
BFREE(input_line); |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
static shape_line_ctx_t *build_shapes_relevant_empty_r() |
| 629 |
{ |
| 630 |
shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t)); |
| 631 |
shapes_relevant[0].empty = 0; |
| 632 |
shapes_relevant[0].text = bxs_from_ascii("WCORNER"); |
| 633 |
shapes_relevant[0].elastic = 0; |
| 634 |
shapes_relevant[1].empty = 1; |
| 635 |
shapes_relevant[1].text = NULL; |
| 636 |
shapes_relevant[1].elastic = 0; |
| 637 |
shapes_relevant[2].empty = 0; |
| 638 |
shapes_relevant[2].text = bxs_from_ascii("xx"); |
| 639 |
shapes_relevant[2].elastic = 1; |
| 640 |
shapes_relevant[3].empty = 1; |
| 641 |
shapes_relevant[3].text = NULL; |
| 642 |
shapes_relevant[3].elastic = 0; |
| 643 |
shapes_relevant[4].empty = 1; |
| 644 |
shapes_relevant[4].text = NULL; |
| 645 |
shapes_relevant[4].elastic = 0; |
| 646 |
return shapes_relevant; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
void test_hmm_empty_shapes_success(void **state) |
| 652 |
{ |
| 653 |
UNUSED(state); |
| 654 |
|
| 655 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_empty_r(); |
| 656 |
|
| 657 |
uint32_t *input_line = u32_strconv_from_arg(" WCORNERxxxxxxxx", "ASCII"); |
| 658 |
uint32_t *cur_pos = input_line + 8; /* first 'x' */ |
| 659 |
uint32_t *end_pos = input_line + 16; /* NUL */ |
| 660 |
|
| 661 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 1); |
| 662 |
|
| 663 |
assert_int_equal(1, actual); /* matches */ |
| 664 |
|
| 665 |
free_shapes_relevant(shapes_relevant); |
| 666 |
BFREE(input_line); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
static shape_line_ctx_t *build_shapes_relevant_backtrack() |
| 672 |
{ |
| 673 |
shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t)); |
| 674 |
shapes_relevant[0].empty = 0; |
| 675 |
shapes_relevant[0].text = bxs_from_ascii("WCORNER"); |
| 676 |
shapes_relevant[0].elastic = 0; |
| 677 |
shapes_relevant[1].empty = 0; |
| 678 |
shapes_relevant[1].text = bxs_from_ascii("-"); |
| 679 |
shapes_relevant[1].elastic = 1; |
| 680 |
shapes_relevant[2].empty = 0; |
| 681 |
shapes_relevant[2].text = bxs_from_ascii("--middle--"); |
| 682 |
shapes_relevant[2].elastic = 0; |
| 683 |
shapes_relevant[3].empty = 0; |
| 684 |
shapes_relevant[3].text = bxs_from_ascii("-"); |
| 685 |
shapes_relevant[3].elastic = 1; |
| 686 |
shapes_relevant[4].empty = 1; |
| 687 |
shapes_relevant[4].text = NULL; |
| 688 |
shapes_relevant[4].elastic = 0; |
| 689 |
return shapes_relevant; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
void test_hmm_backtracking(void **state) |
| 695 |
{ |
| 696 |
UNUSED(state); |
| 697 |
|
| 698 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_backtrack(); |
| 699 |
|
| 700 |
uint32_t *input_line = u32_strconv_from_arg(" WCORNER-----middle-----", "ASCII"); |
| 701 |
uint32_t *cur_pos = input_line + 8; /* first '-' */ |
| 702 |
uint32_t *end_pos = input_line + 24; /* NUL */ |
| 703 |
|
| 704 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 1); |
| 705 |
|
| 706 |
assert_int_equal(1, actual); /* matches, elastic NNW doesn't eat part of N shape */ |
| 707 |
|
| 708 |
free_shapes_relevant(shapes_relevant); |
| 709 |
BFREE(input_line); |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
static shape_line_ctx_t *build_shapes_relevant_shiftable() |
| 715 |
{ |
| 716 |
shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t)); |
| 717 |
shapes_relevant[0].empty = 0; /* maybe there is text in a different shape line */ |
| 718 |
shapes_relevant[0].text = bxs_from_ascii(" "); |
| 719 |
shapes_relevant[0].elastic = 0; |
| 720 |
shapes_relevant[1].empty = 1; |
| 721 |
shapes_relevant[1].text = bxs_from_ascii(" "); |
| 722 |
shapes_relevant[1].elastic = 1; |
| 723 |
shapes_relevant[2].empty = 0; |
| 724 |
shapes_relevant[2].text = bxs_from_ascii(" NORTH "); |
| 725 |
shapes_relevant[2].elastic = 0; |
| 726 |
shapes_relevant[3].empty = 1; |
| 727 |
shapes_relevant[3].text = NULL; |
| 728 |
shapes_relevant[3].elastic = 1; |
| 729 |
shapes_relevant[4].empty = 1; |
| 730 |
shapes_relevant[4].text = NULL; |
| 731 |
shapes_relevant[4].elastic = 0; |
| 732 |
return shapes_relevant; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
void test_hmm_shiftable(void **state) |
| 738 |
{ |
| 739 |
UNUSED(state); |
| 740 |
|
| 741 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_shiftable(); |
| 742 |
|
| 743 |
uint32_t *input_line = u32_strconv_from_arg(" NORTH ", "ASCII"); |
| 744 |
uint32_t *cur_pos = input_line; /* first character */ |
| 745 |
uint32_t *end_pos = input_line + 17; /* NUL */ |
| 746 |
|
| 747 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 0, 0); |
| 748 |
|
| 749 |
assert_int_equal(1, actual); /* matches */ |
| 750 |
|
| 751 |
free_shapes_relevant(shapes_relevant); |
| 752 |
BFREE(input_line); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
void test_hmm_shortened(void **state) |
| 758 |
{ |
| 759 |
UNUSED(state); |
| 760 |
|
| 761 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_shiftable(); |
| 762 |
|
| 763 |
uint32_t *input_line = u32_strconv_from_arg("NORTH", "ASCII"); |
| 764 |
uint32_t *cur_pos = input_line; /* first character */ |
| 765 |
uint32_t *end_pos = input_line + 5; /* NUL */ |
| 766 |
|
| 767 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 0, 0); |
| 768 |
|
| 769 |
assert_int_equal(1, actual); /* matches, because " NORTH " can be shortened because shiftable */ |
| 770 |
|
| 771 |
free_shapes_relevant(shapes_relevant); |
| 772 |
BFREE(input_line); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
static shape_line_ctx_t *build_shapes_relevant_shiftable_left() |
| 778 |
{ |
| 779 |
shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t)); |
| 780 |
shapes_relevant[0].empty = 0; /* maybe there is text in a different shape line */ |
| 781 |
shapes_relevant[0].text = bxs_from_ascii(" "); |
| 782 |
shapes_relevant[0].elastic = 0; |
| 783 |
shapes_relevant[1].empty = 1; |
| 784 |
shapes_relevant[1].text = bxs_from_ascii(" "); |
| 785 |
shapes_relevant[1].elastic = 1; |
| 786 |
shapes_relevant[2].empty = 0; |
| 787 |
shapes_relevant[2].text = bxs_from_ascii(" NORTH "); |
| 788 |
shapes_relevant[2].elastic = 0; |
| 789 |
shapes_relevant[3].empty = 1; |
| 790 |
shapes_relevant[3].text = NULL; |
| 791 |
shapes_relevant[3].elastic = 1; |
| 792 |
shapes_relevant[4].empty = 0; |
| 793 |
shapes_relevant[4].text = bxs_from_ascii("ECORNER "); |
| 794 |
shapes_relevant[4].elastic = 0; |
| 795 |
return shapes_relevant; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
void test_hmm_shortened_right_fail(void **state) |
| 801 |
{ |
| 802 |
UNUSED(state); |
| 803 |
|
| 804 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_shiftable_left(); |
| 805 |
|
| 806 |
uint32_t *input_line = u32_strconv_from_arg("NORTH ECORNER ", "ASCII"); |
| 807 |
uint32_t *cur_pos = input_line; /* first character */ |
| 808 |
uint32_t *end_pos = input_line + 6; /* 'E' of "ECORNER" */ |
| 809 |
|
| 810 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 0, 1); |
| 811 |
|
| 812 |
assert_int_equal(0, actual); /* does not match */ |
| 813 |
/* because " NORTH " cannot be shortened right b/c right not shiftable */ |
| 814 |
|
| 815 |
free_shapes_relevant(shapes_relevant); |
| 816 |
BFREE(input_line); |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
void test_hmm_shortened_right(void **state) |
| 822 |
{ |
| 823 |
UNUSED(state); |
| 824 |
|
| 825 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_shiftable_left(); |
| 826 |
|
| 827 |
uint32_t *input_line = u32_strconv_from_arg("NORTH ECORNER", "ASCII"); |
| 828 |
uint32_t *cur_pos = input_line; /* first character */ |
| 829 |
uint32_t *end_pos = input_line + 7; /* 'E' of "ECORNER" */ |
| 830 |
|
| 831 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 0, 1); |
| 832 |
|
| 833 |
assert_int_equal(1, actual); /* matches */ |
| 834 |
/* because " NORTH " can be shortened left, and ECORNER can be shortened right */ |
| 835 |
|
| 836 |
free_shapes_relevant(shapes_relevant); |
| 837 |
BFREE(input_line); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
static shape_line_ctx_t *build_shapes_relevant_blank() |
| 843 |
{ |
| 844 |
shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t)); |
| 845 |
shapes_relevant[0].empty = 0; /* maybe there is text in a different shape line */ |
| 846 |
shapes_relevant[0].text = bxs_from_ascii(" "); |
| 847 |
shapes_relevant[0].elastic = 0; |
| 848 |
shapes_relevant[1].empty = 0; |
| 849 |
shapes_relevant[1].text = bxs_from_ascii(" "); |
| 850 |
shapes_relevant[1].elastic = 1; |
| 851 |
shapes_relevant[2].empty = 0; |
| 852 |
shapes_relevant[2].text = bxs_from_ascii(" "); |
| 853 |
shapes_relevant[2].elastic = 0; |
| 854 |
shapes_relevant[3].empty = 1; |
| 855 |
shapes_relevant[3].text = bxs_from_ascii(" "); |
| 856 |
shapes_relevant[3].elastic = 1; |
| 857 |
shapes_relevant[4].empty = 1; |
| 858 |
shapes_relevant[4].text = NULL; |
| 859 |
shapes_relevant[4].elastic = 0; |
| 860 |
return shapes_relevant; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
void test_hmm_blank_shiftable(void **state) |
| 866 |
{ |
| 867 |
UNUSED(state); |
| 868 |
|
| 869 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_blank(); |
| 870 |
|
| 871 |
uint32_t *input_line = u32_strconv_from_arg(" ", "ASCII"); |
| 872 |
uint32_t *cur_pos = input_line; /* first character */ |
| 873 |
uint32_t *end_pos = input_line + 6; /* NUL */ |
| 874 |
|
| 875 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 0, 0); |
| 876 |
|
| 877 |
assert_int_equal(1, actual); /* matches */ |
| 878 |
|
| 879 |
free_shapes_relevant(shapes_relevant); |
| 880 |
BFREE(input_line); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
void test_hmm_blank(void **state) |
| 886 |
{ |
| 887 |
UNUSED(state); |
| 888 |
|
| 889 |
shape_line_ctx_t *shapes_relevant = build_shapes_relevant_blank(); |
| 890 |
|
| 891 |
uint32_t *input_line = u32_strconv_from_arg("WCORNER ECORNER", "ASCII"); |
| 892 |
uint32_t *cur_pos = input_line + 7; /* first blank */ |
| 893 |
uint32_t *end_pos = input_line + 14; /* 'E' of "ECORNER" */ |
| 894 |
|
| 895 |
int actual = hmm(shapes_relevant, cur_pos, 1, end_pos, 1, 1); |
| 896 |
|
| 897 |
assert_int_equal(1, actual); /* matches */ |
| 898 |
|
| 899 |
free_shapes_relevant(shapes_relevant); |
| 900 |
BFREE(input_line); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
/* vim: set cindent sw=4: */ |