summaryrefslogtreecommitdiff
path: root/test.cc
blob: 47940ef67330e3ac1038eff4bb6c19f929a46bbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <climits>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <limits>
#include <source_location>
#include <typeinfo>
#include <zp/mem>
#include <zp/mth>
#include <zp/str>

static_assert(::zp::isptr<int>::val                                  == false);
static_assert(::zp::isptr<int                *>::val                 == true);
static_assert(::zp::isptr<int const          *>::val                 == true);
static_assert(::zp::isptr<int volatile       *>::val                 == true);
static_assert(::zp::isptr<int const volatile *>::val                 == true);
static_assert(::zp::isptr<int                * const>::val           == true);
static_assert(::zp::isptr<int const          * const>::val           == true);
static_assert(::zp::isptr<int volatile       * const>::val           == true);
static_assert(::zp::isptr<int const volatile * const>::val           == true);
static_assert(::zp::isptr<int                * volatile>::val        == true);
static_assert(::zp::isptr<int const          * volatile>::val        == true);
static_assert(::zp::isptr<int volatile       * volatile>::val        == true);
static_assert(::zp::isptr<int const volatile * volatile>::val        == true);
static_assert(::zp::isptr<int                * const volatile>::val  == true);
static_assert(::zp::isptr<int const          * const volatile>::val  == true);
static_assert(::zp::isptr<int volatile       * const volatile>::val  == true);
static_assert(::zp::isptr<int const volatile * const volatile>::val  == true);

static_assert(::zp::typequ<int,int>::val  == true);
static_assert(::zp::typequ<int,long>::val == false);

static_assert(::zp::typequ<::zp::sgn<int>::typ,         int>::val == true);
static_assert(::zp::typequ<::zp::sgn<int unsigned>::typ,int>::val == true);

static_assert(::zp::typequ<::zp::usgn<int unsigned>::typ,int unsigned>::val == true);
static_assert(::zp::typequ<::zp::usgn<int>::typ,         int unsigned>::val == true);

static_assert(::zp::typequ<::zp::remqual<int volatile>::typ,      int>::val == true);
static_assert(::zp::typequ<::zp::remqual<int const volatile>::typ,int>::val == true);

static_assert(::zp::typequ<::zp::remqual<int>::typ,               int>::val == true);
static_assert(::zp::typequ<::zp::remqual<int const>::typ,         int>::val == true);
static_assert(::zp::typequ<::zp::remqual<int volatile>::typ,      int>::val == true);
static_assert(::zp::typequ<::zp::remqual<int const volatile>::typ,int>::val == true);
		
static_assert(::zp::typequ<::zp::nulptrtyp,::std::nullptr_t>::val == true);

static_assert(::zp::typequ<::zp::i8m,  ::std::uint_least8_t>::val  == true);
static_assert(::zp::typequ<::zp::i8ms, ::std::int_least8_t>::val   == true);
static_assert(::zp::typequ<::zp::i01m, ::std::uint_least16_t>::val == true);
static_assert(::zp::typequ<::zp::i01ms,::std::int_least16_t>::val  == true);
static_assert(::zp::typequ<::zp::i02m, ::std::uint_least32_t>::val == true);
static_assert(::zp::typequ<::zp::i02ms,::std::int_least32_t>::val  == true);
static_assert(::zp::typequ<::zp::i04m, ::std::uint_least64_t>::val == true);
static_assert(::zp::typequ<::zp::i04ms,::std::int_least64_t>::val  == true);

static_assert(::zp::typequ<::zp::intptr,::std::uintptr_t>::val == true);
static_assert(::zp::typequ<::zp::sz,    ::std::size_t>::val    == true);

static_assert(::zp::isusgn<char>::val               == ::std::is_unsigned<char>::value);
static_assert(::zp::isusgn<char16_t>::val           == ::std::is_unsigned<char16_t>::value);
static_assert(::zp::isusgn<char32_t>::val           == ::std::is_unsigned<char32_t>::value);
static_assert(::zp::isusgn<char8_t>::val            == ::std::is_unsigned<char8_t>::value);
static_assert(::zp::isusgn<double>::val             == ::std::is_unsigned<double>::value);
static_assert(::zp::isusgn<float>::val              == ::std::is_unsigned<float>::value);
static_assert(::zp::isusgn<int>::val                == ::std::is_unsigned<int>::value);
static_assert(::zp::isusgn<long>::val               == ::std::is_unsigned<long>::value);
static_assert(::zp::isusgn<long double>::val        == ::std::is_unsigned<long double>::value);
static_assert(::zp::isusgn<long long>::val          == ::std::is_unsigned<long long>::value);
static_assert(::zp::isusgn<short>::val              == ::std::is_unsigned<short>::value);
static_assert(::zp::isusgn<char signed>::val        == ::std::is_unsigned<char signed>::value);
static_assert(::zp::isusgn<char unsigned>::val      == ::std::is_unsigned<char unsigned>::value);
static_assert(::zp::isusgn<int unsigned>::val       == ::std::is_unsigned<int unsigned>::value);
static_assert(::zp::isusgn<long unsigned>::val      == ::std::is_unsigned<long unsigned>::value);
static_assert(::zp::isusgn<long long unsigned>::val == ::std::is_unsigned<long long unsigned>::value);
static_assert(::zp::isusgn<short unsigned>::val     == ::std::is_unsigned<short unsigned>::value);
static_assert(::zp::isusgn<wchar_t>::val            == ::std::is_unsigned<wchar_t>::value);		

static_assert(::zp::issgn<char>::val               == ::std::is_signed<char>::value);
static_assert(::zp::issgn<double>::val             == ::std::is_signed<double>::value);
static_assert(::zp::issgn<float>::val              == ::std::is_signed<float>::value);
static_assert(::zp::issgn<int>::val                == ::std::is_signed<int>::value);
static_assert(::zp::issgn<long>::val               == ::std::is_signed<long>::value);
static_assert(::zp::issgn<long double>::val        == ::std::is_signed<long double>::value);
static_assert(::zp::issgn<long long>::val          == ::std::is_signed<long long>::value);
static_assert(::zp::issgn<short>::val              == ::std::is_signed<short>::value);
static_assert(::zp::issgn<char signed>::val        == ::std::is_signed<char signed>::value);
static_assert(::zp::issgn<char unsigned>::val      == ::std::is_signed<char unsigned>::value);
static_assert(::zp::issgn<int unsigned>::val       == ::std::is_signed<int unsigned>::value);
static_assert(::zp::issgn<long unsigned>::val      == ::std::is_signed<long unsigned>::value);
static_assert(::zp::issgn<long long unsigned>::val == ::std::is_signed<long long unsigned>::value);
static_assert(::zp::issgn<short unsigned>::val     == ::std::is_signed<short unsigned>::value);
static_assert(::zp::issgn<wchar_t>::val            == ::std::is_signed<wchar_t>::value);

static_assert(::zp::bytelen == CHAR_BIT);

static_assert(::zp::minval<bool>::val               == ::std::numeric_limits<bool>::lowest());
static_assert(::zp::minval<char>::val               == ::std::numeric_limits<char>::lowest());
static_assert(::zp::minval<char16_t>::val           == ::std::numeric_limits<char16_t>::lowest());
static_assert(::zp::minval<char32_t>::val           == ::std::numeric_limits<char32_t>::lowest());
static_assert(::zp::minval<char8_t>::val            == ::std::numeric_limits<char8_t>::lowest());
static_assert(::zp::minval<double>::val             == ::std::numeric_limits<double>::lowest());
static_assert(::zp::minval<float>::val              == ::std::numeric_limits<float>::lowest());
static_assert(::zp::minval<int>::val                == ::std::numeric_limits<int>::lowest());
static_assert(::zp::minval<long>::val               == ::std::numeric_limits<long>::lowest());
static_assert(::zp::minval<long double>::val        == ::std::numeric_limits<long double>::lowest());
static_assert(::zp::minval<long long>::val          == ::std::numeric_limits<long long>::lowest());
static_assert(::zp::minval<short>::val              == ::std::numeric_limits<short>::lowest());
static_assert(::zp::minval<char signed>::val        == ::std::numeric_limits<char signed>::lowest());
static_assert(::zp::minval<char unsigned>::val      == ::std::numeric_limits<char unsigned>::lowest());
static_assert(::zp::minval<short unsigned>::val     == ::std::numeric_limits<short unsigned>::lowest());
static_assert(::zp::minval<int unsigned>::val       == ::std::numeric_limits<int unsigned>::lowest());
static_assert(::zp::minval<long unsigned>::val      == ::std::numeric_limits<long unsigned>::lowest());
static_assert(::zp::minval<long long unsigned>::val == ::std::numeric_limits<long long unsigned>::lowest());
static_assert(::zp::minval<wchar_t>::val            == ::std::numeric_limits<wchar_t>::lowest());

static_assert(::zp::maxval<bool>::val               == ::std::numeric_limits<bool>::max());
static_assert(::zp::maxval<char>::val               == ::std::numeric_limits<char>::max());
static_assert(::zp::maxval<char16_t>::val           == ::std::numeric_limits<char16_t>::max());
static_assert(::zp::maxval<char32_t>::val           == ::std::numeric_limits<char32_t>::max());
static_assert(::zp::maxval<char8_t>::val            == ::std::numeric_limits<char8_t>::max());
static_assert(::zp::maxval<double>::val             == ::std::numeric_limits<double>::max());
static_assert(::zp::maxval<float>::val              == ::std::numeric_limits<float>::max());
static_assert(::zp::maxval<int>::val                == ::std::numeric_limits<int>::max());
static_assert(::zp::maxval<long>::val               == ::std::numeric_limits<long>::max());
static_assert(::zp::maxval<long double>::val        == ::std::numeric_limits<long double>::max());
static_assert(::zp::maxval<long long>::val          == ::std::numeric_limits<long long>::max());
static_assert(::zp::maxval<short>::val              == ::std::numeric_limits<short>::max());
static_assert(::zp::maxval<char signed>::val        == ::std::numeric_limits<char signed>::max());
static_assert(::zp::maxval<char unsigned>::val      == ::std::numeric_limits<char unsigned>::max());
static_assert(::zp::maxval<short unsigned>::val     == ::std::numeric_limits<short unsigned>::max());
static_assert(::zp::maxval<int unsigned>::val       == ::std::numeric_limits<int unsigned>::max());
static_assert(::zp::maxval<long unsigned>::val      == ::std::numeric_limits<long unsigned>::max());
static_assert(::zp::maxval<long long unsigned>::val == ::std::numeric_limits<long long unsigned>::max());
static_assert(::zp::maxval<wchar_t>::val            == ::std::numeric_limits<wchar_t>::max());

int main() {
	int unsigned num;
	int unsigned numerr = 0x0u;

	auto const tst = [&num](char const * const cmp) noexcept {
		::std::cout <<"\n\x1B[38;5;75mtesting\x1B[0m " <<cmp <<"\n\n";

		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>::val) {return static_cast<::zp::i02m>(valref);}
			else if constexpr (::zp::isptr<typ>::val) {return reinterpret_cast<void *>(valref);}
			else                                      {return valref;}
		};

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

		::std::cout <<"  " <<num++ <<". comparing " <<ltypnm <<" (" <<lval <<") vs. " <<rtypnm <<" (" <<rval <<")... ";

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

		::std::cout <<"\x1B[38;5;77mequal\x1B[0m\n";
	};

	::std::cout <<"zp " <<::zp::ver <<"." <<::zp::extver <<", run-time test\n";

	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,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);

			cmp(filbuf,0xFFFFFFFF7F7F3F1Fu);

			::zp::i04 cpybuf;
			::zp::memcpy(&cpybuf,&filbuf,0x8u);

			cmp(cpybuf,filbuf);

			cmp(::zp::memsrh(&cpybuf,0x1Fu,0x8u),reinterpret_cast<char unsigned *>(&cpybuf));
		}();

		[&] {
			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+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);

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

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

			::zp_utf8dec(buf02,buf8);

			cmp(buf02[0x0],0x1F480u);
			cmp(buf02[0x1],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+0x1];

			::zp_utf16enc(buf01,src);

			cmp(buf01[0x0],0xD83Du);
			cmp(buf01[0x1],0xDC80u);
			cmp(buf01[0x2],0x00F0u);
			cmp(buf01[0x3],0x0000u);

			::zp::sz const buf02len = ::zp_utf16declen(buf01);
			cmp(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);

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

	::std::cout <<"\nDone \u2212 " <<numerr <<" error(s)\n";

	return EXIT_SUCCESS;
}