summaryrefslogblamecommitdiff
path: root/test.cc
blob: 8a181f9faddbb67b602e659a2528b374afcd32d2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                          
































                                                                                 
                                                                               













                                                                           









                                                                
                                       










                                                                                      
                                     
                                     
                                   




                                                                        
                                     
                                     
                                   




                                                                        
                                         
























                                                                       
                                                         





















                                                                                                   





                                    

                                    






















                                                                                                  

                                                          
/* c++ -Izap/include -L. -otest test.cc -lzap */

#include <cassert>
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <zap/base.h>

int main(void) {
	::std::fprintf(stderr,"zap test\n");
	::std::fprintf(stderr,"arch: %s\n",sus_archstr);
	::std::fprintf(stderr,"fast: %s\n",::zap_fastimpl ? "yes" : "no");
	::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]);
		assert(arr0[0x0u] == 0x0u);
		assert(arr0[0x1u] == 0x0u);
		assert(arr0[0x2u] == 0x0u);
		assert(arr0[0x3u] == 0x0u);
		assert(arr0[0x4u] == 0x0u);
		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]);
		assert(arr0[0x0u] == 0x7F7F7F7F7F7F7F7Fu);
		assert(arr0[0x1u] == 0x7F7F7F7F7F7F7F7Fu);
		assert(arr0[0x2u] == 0x7F7F7F7F7F7F7F7Fu);
		assert(arr0[0x3u] == 0x7F7F7F7F7F7F7F7Fu);
		assert(arr0[0x4u] == 0x7F7F7F7F7F7F7F7Fu);
		assert(arr0[0x5u] == 0x7F7F7F7F7F7F7F7Fu);
		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]);
		assert(arr1[0x0u] == arr0[0x0u]);
		assert(arr1[0x1u] == arr0[0x1u]);
		assert(arr1[0x2u] == arr0[0x2u]);
		assert(arr1[0x3u] == arr0[0x3u]);
		assert(arr1[0x4u] == arr0[0x4u]);
		assert(arr1[0x5u] == arr0[0x5u]);
		assert(arr1[0x6u] == arr0[0x6u]);
		auto const eq {::zap_memeq(arr1,arrsz,arr0)};
		::std::fprintf(stderr,"eq:      %u\n",eq);
		assert(eq);
	}
	::std::fprintf(stderr,"\n");
	{
		auto const str {"Hello there! General Kenobi?"};
		::std::fprintf(stderr,"str:     \"%s\"\n",str);
		auto const strsz {::zap_strlen(str)};
		::std::fprintf(stderr,"strsz:   %zX\n",strsz);
		assert(strsz == 0x1Cu);
	}
	::std::fprintf(stderr,"\n");
	{
		auto str {"Oh my science!"};
		::std::fprintf(stderr,"str:     \"%s\"\n",str);
		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::fprintf(stderr,"pos0:    %zX\n",pos0);
		::std::fprintf(stderr,"pos1:    %zX\n",pos1);
		assert(pos0 == 0x2u);
		assert(pos1 == pos0);
		str += pos0 + 0x1u;
		len = ::zap_strlen(str);
		pos0 = ::zap_fndchr(str,' ');
		pos1 = ::zap_fndbyte(str,len,(::std::uint_least8_t)' ');
		::std::fprintf(stderr,"pos0:    %zX\n",pos0);
		::std::fprintf(stderr,"pos1:    %zX\n",pos1);
		assert(pos0 == 0x2u);
		assert(pos1 == pos0);
		str += pos0 + 0x1u;
		len = ::zap_strlen(str);
		pos0 = ::zap_fndchr(str,' ');
		pos1 = ::zap_fndbyte(str,len,(::std::uint_least8_t)' ');
		::std::fprintf(stderr,"pos0:    %zX\n",pos0);
		::std::fprintf(stderr,"pos1:    %zX\n",pos1);
		assert(pos0 == SIZE_MAX);
		assert(pos1 == pos0);
	}
	::std::fprintf(stderr,"\n");
	{
		char const str0[] = "What's up, my guy?";
		::std::fprintf(stderr,"str0:    \"%s\"\n",str0);
		char const str1[] = "What's up, my guy?";
		::std::fprintf(stderr,"str1:    \"%s\"\n",str1);
		char const str2[] = "I don't know you!";
		::std::fprintf(stderr,"str2:    \"%s\"\n",str2);
		bool const cmp0 = ::zap_streq(str0,str1);
		bool const cmp1 = ::zap_streq(str0,str2);
		bool const cmp2 = ::zap_streq(str1,str2);
		::std::fprintf(stderr,"cmp0:    %u\n",cmp0);
		::std::fprintf(stderr,"cmp1:    %u\n",cmp1);
		::std::fprintf(stderr,"cmp2:    %u\n",cmp2);
		assert(cmp0);
		assert(!cmp1);
		assert(!cmp2);
	}
	::std::fprintf(stderr,"\n");
	{
		char const str0[] = "What in the world are you doing?";
		::std::fprintf(stderr,"str0:    \"%s\"\n",str0);
		char str1[sizeof (str0)];
		assert(::zap_strcpy(str0,str1) == 0x20u);
		::std::fprintf(stderr,"str1:    \"%s\"\n",str1);
		assert(::zap_streq(str0,str1));
	}
	::std::fprintf(stderr,"\n");
	{
		char const str0[] {"Oej Moej Goejd"};
		char const str1[] {"Er jeg egentlig okay med AWP?"};
		::std::fprintf(stderr,"str0:    \"%s\"\n",str0);
		::std::fprintf(stderr,"str1:    \"%s\"\n",str1);
		auto const cmp0    {::zap_strcmp(str0,str1)};
		auto const cmp1    {::zap_strcmp(str0,str0)};
		auto const cmp2    {::zap_strcmp(str1,str0)};
		auto const leastsz {sizeof (str0) < sizeof (str1) ? sizeof (str0) : sizeof (str1)};
		auto const cmp3    {::zap_memcmp(str0,leastsz,str1)};
		auto const cmp4    {::zap_memcmp(str0,leastsz,str0)};
		auto const cmp5    {::zap_memcmp(str1,leastsz,str0)};
		::std::fprintf(stderr,"cmp0:    %i\n",cmp0);
		::std::fprintf(stderr,"cmp1:    %i\n",cmp1);
		::std::fprintf(stderr,"cmp2:    %i\n",cmp2);
		::std::fprintf(stderr,"cmp3:    %i\n",cmp3);
		::std::fprintf(stderr,"cmp4:    %i\n",cmp4);
		::std::fprintf(stderr,"cmp5:    %i\n",cmp5);
		assert(cmp0 >  0x0);
		assert(cmp1 == 0x0);
		assert(cmp2 <  0x0);
		assert(cmp3 >  0x0);
		assert(cmp4 == 0x0);
		assert(cmp5 <  0x0);
	}
	::std::fprintf(stderr,"\n");
	{
		int arr[] {
			0x0,
			0x1,
			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;
		});
		::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]);
		::std::fprintf(stderr,"arr[3]:  %i\n",arr[0x3u]);
		assert(arr[0x0u] == 0x0);
		assert(arr[0x1u] == 0x1);
		assert(arr[0x2u] == 0x0);
		assert(arr[0x3u] == 0x1);
	}
	::std::fprintf(stderr,"\n");
	::std::fprintf(stderr,"All tests have passed!\n");
}