Files
mars-nwe/tests/nwnss/unicode/test_nwnss_unicode.c
Mario Fetka 0f82de2743
All checks were successful
Source release / source-package (push) Successful in 1m20s
0536 build: introduce libnwnss for imported NSS runtime
2026-06-14 22:34:04 +02:00

194 lines
5.9 KiB
C

#define MARS_NWE_NWNSS_UNICODE
#include "unicodeInit.h"
#include "library/xUnicode.h"
#include "public/zError.h"
#include <stdio.h>
#include <string.h>
extern STATUS LB_UnicodeStartup(void);
extern void LB_UnicodeShutdown(void);
extern NINT LB_GetNssUnicodeVersion(void);
extern unicode_t *LB_componentUnicpy(unicode_t *dest, const unicode_t *src);
extern size_t LB_componentUnilen(const unicode_t *src);
#define CHECK(expr) \
do { \
if (!(expr)) { \
fprintf(stderr, "CHECK failed at %s:%d: %s\n", __FILE__, __LINE__, #expr); \
return 1; \
} \
} while (0)
static int check_unicode_string_helpers(void)
{
unicode_t text[32] = { 'M', 'a', 'r', 's', 0 };
unicode_t copy[32];
unicode_t append[32] = { 'N', 'W', 0 };
unicode_t truncated[4];
unicode_t components[] = { 'S', 'Y', 'S', 0, 'P', 'U', 'B', 0, 0 };
unicode_t component_copy[16];
CHECK(LB_unilen(text) == 4);
CHECK(LB_unicpy(copy, text) == copy);
CHECK(LB_unicmp(copy, text) == 0);
CHECK(LB_unincmp(copy, text, 4) == 0);
CHECK(LB_unincmp(copy, (const unicode_t[]){ 'M', 'a', 'r', 't', 0 }, 3) == 0);
CHECK(LB_unincmp(copy, (const unicode_t[]){ 'M', 'a', 'r', 't', 0 }, 4) < 0);
CHECK(LB_unicat(append, (const unicode_t[]){ 'F', 'S', 0 }) == append);
CHECK(append[0] == 'N');
CHECK(append[1] == 'W');
CHECK(append[2] == 'F');
CHECK(append[3] == 'S');
CHECK(append[4] == 0);
CHECK(LB_unimcpy(truncated, text, 4) == truncated);
CHECK(truncated[0] == 'M');
CHECK(truncated[1] == 'a');
CHECK(truncated[2] == 'r');
CHECK(truncated[3] == 0);
CHECK(LB_componentUnilen(components) == 8);
CHECK(LB_componentUnicpy(component_copy, components) == component_copy);
CHECK(memcmp(component_copy, components, sizeof(components)) == 0);
return 0;
}
static int check_unicode_case_helpers(void)
{
unicode_t mixed[16] = { 'M', 'a', 'R', 'S', '-', 'N', 'w', 'E', 0 };
const unsigned char ascii_upper[] = { 'Z', 0 };
const unsigned char utf8_upper_a_umlaut[] = { 0xc3, 0x84, 0 };
const unsigned char *next = NULL;
CHECK(LB_GetNssUnicodeVersion() == 1);
CHECK(LB_unitolower('A') == 'a');
CHECK(LB_unitoupper('z') == 'Z');
CHECK(LB_unilwr(mixed) == mixed);
CHECK(LB_unicmp(mixed, (const unicode_t[]){ 'm', 'a', 'r', 's', '-', 'n', 'w', 'e', 0 }) == 0);
CHECK(LB_uniupr(mixed) == mixed);
CHECK(LB_unicmp(mixed, (const unicode_t[]){ 'M', 'A', 'R', 'S', '-', 'N', 'W', 'E', 0 }) == 0);
CHECK(LB_uniicmp((const unicode_t[]){ 'S', 'Y', 'S', 0 },
(const unicode_t[]){ 's', 'y', 's', 0 }) == 0);
CHECK(LB_uninicmp((const unicode_t[]){ 'S', 'Y', 'S', '1', 0 },
(const unicode_t[]){ 's', 'y', 's', '2', 0 }, 3) == 0);
CHECK(LB_uninicmp((const unicode_t[]){ 'S', 'Y', 'S', '1', 0 },
(const unicode_t[]){ 's', 'y', 's', '2', 0 }, 4) < 0);
CHECK(utf_tolower(ascii_upper, &next) == 'z');
CHECK(next == ascii_upper + 1);
CHECK(utf_tolower(utf8_upper_a_umlaut, &next) == 0x00e4);
CHECK(next == utf8_upper_a_umlaut + 2);
return 0;
}
static int check_unicode_parse_overrides(void)
{
unicode_t out = 0;
BYTE bytes[4];
bytes[0] = ASCII_WILDCARD_BREAK;
bytes[1] = '?';
bytes[2] = 0;
CHECK(Byte2UniDefaultOverride(&out, bytes) == TRUE);
CHECK(out == UNI_WILD_QMARK);
bytes[0] = ASCII_WILDCARD_BREAK;
bytes[1] = '*';
bytes[2] = 0;
CHECK(Byte2UniDefaultOverride(&out, bytes) == TRUE);
CHECK(out == UNI_WILD_ASTERISK);
bytes[0] = ASCII_WILDCARD_BREAK;
bytes[1] = 'x';
bytes[2] = 0;
CHECK(Byte2UniDefaultOverride(&out, bytes) == TRUE);
CHECK(out == UNI_INVALID2);
bytes[0] = ASCII_WILDCARD_BREAK;
bytes[1] = ASCII_WILDCARD_BREAK;
bytes[2] = 0;
CHECK(Byte2UniDOSOverride(&out, bytes) == TRUE);
CHECK(out == NSSUnicodeFF);
bytes[0] = 'A';
bytes[1] = 0;
CHECK(Byte2UniDefaultOverride(&out, bytes) == FALSE);
CHECK(Byte2UniRawOverride(&out, bytes) == FALSE);
memset(bytes, 0, sizeof(bytes));
CHECK(Uni2ByteOverride(bytes, UNI_WILD_AUG_PERIOD) == TRUE);
CHECK(bytes[0] == ASCII_WILDCARD_BREAK);
CHECK(bytes[1] == ASCII_AUG_PERIOD);
CHECK(bytes[2] == 0);
CHECK(Uni2ByteOverride(bytes, 'A') == FALSE);
return 0;
}
static BOOL custom_byte2uni(unicode_t *output, BYTE *input)
{
if (input[0] == '!') {
*output = (unicode_t)'?';
return TRUE;
}
return FALSE;
}
static BOOL custom_uni2byte(BYTE *output, unicode_t input)
{
if (input == (unicode_t)'?') {
output[0] = '!';
output[1] = 0;
return TRUE;
}
return FALSE;
}
static int check_unicode_converter_registration(void)
{
NINT conversion_type = -1;
unicode_t unicode[8];
char bytes[8];
NINT actual = -1;
unicode_t question[] = { '?', 0 };
CHECK(LB_RegisterUnicodeConverter(&conversion_type, NULL,
custom_uni2byte) == zERR_BAD_PARAMETER_VALUE);
CHECK(LB_RegisterUnicodeConverter(&conversion_type, custom_byte2uni,
NULL) == zERR_BAD_PARAMETER_VALUE);
CHECK(LB_RegisterUnicodeConverter(&conversion_type, custom_byte2uni,
custom_uni2byte) == zOK);
CHECK(conversion_type > NSS_UNI_CONVERSION_LAST_DEFINED);
CHECK(conversion_type < NSS_UNI_CONVERSION_COUNT);
CHECK(LB_ByteToUnicode(conversion_type, unicode, 8, "!", &actual) == zOK);
CHECK(actual == 1);
CHECK(unicode[0] == '?');
CHECK(unicode[1] == 0);
CHECK(LB_UnicodeToByte(conversion_type, bytes, sizeof(bytes),
question, &actual) == zOK);
CHECK(actual == 1);
CHECK(strcmp(bytes, "!") == 0);
LB_UnRegisterUnicodeConverter(conversion_type);
CHECK(LB_ByteToUnicode(conversion_type, unicode, 8, "!", &actual) == zERR_UNICODE_INVALID_CONVERSION_TYPE);
return 0;
}
int main(void)
{
CHECK(LB_UnicodeStartup() == zOK);
CHECK(check_unicode_string_helpers() == 0);
CHECK(check_unicode_case_helpers() == 0);
CHECK(check_unicode_parse_overrides() == 0);
CHECK(check_unicode_converter_registration() == 0);
LB_UnicodeShutdown();
return 0;
}