summaryrefslogblamecommitdiff
path: root/test.cc
blob: d7d3a7328051cb6778693f8b9df48549ddefcdda (plain) (tree)
1
2
3
4
5
6
7
8
9








                      

                               
                                                                                                                                
                                       
                                                                                

                                      
                                                                      

                
                                        
         
































                                                                                                           
         
               
         
                                                          
                                      
                                      
         
              
         
                                              






                                                                        
                  

                                                                                

                                                      





















































                                          

         


                                                                    

         




                                                                      

         


                                                                                                      
         
              
         
                                          

















                              
                                                                
                                    

                                                                         


                                                                    
                                          






                                             
                               

         
                                            







                                 
                                                                 
                                     

                                                                      


                                                                    
                                          
















                                          

                              

         
                                             






























                              
                                                                 
                                     

                                                                         


                                                                    
                                                





























                                              

                               

         
                                            






























                                
                                                                                      
                                     

                                                                         


                                                                    
                                                





























                                              

                                 
         


                                                   

                                                           





                                             

                                                               


                                   

                                  
                                                         
                                  

                                               





                                           
                                              
                                  
                                              


                                           
 
#include <cassert>
#include <cinttypes>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <limits>
#include <zap/math.hh>
#include <zap/mem.hh>
#include <zap/str.hh>

#define test(_expr,_val,_cmp) \
	::std::fprintf(stderr,"(" #_expr "): %" PRIXMAX "\n(" #_val "): %" PRIXMAX "\n",(uintmax_t)(_expr),(uintmax_t)(_val)); \
	if (!((_expr) _cmp (_val))) { \
		::std::fputs("\x1B[91mFailure\x1B[0m (" #_cmp ")\n\n",stderr); \
		return EXIT_FAILURE; \
	} \
	::std::fputs("\x1B[92mSuccess\x1B[0m (" #_cmp ")\n\n",stderr);

int main(void) {
	test(::zap::bytelen,CHAR_BIT,==)
	{
		test(::zap::minval<bool>,              ::std::numeric_limits<bool>::min(),              ==)
		test(::zap::minval<signed char>,       ::std::numeric_limits<signed char>::min(),       ==)
		test(::zap::minval<short>,             ::std::numeric_limits<short>::min(),             ==)
		test(::zap::minval<int>,               ::std::numeric_limits<int>::min(),               ==)
		test(::zap::minval<long>,              ::std::numeric_limits<long>::min(),              ==)
		test(::zap::minval<long long>,         ::std::numeric_limits<long long>::min(),         ==)
		test(::zap::minval<unsigned char>,     ::std::numeric_limits<unsigned char>::min(),     ==)
		test(::zap::minval<unsigned short>,    ::std::numeric_limits<unsigned short>::min(),    ==)
		test(::zap::minval<unsigned int>,      ::std::numeric_limits<unsigned int>::min(),      ==)
		test(::zap::minval<unsigned long>,     ::std::numeric_limits<unsigned long>::min(),     ==)
		test(::zap::minval<unsigned long long>,::std::numeric_limits<unsigned long long>::min(),==)
		test(::zap::minval<char>,              ::std::numeric_limits<char>::min(),              ==)
		test(::zap::minval<wchar_t>,           ::std::numeric_limits<wchar_t>::min(),           ==)
		test(::zap::minval<char8_t>,           ::std::numeric_limits<char8_t>::min(),           ==)
		test(::zap::minval<char16_t>,          ::std::numeric_limits<char16_t>::min(),          ==)
		test(::zap::minval<char32_t>,          ::std::numeric_limits<char32_t>::min(),          ==)

		test(::zap::maxval<bool>,              ::std::numeric_limits<bool>::max(),              ==)
		test(::zap::maxval<signed char>,       ::std::numeric_limits<signed char>::max(),       ==)
		test(::zap::maxval<short>,             ::std::numeric_limits<short>::max(),             ==)
		test(::zap::maxval<int>,               ::std::numeric_limits<int>::max(),               ==)
		test(::zap::maxval<long>,              ::std::numeric_limits<long>::max(),              ==)
		test(::zap::maxval<long long>,         ::std::numeric_limits<long long>::max(),         ==)
		test(::zap::maxval<unsigned char>,     ::std::numeric_limits<unsigned char>::max(),     ==)
		test(::zap::maxval<unsigned short>,    ::std::numeric_limits<unsigned short>::max(),    ==)
		test(::zap::maxval<unsigned int>,      ::std::numeric_limits<unsigned int>::max(),      ==)
		test(::zap::maxval<unsigned long>,     ::std::numeric_limits<unsigned long>::max(),     ==)
		test(::zap::maxval<unsigned long long>,::std::numeric_limits<unsigned long long>::max(),==)
		test(::zap::maxval<char>,              ::std::numeric_limits<char>::max(),              ==)
		test(::zap::maxval<wchar_t>,           ::std::numeric_limits<wchar_t>::max(),           ==)
		test(::zap::maxval<char8_t>,           ::std::numeric_limits<char8_t>::max(),           ==)
		test(::zap::maxval<char16_t>,          ::std::numeric_limits<char16_t>::max(),          ==)
		test(::zap::maxval<char32_t>,          ::std::numeric_limits<char32_t>::max(),          ==)
	}
	// math
	{
		auto const pair = ::zap::divmod(0x45,0x5);
		test(pair.lval,0xD,==)
		test(pair.rval,0x4,==)
	}
	// mem
	{
		::zap::i8 const src[0x36u] = {
			0x02u,0x03u,0x05u,0x07u,0x0Bu,0x0Du,0x11u,0x13u,
			0x17u,0x1Du,0x1Fu,0x25u,0x29u,0x2Bu,0x2Fu,0x35u,
			0x3Bu,0x3Du,0x43u,0x47u,0x49u,0x4Fu,0x53u,0x59u,
			0x61u,0x65u,0x67u,0x6Bu,0x6Du,0x71u,0x7Fu,0x83u,
			0x89u,0x8Bu,0x95u,0x97u,0x9Du,0xA3u,0xA7u,0xADu,
			0xB3u,0xB5u,0xBFu,0xC1u,0xC5u,0xC7u,0xD3u,0xDFu,
			0xE3u,0xE5u,0xE9u,0xEFu,0xF1u,0xFBu,
		};
		::zap::i8 dest[sizeof (src)];
		::zap::cpret const cpret = ::zap::bytecp(dest,src,sizeof (src));
		test(cpret.dest,dest + sizeof(src),==)
		test(cpret.src,src + sizeof(src),==)
		test(dest[0x00u],0x02u,==)
		test(dest[0x01u],0x03u,==)
		test(dest[0x02u],0x05u,==)
		test(dest[0x03u],0x07u,==)
		test(dest[0x04u],0x0Bu,==)
		test(dest[0x05u],0x0Du,==)
		test(dest[0x06u],0x11u,==)
		test(dest[0x07u],0x13u,==)
		test(dest[0x08u],0x17u,==)
		test(dest[0x09u],0x1Du,==)
		test(dest[0x0Au],0x1Fu,==)
		test(dest[0x0Bu],0x25u,==)
		test(dest[0x0Cu],0x29u,==)
		test(dest[0x0Du],0x2Bu,==)
		test(dest[0x0Eu],0x2Fu,==)
		test(dest[0x0Fu],0x35u,==)
		test(dest[0x10u],0x3Bu,==)
		test(dest[0x11u],0x3Du,==)
		test(dest[0x12u],0x43u,==)
		test(dest[0x13u],0x47u,==)
		test(dest[0x14u],0x49u,==)
		test(dest[0x15u],0x4Fu,==)
		test(dest[0x16u],0x53u,==)
		test(dest[0x17u],0x59u,==)
		test(dest[0x18u],0x61u,==)
		test(dest[0x19u],0x65u,==)
		test(dest[0x1Au],0x67u,==)
		test(dest[0x1Bu],0x6Bu,==)
		test(dest[0x1Cu],0x6Du,==)
		test(dest[0x1Du],0x71u,==)
		test(dest[0x1Eu],0x7Fu,==)
		test(dest[0x1Fu],0x83u,==)
		test(dest[0x20u],0x89u,==)
		test(dest[0x21u],0x8Bu,==)
		test(dest[0x22u],0x95u,==)
		test(dest[0x23u],0x97u,==)
		test(dest[0x24u],0x9Du,==)
		test(dest[0x25u],0xA3u,==)
		test(dest[0x26u],0xA7u,==)
		test(dest[0x27u],0xADu,==)
		test(dest[0x28u],0xB3u,==)
		test(dest[0x29u],0xB5u,==)
		test(dest[0x2Au],0xBFu,==)
		test(dest[0x2Bu],0xC1u,==)
		test(dest[0x2Cu],0xC5u,==)
		test(dest[0x2Du],0xC7u,==)
		test(dest[0x2Eu],0xD3u,==)
		test(dest[0x2Fu],0xDFu,==)
		test(dest[0x30u],0xE3u,==)
		test(dest[0x31u],0xE5u,==)
		test(dest[0x32u],0xE9u,==)
		test(dest[0x33u],0xEFu,==)
		test(dest[0x34u],0xF1u,==)
		test(dest[0x35u],0xFBu,==)
	}
	{
		::zap::i04 val = 0x0u;
		::zap::bytefill(&val,(::zap::i8)-0x1u,sizeof (val));
		test(val,~(::zap::i04)0x0u,==)
	}
	{
		::zap::i01 const val0 = 0x4545u;
		::zap::i01 const val1 = 0x4545u;
		::zap::i01 const val2 = 0x4560u;
		test(::zap::byteeq(&val0,&val1,sizeof (val0)),0x1u,==)
		test(::zap::byteeq(&val0,&val2,sizeof (val0)),0x0u,==)
	}
	{
		::zap::i02 const val = 0xFF7F6045u;
		unsigned char const * const arr = reinterpret_cast<unsigned char const * const>(&val);
		test(::zap::bytesrch(arr,0x7Fu,sizeof (arr)),&arr[0x2u],==)
	}
	// str
	{
		::zap::i8 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::i02 * const utf02 = new ::zap::i02[enclen + 0x1u];
		if (utf02 == 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,    ==)
		delete[] utf02;
	}
	{
		::zap::i02 const utf02[] = {
			0x26u,
			0xF0u,
			0xD9Eu,
			0x218Au,
			0x218Bu,
			0x1F5FFu,
			0x0u,
		};
		::zap::sz const enclen = ::zap_utf8enclen(utf02);
		test(enclen,0x10u,==)
		::zap::i8 * const utf8 = new ::zap::i8[enclen + 0x1u];
		if (utf8 == 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, ==)

		delete[] utf8;
	}
	{
		::zap::i8 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::i02 * const utf02 = new ::zap::i02[enclen + 0x1u];
		if (utf02 == 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,   ==);

		delete[] utf02;
	}
	{
		::zap::i02 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::i8 * const win1252 = new ::zap::i8[enclen + 0x1u];
		if (win1252 == 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, ==);
		
		delete[] win1252;
	}
	{
		char const * str0 = "Hello there!";
		char const * str1 = "";
		::zap::sz const len0 = ::zap::strlen(str0);
		::zap::sz const len1 = ::zap::strlen(str1);
		test(len0,0xCu,==);
		test(len1,0x0u,==);
	}
	{
		char const * str0 = "Cogito";
		char const * str1 = "Sum";
		::zap::i8 const cmp0 = ::zap::streq(str0,str0);
		::zap::i8 const cmp1 = ::zap::streq(str0,str1);
		test(cmp0,0x0u,>);
		test(cmp1,0x0u,==);
	}
	{
		int val = -0x4560;
		::zap::i8 len = ::zap_fmtleni(val,0x10u);
		test(len,0x5u,==);
		::zap::i02 str[0x5u];
		::zap_fmti(str,val,0x10u,0x1u);
		test(str[0x0u],0x2212u,==);
		test(str[0x1u],0x0034u,==);
		test(str[0x2u],0x0035u,==);
		test(str[0x3u],0x0036u,==);
		test(str[0x4u],0x0030u,==);
		val = 0x8E;
		len = ::zap_fmtleni(val,0xCu);
		test(len,0x2u,==);
		::zap_fmti(str,val,0xCu,0x0u);
		test(str[0x0u],0x218Au,==);
		test(str[0x1u],0x218Bu,==);
	}
}