summaryrefslogblamecommitdiff
path: root/rttest.cc
blob: 2feafae486f00ca54c0afb11f3293c7a47997d61 (plain) (tree)























                                                                                                                                                                                         


                                                                                                       










                                                                                                      

                               




                                                            
                                                               



















































                                                                   












                                                                                                                              








                                                           


                                                                    








                                                               


                                                              










                                                             
                                                                      



                                                                   
                                               






                                              
                                                                        



                                                                       
                                                 












                                                                   
                                                                        



                                                                       
                                                 





                                                 
                                                                          



                                                                       
                                                   
















                                                      
#include <cstdlib>
#include <exception>
#include <fmt/core.h>
#include <source_location>
#include <typeinfo>
#include <zp/mem>
#include <zp/mth>
#include <zp/str>

int main() {
	int unsigned num;
	int unsigned numerr;

	auto const tst = [&num](char const * const cmp) noexcept {
		::fmt::print("\n\x1B[38;5;75mtesting\x1B[0m {}\n\n",cmp);

		num = 0x0;
	};

	auto const cmp = [&num,&numerr]<typename ltyp,typename rtyp>(ltyp const & lvalref,rtyp const & rvalref,::std::source_location const srcloc = ::std::source_location::current()) {
		char const * const ltypnm = typeid (ltyp).name();
		char const * const rtypnm = typeid (rtyp).name();

		auto const getval = []<typename typ>(typ const & valref) {
			if constexpr      (::zp::ischr<typ>) {return static_cast<::zp::i02m>(valref);}
			else if constexpr (::zp::isptr<typ>) {return reinterpret_cast<void *>(valref);}
			else                                 {return valref;}
		};

		auto const lval = getval(lvalref);
		auto const rval = getval(rvalref);

		::fmt::print("  {}. comparing {} ({}) vs. {} ({})... ",num++,ltypnm,lval,rtypnm,rval);

		if (lval != rval) {
			::fmt::print("\x1B[38;5;161munequal\x1B[0m (at #{})\n",srcloc.line());
			//throw ::std::exception {};
			++numerr;
			
			return;
		}

		::fmt::print("\x1B[38;5;77mequal\x1B[0m\n");
	};

	::fmt::print("zp test {}.{}\n",::zp::ver,::zp::extver);

	try {
		[&] {
			tst("vectors");

			::zp::vec2<int> const lvec2 = {
				.x = +0x1,
				.y = -0x3,
			};
			::zp::vec2<int> const rvec2 = {
				.x = +0x2,
				.y = -0x4,
			};
			cmp(::zp::dot(lvec2,rvec2),0xE);

			::zp::vec3<int> const lvec3 = {
				.x = +0x1,
				.y = -0x3,
				.z = +0x5,
			};
			::zp::vec3<int> const rvec3 = {
				.x = +0x2,
				.y = -0x4,
				.z = +0x6,
			};
			cmp(::zp::dot(lvec3,rvec3),0x2C);

			::zp::vec4<int> const lvec4 = {
				.x = +0x1,
				.y = -0x3,
				.z = +0x5,
				.w = -0x7,
			};
			::zp::vec4<int> const rvec4 = {
				.x = +0x2,
				.y = -0x4,
				.z = +0x6,
				.w = -0x8,
			};
			cmp(::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);
		}();

		[&] {
			tst("special numbers");

			cmp(zp_isnanf( zp_nanf), true);
			cmp(zp_isnand( zp_nand), true);
			cmp(zp_isnanld(zp_nanld),true);
		}();

		[&] {
			tst("memory sequences");

			::zp::i04 filbuf;

			cmp(::zp::memfil(&filbuf,0xFFu,sizeof (filbuf)),reinterpret_cast<char unsigned *>(&filbuf+0x1u)+0x1u);

			cmp(filbuf,0xFFFFFFFFFFFFFFFFu);
		}();

		[&] {
			tst("strings");

			char     str[]   = "Hello there!";
			wchar_t  wstr[]  = L"Hello there!";
			char32_t str02[] = U"Hello there!";

			::zp::sz const len   = ::zp_strlen(  str);
			::zp::sz const wlen  = ::zp_wstrlen( wstr);
			::zp::sz const len02 = ::zp_utf32len(str02);

			cmp(len,  0xCu);
			cmp(wlen, 0xCu);
			cmp(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);

			delete[] buf;
			delete[] wbuf;
			delete[] buf02;
		}();

		[&] {
			tst("UTF-8");

			char32_t const src[] = U"\U0001F480";

			::zp::sz const buf8len = ::zp_utf8enclen(src);
			cmp(buf8len,0x4u);
			
			char8_t * buf8 = new char8_t[buf8len+0x1u];

			::zp_utf8enc(buf8,src);

			cmp(buf8[0x0u],0xF0u);
			cmp(buf8[0x1u],0x9Fu);
			cmp(buf8[0x2u],0x92u);
			cmp(buf8[0x3u],0x80u);
			cmp(buf8[0x4u],0x00u);

			::zp::sz const buf02len = ::zp_utf8declen(buf8);
			cmp(buf02len,0x1u);

			char32_t * buf02 = new char32_t[buf02len+0x1u];

			::zp_utf8dec(buf02,buf8);

			cmp(buf02[0x0u],0x1F480u);
			cmp(buf02[0x1u],0x0u);

			delete[] buf8;
			delete[] buf02;
		}();

		[&] {
			tst("UTF-16");

			char32_t const src[] = U"\U0001F480\u00F0";

			::zp::sz const buf01len = ::zp_utf16enclen(src);
			cmp(buf01len,0x3u);
			
			char16_t * buf01 = new char16_t[buf01len+0x1u];

			::zp_utf16enc(buf01,src);

			cmp(buf01[0x0u],0xD83Du);
			cmp(buf01[0x1u],0xDC80u);
			cmp(buf01[0x2u],0x00F0u);
			cmp(buf01[0x3u],0x0000u);

			::zp::sz const buf02len = ::zp_utf16declen(buf01);
			cmp(buf02len,0x2u);

			char32_t * buf02 = new char32_t[buf02len+0x1u];

			::zp_utf16dec(buf02,buf01);

			cmp(buf02[0x0u],0x1F480u);
			cmp(buf02[0x1u],0xF0u);
			cmp(buf02[0x2u],0x0u);

			delete[] buf01;
			delete[] buf02;
		}();
	}
	catch (::std::exception const &) {
		return EXIT_FAILURE;
	}

	::fmt::print("\nDone - {} error(s)\n",numerr);

	return EXIT_SUCCESS;
}