#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <zap/math.h>
#include <zap/mem.h>
#define test(_expr,_val,_cmp) \
fprintf(stderr,"(" #_expr "): %" PRIXMAX "\n(" #_val "): %" PRIXMAX "\n",(uintmax_t)(_expr),(uintmax_t)(_val)); \
if (!((_expr) _cmp (_val))) { \
fputs("\x1B[91mFailure\x1B[0m (" #_cmp ")\n\n",stderr); \
return EXIT_FAILURE; \
} \
fputs("\x1B[92mSuccess\x1B[0m (" #_cmp ")\n\n",stderr);
int main(void) {
test(sizeof (zap_byte),sizeof (unsigned char),==)
/* math */
{
zap_i8 const val = zap_abs8(-0x80);
test(val,0x80u,==)
}
{
zap_quotrem8 const quotrem = zap_divmod8(0x45u,0x5u);
test(quotrem.quot,0xDu,==)
test(quotrem.rem, 0x4u,==)
}
{
/* 3^(-3) */
zap_i02 const tmp = zap_exp04(0x3,0x3u);
zap_i02 const val = zap_divmod02(0x10000u,tmp).quot;
test(val,0x97Bu,==)
}
/* mem */
{
zap_i8 const src[0x3Fu] = {
0x02u,
0x03u,
0x05u,
0x07u,
0x0Bu,
0x0Du,
0x11u,
0x13u,
0x17u,
0x1Du,
0x1Fu,
0x25u,
0x29u,
0x2Bu,
0x2Fu,
0x35u,
0x3Bu,
0x3Du,
0x43u,
0x47u,
0x49u,
0x4Fu,
0x53u,
0x59u,
0x61u,
0x65u,
0x67u,
0x6Bu,
0x6Du,
0x71u,
0x7Fu,
0x83u,
0x89u,
0x8Bu,
0x95u,
0x97u,
0x9Du,
0xA3u,
0xA7u,
0xADu,
0xB3u,
0xB5u,
0xBFu,
0xC1u,
0xC5u,
0xC7u,
0xD3u,
0xDFu,
0xE3u,
0xE5u,
0xE9u,
0xEFu,
0xF1u,
0xFBu,
};
zap_i8 dest[sizeof (src)];
zap_cp(dest,src,sizeof (src));
test(dest[0x00u],src[0x00u],==)
test(dest[0x01u],src[0x01u],==)
test(dest[0x02u],src[0x02u],==)
test(dest[0x03u],src[0x03u],==)
test(dest[0x04u],src[0x04u],==)
test(dest[0x05u],src[0x05u],==)
test(dest[0x06u],src[0x06u],==)
test(dest[0x07u],src[0x07u],==)
test(dest[0x08u],src[0x08u],==)
test(dest[0x09u],src[0x09u],==)
test(dest[0x0Au],src[0x0Au],==)
test(dest[0x0Bu],src[0x0Bu],==)
test(dest[0x0Cu],src[0x0Cu],==)
test(dest[0x0Du],src[0x0Du],==)
test(dest[0x0Eu],src[0x0Eu],==)
test(dest[0x0Fu],src[0x0Fu],==)
test(dest[0x10u],src[0x10u],==)
test(dest[0x11u],src[0x11u],==)
test(dest[0x12u],src[0x12u],==)
test(dest[0x13u],src[0x13u],==)
test(dest[0x14u],src[0x14u],==)
test(dest[0x15u],src[0x15u],==)
test(dest[0x16u],src[0x16u],==)
test(dest[0x17u],src[0x17u],==)
test(dest[0x18u],src[0x18u],==)
test(dest[0x19u],src[0x19u],==)
test(dest[0x1Au],src[0x1Au],==)
test(dest[0x1Bu],src[0x1Bu],==)
test(dest[0x1Cu],src[0x1Cu],==)
test(dest[0x1Du],src[0x1Du],==)
test(dest[0x1Eu],src[0x1Eu],==)
test(dest[0x1Fu],src[0x1Fu],==)
test(dest[0x20u],src[0x20u],==)
test(dest[0x21u],src[0x21u],==)
test(dest[0x22u],src[0x22u],==)
}
{
zap_i04 val = 0x0u;
zap_fill(&val,~(zap_byte)0x0u,sizeof (val));
test(val,~(zap_i04)0x0u,==)
}
{
zap_i01 const val0 = 0x4545u;
zap_i01 const val1 = 0x4545u;
zap_i01 const val2 = 0x4560u;
test(zap_eq(&val0,&val1,sizeof (val0)),zap_true,==)
test(zap_eq(&val0,&val2,sizeof (val0)),zap_false,==)
}
{
zap_i8 const arr[] = {
0x45u,
0x60u,
0x7Fu,
0xFFu,
};
test(zap_srch(arr,0x7Fu,sizeof (arr)),&arr[0x2u],==)
}
{
zap_chr8 const utf8[] = {
0x26u,
0xC3u,
0xB0u,
0xE0u,
0xB6u,
0x9Eu,
0xE2u,
0x86u,
0x8Au,
0xE2u,
0x86u,
0x8Bu,
0xF0u,
0x9Fu,
0x97u,
0xBFu,
0x0u,
};
zap_sz const enclen = zap_utf8declen(utf8);
test(enclen,0x6u,==)
zap_chr02 * const utf02 = malloc(sizeof (zap_chr02) * (enclen + 0x1u));
if (utf02 == zap_nullptr) {
fputs("Memory allocation failed!\n",stderr);
return EXIT_FAILURE;
}
zap_utf8dec(utf02,utf8);
test(utf02[0x0u],0x26u, ==)
test(utf02[0x1u],0xF0u, ==)
test(utf02[0x2u],0xD9Eu, ==)
test(utf02[0x3u],0x218Au, ==)
test(utf02[0x4u],0x218Bu, ==)
test(utf02[0x5u],0x1F5FFu,==)
test(utf02[0x6u],0x0u, ==)
free(utf02);
}
{
zap_chr02 const utf02[] = {
0x26u,
0xF0u,
0xD9Eu,
0x218Au,
0x218Bu,
0x1F5FFu,
0x0u,
};
zap_sz const enclen = zap_utf8enclen(utf02);
test(enclen,0x10u,==)
zap_chr8 * const utf8 = malloc(enclen + 0x1u);
if (utf8 == zap_nullptr) {
fputs("Memory allocation failed!\n",stderr);
return EXIT_FAILURE;
}
zap_utf8enc(utf8,utf02);
test(utf8[0x0u], 0x26u,==)
test(utf8[0x1u], 0xC3u,==)
test(utf8[0x2u], 0xB0u,==)
test(utf8[0x3u], 0xE0u,==)
test(utf8[0x4u], 0xB6u,==)
test(utf8[0x5u], 0x9Eu,==)
test(utf8[0x6u], 0xE2u,==)
test(utf8[0x7u], 0x86u,==)
test(utf8[0x8u], 0x8Au,==)
test(utf8[0x9u], 0xE2u,==)
test(utf8[0xAu], 0x86u,==)
test(utf8[0xBu], 0x8Bu,==)
test(utf8[0xCu], 0xF0u,==)
test(utf8[0xDu], 0x9Fu,==)
test(utf8[0xEu], 0x97u,==)
test(utf8[0xFu], 0xBFu,==)
test(utf8[0x10u],0x0u, ==)
free(utf8);
}
{
zap_chr8 const win1252[] = {
0x26u,
0xF0u,
0x80u,
0x82u,
0x83u,
0x84u,
0x85u,
0x86u,
0x87u,
0x88u,
0x89u,
0x8Au,
0x8Bu,
0x8Cu,
0x8Eu,
0x91u,
0x92u,
0x93u,
0x94u,
0x95u,
0x96u,
0x97u,
0x98u,
0x99u,
0x9Au,
0x9Bu,
0x9Cu,
0x9Eu,
0x9Fu,
0x0u,
};
zap_sz const enclen = sizeof (win1252) - 0x1u;
test(enclen,0x1Du,==)
zap_chr02 * const utf02 = malloc(sizeof (zap_chr02) * (enclen + 0x1u));
if (utf02 == zap_nullptr) {
fputs("Memory allocation failed!\n",stderr);
return EXIT_FAILURE;
}
zap_win1252dec(utf02,win1252);
test(utf02[0x0u], 0x26u, ==);
test(utf02[0x1u], 0xF0u, ==);
test(utf02[0x2u], 0x20ACu,==);
test(utf02[0x3u], 0x201Au,==);
test(utf02[0x4u], 0x192u, ==);
test(utf02[0x5u], 0x201Eu,==);
test(utf02[0x6u], 0x2026u,==);
test(utf02[0x7u], 0x2020u,==);
test(utf02[0x8u], 0x2021u,==);
test(utf02[0x9u], 0x2C6u, ==);
test(utf02[0xAu], 0x2030u,==);
test(utf02[0xBu], 0x160u, ==);
test(utf02[0xCu], 0x2039u,==);
test(utf02[0xDu], 0x152u, ==);
test(utf02[0xEu], 0x17Du, ==);
test(utf02[0xFu], 0x2018u,==);
test(utf02[0x10u],0x2019u,==);
test(utf02[0x11u],0x201Cu,==);
test(utf02[0x12u],0x201Du,==);
test(utf02[0x13u],0x2022u,==);
test(utf02[0x14u],0x2013u,==);
test(utf02[0x15u],0x2014u,==);
test(utf02[0x16u],0x2DCu, ==);
test(utf02[0x17u],0x2122u,==);
test(utf02[0x18u],0x161u, ==);
test(utf02[0x19u],0x203Au,==);
test(utf02[0x1Au],0x153u, ==);
test(utf02[0x1Bu],0x17Eu, ==);
test(utf02[0x1Cu],0x178u, ==);
test(utf02[0x1Du],0x0u, ==);
free(utf02);
}
{
zap_chr02 const utf02[] = {
0x26u,
0xF0u,
0x20ACu,
0x201Au,
0x192u,
0x201Eu,
0x2026u,
0x2020u,
0x2021u,
0x2C6u,
0x2030u,
0x160u,
0x2039u,
0x152u,
0x17Du,
0x2018u,
0x2019u,
0x201Cu,
0x201Du,
0x2022u,
0x2013u,
0x2014u,
0x2DCu,
0x2122u,
0x161u,
0x203Au,
0x153u,
0x17Eu,
0x178u,
0x0u,
};
zap_sz const enclen = sizeof (utf02) / sizeof (utf02[0x0u]) - 0x1u;
test(enclen,0x1Du,==)
zap_chr8 * const win1252 = malloc(enclen + 0x1u);
if (win1252 == zap_nullptr) {
fputs("Memory allocation failed!\n",stderr);
return EXIT_FAILURE;
}
zap_win1252enc(win1252,utf02);
test(win1252[0x0u], 0x26u,==);
test(win1252[0x1u], 0xF0u,==);
test(win1252[0x2u], 0x80u,==);
test(win1252[0x3u], 0x82u,==);
test(win1252[0x4u], 0x83u,==);
test(win1252[0x5u], 0x84u,==);
test(win1252[0x6u], 0x85u,==);
test(win1252[0x7u], 0x86u,==);
test(win1252[0x8u], 0x87u,==);
test(win1252[0x9u], 0x88u,==);
test(win1252[0xAu], 0x89u,==);
test(win1252[0xBu], 0x8Au,==);
test(win1252[0xCu], 0x8Bu,==);
test(win1252[0xDu], 0x8Cu,==);
test(win1252[0xEu], 0x8Eu,==);
test(win1252[0xFu], 0x91u,==);
test(win1252[0x10u],0x92u,==);
test(win1252[0x11u],0x93u,==);
test(win1252[0x12u],0x94u,==);
test(win1252[0x13u],0x95u,==);
test(win1252[0x14u],0x96u,==);
test(win1252[0x15u],0x97u,==);
test(win1252[0x16u],0x98u,==);
test(win1252[0x17u],0x99u,==);
test(win1252[0x18u],0x9Au,==);
test(win1252[0x19u],0x9Bu,==);
test(win1252[0x1Au],0x9Cu,==);
test(win1252[0x1Bu],0x9Eu,==);
test(win1252[0x1Cu],0x9Fu,==);
test(win1252[0x1Du],0x0u, ==);
free(win1252);
}
}