| 1 | /* liblouis Braille Translation and Back-Translation Library |
| 2 | |
| 3 | Copyright (C) 2008 Eitan Isaacson <[email protected]> |
| 4 | Copyright (C) 2012 James Teh <[email protected]> |
| 5 | Copyright (C) 2012 Bert Frees <[email protected]> |
| 6 | Copyright (C) 2014 Mesar Hameed <[email protected]> |
| 7 | Copyright (C) 2015 Mike Gray <[email protected]> |
| 8 | Copyright (C) 2010-2017 Swiss Library for the Blind, Visually Impaired and Print |
| 9 | Disabled |
| 10 | Copyright (C) 2016-2017 Davy Kager <[email protected]> |
| 11 | |
| 12 | Copying and distribution of this file, with or without modification, |
| 13 | are permitted in any medium without royalty provided the copyright |
| 14 | notice and this notice are preserved. This file is offered as-is, |
| 15 | without any warranty. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * @file |
| 20 | * @brief Test helper functions |
| 21 | */ |
| 22 | |
| 23 | #include <config.h> |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include "liblouis.h" |
| 30 | #include "internal.h" |
| 31 | #include "brl_checks.h" |
| 32 | #include "unistr.h" |
| 33 | |
| 34 | /* see http://stackoverflow.com/questions/5117393/utf-8-strings-length-in-linux-c */ |
| 35 | static int |
| 36 | my_strlen_utf8_c(char *s) { |
| 37 | int i = 0, j = 0; |
| 38 | while (s[i]) { |
| 39 | if ((s[i] & 0xc0) != 0x80) j++; |
| 40 | i++; |
| 41 | } |
| 42 | return j; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * String parsing is also done later in check_base. At this point we |
| 47 | * only need it to compute the actual string length in order to be |
| 48 | * able to provide error messages when parsing typeform and position arrays. |
| 49 | */ |
| 50 | int |
| 51 | parsed_strlen(char *s) { |
| 52 | widechar *buf; |
| 53 | int len, maxlen; |
| 54 | maxlen = my_strlen_utf8_c(s); |
| 55 | buf = malloc(sizeof(widechar) * maxlen); |
| 56 | len = _lou_extParseChars(s, buf); |
| 57 | free(buf); |
| 58 | return len; |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | print_int_array(const char *prefix, int *pos_list, int len) { |
| 63 | int i; |
| 64 | fprintf(stderr, "%s ", prefix); |
| 65 | for (i = 0; i < len; i++) fprintf(stderr, "%d ", pos_list[i]); |
| 66 | fprintf(stderr, "\n"); |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | print_typeform(const formtype *typeform, int len) { |
| 71 | int i; |
| 72 | fprintf(stderr, "Typeform: "); |
| 73 | for (i = 0; i < len; i++) fprintf(stderr, "%hi", typeform[i]); |
| 74 | fprintf(stderr, "\n"); |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | print_widechars(widechar *buffer, int length) { |
| 79 | uint8_t *result_buf; |
| 80 | size_t result_len; |
| 81 | |
| 82 | #ifdef WIDECHARS_ARE_UCS4 |
| 83 | result_buf = u32_to_u8(buffer, length, NULL, &result_len); |
| 84 | #else |
| 85 | result_buf = u16_to_u8(buffer, length, NULL, &result_len); |
| 86 | #endif |
| 87 | fprintf(stderr, "%.*s", (int)result_len, result_buf); |
| 88 | free(result_buf); |
| 89 | } |
| 90 | |
| 91 | /* direction, 0=forward, 1=backwards. If diagnostics is 1 then print |
| 92 | * diagnostics in case where the translation is not as expected */ |
| 93 | |
| 94 | int |
| 95 | check_base(const char *tableList, const char *input, const char *expected, |
| 96 | optional_test_params in) { |
| 97 | |
| 98 | int i, retval = 0; |
| 99 | int direction = in.direction; |
| 100 | const int *expected_inputPos = in.expected_inputPos; |
| 101 | const int *expected_outputPos = in.expected_outputPos; |
| 102 | if (in.direction < 0 || in.direction > 1) { |
| 103 | fprintf(stderr, "Invalid direction.\n"); |
| 104 | return 1; |
| 105 | } |
| 106 | if (in.direction != 0 && in.typeform != NULL) { |
| 107 | // In backward translation, the typeform argument is for output only (emphasis |
| 108 | // detection). Use expected_typeform instead to verify backtranslation typeform. |
| 109 | fprintf(stderr, |
| 110 | "typeform (input) only supported with testmode 'forward'. " |
| 111 | "Use expected_typeform for backward translation.\n"); |
| 112 | return 1; |
| 113 | } |
| 114 | if (in.direction == 0 && in.expected_typeform != NULL) { |
| 115 | // In forward translation, typeform is an input parameter. Use expected_typeform |
| 116 | // only for backward translation to verify emphasis detection output. |
| 117 | fprintf(stderr, "expected_typeform only supported with testmode 'backward'\n"); |
| 118 | return 1; |
| 119 | } |
| 120 | if (in.direction == 2 && in.cursorPos >= 0) { |
| 121 | fprintf(stderr, "cursorPos not supported with testmode 'bothDirections'\n"); |
| 122 | return 1; |
| 123 | } |
| 124 | if (in.direction == 2 && in.max_outlen >= 0) { |
| 125 | fprintf(stderr, "maxOutputLength not supported with testmode 'bothDirections'\n"); |
| 126 | return 1; |
| 127 | } |
| 128 | if (in.real_inlen >= 0 && in.max_outlen < 0) { |
| 129 | fprintf(stderr, |
| 130 | "realInputLength not supported when maxOutputLength is not specified\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | widechar *inbuf, *outbuf, *expectedbuf; |
| 134 | int inlen = parsed_strlen(input); |
| 135 | int actualInlen; |
| 136 | const int outlen_multiplier = 4 + sizeof(widechar) * 2; |
| 137 | int outlen = inlen * outlen_multiplier; |
| 138 | int expectedlen = strlen(expected); |
| 139 | int funcStatus = 0; |
| 140 | formtype *typeformbuf = NULL; |
| 141 | int *inputPos = NULL; |
| 142 | int *outputPos = NULL; |
| 143 | int cursorPos = 0; |
| 144 | inbuf = malloc(sizeof(widechar) * inlen); |
| 145 | // + 1 : reserved for the '\0' terminator of C-format string argument used in |
| 146 | // `print_widechars` |
| 147 | outbuf = malloc(sizeof(widechar) * (outlen + 1)); |
| 148 | expectedbuf = malloc(sizeof(widechar) * expectedlen); |
| 149 | if (in.typeform != NULL) { |
| 150 | typeformbuf = malloc(outlen * sizeof(formtype)); |
| 151 | memcpy(typeformbuf, in.typeform, inlen * sizeof(formtype)); |
| 152 | } else if (in.expected_typeform != NULL) { |
| 153 | // Allocate typeform buffer to receive backtranslation emphasis output |
| 154 | typeformbuf = calloc(outlen, sizeof(formtype)); |
| 155 | } |
| 156 | if (in.cursorPos >= 0) { |
| 157 | cursorPos = in.cursorPos; |
| 158 | } |
| 159 | if (in.max_outlen >= 0) { |
| 160 | outlen = in.max_outlen; |
| 161 | } |
| 162 | inlen = _lou_extParseChars(input, inbuf); |
| 163 | if (!inlen) { |
| 164 | fprintf(stderr, "Cannot parse input string.\n"); |
| 165 | retval = 1; |
| 166 | goto fail; |
| 167 | } |
| 168 | if (in.real_inlen > inlen) { |
| 169 | fprintf(stderr, |
| 170 | "expected realInputLength (%d) may not exceed total input length " |
| 171 | "(%d)\n", |
| 172 | in.real_inlen, inlen); |
| 173 | return 1; |
| 174 | } |
| 175 | if (expected_inputPos) { |
| 176 | inputPos = malloc(sizeof(int) * outlen); |
| 177 | } |
| 178 | if (expected_outputPos) { |
| 179 | outputPos = malloc(sizeof(int) * inlen); |
| 180 | } |
| 181 | actualInlen = inlen; |
| 182 | // Note that this loop is not strictly needed to make the current tests pass, but |
| 183 | // in the general case it is needed because it is theoretically possible that we |
| 184 | // provided a too short output buffer. |
| 185 | for (int k = 1; k <= 3; k++) { |
| 186 | if (direction == 1) { |
| 187 | funcStatus = _lou_backTranslate(tableList, in.display_table, inbuf, |
| 188 | &actualInlen, outbuf, &outlen, typeformbuf, NULL, outputPos, inputPos, |
| 189 | &cursorPos, in.mode, NULL, NULL); |
| 190 | } else { |
| 191 | funcStatus = _lou_translate(tableList, in.display_table, inbuf, &actualInlen, |
| 192 | outbuf, &outlen, typeformbuf, NULL, outputPos, inputPos, &cursorPos, |
| 193 | in.mode, NULL, NULL); |
| 194 | } |
| 195 | if (!funcStatus) { |
| 196 | fprintf(stderr, "Translation failed.\n"); |
| 197 | retval = 1; |
| 198 | goto fail; |
| 199 | } |
| 200 | if (in.max_outlen >= 0 || inlen == actualInlen) { |
| 201 | break; |
| 202 | } else if (k < 3) { |
| 203 | // Hm, something is not quite right. Try again with a larger outbuf |
| 204 | free(outbuf); |
| 205 | outlen = inlen * outlen_multiplier * (k + 1); |
| 206 | // + 1 : reserved for the '\0' terminator of C-format string argument used in |
| 207 | // `print_widechars` |
| 208 | outbuf = malloc(sizeof(widechar) * (outlen + 1)); |
| 209 | if (expected_inputPos) { |
| 210 | free(inputPos); |
| 211 | inputPos = malloc(sizeof(int) * outlen); |
| 212 | } |
| 213 | fprintf(stderr, |
| 214 | "Warning: For %s: returned inlen (%d) differs from passed inlen " |
| 215 | "(%d) " |
| 216 | "using outbuf of size %d. Trying again with bigger outbuf " |
| 217 | "(%d).\n", |
| 218 | input, actualInlen, inlen, inlen * outlen_multiplier * k, outlen); |
| 219 | actualInlen = inlen; |
| 220 | } |
| 221 | } |
| 222 | expectedlen = _lou_extParseChars(expected, expectedbuf); |
| 223 | for (i = 0; i < outlen && i < expectedlen && expectedbuf[i] == outbuf[i]; i++); |
| 224 | if (i < outlen || i < expectedlen) { |
| 225 | retval = 1; |
| 226 | if (in.diagnostics) { |
| 227 | outbuf[outlen] = 0; |
| 228 | fprintf(stderr, "Input: '%s'\n", input); |
| 229 | /* Print the original typeform not the typeformbuf, as the |
| 230 | * latter has been modified by the translation and contains some |
| 231 | * information about outbuf */ |
| 232 | if (in.typeform != NULL) print_typeform(in.typeform, inlen); |
| 233 | if (in.cursorPos >= 0) fprintf(stderr, "Cursor: %d\n", in.cursorPos); |
| 234 | fprintf(stderr, "Expected: '%s' (length %d)\n", expected, expectedlen); |
| 235 | fprintf(stderr, "Received: '"); |
| 236 | print_widechars(outbuf, outlen); |
| 237 | fprintf(stderr, "' (length %d)\n", outlen); |
| 238 | |
| 239 | uint8_t *expected_utf8; |
| 240 | uint8_t *out_utf8; |
| 241 | size_t expected_utf8_len; |
| 242 | size_t out_utf8_len; |
| 243 | #ifdef WIDECHARS_ARE_UCS4 |
| 244 | expected_utf8 = u32_to_u8(&expectedbuf[i], 1, NULL, &expected_utf8_len); |
| 245 | out_utf8 = u32_to_u8(&outbuf[i], 1, NULL, &out_utf8_len); |
| 246 | #else |
| 247 | expected_utf8 = u16_to_u8(&expectedbuf[i], 1, NULL, &expected_utf8_len); |
| 248 | out_utf8 = u16_to_u8(&outbuf[i], 1, NULL, &out_utf8_len); |
| 249 | #endif |
| 250 | |
| 251 | if (i < outlen && i < expectedlen) { |
| 252 | fprintf(stderr, |
| 253 | "Diff: Expected '%.*s' but received '%.*s' in index %d\n", |
| 254 | (int)expected_utf8_len, expected_utf8, (int)out_utf8_len, |
| 255 | out_utf8, i); |
| 256 | } else if (i < expectedlen) { |
| 257 | fprintf(stderr, |
| 258 | "Diff: Expected '%.*s' but received nothing in index " |
| 259 | "%d\n", |
| 260 | (int)expected_utf8_len, expected_utf8, i); |
| 261 | } else { |
| 262 | fprintf(stderr, |
| 263 | "Diff: Expected nothing but received '%.*s' in index " |
| 264 | "%d\n", |
| 265 | (int)out_utf8_len, out_utf8, i); |
| 266 | } |
| 267 | free(expected_utf8); |
| 268 | free(out_utf8); |
| 269 | } |
| 270 | } |
| 271 | if (expected_inputPos) { |
| 272 | int error_printed = 0; |
| 273 | // The expected_inputPos array may be shorter than the actual input length. |
| 274 | for (i = 0; i < outlen || i < expectedlen; i++) { |
| 275 | if (i < outlen && i < expectedlen && expected_inputPos[i] == inputPos[i]) |
| 276 | continue; |
| 277 | retval = 1; |
| 278 | if (in.diagnostics) { |
| 279 | if (!error_printed) { // Print only once |
| 280 | fprintf(stderr, "Input position failure:\n"); |
| 281 | error_printed = 1; |
| 282 | } |
| 283 | if (i < outlen && i < expectedlen) { |
| 284 | fprintf(stderr, "Expected %d, received %d in index %d\n", |
| 285 | expected_inputPos[i], inputPos[i], i); |
| 286 | } else if (i < expectedlen) { |
| 287 | fprintf(stderr, "Expected %d, received nothing in index %d\n", |
| 288 | expected_inputPos[i], i); |
| 289 | } else { |
| 290 | fprintf(stderr, "Expected nothing, received %d in index %d\n", |
| 291 | inputPos[i], i); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | if (expected_outputPos) { |
| 297 | int error_printed = 0; |
| 298 | for (i = 0; i < inlen; i++) { |
| 299 | if (expected_outputPos[i] != outputPos[i]) { |
| 300 | retval = 1; |
| 301 | if (in.diagnostics) { |
| 302 | if (!error_printed) { // Print only once |
| 303 | fprintf(stderr, "Output position failure:\n"); |
| 304 | error_printed = 1; |
| 305 | } |
| 306 | fprintf(stderr, "Expected %d, received %d in index %d\n", |
| 307 | expected_outputPos[i], outputPos[i], i); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | if ((in.expected_cursorPos >= 0) && (cursorPos != in.expected_cursorPos)) { |
| 313 | retval = 1; |
| 314 | if (in.diagnostics) { |
| 315 | fprintf(stderr, "Cursor position failure:\n"); |
| 316 | fprintf(stderr, "Initial:%d Expected:%d Actual:%d \n", in.cursorPos, |
| 317 | in.expected_cursorPos, cursorPos); |
| 318 | } |
| 319 | } |
| 320 | if (in.expected_typeform != NULL) { |
| 321 | int error_printed = 0; |
| 322 | for (i = 0; i < outlen; i++) { |
| 323 | if (in.expected_typeform[i] != typeformbuf[i]) { |
| 324 | retval = 1; |
| 325 | if (in.diagnostics) { |
| 326 | if (!error_printed) { |
| 327 | fprintf(stderr, "Typeform failure:\n"); |
| 328 | fprintf(stderr, "Expected: "); |
| 329 | print_typeform(in.expected_typeform, outlen); |
| 330 | fprintf(stderr, "Received: "); |
| 331 | print_typeform(typeformbuf, outlen); |
| 332 | error_printed = 1; |
| 333 | } |
| 334 | fprintf(stderr, "Expected %d, received %d in index %d\n", |
| 335 | in.expected_typeform[i], typeformbuf[i], i); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | if (in.max_outlen < 0 && inlen != actualInlen) { |
| 341 | retval = 1; |
| 342 | if (in.diagnostics) { |
| 343 | fprintf(stderr, |
| 344 | "Unexpected error happened: input length is not the same before " |
| 345 | "as " |
| 346 | "after the translation:\n"); |
| 347 | fprintf(stderr, "Before: %d After: %d \n", inlen, actualInlen); |
| 348 | } |
| 349 | } else if (actualInlen > inlen) { |
| 350 | retval = 1; |
| 351 | if (in.diagnostics) { |
| 352 | fprintf(stderr, |
| 353 | "Unexpected error happened: returned input length (%d) exceeds " |
| 354 | "total input length (%d)\n", |
| 355 | actualInlen, inlen); |
| 356 | } |
| 357 | } else if (in.real_inlen >= 0 && in.real_inlen != actualInlen) { |
| 358 | retval = 1; |
| 359 | if (in.diagnostics) { |
| 360 | fprintf(stderr, "Real input length failure:\n"); |
| 361 | fprintf(stderr, "Expected: %d, received: %d\n", in.real_inlen, actualInlen); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | fail: |
| 366 | free(inbuf); |
| 367 | free(outbuf); |
| 368 | free(expectedbuf); |
| 369 | free(typeformbuf); |
| 370 | free(inputPos); |
| 371 | free(outputPos); |
| 372 | |
| 373 | return retval; |
| 374 | } |
| 375 | |
| 376 | /* Helper function to convert a typeform string of '0's, '1's, '2's etc. |
| 377 | * to the required format, which is an array of 0s, 1s, 2s, etc. |
| 378 | * For example, "0000011111000" is converted to {0,0,0,0,0,1,1,1,1,1,0,0,0} |
| 379 | * The caller is responsible for freeing the returned array. */ |
| 380 | formtype * |
| 381 | convert_typeform(const char *typeform_string) { |
| 382 | int len = strlen(typeform_string); |
| 383 | formtype *typeform = malloc(len * sizeof(formtype)); |
| 384 | int i; |
| 385 | for (i = 0; i < len; i++) typeform[i] = typeform_string[i] - '0'; |
| 386 | return typeform; |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | update_typeform(const char *typeform_string, formtype *typeform, const typeforms kind) { |
| 391 | int len = strlen(typeform_string); |
| 392 | int i; |
| 393 | for (i = 0; i < len; i++) |
| 394 | if (typeform_string[i] != ' ') typeform[i] |= kind; |
| 395 | } |
| 396 | |
| 397 | int |
| 398 | check_cursor_pos(const char *tableList, const char *str, const int *expected_pos) { |
| 399 | widechar *inbuf; |
| 400 | widechar *outbuf; |
| 401 | int *inpos, *outpos; |
| 402 | int inlen = strlen(str); |
| 403 | int outlen = inlen; |
| 404 | int cursor_pos; |
| 405 | int i, retval = 0; |
| 406 | |
| 407 | inbuf = malloc(sizeof(widechar) * inlen); |
| 408 | outbuf = malloc(sizeof(widechar) * inlen); |
| 409 | inpos = malloc(sizeof(int) * inlen); |
| 410 | outpos = malloc(sizeof(int) * inlen); |
| 411 | inlen = _lou_extParseChars(str, inbuf); |
| 412 | |
| 413 | for (i = 0; i < inlen; i++) { |
| 414 | cursor_pos = i; |
| 415 | if (!lou_translate(tableList, inbuf, &inlen, outbuf, &outlen, NULL, NULL, NULL, |
| 416 | NULL, &cursor_pos, compbrlAtCursor)) { |
| 417 | fprintf(stderr, "Translation failed.\n"); |
| 418 | retval = 1; |
| 419 | goto fail; |
| 420 | } |
| 421 | if (expected_pos[i] != cursor_pos) { |
| 422 | if (!retval) // Print only once |
| 423 | fprintf(stderr, "Cursorpos failure:\n"); |
| 424 | fprintf(stderr, |
| 425 | "string='%s' cursor=%d ('%c') expected=%d received=%d ('%c')\n", str, |
| 426 | i, str[i], expected_pos[i], cursor_pos, (char)outbuf[cursor_pos]); |
| 427 | retval = 1; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | fail: |
| 432 | free(inbuf); |
| 433 | free(outbuf); |
| 434 | free(inpos); |
| 435 | free(outpos); |
| 436 | return retval; |
| 437 | } |
| 438 | |
| 439 | /** Check if a display table maps characters to the right dots. |
| 440 | * |
| 441 | * The dots are read as Unicode braille. Multiple input characters are |
| 442 | * allowed to map to the same dot pattern. Virtual dots in the actual |
| 443 | * output are discarded. |
| 444 | * |
| 445 | * @return 0 if the result is as expected and 1 otherwise. |
| 446 | */ |
| 447 | int |
| 448 | check_display(const char *displayTableList, const char *input, const char *expected) { |
| 449 | widechar *inbuf = NULL; |
| 450 | widechar *outbuf = NULL; |
| 451 | widechar *expectedbuf = NULL; |
| 452 | int retval = 0; |
| 453 | int inlen = strlen(input); |
| 454 | inbuf = malloc(sizeof(widechar) * inlen); |
| 455 | inlen = _lou_extParseChars(input, inbuf); |
| 456 | if (!inlen) { |
| 457 | fprintf(stderr, "Cannot parse input string.\n"); |
| 458 | retval = 1; |
| 459 | goto fail; |
| 460 | } |
| 461 | int expectedlen = strlen(expected); |
| 462 | expectedbuf = malloc(sizeof(widechar) * expectedlen); |
| 463 | expectedlen = _lou_extParseChars(expected, expectedbuf); |
| 464 | if (!expectedlen) { |
| 465 | fprintf(stderr, "Cannot parse output string.\n"); |
| 466 | retval = 1; |
| 467 | goto fail; |
| 468 | } |
| 469 | if (inlen != expectedlen) { |
| 470 | fprintf(stderr, "Input and output string must be the same length.\n"); |
| 471 | retval = 1; |
| 472 | goto fail; |
| 473 | } |
| 474 | for (int i = 0; i < expectedlen; i++) { |
| 475 | if ((expectedbuf[i] & 0xff00) != LOU_ROW_BRAILLE) { |
| 476 | fprintf(stderr, "Output string must be Unicode braille.\n"); |
| 477 | retval = 1; |
| 478 | goto fail; |
| 479 | } |
| 480 | } |
| 481 | outbuf = malloc(sizeof(widechar) * inlen); |
| 482 | if (!lou_charToDots(displayTableList, inbuf, outbuf, inlen, ucBrl)) { |
| 483 | // This should only happen if the table can not be compiled. |
| 484 | // If the table does not have a display rule for a character |
| 485 | // in the input, it will result in a blank dot pattern in the |
| 486 | // output. |
| 487 | fprintf(stderr, "Mapping to dots failed.\n"); |
| 488 | retval = 1; |
| 489 | goto fail; |
| 490 | } |
| 491 | for (int i = 0; i < inlen; i++) { |
| 492 | if (outbuf[i] != expectedbuf[i]) { |
| 493 | retval = 1; |
| 494 | fprintf(stderr, "Input: '%s'\n", input); |
| 495 | fprintf(stderr, "Expected: '%s'\n", expected); |
| 496 | fprintf(stderr, "Received: '"); |
| 497 | print_widechars(outbuf, inlen); |
| 498 | fprintf(stderr, "'\n"); |
| 499 | uint8_t *expected_utf8; |
| 500 | uint8_t *out_utf8; |
| 501 | size_t expected_utf8_len; |
| 502 | size_t out_utf8_len; |
| 503 | #ifdef WIDECHARS_ARE_UCS4 |
| 504 | expected_utf8 = u32_to_u8(&expectedbuf[i], 1, NULL, &expected_utf8_len); |
| 505 | out_utf8 = u32_to_u8(&outbuf[i], 1, NULL, &out_utf8_len); |
| 506 | #else |
| 507 | expected_utf8 = u16_to_u8(&expectedbuf[i], 1, NULL, &expected_utf8_len); |
| 508 | out_utf8 = u16_to_u8(&outbuf[i], 1, NULL, &out_utf8_len); |
| 509 | #endif |
| 510 | expectedbuf[i] = (expectedbuf[i] & 0x00ff) | LOU_DOTS; |
| 511 | outbuf[i] = (outbuf[i] & 0x00ff) | LOU_DOTS; |
| 512 | fprintf(stderr, "Diff: Expected '%.*s' (dots %s)", (int)expected_utf8_len, |
| 513 | expected_utf8, _lou_showDots(&expectedbuf[i], 1)); |
| 514 | fprintf(stderr, " but received '%.*s' (dots %s) in index %d\n", |
| 515 | (int)out_utf8_len, out_utf8, _lou_showDots(&outbuf[i], 1), i); |
| 516 | free(expected_utf8); |
| 517 | free(out_utf8); |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | fail: |
| 522 | free(inbuf); |
| 523 | free(outbuf); |
| 524 | free(expectedbuf); |
| 525 | return retval; |
| 526 | } |
| 527 | |
| 528 | /* Check if a string is hyphenated as expected, by passing the |
| 529 | * expected hyphenation position array. |
| 530 | * |
| 531 | * @return 0 if the hyphenation is as expected and 1 otherwise. |
| 532 | */ |
| 533 | int |
| 534 | check_hyphenation_pos(const char *tableList, const char *str, const char *expected) { |
| 535 | widechar *inbuf; |
| 536 | char *hyphens = NULL; |
| 537 | int inlen = strlen(str); |
| 538 | int retval = 0; |
| 539 | |
| 540 | inbuf = malloc(sizeof(widechar) * inlen); |
| 541 | inlen = _lou_extParseChars(str, inbuf); |
| 542 | if (!inlen) { |
| 543 | fprintf(stderr, "Cannot parse input string.\n"); |
| 544 | retval = 1; |
| 545 | goto fail; |
| 546 | } |
| 547 | hyphens = calloc(inlen + 1, sizeof(char)); |
| 548 | |
| 549 | if (!lou_hyphenate(tableList, inbuf, inlen, hyphens, 0)) { |
| 550 | fprintf(stderr, "Hyphenation failed.\n"); |
| 551 | retval = 1; |
| 552 | goto fail; |
| 553 | } |
| 554 | |
| 555 | if (strcmp(expected, hyphens)) { |
| 556 | fprintf(stderr, "Input: '%s'\n", str); |
| 557 | fprintf(stderr, "Expected: '%s'\n", expected); |
| 558 | fprintf(stderr, "Received: '%s'\n", hyphens); |
| 559 | retval = 1; |
| 560 | } |
| 561 | |
| 562 | fail: |
| 563 | free(inbuf); |
| 564 | free(hyphens); |
| 565 | return retval; |
| 566 | } |
| 567 | |
| 568 | /** Check if a string is hyphenated as expected. |
| 569 | * |
| 570 | * mode is '0' when input is text and '1' when input is braille |
| 571 | * |
| 572 | * @return 0 if the hyphenation is as expected and 1 otherwise. |
| 573 | */ |
| 574 | int |
| 575 | check_hyphenation( |
| 576 | const char *tableList, const char *str, const char *expected, int mode) { |
| 577 | widechar *inbuf; |
| 578 | widechar *hyphenatedbuf = NULL; |
| 579 | uint8_t *hyphenated = NULL; |
| 580 | char *hyphens = NULL; |
| 581 | int inlen = strlen(str); |
| 582 | size_t hyphenatedlen = inlen * 2; |
| 583 | int retval = 0; |
| 584 | |
| 585 | inbuf = malloc(sizeof(widechar) * inlen); |
| 586 | inlen = _lou_extParseChars(str, inbuf); |
| 587 | if (!inlen) { |
| 588 | fprintf(stderr, "Cannot parse input string.\n"); |
| 589 | retval = 1; |
| 590 | goto fail; |
| 591 | } |
| 592 | hyphens = calloc(inlen + 1, sizeof(char)); |
| 593 | |
| 594 | if (!lou_hyphenate(tableList, inbuf, inlen, hyphens, mode)) { |
| 595 | fprintf(stderr, "Hyphenation failed.\n"); |
| 596 | retval = 1; |
| 597 | goto fail; |
| 598 | } |
| 599 | if (hyphens[0] != '0') { |
| 600 | fprintf(stderr, "Unexpected output from lou_hyphenate.\n"); |
| 601 | retval = 1; |
| 602 | goto fail; |
| 603 | } |
| 604 | |
| 605 | hyphenatedbuf = malloc(sizeof(widechar) * hyphenatedlen); |
| 606 | int i = 0; |
| 607 | int j = 0; |
| 608 | hyphenatedbuf[i++] = inbuf[j++]; |
| 609 | for (; j < inlen; j++) { |
| 610 | if (hyphens[j] == '2') |
| 611 | hyphenatedbuf[i++] = (widechar)'|'; |
| 612 | else if (hyphens[j] != '0') |
| 613 | hyphenatedbuf[i++] = (widechar)'-'; |
| 614 | hyphenatedbuf[i++] = inbuf[j]; |
| 615 | } |
| 616 | |
| 617 | #ifdef WIDECHARS_ARE_UCS4 |
| 618 | hyphenated = u32_to_u8(hyphenatedbuf, i, NULL, &hyphenatedlen); |
| 619 | #else |
| 620 | hyphenated = u16_to_u8(hyphenatedbuf, i, NULL, &hyphenatedlen); |
| 621 | #endif |
| 622 | |
| 623 | if (!hyphenated) { |
| 624 | fprintf(stderr, "Unexpected error during UTF-8 encoding\n"); |
| 625 | free(hyphenatedbuf); |
| 626 | retval = 2; |
| 627 | goto fail; |
| 628 | } |
| 629 | |
| 630 | if (strlen(expected) != hyphenatedlen || |
| 631 | strncmp(expected, (const char *)hyphenated, hyphenatedlen)) { |
| 632 | fprintf(stderr, "Input: '%s'\n", str); |
| 633 | fprintf(stderr, "Expected: '%s'\n", expected); |
| 634 | fprintf(stderr, "Received: '%.*s'\n", (int)hyphenatedlen, hyphenated); |
| 635 | retval = 1; |
| 636 | } |
| 637 | |
| 638 | free(hyphenatedbuf); |
| 639 | free(hyphenated); |
| 640 | |
| 641 | fail: |
| 642 | free(inbuf); |
| 643 | free(hyphens); |
| 644 | return retval; |
| 645 | } |