Files
mojibake/tests/utils/utils.c
T
Francesco Bigiarini 6c06fa9150 Add NULL terminated strings (#39)
* Remove MJB_ALLOW_EMBEDDED_NULLS

* Fix: doc

* Add mjb_resolve_input_byte_length and MJB_NUL_TERMINATED

* Remove obsolete tests

* New test

* Fix: example test

* Minor change

* Fix: lint
2026-07-22 22:56:39 +02:00

167 lines
4.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The Mojibake library
*
* This file is distributed under the MIT License. See LICENSE for details.
*/
#include "../test.h"
/**
* Get an UTF-8 string from a string of hex-encoded codepoints
* Example: "0061 0062 0063", gives "abc"
*/
size_t get_string_from_codepoints(char *buffer, size_t byte_length, char *codepoints) {
char *token, *string, *tofree;
tofree = string = strdup(buffer != NULL ? (buffer[0] == ' ' ? buffer + 1 : buffer) : "");
unsigned int index = 0;
while((token = mjb_test_strsep(&string, " ")) != NULL) {
if(strlen(token) == 0) {
continue; // Skip empty tokens
}
mjb_codepoint codepoint = strtoul((const char *)token, NULL, 16);
if(codepoint == 0) {
continue; // Skip invalid codepoints
}
unsigned int encoded_size = mjb_codepoint_encode(codepoint, codepoints + index,
byte_length - index, MJB_ENC_UTF_8);
if(encoded_size == 0) {
break; // Failed to encode
}
index += encoded_size;
}
codepoints[index] = '\0';
free(tofree);
return index;
}
/**
* Call mjb_map_case and return the output string, or NULL when the call fails.
*/
char *run_mjb_map_case(const char *buffer, size_t byte_length, mjb_map_case_type type,
mjb_encoding encoding) {
mjb_result result = { NULL, 0, false };
if(mjb_map_case(buffer, byte_length, encoding, type, encoding, &result) != MJB_STATUS_OK) {
return NULL;
}
MJB_TEST_COVERAGE(mjb_map_case_into);
size_t required = 0;
mjb_status status = mjb_map_case_into(buffer, byte_length, encoding, type, encoding, NULL,
&required);
ATT_ASSERT_STATUS(status, MJB_STATUS_OK, "Case into sizing matches allocating case mapping")
ATT_ASSERT(required, result.output_size, "Case into required size matches allocated size")
if(status != MJB_STATUS_OK || required != result.output_size) {
return result.output;
}
unsigned char *output = (unsigned char *)malloc(required + 1);
ATT_ASSERT(output != NULL, true, "Allocate case into comparison buffer")
if(output == NULL) {
return result.output;
}
memset(output, 0xA5, required + 1);
size_t output_size = required;
status = mjb_map_case_into(buffer, byte_length, encoding, type, encoding, output, &output_size);
ATT_ASSERT_STATUS(status, MJB_STATUS_OK, "Case into output matches allocating case mapping")
ATT_ASSERT(output_size, result.output_size, "Case into written size matches allocated size")
ATT_ASSERT(memcmp(output, result.output, result.output_size), 0,
"Case into bytes match allocating case mapping")
ATT_ASSERT((unsigned int)output[required], 0xA5u, "Case into does not write a terminator")
free(output);
return result.output;
}
void read_test_file(const char *filename, test_file_callback callback) {
char line[16384];
char generated_input[1024];
mjb_break_type expected_types[1024];
unsigned int current_line = 1;
FILE *file = fopen(filename, "r");
if(file == NULL) {
ATT_ASSERT("Not opened", "Opened file", "Valid breaking test file")
return;
}
while(fgets(line, 16384, file)) {
if(line[0] == '#' || strnlen(line, 16384) <= 1) {
++current_line;
continue;
}
char *token, *string, *tofree;
tofree = string = strdup(line + 3);
unsigned int types_i = 0;
unsigned int i = 0;
unsigned int generated_index = 0;
for(size_t expected_i = 0; expected_i < 1024; ++expected_i) {
expected_types[expected_i] = MJB_BT_NOT_SET;
}
memset(generated_input, 0, 1024);
// × (U+00D7) = 0xC3 0x97
// ÷ (U+00F7) = 0xC3 0xB7
while((token = mjb_test_strsep(&string, " ")) != NULL) {
if(token == NULL || token[0] == '\0') {
i = 0;
continue;
}
// Odd index means break type
if(i++ % 2 != 0) {
if((unsigned char)token[1] == 0xB7) { // ÷
expected_types[types_i++] = MJB_BT_ALLOWED;
if((unsigned char)token[2] == 0x09) { // Tab # comment until next line
break;
}
continue;
} else if((unsigned char)token[1] == 0x97) {
expected_types[types_i++] = MJB_BT_NO_BREAK;
continue;
}
}
mjb_codepoint codepoint = strtoul((const char *)(token), NULL, 16);
unsigned int encoded_size = mjb_codepoint_encode(codepoint,
generated_input + generated_index, 1024 - generated_index, MJB_ENC_UTF_8);
generated_index += encoded_size;
}
generated_input[generated_index] = '\0';
(*callback)(generated_input, generated_index, current_line, expected_types);
free(tofree);
++current_line;
}
fclose(file);
}