summaryrefslogtreecommitdiff
path: root/test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test.cc')
-rw-r--r--test.cc204
1 files changed, 148 insertions, 56 deletions
diff --git a/test.cc b/test.cc
index 6ce453f..5f625af 100644
--- a/test.cc
+++ b/test.cc
@@ -4,6 +4,7 @@
#include <exception>
#include <iostream>
#include <limits>
+#include <memory>
#include <typeinfo>
#include <zp/mem>
#include <zp/mth>
@@ -158,11 +159,11 @@ static_assert(::zp::maxval<int long unsigned>::val == ::std::numeric_limits
static_assert(::zp::maxval<int long long unsigned>::val == ::std::numeric_limits<int long long unsigned>::max());
static_assert(::zp::maxval<wchar_t>::val == ::std::numeric_limits<wchar_t>::max());
-template<typename ltyp,typename rtyp> static auto cmp(int unsigned & numerr,int long const ln,ltyp const & lvalref,rtyp const & rvalref) noexcept {
+template<typename ltyp,typename rtyp> static auto cmp(int unsigned & numerr,int long const ln,::zp::i8 const mth,ltyp const& lvalref,rtyp const& rvalref) noexcept {
char const* const ltypnm = typeid (ltyp).name();
char const* const rtypnm = typeid (rtyp).name();
- auto const getval = [](auto const & valref) {
+ auto const getval = [](auto const& valref) {
using typ = typename ::zp::remqual<typename ::zp::remref<decltype (valref)>::typ>::typ;
if constexpr (::zp::ischrtyp<typ>::val) {return static_cast<::zp::i02>(valref);}
@@ -173,20 +174,63 @@ template<typename ltyp,typename rtyp> static auto cmp(int unsigned & numerr,int
auto const lval = getval(lvalref);
auto const rval = getval(rvalref);
- ::std::cout << " " << ln << ". comparing " << ltypnm << " (" << lval << ") vs. " << rtypnm << " (" << rval << ")... ";
+ auto const cmp = [&mth](auto const& lval,auto const& rval) -> bool {
+ switch (mth) {
+ default:
+ ::zp::unrch();
+ case 0x0u:
+ return lval == rval;
+ case 0x1u:
+ return lval >= rval;
+ case 0x2u:
+ return lval > rval;
+ case 0x3u:
+ return lval <= rval;
+ case 0x4u:
+ return lval < rval;
+ case 0x5u:
+ return lval != rval;
+ }
+ };
- if (lval != rval) {
- ::std::cout << "\x1B[38;5;161munequal\x1B[0m\n";
+ auto const mthstr = [&mth]() -> char const * {
+ switch (mth) {
+ default:
+ ::zp::unrch();
+ case 0x0u:
+ return "==";
+ case 0x1u:
+ return ">=";
+ case 0x2u:
+ return ">";
+ case 0x3u:
+ return "<=";
+ case 0x4u:
+ return "<";
+ case 0x5u:
+ return "!=";
+ }
+ }();
+
+ ::std::cout << " " << ln << ". " << ltypnm << " (" << lval << ") " << mthstr << " " << rtypnm << " (" << rval << ")... ";
+
+ if (!cmp(lval,rval)) {
+ ::std::cout << "\x1B[38;5;161merror\x1B[0m\n";
//throw ::std::exception {};
++numerr;
return;
}
- ::std::cout <<"\x1B[38;5;77mequal\x1B[0m\n";
+ ::std::cout <<"\x1B[38;5;77mokay\x1B[0m\n";
}
-#define cmp(lval,rval) (::cmp(numerr,__LINE__,(lval),(rval)))
+#define chkeq(lval,rval) (::cmp(numerr,__LINE__,0x0u,(lval),(rval)))
+#define chkge(lval,rval) (::cmp(numerr,__LINE__,0x1u,(lval),(rval)))
+#define chkgt(lval,rval) (::cmp(numerr,__LINE__,0x2u,(lval),(rval)))
+#define chkle(lval,rval) (::cmp(numerr,__LINE__,0x3u,(lval),(rval)))
+#define chklt(lval,rval) (::cmp(numerr,__LINE__,0x4u,(lval),(rval)))
+#define chkne(lval,rval) (::cmp(numerr,__LINE__,0x5u,(lval),(rval)))
int main() {
int unsigned numerr = 0x0u;
@@ -199,12 +243,31 @@ int main() {
try {
[&] {
+ tst("optionals");
+
+ ::zp::opt<::zp::i8s> opt = ::zp::nulopt;
+
+ chkeq(opt.chk(),false);
+
+ auto const fun = [](char const* msg) -> void {
+ ::std::cout << msg << ::std::endl;
+
+ ::std::abort();
+ };
+
+ opt = -0x45;
+
+ chkeq(opt.chk(),true);
+ chkeq(opt.exp(fun,"expectation failed"),-0x45);
+ }();
+
+ [&] {
tst("results");
::zp::res<::zp::i8s,::zp::i8s> res = ::zp::err<::zp::i8s>(0x45);
- cmp(res.chk(),false);
- cmp(res.err(),0x45);
+ chkeq(res.chk(),false);
+ chkeq(res.err(),0x45);
res = -0x45;
@@ -214,8 +277,8 @@ int main() {
::std::abort();
};
- cmp(res.chk(),true);
- cmp(res.exp(fun,"expectation failed"),-0x45);
+ chkeq(res.chk(),true);
+ chkeq(res.exp(fun,"expectation failed"),-0x45);
}();
[&] {
@@ -224,20 +287,20 @@ int main() {
::zp::f02 f02;
::zp::f04 f04;
- cmp(sizeof (f02),0x4u);
- cmp(sizeof (f04),0x8u);
+ chkeq(sizeof (f02),0x4u);
+ chkeq(sizeof (f04),0x8u);
f02 = ::zp::nan<::zp::f02>::val;
f04 = ::zp::nan<::zp::f04>::val;
- cmp(::zp::isnan(f02),true);
- cmp(::zp::isnan(f04),true);
+ chkeq(::zp::isnan(f02),true);
+ chkeq(::zp::isnan(f04),true);
f02 = ::zp::inf<::zp::f02>::val;
f04 = ::zp::inf<::zp::f04>::val;
- cmp(f02 > ::zp::maxval<::zp::f02>::val,true);
- cmp(f04 > ::zp::maxval<::zp::f04>::val,true);
+ chkeq(f02 > ::zp::maxval<::zp::f02>::val,true);
+ chkeq(f04 > ::zp::maxval<::zp::f04>::val,true);
}();
[&] {
@@ -255,7 +318,7 @@ int main() {
.x = +0x2,
.y = -0x4,
};
- cmp(::zp::dot(lvec2,rvec2),0xE);
+ chkeq(::zp::dot(lvec2,rvec2),0xE);
::zp::vec3<int> const lvec3 = {
.x = +0x1,
@@ -267,7 +330,7 @@ int main() {
.y = -0x4,
.z = +0x6,
};
- cmp(::zp::dot(lvec3,rvec3),0x2C);
+ chkeq(::zp::dot(lvec3,rvec3),0x2C);
::zp::vec4<int> const lvec4 = {
.x = +0x1,
@@ -281,14 +344,14 @@ int main() {
.z = +0x6,
.w = -0x8,
};
- cmp(::zp::dot(lvec4,rvec4),0x64);
+ chkeq(::zp::dot(lvec4,rvec4),0x64);
auto const avec2 = ::zp::vadd(lvec2,rvec2);
auto const svec2 = ::zp::vsub(lvec2,rvec2);
- cmp(avec2.x,+0x3);
- cmp(avec2.y,-0x7);
- cmp(svec2.x,-0x1);
- cmp(svec2.y,+0x1);
+ chkeq(avec2.x,+0x3);
+ chkeq(avec2.y,-0x7);
+ chkeq(svec2.x,-0x1);
+ chkeq(svec2.y,+0x1);
}();*/
[&] {
@@ -296,19 +359,19 @@ int main() {
::zp::i04 filbuf;
- cmp(::zp::memfil(&filbuf,0xFFu,0x8u),reinterpret_cast<char unsigned *>(&filbuf)+0x8u);
- cmp(::zp::memfil(&filbuf,0x7Fu,0x4u),reinterpret_cast<char unsigned *>(&filbuf)+0x4u);
- cmp(::zp::memfil(&filbuf,0x3Fu,0x2u),reinterpret_cast<char unsigned *>(&filbuf)+0x2u);
- cmp(::zp::memfil(&filbuf,0x1Fu,0x1u),reinterpret_cast<char unsigned *>(&filbuf)+0x1u);
+ chkeq(::zp::memfil(&filbuf,0xFFu,0x8u),reinterpret_cast<char unsigned *>(&filbuf)+0x8u);
+ chkeq(::zp::memfil(&filbuf,0x7Fu,0x4u),reinterpret_cast<char unsigned *>(&filbuf)+0x4u);
+ chkeq(::zp::memfil(&filbuf,0x3Fu,0x2u),reinterpret_cast<char unsigned *>(&filbuf)+0x2u);
+ chkeq(::zp::memfil(&filbuf,0x1Fu,0x1u),reinterpret_cast<char unsigned *>(&filbuf)+0x1u);
- cmp(filbuf,0xFFFFFFFF7F7F3F1Fu);
+ chkeq(filbuf,0xFFFFFFFF7F7F3F1Fu);
::zp::i04 cpybuf;
::zp::memcpy(&cpybuf,&filbuf,0x8u);
- cmp(cpybuf,filbuf);
+ chkeq(cpybuf,filbuf);
- cmp(::zp::memsrh(&cpybuf,0x1Fu,0x8u),reinterpret_cast<char unsigned *>(&cpybuf));
+ chkeq(::zp::memsrh(&cpybuf,0x1Fu,0x8u),reinterpret_cast<char unsigned *>(&cpybuf));
}();
[&] {
@@ -322,17 +385,17 @@ int main() {
::zp::siz const wlen = ::zp_wstrlen( wstr);
::zp::siz const len02 = ::zp_utf32len(str02);
- cmp(len, 0xCu);
- cmp(wlen, 0xCu);
- cmp(len02,0xCu);
+ chkeq(len, 0xCu);
+ chkeq(wlen, 0xCu);
+ chkeq(len02,0xCu);
auto const buf = new char[ len];
auto const wbuf = new wchar_t[ wlen];
auto const buf02 = new char32_t[len02];
- cmp(::zp_strcpy( buf, str), len);
- cmp(::zp_wstrcpy( wbuf, wstr), wlen);
- cmp(::zp_utf32cpy(buf02,str02),len02);
+ chkeq(::zp_strcpy( buf, str), len);
+ chkeq(::zp_wstrcpy( wbuf, wstr), wlen);
+ chkeq(::zp_utf32cpy(buf02,str02),len02);
delete[] buf;
delete[] wbuf;
@@ -345,27 +408,27 @@ int main() {
char32_t const src[] = U"\U0001F480";
::zp::siz const buf8len = ::zp_utf8enclen(src);
- cmp(buf8len,0x4u);
+ chkeq(buf8len,0x4u);
::zp::c8 * buf8 = new ::zp::c8[buf8len+0x1];
::zp_utf8enc(buf8,src);
- cmp(buf8[0x0],0xF0u);
- cmp(buf8[0x1],0x9Fu);
- cmp(buf8[0x2],0x92u);
- cmp(buf8[0x3],0x80u);
- cmp(buf8[0x4],0x00u);
+ chkeq(buf8[0x0],0xF0u);
+ chkeq(buf8[0x1],0x9Fu);
+ chkeq(buf8[0x2],0x92u);
+ chkeq(buf8[0x3],0x80u);
+ chkeq(buf8[0x4],0x00u);
::zp::siz const buf02len = ::zp_utf8declen(buf8);
- cmp(buf02len,0x1u);
+ chkeq(buf02len,0x1u);
char32_t * buf02 = new char32_t[buf02len+0x1];
::zp_utf8dec(buf02,buf8);
- cmp(buf02[0x0],0x1F480u);
- cmp(buf02[0x1],0x0u);
+ chkeq(buf02[0x0],0x1F480u);
+ chkeq(buf02[0x1],0x0u);
delete[] buf8;
delete[] buf02;
@@ -377,33 +440,62 @@ int main() {
char32_t const src[] = U"\U0001F480\u00F0";
::zp::siz const buf01len = ::zp_utf16enclen(src);
- cmp(buf01len,0x3u);
+ chkeq(buf01len,0x3u);
char16_t * buf01 = new char16_t[buf01len+0x1];
::zp_utf16enc(buf01,src);
- cmp(buf01[0x0],0xD83Du);
- cmp(buf01[0x1],0xDC80u);
- cmp(buf01[0x2],0x00F0u);
- cmp(buf01[0x3],0x0000u);
+ chkeq(buf01[0x0],0xD83Du);
+ chkeq(buf01[0x1],0xDC80u);
+ chkeq(buf01[0x2],0x00F0u);
+ chkeq(buf01[0x3],0x0000u);
::zp::siz const buf02len = ::zp_utf16declen(buf01);
- cmp(buf02len,0x2u);
+ chkeq(buf02len,0x2u);
char32_t * buf02 = new char32_t[buf02len+0x1];
::zp_utf16dec(buf02,buf01);
- cmp(buf02[0x0],0x1F480u);
- cmp(buf02[0x1],0xF0u);
- cmp(buf02[0x2],0x0u);
+ chkeq(buf02[0x0],0x1F480u);
+ chkeq(buf02[0x1],0xF0u);
+ chkeq(buf02[0x2],0x0u);
delete[] buf01;
delete[] buf02;
}();
+
+ [&] {
+ tst("arrays");
+
+ ::zp::arr<::zp::i8,::std::allocator<::zp::i8>> arr(0x8u);
+
+ chkeq(arr.len(),0x8u);
+
+ arr[0x0] = 0x7Fu;
+ arr[0x1] = 0x3Fu;
+ arr[0x2] = 0x1Fu;
+ arr[0x3] = 0x0Fu;
+ arr[0x4] = 0x07u;
+ arr[0x5] = 0x03u;
+ arr[0x6] = 0x01u;
+ arr[0x7] = 0x00u;
+
+ ::zp::i8 prv = 0xFFu;
+ for (::zp::i8 const val : arr) {
+ chkeq(val,static_cast<::zp::i8>(prv>>0x1u));
+ prv = val;
+ }
+
+ arr.fil(0xFFu);
+
+ for (::zp::i8 const val : arr) {
+ chkeq(val,0xFFu);
+ }
+ }();
}
- catch (::std::exception const &) {
+ catch (::std::exception const&) {
return EXIT_FAILURE;
}