| 1 | /* liblouis Braille Translation and Back-Translation Library |
| 2 | |
| 3 | Copyright (C) 2012 James Teh <[email protected]> |
| 4 | Copyright (C) 2014 Mesar Hameed <[email protected]> |
| 5 | Copyright (C) 2015 Mike Gray <[email protected]> |
| 6 | Copyright (C) 2010-2017 Swiss Library for the Blind, Visually Impaired and Print |
| 7 | Disabled |
| 8 | Copyright (C) 2016-2017 Davy Kager <[email protected]> |
| 9 | |
| 10 | Copying and distribution of this file, with or without modification, |
| 11 | are permitted in any medium without royalty provided the copyright |
| 12 | notice and this notice are preserved. This file is offered as-is, |
| 13 | without any warranty. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * @file |
| 18 | * @brief Test helper functions |
| 19 | * |
| 20 | * Functionality to check a translation. This is mostly needed for the |
| 21 | * tests in ../tests but it is also needed for lou_checkyaml. So this |
| 22 | * functionality is packaged up in what automake calls a convenience |
| 23 | * library, a lib that is solely built at compile time but never |
| 24 | * installed. |
| 25 | */ |
| 26 | |
| 27 | #include "liblouis.h" |
| 28 | |
| 29 | /** Optional parameters for a test |
| 30 | * |
| 31 | * Used to define optional and named arguments for the check() macro |
| 32 | */ |
| 33 | typedef struct { |
| 34 | const char *display_table; |
| 35 | const formtype *typeform; |
| 36 | const formtype *expected_typeform; |
| 37 | const int cursorPos; |
| 38 | const int mode; |
| 39 | const int direction; |
| 40 | const int diagnostics; |
| 41 | const int *expected_inputPos; |
| 42 | const int *expected_outputPos; |
| 43 | const int expected_cursorPos; |
| 44 | const int max_outlen; |
| 45 | const int real_inlen; |
| 46 | } optional_test_params; |
| 47 | |
| 48 | #define DIRECTION_FORWARD 0 |
| 49 | #define DIRECTION_BACKWARD 1 |
| 50 | #define DIRECTION_BOTH 2 |
| 51 | |
| 52 | /** Check a translation |
| 53 | * |
| 54 | * Check if an input string is translated as expected. |
| 55 | * |
| 56 | * @param tableList comma separated list of tables |
| 57 | * @param input string to translate |
| 58 | * @param expected expected output |
| 59 | * @param display_table (optional) the display table to use (comma separated list of |
| 60 | * files). If not specified the translation table is used. |
| 61 | * @param typeform (optional) the typeform input for forward translation. If not specified |
| 62 | * it defaults to NULL. Only valid for forward translation. |
| 63 | * @param expected_typeform (optional) the expected typeform output for backward |
| 64 | * translation. Used to verify that emphasis indicators in braille are correctly detected |
| 65 | * and the typeform buffer is populated. Only valid for backward translation. |
| 66 | * @param mode (optional) the translation mode. If not specified it defaults to 0. |
| 67 | * @param expected_inputPos (optional) the expected input positions. If not specified |
| 68 | * it defaults to NULL. |
| 69 | * @param expected_outputPos (optional) the expected input positions. If not specified |
| 70 | * it defaults to NULL. |
| 71 | * @param cursorPos (optional) the cursor position for this translation. If not specified |
| 72 | * it defaults to -1. |
| 73 | * @param expected_cursorPos (optional) the expected cursor position after this |
| 74 | * translation. If not specified it defaults to -1. |
| 75 | * @param max_outlen (optional) the maximum length of the output. If not specified it |
| 76 | * defaults to -1. |
| 77 | * @param real_inlen (optional) the length of the portion of the input corresponding to |
| 78 | * the returned output. Defaults to -1. May only be specified if max_outlen is also |
| 79 | * specified, and must be smaller than or equal to the total length of the input. |
| 80 | * @param direction (optional) 0 for forward translation, 1 for backwards translation, |
| 81 | * 2 for both directions. If |
| 82 | * not specified it defaults to 0. |
| 83 | * @param diagnostics (optional) Print diagnostic output on failure if diagnostics is not |
| 84 | * 0. If not specified it defaults to 1. |
| 85 | * @return Return 0 if the translation is as expected and 1 otherwise. |
| 86 | * |
| 87 | * An example how to invoke this is shown below: |
| 88 | * ~~~~~~~~~~~~~~~~~~~~~~ |
| 89 | * result = check(table, word, expected_translation, |
| 90 | * .typeform = typeform, |
| 91 | * .cursorPos = 5); |
| 92 | * ~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | */ |
| 94 | #define check(table, input, expected, ...) \ |
| 95 | check_base(table, input, expected, \ |
| 96 | (optional_test_params){ .display_table = NULL, \ |
| 97 | .typeform = NULL, \ |
| 98 | .expected_typeform = NULL, \ |
| 99 | .cursorPos = -1, \ |
| 100 | .expected_cursorPos = -1, \ |
| 101 | .expected_inputPos = NULL, \ |
| 102 | .expected_outputPos = NULL, \ |
| 103 | .max_outlen = -1, \ |
| 104 | .real_inlen = -1, \ |
| 105 | .mode = 0, \ |
| 106 | .direction = DIRECTION_FORWARD, \ |
| 107 | .diagnostics = 1, \ |
| 108 | __VA_ARGS__ }) |
| 109 | |
| 110 | int |
| 111 | check_base(const char *tableList, const char *input, const char *expected, |
| 112 | optional_test_params in); |
| 113 | |
| 114 | /** Check the cursor positions for a translation |
| 115 | * |
| 116 | * For a given input string iterate over all initial cursor positions |
| 117 | * and check if the returned cursor position equals the one in |
| 118 | * expected_pos at the same index. |
| 119 | * |
| 120 | * Note: This check always translates with compbrlAtCursor and does |
| 121 | * not check the translation. This would not make sense anyway as the |
| 122 | * translation changes depending on the initial cursor position. For |
| 123 | * that reason this function is no longer used in checkyaml. |
| 124 | * |
| 125 | * @return 0 if the cursor position for each initial position in the |
| 126 | * input string equals the one in expected_pos at the same index and 1 |
| 127 | * otherwise. |
| 128 | * @deprecated use the check() function instead |
| 129 | */ |
| 130 | int |
| 131 | check_cursor_pos(const char *tableList, const char *str, const int *expected_pos); |
| 132 | |
| 133 | /** Check if a display table maps characters to the right dots. |
| 134 | * |
| 135 | * The dots are read as Unicode braille. Multiple input characters are |
| 136 | * allowed to map to the same dot pattern. Virtual dots in the actual |
| 137 | * output are discarded. |
| 138 | * |
| 139 | * @return 0 if the result is as expected and 1 otherwise. |
| 140 | */ |
| 141 | int |
| 142 | check_display(const char *displayTableList, const char *input, const char *expected); |
| 143 | |
| 144 | /* Check if a string is hyphenated as expected, by passing the |
| 145 | * expected hyphenation position array. |
| 146 | * |
| 147 | * @return 0 if the hyphenation is as expected and 1 otherwise. |
| 148 | */ |
| 149 | int |
| 150 | check_hyphenation_pos(const char *tableList, const char *str, const char *expected); |
| 151 | |
| 152 | /** Check if a string is hyphenated as expected. |
| 153 | * |
| 154 | * mode is '0' when input is text and '1' when input is braille |
| 155 | * |
| 156 | * @return 0 if the hyphenation is as expected and 1 otherwise. |
| 157 | */ |
| 158 | int |
| 159 | check_hyphenation(const char *tableList, const char *str, const char *expected, int mode); |
| 160 | |
| 161 | /** Helper function to convert a typeform string to the required format |
| 162 | * |
| 163 | * In other words convert a string of '0's, '1's, '2's etc. to an |
| 164 | * array of 0s, 1s, 2s, etc. For example, "0000011111000" is converted |
| 165 | * to {0,0,0,0,0,1,1,1,1,1,0,0,0}. |
| 166 | * |
| 167 | * The caller is responsible for freeing the returned array. |
| 168 | */ |
| 169 | formtype * |
| 170 | convert_typeform(const char *typeform_string); |
| 171 | |
| 172 | void |
| 173 | update_typeform(const char *typeform_string, formtype *typeform, typeforms kind); |
| 174 | |
| 175 | int |
| 176 | parsed_strlen(char *s); |