summaryrefslogblamecommitdiff
path: root/test.c
blob: 43d8ff48a92acc5053668243e577e7b4be1490a5 (plain) (tree)



























































































































































































































































































                                                                                                                         
#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,==)
	}
	/* mem */
	{
		zap_i01 const in[0x4u] = {
			0xFFFFu,
			0xFFFu,
			0xFFu,
			0xFu,
		};
		zap_i01 out[0x4u] = {0x0u};
		zap_cp(out,in,sizeof (in));
		test(out[0x0u],0xFFFFu,==)
		test(out[0x1u],0xFFFu, ==)
		test(out[0x2u],0xFFu,  ==)
		test(out[0x3u],0xFu,   ==)
	}
	{
		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);
	}
}