mirror of
https://github.com/zaerl/mojibake.git
synced 2026-12-01 16:56:40 +01:00
* Rename mjb_for_each_character to mjb_for_each_codepoint * Fix: rename various functions * Fix: coverage
589 lines
24 KiB
C
589 lines
24 KiB
C
/**
|
|
* The Mojibake library
|
|
*
|
|
* This file is distributed under the MIT License. See LICENSE for details.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "../src/utf8.h"
|
|
#include "test.h"
|
|
|
|
static bool next_codepoint(mjb_character *character, mjb_character_position type) {
|
|
printf(" \x1B[31mU+%04X\x1B[0m", (unsigned int)character->codepoint);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool has_only_latin1(const char *source, size_t source_size) {
|
|
uint8_t state = MJB_UTF_ACCEPT;
|
|
mjb_codepoint current_codepoint = 0;
|
|
|
|
for(size_t i = 0; i < source_size && source[i]; ++i) {
|
|
// Find next codepoint.
|
|
state = mjb_utf8_decode_step(state, source[i], ¤t_codepoint);
|
|
|
|
if(state == MJB_UTF_REJECT) {
|
|
return false;
|
|
}
|
|
|
|
if(state != MJB_UTF_ACCEPT) {
|
|
continue;
|
|
}
|
|
|
|
if(current_codepoint >= 0x100) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static int check_normalization(char *source, size_t source_size, char *normalized,
|
|
size_t normalized_size, mjb_normalization form, unsigned int current_line, const char *step) {
|
|
mjb_result result = { NULL, 0, false };
|
|
char test_name[128];
|
|
|
|
MJB_TEST_COVERAGE(mjb_normalize);
|
|
mjb_status status = mjb_normalize(source, source_size, MJB_ENC_UTF_8, form, MJB_ENC_UTF_8,
|
|
&result);
|
|
|
|
if(status != MJB_STATUS_OK) {
|
|
snprintf(test_name, 128, "#%u %s", current_line, step);
|
|
ATT_ASSERT(true, false, test_name)
|
|
|
|
if(result.output != NULL && result.output != source) {
|
|
mjb_free(result.output);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
snprintf(test_name, 128, "#%u %s", current_line, step);
|
|
|
|
if(is_exit_on_error()) {
|
|
if(strcmp(result.output, normalized) != 0) {
|
|
mjb_status print_status;
|
|
|
|
printf("\n%s: source:", test_name);
|
|
print_status = mjb_for_each_codepoint(source, source_size, MJB_ENC_UTF_8,
|
|
next_codepoint);
|
|
(void)print_status; // we can ignore this.
|
|
printf("\nExpected: ");
|
|
print_status = mjb_for_each_codepoint(normalized, normalized_size, MJB_ENC_UTF_8,
|
|
next_codepoint);
|
|
(void)print_status; // we can ignore this.
|
|
printf("\nGot: ");
|
|
print_status = mjb_for_each_codepoint(result.output, result.output_size, MJB_ENC_UTF_8,
|
|
next_codepoint);
|
|
(void)print_status; // we can ignore this.
|
|
puts("");
|
|
}
|
|
}
|
|
|
|
// clang-format off
|
|
int test_ret = ATT_ASSERT(result.output, normalized, test_name)
|
|
|
|
// (strange reformatting of the ATT_ASSERT macro)
|
|
if(result.output != NULL && result.output != source) {
|
|
mjb_free(result.output);
|
|
}
|
|
// clang-format on
|
|
|
|
if(!test_ret) {
|
|
return test_ret;
|
|
}
|
|
|
|
bool is_ascii = mjb_is_ascii(source, source_size);
|
|
|
|
// An ASCII string should not be normalized.
|
|
if(is_ascii) {
|
|
snprintf(test_name, 128, "#%u %s is ASCII and not normalized", current_line, step);
|
|
test_ret = ATT_ASSERT(result.transformed, false, test_name)
|
|
}
|
|
|
|
if(form == MJB_NORMALIZATION_NFC && has_only_latin1(source, source_size)) {
|
|
snprintf(test_name, 128, "#%u %s \"%s\" is Latin-1 and not normalized", current_line, step,
|
|
source);
|
|
test_ret = ATT_ASSERT(result.transformed, false, test_name)
|
|
}
|
|
|
|
MJB_TEST_COVERAGE(mjb_normalize_into);
|
|
size_t required = 0;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(source, source_size, MJB_ENC_UTF_8, form, MJB_ENC_UTF_8,
|
|
NULL, &required),
|
|
MJB_STATUS_OK, test_name)
|
|
ATT_ASSERT(required, normalized_size, test_name)
|
|
|
|
char into_output[513];
|
|
ATT_ASSERT((int)(required < sizeof(into_output)), true, test_name)
|
|
|
|
if(required >= sizeof(into_output)) {
|
|
return 0;
|
|
}
|
|
|
|
memset(into_output, '#', sizeof(into_output));
|
|
size_t output_size = required;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(source, source_size, MJB_ENC_UTF_8, form, MJB_ENC_UTF_8,
|
|
into_output, &output_size),
|
|
MJB_STATUS_OK, test_name)
|
|
ATT_ASSERT(output_size, normalized_size, test_name)
|
|
ATT_ASSERT((int)memcmp(into_output, normalized, normalized_size), 0, test_name)
|
|
ATT_ASSERT(into_output[output_size], '#', test_name)
|
|
|
|
return test_ret;
|
|
}
|
|
|
|
static void check_nfkc_casefold_into(const char *source, size_t source_size,
|
|
mjb_encoding output_encoding, const mjb_result *allocated, const char *name) {
|
|
size_t required = 0;
|
|
|
|
MJB_TEST_COVERAGE(mjb_nfkc_casefold_into);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(source, source_size, MJB_ENC_UTF_8, output_encoding,
|
|
NULL, &required),
|
|
MJB_STATUS_OK, name)
|
|
ATT_ASSERT(required, allocated->output_size, name)
|
|
|
|
char output[513];
|
|
ATT_ASSERT((int)(required < sizeof(output)), true, name)
|
|
|
|
if(required >= sizeof(output)) {
|
|
return;
|
|
}
|
|
|
|
memset(output, '#', sizeof(output));
|
|
size_t output_size = required;
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(source, source_size, MJB_ENC_UTF_8, output_encoding,
|
|
output, &output_size),
|
|
MJB_STATUS_OK, name)
|
|
ATT_ASSERT(output_size, allocated->output_size, name)
|
|
ATT_ASSERT((int)memcmp(output, allocated->output, output_size), 0, name)
|
|
ATT_ASSERT(output[output_size], '#', name)
|
|
}
|
|
|
|
static void check_nfkc_casefold(const char *source, size_t source_size, const char *expected,
|
|
size_t expected_size, const char *name) {
|
|
mjb_result result = { NULL, 0, false };
|
|
|
|
MJB_TEST_COVERAGE(mjb_nfkc_casefold);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(source, source_size, MJB_ENC_UTF_8, MJB_ENC_UTF_8, &result),
|
|
MJB_STATUS_OK, name)
|
|
ATT_ASSERT(result.output_size, expected_size, name)
|
|
ATT_ASSERT((int)memcmp(result.output, expected, expected_size), 0, name)
|
|
ATT_ASSERT(result.transformed, true, name)
|
|
check_nfkc_casefold_into(source, source_size, MJB_ENC_UTF_8, &result, name);
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK, name)
|
|
}
|
|
|
|
static void test_nfkc_casefold(void) {
|
|
mjb_result result = { NULL, 0, false };
|
|
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(NULL, 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8, &result),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold rejects NULL buffer")
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold("A", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold rejects NULL result")
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold("", 0, MJB_ENC_UTF_8, MJB_ENC_UTF_8, &result),
|
|
MJB_STATUS_OK, "NFKC casefold accepts empty input")
|
|
ATT_ASSERT(result.transformed, false, "NFKC casefold empty input is borrowed")
|
|
|
|
size_t into_size = 9;
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(NULL, 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8, NULL,
|
|
&into_size),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold into rejects NULL buffer")
|
|
ATT_ASSERT(into_size, (size_t)0, "NFKC casefold into clears size after invalid input")
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into("A", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8, NULL, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold into rejects NULL size")
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into("", 0, MJB_ENC_UTF_8, MJB_ENC_UTF_8, NULL,
|
|
&into_size),
|
|
MJB_STATUS_OK, "NFKC casefold into measures empty input")
|
|
ATT_ASSERT(into_size, (size_t)0, "NFKC casefold into empty size")
|
|
|
|
const char *into_input = "Stra\xC3\x9F"
|
|
"e\xC2\xAD";
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(into_input, 9, MJB_ENC_UTF_8, MJB_ENC_UTF_8, NULL,
|
|
&into_size),
|
|
MJB_STATUS_OK, "NFKC casefold into queries required size")
|
|
ATT_ASSERT(into_size, (size_t)7, "NFKC casefold into required size")
|
|
|
|
char into_output[8] = { '#', '#', '#', '#', '#', '#', '#', '#' };
|
|
into_size = 6;
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(into_input, 9, MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
|
into_output, &into_size),
|
|
MJB_STATUS_OUTPUT_TOO_SMALL, "NFKC casefold into reports a small output buffer")
|
|
ATT_ASSERT(into_size, (size_t)7, "NFKC casefold into preserves required size")
|
|
ATT_ASSERT((int)memcmp(into_output, "########", sizeof(into_output)), 0,
|
|
"NFKC casefold into leaves a small buffer untouched")
|
|
|
|
into_size = 7;
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(into_input, 9, MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
|
into_output, &into_size),
|
|
MJB_STATUS_OK, "NFKC casefold into writes into exact capacity")
|
|
ATT_ASSERT(into_size, (size_t)7, "NFKC casefold into written size")
|
|
ATT_ASSERT((int)memcmp(into_output, "strasse", 7), 0, "NFKC casefold into output")
|
|
ATT_ASSERT(into_output[7], '#', "NFKC casefold into does not write a terminator")
|
|
|
|
check_nfkc_casefold("Stra\xC3\x9F"
|
|
"e\xC2\xAD",
|
|
9, "strasse", 7, "NFKC casefold folds sharp s and removes soft hyphen");
|
|
check_nfkc_casefold("\xEF\xAC\x83", 3, "ffi", 3,
|
|
"NFKC casefold expands compatibility ligature");
|
|
check_nfkc_casefold("A\xCC\x8A", 3, "\xC3\xA5", 2, "NFKC casefold composes mappings to NFC");
|
|
check_nfkc_casefold("\xE2\x84\xAA", 3, "k", 1,
|
|
"NFKC casefold applies compatibility case mapping");
|
|
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold("A", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, &result),
|
|
MJB_STATUS_OK, "NFKC casefold supports UTF-16 output")
|
|
ATT_ASSERT(result.output_size, (size_t)2, "NFKC casefold UTF-16 output size")
|
|
ATT_ASSERT((int)memcmp(result.output, "a\0", 2), 0, "NFKC casefold UTF-16 output bytes")
|
|
check_nfkc_casefold_into("A", 1, MJB_ENC_UTF_16LE, &result,
|
|
"NFKC casefold into supports UTF-16 output");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK, "NFKC casefold frees UTF-16 output")
|
|
}
|
|
|
|
// Verify Unicode R5 against every explicit NFKC_CF mapping. The normative string transform maps
|
|
// each character and then applies NFC, so expected property values are normalized before
|
|
// comparison.
|
|
static void test_nfkc_casefold_file(void) {
|
|
FILE *file = fopen("./utils/generate/unicode-data/UCD/DerivedNormalizationProps.txt", "r");
|
|
|
|
if(file == NULL) {
|
|
ATT_ASSERT("Not opened", "Opened file", "Valid NFKC casefold data file")
|
|
return;
|
|
}
|
|
|
|
char line[2048];
|
|
unsigned int current_line = 1;
|
|
|
|
while(fgets(line, sizeof(line), file) != NULL) {
|
|
char *first_semicolon = strchr(line, ';');
|
|
|
|
if(first_semicolon == NULL) {
|
|
++current_line;
|
|
continue;
|
|
}
|
|
|
|
char *property = first_semicolon + 1;
|
|
while(*property == ' ') {
|
|
++property;
|
|
}
|
|
|
|
if(strncmp(property, "NFKC_CF", 7) != 0) {
|
|
++current_line;
|
|
continue;
|
|
}
|
|
|
|
char *second_semicolon = strchr(property, ';');
|
|
if(second_semicolon == NULL) {
|
|
++current_line;
|
|
continue;
|
|
}
|
|
|
|
*first_semicolon = '\0';
|
|
char *comment = strchr(second_semicolon + 1, '#');
|
|
if(comment != NULL) {
|
|
*comment = '\0';
|
|
}
|
|
|
|
unsigned int start;
|
|
unsigned int end;
|
|
if(sscanf(line, "%X..%X", &start, &end) != 2) {
|
|
if(sscanf(line, "%X", &start) != 1) {
|
|
++current_line;
|
|
continue;
|
|
}
|
|
end = start;
|
|
}
|
|
|
|
char expected_mapping[128];
|
|
size_t mapping_size = get_string_from_codepoints(second_semicolon + 1,
|
|
sizeof(expected_mapping), expected_mapping);
|
|
mjb_result expected;
|
|
ATT_ASSERT_STATUS(mjb_normalize(expected_mapping, mapping_size, MJB_ENC_UTF_8,
|
|
MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, &expected),
|
|
MJB_STATUS_OK, "Normalize expected NFKC casefold mapping")
|
|
|
|
for(unsigned int codepoint = start; codepoint <= end; ++codepoint) {
|
|
char source[8];
|
|
unsigned int source_size = mjb_codepoint_encode(codepoint, source, sizeof(source),
|
|
MJB_ENC_UTF_8);
|
|
mjb_result actual;
|
|
char test_name[128];
|
|
snprintf(test_name, sizeof(test_name), "NFKC_CF #%u U+%04X", current_line, codepoint);
|
|
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(source, source_size, MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
|
&actual),
|
|
MJB_STATUS_OK, test_name)
|
|
ATT_ASSERT(actual.output_size, expected.output_size, test_name)
|
|
ATT_ASSERT((int)memcmp(actual.output, expected.output, expected.output_size), 0,
|
|
test_name)
|
|
check_nfkc_casefold_into(source, source_size, MJB_ENC_UTF_8, &actual, test_name);
|
|
ATT_ASSERT_STATUS(mjb_result_free(&actual), MJB_STATUS_OK, test_name)
|
|
}
|
|
|
|
if(expected.transformed) {
|
|
mjb_free(expected.output);
|
|
}
|
|
++current_line;
|
|
}
|
|
|
|
fclose(file);
|
|
}
|
|
|
|
/**
|
|
* Run utils/generate/unicode-data/UCD/NormalizationTest.txt tests
|
|
*/
|
|
int test_normalization(void *arg) {
|
|
char line[1024];
|
|
unsigned int current_line = 1;
|
|
// unsigned int index = 0;
|
|
mjb_result guard_result;
|
|
|
|
test_nfkc_casefold();
|
|
test_nfkc_casefold_file();
|
|
|
|
ATT_ASSERT_STATUS(mjb_normalize(NULL, 1, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8,
|
|
&guard_result),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize rejects NULL buffer")
|
|
ATT_ASSERT_STATUS(mjb_normalize("", 0, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8,
|
|
NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize rejects NULL result")
|
|
|
|
char noncharacter_source[] = "\xF1\x8F\xBF\xBE>\xCC\xB8";
|
|
char noncharacter_normalized[] = "\xF1\x8F\xBF\xBE\xE2\x89\xAF";
|
|
check_normalization(noncharacter_source, 7, noncharacter_normalized, 7, MJB_NORMALIZATION_NFC,
|
|
0, "NFC preserves noncharacters while composing");
|
|
char hangul_source[] = "\xE1\x84\x8F\xE1\x85\xB5\xE1\x86\xBF.>\xCC\xB8";
|
|
char hangul_normalized[] = "\xED\x82\xBC.\xE2\x89\xAF";
|
|
check_normalization(hangul_source, 13, hangul_normalized, 7, MJB_NORMALIZATION_NFC, 0,
|
|
"NFC composes after Hangul compaction");
|
|
|
|
ATT_ASSERT_STATUS(mjb_normalize("A", 1, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_16LE,
|
|
&guard_result),
|
|
MJB_STATUS_OK, "Normalize converts output encoding for already-normalized input")
|
|
ATT_ASSERT(guard_result.transformed, true,
|
|
"Normalize converted already-normalized input transformed")
|
|
ATT_ASSERT(guard_result.output_size, (size_t)2,
|
|
"Normalize converted already-normalized input size")
|
|
ATT_ASSERT((int)memcmp(guard_result.output, "A\0", 2), 0,
|
|
"Normalize converted already-normalized input bytes")
|
|
if(guard_result.transformed) {
|
|
mjb_free(guard_result.output);
|
|
}
|
|
|
|
size_t into_size = 9;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(NULL, 1, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_8, NULL, &into_size),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize into rejects NULL buffer")
|
|
ATT_ASSERT(into_size, (size_t)0, "Normalize into clears size after invalid input")
|
|
ATT_ASSERT_STATUS(mjb_normalize_into("A", 1, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_8, NULL, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize into rejects NULL size")
|
|
|
|
into_size = 9;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into("A", 1, MJB_ENC_UTF_8, (mjb_normalization)99,
|
|
MJB_ENC_UTF_8, NULL, &into_size),
|
|
MJB_STATUS_INVALID_FORM, "Normalize into rejects invalid form")
|
|
ATT_ASSERT(into_size, (size_t)0, "Normalize into clears size after invalid form")
|
|
|
|
into_size = 9;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into("", 0, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_8, NULL, &into_size),
|
|
MJB_STATUS_OK, "Normalize into measures empty input")
|
|
ATT_ASSERT(into_size, (size_t)0, "Normalize into empty size")
|
|
|
|
const char *into_input = "Cafe\xCC\x81";
|
|
into_size = 0;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(into_input, 6, MJB_ENC_UTF_8,
|
|
MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, NULL, &into_size),
|
|
MJB_STATUS_OK, "Normalize into queries required size")
|
|
ATT_ASSERT(into_size, (size_t)5, "Normalize into required size")
|
|
|
|
char into_output[6] = { '#', '#', '#', '#', '#', '#' };
|
|
into_size = 4;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(into_input, 6, MJB_ENC_UTF_8,
|
|
MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, into_output, &into_size),
|
|
MJB_STATUS_OUTPUT_TOO_SMALL, "Normalize into reports a small output buffer")
|
|
ATT_ASSERT(into_size, (size_t)5, "Normalize into preserves required size")
|
|
ATT_ASSERT((int)memcmp(into_output, "######", sizeof(into_output)), 0,
|
|
"Normalize into leaves a small buffer untouched")
|
|
|
|
into_size = 5;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(into_input, 6, MJB_ENC_UTF_8,
|
|
MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, into_output, &into_size),
|
|
MJB_STATUS_OK, "Normalize into writes into exact capacity")
|
|
ATT_ASSERT(into_size, (size_t)5, "Normalize into written size")
|
|
ATT_ASSERT((int)memcmp(into_output, "Caf\xC3\xA9", 5), 0, "Normalize into output")
|
|
ATT_ASSERT(into_output[5], '#', "Normalize into does not write a terminator")
|
|
|
|
char nfd_output[4] = { '#', '#', '#', '#' };
|
|
into_size = 0;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into("\xC3\xA9", 2, MJB_ENC_UTF_8,
|
|
MJB_NORMALIZATION_NFD, MJB_ENC_UTF_8, NULL, &into_size),
|
|
MJB_STATUS_OK, "Normalize into measures decomposed output")
|
|
ATT_ASSERT(into_size, (size_t)3, "Normalize into decomposed required size")
|
|
ATT_ASSERT_STATUS(mjb_normalize_into("\xC3\xA9", 2, MJB_ENC_UTF_8,
|
|
MJB_NORMALIZATION_NFD, MJB_ENC_UTF_8, nfd_output, &into_size),
|
|
MJB_STATUS_OK, "Normalize into writes decomposed output")
|
|
ATT_ASSERT((int)memcmp(nfd_output, "e\xCC\x81", 3), 0,
|
|
"Normalize into decomposed output bytes")
|
|
ATT_ASSERT(nfd_output[3], '#', "Normalize into decomposed output has no terminator")
|
|
|
|
char utf16_output[3] = { '#', '#', '#' };
|
|
into_size = sizeof(utf16_output) - 1;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into("A", 1, MJB_ENC_UTF_8, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_16LE, utf16_output, &into_size),
|
|
MJB_STATUS_OK, "Normalize into converts already-normalized output encoding")
|
|
ATT_ASSERT(into_size, (size_t)2, "Normalize into converted output size")
|
|
ATT_ASSERT((int)memcmp(utf16_output, "A\0", 2), 0,
|
|
"Normalize into converted output bytes")
|
|
ATT_ASSERT(utf16_output[2], '#', "Normalize into converted output has no terminator")
|
|
|
|
// 256 characters is enough for any test.
|
|
const char source[256] = { 0 };
|
|
const char nfc[256] = { 0 };
|
|
const char nfd[256] = { 0 };
|
|
const char nfkc[256] = { 0 };
|
|
const char nfkd[256] = { 0 };
|
|
|
|
size_t source_size = 0;
|
|
size_t nfc_size = 0;
|
|
size_t nfd_size = 0;
|
|
size_t nfkc_size = 0;
|
|
size_t nfkd_size = 0;
|
|
|
|
FILE *file = fopen("./utils/generate/unicode-data/UCD/NormalizationTest.txt", "r");
|
|
|
|
if(file == NULL) {
|
|
ATT_ASSERT("Not opened", "Opened file", "Valid normalization test file")
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Parse the file
|
|
while(fgets(line, 1024, file)) {
|
|
if(line[0] == '#' || line[0] == '@' || strnlen(line, 1024) <= 1) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
|
|
// Reset everything
|
|
source_size = 0;
|
|
nfc_size = 0;
|
|
nfd_size = 0;
|
|
nfkc_size = 0;
|
|
nfkd_size = 0;
|
|
|
|
char *token, *string, *tofree;
|
|
tofree = string = strdup(line);
|
|
unsigned int field = 0;
|
|
|
|
while((token = mjb_test_strsep(&string, ";")) != NULL) {
|
|
switch(field) {
|
|
case 0: // Source
|
|
source_size = get_string_from_codepoints(token, 256, (char *)source);
|
|
break;
|
|
|
|
case 1: // NFC
|
|
nfc_size = get_string_from_codepoints(token, 256, (char *)nfc);
|
|
break;
|
|
|
|
case 2: // NFD
|
|
nfd_size = get_string_from_codepoints(token, 256, (char *)nfd);
|
|
break;
|
|
|
|
case 3: // NFKC
|
|
nfkc_size = get_string_from_codepoints(token, 256, (char *)nfkc);
|
|
break;
|
|
|
|
case 4: // NFKD
|
|
nfkd_size = get_string_from_codepoints(token, 256, (char *)nfkd);
|
|
break;
|
|
}
|
|
|
|
// Skip trailing comments
|
|
if(++field == 5) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
free(tofree);
|
|
|
|
/**
|
|
* NFD
|
|
* nfd == toNFD(source) == toNFD(nfc) == toNFD(nfd)
|
|
* nfkd == toNFD(nfkc) == toNFD(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfd, nfd_size,
|
|
MJB_NORMALIZATION_NFD, current_line, "nfd == toNFD(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfd, nfd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfd, nfd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfkd)");
|
|
|
|
/**
|
|
* NFKD
|
|
* nfkd == toNFKD(source) == toNFKD(nfc) == toNFKD(nfd) == toNFKD(nfkc) == toNFKD(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfkd, nfkd_size,
|
|
MJB_NORMALIZATION_NFKD, current_line, "nfkd == toNFKD(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFKD,
|
|
current_line, "nfkd == toNFKD(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFKD,
|
|
current_line, "nfkd == toNFKD(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkd, nfkd_size,
|
|
MJB_NORMALIZATION_NFKD, current_line, "nfkd == toNFKD(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkd, nfkd_size,
|
|
MJB_NORMALIZATION_NFKD, current_line, "nfkd == toNFKD(nfkd)");
|
|
|
|
/**
|
|
* NFC
|
|
* nfc == toNFC(source) == toNFC(nfc) == toNFC(nfd)
|
|
* nfkc == toNFC(nfkc) == toNFC(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfc, nfc_size,
|
|
MJB_NORMALIZATION_NFC, current_line, "nfc == toNFC(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfc, nfc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfc, nfc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfkd)");
|
|
|
|
/**
|
|
* NFKC
|
|
* nfkc == toNFKC(source) == toNFKC(nfc) == toNFKC(nfd) == toNFKC(nfkc) == toNFKC(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfkc, nfkc_size,
|
|
MJB_NORMALIZATION_NFKC, current_line, "nfkc == toNFKC(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFKC,
|
|
current_line, "nfkc == toNFKC(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFKC,
|
|
current_line, "nfkc == toNFKC(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkc, nfkc_size,
|
|
MJB_NORMALIZATION_NFKC, current_line, "nfkc == toNFKC(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkc, nfkc_size,
|
|
MJB_NORMALIZATION_NFKC, current_line, "nfkc == toNFKC(nfkd)");
|
|
|
|
memset((void *)source, 0, 256);
|
|
memset((void *)nfc, 0, 256);
|
|
memset((void *)nfd, 0, 256);
|
|
memset((void *)nfkc, 0, 256);
|
|
memset((void *)nfkd, 0, 256);
|
|
|
|
++current_line;
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
return 0;
|
|
}
|