summaryrefslogtreecommitdiff
path: root/test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test.cc')
-rw-r--r--test.cc151
1 files changed, 119 insertions, 32 deletions
diff --git a/test.cc b/test.cc
index 8a181f9..521d556 100644
--- a/test.cc
+++ b/test.cc
@@ -1,11 +1,21 @@
-/* c++ -Izap/include -L. -otest test.cc -lzap */
+/* c++ -Izap/include -Lzap -otest test.cc -lzap */
#include <cassert>
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdio>
-#include <zap/base.h>
+#include <stdexcept>
+#include <zap/math.h>
+#include <zap/mem.h>
+
+extern "C" auto zap_test_foreachfn(void * const _ptr) -> void { /* This function is passed to foreach, which requires C-linkage function pointers. I tried to overload the function to also accept C++-linkage function pointers, but that only made GCC moan. */
+ auto const ptr {static_cast<int *>(_ptr)};
+ auto val {*ptr};
+ if (val == 0x45) {throw ::std::domain_error {"Damn!"};}
+ val %= 0x2;
+ *ptr = val;
+}
int main(void) {
::std::fprintf(stderr,"zap test\n");
@@ -13,16 +23,97 @@ int main(void) {
::std::fprintf(stderr,"fast: %s\n",::zap_fastimpl ? "yes" : "no");
::std::fprintf(stderr,"\n");
{
+ {
+ auto val {::zap_abs_c(-0x5D)};
+ ::std::fprintf(stderr,"val: %hhi\n",val);
+ assert(val == 0x5D);
+ }
+ {
+ auto val {::zap_abs_i(-0xEF4)};
+ ::std::fprintf(stderr,"val: %i\n",val);
+ assert(val == 0xEF4);
+ }
+ {
+ auto val {::zap_abs_l(-0x6666E67B)};
+ ::std::fprintf(stderr,"val: %li\n",val);
+ assert(val == 0x6666E67B);
+ }
+ {
+ auto val {::zap_abs_ll(-0x7C91A4B244F0DC99)};
+ ::std::fprintf(stderr,"val: %lli\n",val);
+ assert(val == 0x7C91A4B244F0DC99);
+ }
+ {
+ auto val {::zap_abs_s(-0xDC9)};
+ ::std::fprintf(stderr,"val: %i\n",val);
+ assert(val == 0xDC9);
+ }
+ }
+ ::std::fprintf(stderr,"\n");
+ {
+ { /* Largest number for each signed parameter: (floor(cbrt(2^(x-1)-1))) */
+ auto val {::zap_fma_c(0x5,0x2,0x2)};
+ ::std::fprintf(stderr,"val: %hhi\n",val);
+ assert(val == 0x9);
+ }
+ {
+ auto val {::zap_fma_i(0x29,0x4AD,0x453)};
+ ::std::fprintf(stderr,"val: %i\n",val);
+ assert(val == 0x143840);
+ }
+ {
+ auto val {::zap_fma_l(0x1BAA11,0x361DA,0x17462F)};
+ ::std::fprintf(stderr,"val: %li\n",val);
+ assert(val == 0x4EB8123D17);
+ }
+ {
+ auto val {::zap_fma_ll(0x45,0x60,0x1A4)};
+ ::std::fprintf(stderr,"val: %lli\n",val);
+ assert(val == 0x9DC5);
+ }
+ {
+ auto val {::zap_fma_s(0x7,0xE,0x1D)};
+ ::std::fprintf(stderr,"val: %i\n",val);
+ assert(val == 0x19D);
+ }
+ { /* Largest number for each unsigned parameter: (floor(cbrt(2^x-1))) */
+ auto val {::zap_fma_uc(0x6u,0x6u,0x3u)};
+ ::std::fprintf(stderr,"val: %hhu\n",val);
+ assert(val == 0x18u);
+ }
+ {
+ auto val {::zap_fma_ui(0x14u,0x17u,0x3u)};
+ ::std::fprintf(stderr,"val: %u\n",val);
+ assert(val == 0x59);
+ }
+ {
+ auto val {::zap_fma_ul(0x5AAu,0x412u,0x16Du)};
+ ::std::fprintf(stderr,"val: %lu\n",val);
+ assert(val == 0x5D354u);
+ }
+ {
+ auto val {::zap_fma_ull(0x10EFC7u,0x19FB63u,0x1133259u)};
+ ::std::fprintf(stderr,"val: %llu\n",val);
+ assert(val == 0x1BEE278BAB32u);
+ }
+ {
+ auto val {::zap_fma_us(0x23u,0x19u,0x1Cu)};
+ ::std::fprintf(stderr,"val: %u\n",val);
+ assert(val == 0x2DF);
+ }
+ }
+ ::std::fprintf(stderr,"\n");
+ {
constexpr ::std::size_t arrsz {0x8u};
::std::uint_least64_t arr0[arrsz] {0x0u};
::zap_memfill(arr0,arrsz * sizeof (::std::uint_least64_t),0x0u);
- ::std::fprintf(stderr,"arr0[0]: %" PRIX64 "\n",arr0[0x0u]);
- ::std::fprintf(stderr,"arr0[1]: %" PRIX64 "\n",arr0[0x1u]);
- ::std::fprintf(stderr,"arr0[2]: %" PRIX64 "\n",arr0[0x2u]);
- ::std::fprintf(stderr,"arr0[3]: %" PRIX64 "\n",arr0[0x3u]);
- ::std::fprintf(stderr,"arr0[4]: %" PRIX64 "\n",arr0[0x4u]);
- ::std::fprintf(stderr,"arr0[5]: %" PRIX64 "\n",arr0[0x5u]);
- ::std::fprintf(stderr,"arr0[6]: %" PRIX64 "\n",arr0[0x6u]);
+ ::std::fprintf(stderr,"arr0[0]: %" PRIXLEAST64 "\n",arr0[0x0u]);
+ ::std::fprintf(stderr,"arr0[1]: %" PRIXLEAST64 "\n",arr0[0x1u]);
+ ::std::fprintf(stderr,"arr0[2]: %" PRIXLEAST64 "\n",arr0[0x2u]);
+ ::std::fprintf(stderr,"arr0[3]: %" PRIXLEAST64 "\n",arr0[0x3u]);
+ ::std::fprintf(stderr,"arr0[4]: %" PRIXLEAST64 "\n",arr0[0x4u]);
+ ::std::fprintf(stderr,"arr0[5]: %" PRIXLEAST64 "\n",arr0[0x5u]);
+ ::std::fprintf(stderr,"arr0[6]: %" PRIXLEAST64 "\n",arr0[0x6u]);
assert(arr0[0x0u] == 0x0u);
assert(arr0[0x1u] == 0x0u);
assert(arr0[0x2u] == 0x0u);
@@ -31,13 +122,13 @@ int main(void) {
assert(arr0[0x5u] == 0x0u);
assert(arr0[0x6u] == 0x0u);
::zap_memfill(arr0,arrsz * sizeof (::std::uint_least64_t),0x7Fu);
- ::std::fprintf(stderr,"arr0[0]: %" PRIX64 "\n",arr0[0x0u]);
- ::std::fprintf(stderr,"arr0[1]: %" PRIX64 "\n",arr0[0x1u]);
- ::std::fprintf(stderr,"arr0[2]: %" PRIX64 "\n",arr0[0x2u]);
- ::std::fprintf(stderr,"arr0[3]: %" PRIX64 "\n",arr0[0x3u]);
- ::std::fprintf(stderr,"arr0[4]: %" PRIX64 "\n",arr0[0x4u]);
- ::std::fprintf(stderr,"arr0[5]: %" PRIX64 "\n",arr0[0x5u]);
- ::std::fprintf(stderr,"arr0[6]: %" PRIX64 "\n",arr0[0x6u]);
+ ::std::fprintf(stderr,"arr0[0]: %" PRIXLEAST64 "\n",arr0[0x0u]);
+ ::std::fprintf(stderr,"arr0[1]: %" PRIXLEAST64 "\n",arr0[0x1u]);
+ ::std::fprintf(stderr,"arr0[2]: %" PRIXLEAST64 "\n",arr0[0x2u]);
+ ::std::fprintf(stderr,"arr0[3]: %" PRIXLEAST64 "\n",arr0[0x3u]);
+ ::std::fprintf(stderr,"arr0[4]: %" PRIXLEAST64 "\n",arr0[0x4u]);
+ ::std::fprintf(stderr,"arr0[5]: %" PRIXLEAST64 "\n",arr0[0x5u]);
+ ::std::fprintf(stderr,"arr0[6]: %" PRIXLEAST64 "\n",arr0[0x6u]);
assert(arr0[0x0u] == 0x7F7F7F7F7F7F7F7Fu);
assert(arr0[0x1u] == 0x7F7F7F7F7F7F7F7Fu);
assert(arr0[0x2u] == 0x7F7F7F7F7F7F7F7Fu);
@@ -47,13 +138,13 @@ int main(void) {
assert(arr0[0x6u] == 0x7F7F7F7F7F7F7F7Fu);
::std::uint_least64_t arr1[arrsz] {0x0u};
::zap_memcpy(arr0,arrsz * sizeof (::std::uint_least64_t),arr1);
- ::std::fprintf(stderr,"arr1[0]: %" PRIX64 "\n",arr1[0x0u]);
- ::std::fprintf(stderr,"arr1[1]: %" PRIX64 "\n",arr1[0x1u]);
- ::std::fprintf(stderr,"arr1[2]: %" PRIX64 "\n",arr1[0x2u]);
- ::std::fprintf(stderr,"arr1[3]: %" PRIX64 "\n",arr1[0x3u]);
- ::std::fprintf(stderr,"arr1[4]: %" PRIX64 "\n",arr1[0x4u]);
- ::std::fprintf(stderr,"arr1[5]: %" PRIX64 "\n",arr1[0x5u]);
- ::std::fprintf(stderr,"arr1[6]: %" PRIX64 "\n",arr1[0x6u]);
+ ::std::fprintf(stderr,"arr1[0]: %" PRIXLEAST64 "\n",arr1[0x0u]);
+ ::std::fprintf(stderr,"arr1[1]: %" PRIXLEAST64 "\n",arr1[0x1u]);
+ ::std::fprintf(stderr,"arr1[2]: %" PRIXLEAST64 "\n",arr1[0x2u]);
+ ::std::fprintf(stderr,"arr1[3]: %" PRIXLEAST64 "\n",arr1[0x3u]);
+ ::std::fprintf(stderr,"arr1[4]: %" PRIXLEAST64 "\n",arr1[0x4u]);
+ ::std::fprintf(stderr,"arr1[5]: %" PRIXLEAST64 "\n",arr1[0x5u]);
+ ::std::fprintf(stderr,"arr1[6]: %" PRIXLEAST64 "\n",arr1[0x6u]);
assert(arr1[0x0u] == arr0[0x0u]);
assert(arr1[0x1u] == arr0[0x1u]);
assert(arr1[0x2u] == arr0[0x2u]);
@@ -80,7 +171,7 @@ int main(void) {
auto len {::zap_strlen(str)};
::std::fprintf(stderr,"len: %zX\n",len);
::std::size_t pos0 = ::zap_fndchr(str,' ');
- ::std::size_t pos1 = ::zap_fndbyte(str,len,(::std::uint_least8_t)' ');
+ ::std::size_t pos1 = ::zap_fndbyte(str,len,(unsigned char)' ');
::std::fprintf(stderr,"pos0: %zX\n",pos0);
::std::fprintf(stderr,"pos1: %zX\n",pos1);
assert(pos0 == 0x2u);
@@ -88,7 +179,7 @@ int main(void) {
str += pos0 + 0x1u;
len = ::zap_strlen(str);
pos0 = ::zap_fndchr(str,' ');
- pos1 = ::zap_fndbyte(str,len,(::std::uint_least8_t)' ');
+ pos1 = ::zap_fndbyte(str,len,(unsigned char)' ');
::std::fprintf(stderr,"pos0: %zX\n",pos0);
::std::fprintf(stderr,"pos1: %zX\n",pos1);
assert(pos0 == 0x2u);
@@ -96,7 +187,7 @@ int main(void) {
str += pos0 + 0x1u;
len = ::zap_strlen(str);
pos0 = ::zap_fndchr(str,' ');
- pos1 = ::zap_fndbyte(str,len,(::std::uint_least8_t)' ');
+ pos1 = ::zap_fndbyte(str,len,(unsigned char)' ');
::std::fprintf(stderr,"pos0: %zX\n",pos0);
::std::fprintf(stderr,"pos1: %zX\n",pos1);
assert(pos0 == SIZE_MAX);
@@ -163,12 +254,8 @@ int main(void) {
0x2,
0x3,
};
- ::zap_foreach(arr,sizeof (arr) / sizeof (int),sizeof (int),[](void * const _ptr) {
- auto const ptr {static_cast<int *>(_ptr)};
- auto val {*ptr};
- val %= 0x2;
- *ptr = val;
- });
+ void * rem {nullptr};
+ ::zap_foreach(arr,sizeof (int),sizeof (arr) / sizeof (int),::zap_test_foreachfn);
::std::fprintf(stderr,"arr[0]: %i\n",arr[0x0u]);
::std::fprintf(stderr,"arr[1]: %i\n",arr[0x1u]);
::std::fprintf(stderr,"arr[2]: %i\n",arr[0x2u]);