summaryrefslogtreecommitdiff
path: root/test.cc
blob: ac5c6363592209bf72b0f0352f5d34453428a7d4 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include <climits>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <limits>
#include <memory>
#include <typeinfo>
#include <zp/mem>
#include <zp/mth>
#include <zp/str>

#if !zp_std_cxx17
#error At least C++17 is required for this test!
#endif

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::isref<int>::val                                  == false);
static_assert(::zp::isref<int &>::val               == true);
static_assert(::zp::isref<int const&>::val          == true);
static_assert(::zp::isref<int volatile&>::val       == true);
static_assert(::zp::isref<int const volatile&>::val == true);

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

static_assert(::zp::typequ<::zp::i8,  ::std::uint8_t>::val  == true);
static_assert(::zp::typequ<::zp::i8s, ::std::int8_t>::val   == true);
static_assert(::zp::typequ<::zp::i01, ::std::uint16_t>::val == true);
static_assert(::zp::typequ<::zp::i01s,::std::int16_t>::val  == true);
static_assert(::zp::typequ<::zp::i02, ::std::uint32_t>::val == true);
static_assert(::zp::typequ<::zp::i02s,::std::int32_t>::val  == true);
static_assert(::zp::typequ<::zp::i04, ::std::uint64_t>::val == true);
static_assert(::zp::typequ<::zp::i04s,::std::int64_t>::val  == true);

static_assert(::zp::typequ<::zp::ptr,   ::std::uintptr_t>::val == true);
static_assert(::zp::typequ<::zp::ptrdif,::std::ptrdiff_t>::val == true);
static_assert(::zp::typequ<::zp::siz,   ::std::size_t>::val    == true);

static_assert(::zp::typequ<::zp::remref<int>::typ,  int>::val == true);
static_assert(::zp::typequ<::zp::remref<int &>::typ,int>::val == true);

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

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

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

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

static_assert(::zp::typequ<::zp::nulptrtyp,::std::nullptr_t>::val == true);

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

template<typename typtyp> constexpr static auto chktyp() noexcept -> bool {
	using ::zp::nulptr;
	using ::zp::typequ;

	using typ    = ::zp::typ<typtyp>;
	using val    = ::zp::val<typtyp>;
	using numlim = ::std::numeric_limits<typtyp>;

	if constexpr (!typequ<typename ::zp::remcv<typtyp>::typ,bool>::val && !typ::ischr) {static_assert(typ::isint == ::std::is_integral_v<typtyp>);}

	static_assert(typ::isflt == ::std::is_floating_point_v<typtyp>);

	static_assert(nulptr<typtyp>() == nullptr);

	static_assert(typ::hassgn == numlim::is_signed);

	static_assert(val::min() == numlim::lowest());
	static_assert(val::max() == numlim::max());

	if constexpr (typ::isflt) {
		static_assert(val::inf() == numlim::infinity());
	}

	return true;
}

template<typename typ> constexpr static auto chktypcv() noexcept -> bool {
	return ::chktyp<typ>() && ::chktyp<typ const>() && ::chktyp<typ volatile>() && ::chktyp<typ const volatile>();
}

static_assert(::chktypcv<bool>()                   == true);
static_assert(::chktypcv<char>()                   == true);
static_assert(::chktypcv<char signed>()            == true);
static_assert(::chktypcv<char unsigned>()          == true);
static_assert(::chktypcv<char16_t>()               == true);
static_assert(::chktypcv<char32_t>()               == true);
static_assert(::chktypcv<::zp::c8>()               == true);
static_assert(::chktypcv<double>()                 == true);
static_assert(::chktypcv<double long>()            == true);
static_assert(::chktypcv<float>()                  == true);
static_assert(::chktypcv<int>()                    == true);
static_assert(::chktypcv<int long>()               == true);
static_assert(::chktypcv<int long long>()          == true);
static_assert(::chktypcv<int long long unsigned>() == true);
static_assert(::chktypcv<int long unsigned>()      == true);
static_assert(::chktypcv<int short>()              == true);
static_assert(::chktypcv<int short unsigned>()     == true);
static_assert(::chktypcv<int unsigned>()           == true);
static_assert(::chktypcv<wchar_t>()                == true);

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) {
		using typ = typename ::zp::remcv<typename ::zp::remref<decltype (valref)>::typ>::typ;

		if constexpr      (::zp::typ<typ>::ischr) {return static_cast<::zp::i02>(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);

	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;
		}
	};

	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" << ::std::endl;
		//throw ::std::exception {};
		++numerr;

		return;
	}

	::std::cout <<"\x1B[38;5;77mokay\x1B[0m" << ::std::endl;
}

#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;

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

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

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

			::zp::opt<::zp::i8s> const optasg = opt;

			chkeq(optasg.chk(),true);
			chkeq(optasg.exp(fun,"expectation failed"),-0x45);
		}();

		[&] {
			tst("results");

			auto res = ::zp::res<::zp::i8s,::zp::i8s>::err(0x45);

			chkeq(res.chk(),false);
			chkeq(res.geterr(),0x45);

			res = ::zp::res<::zp::i8s,::zp::i8s>::oky(-0x45);

			auto const fun = [](char const* msg) -> void {
				::std::cout << msg << ::std::endl;

				::std::abort();
			};

			chkeq(res.chk(),true);
			chkeq(res.exp(fun,"expectation failed"),-0x45);

			class nopod0 {
			public:
				char val;

				nopod0() noexcept : val('\x00') {}
				nopod0(char const* const str) noexcept : val(str[0x0]) {}
			
				~nopod0() noexcept(false) {}
			};

			class nopod1 {
			public:
				int val;

				nopod1() noexcept : val(0x0) {}
				nopod1(int const val) noexcept : val(0x0-val) {}

				~nopod1() noexcept(false) {}
			};

			auto resnopod = ::zp::res<nopod0,nopod1>::oky("Hallo");

			chkeq(resnopod.chk(),true);
			chkeq(resnopod.getoky().val,'H');

			resnopod = ::zp::res<nopod0,nopod1>::err(0x4);

			chkeq(resnopod.chk(),false);
			chkeq(resnopod.geterr().val,-0x4);
		}();

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

			::zp::f02 f02;
			::zp::f04 f04;

			chkeq(sizeof (f02),0x4u);
			chkeq(sizeof (f04),0x8u);

			f02 = ::zp::val<::zp::f02>::nan();
			f04 = ::zp::val<::zp::f04>::nan();

			chkeq(::zp::isnan(f02),true);
			chkeq(::zp::isnan(f04),true);

			f02 = ::zp::val<::zp::f02>::inf();
			f04 = ::zp::val<::zp::f04>::inf();

			chkgt(f02,::zp::val<::zp::f02>::max());
			chkgt(f04,::zp::val<::zp::f04>::max());
		}();

		[&] {
			tst("mathematics");
		}();

		[&] {
			tst("vectors");

		/*	::zp::vec2<int> const lvec2 = {
				+0x1,
				-0x3,
			};
			::zp::vec2<int> const rvec2 = {
				+0x2,
				-0x4,
			};
			chkeq(::zp::dot2(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,
			};
			chkeq(::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,
			};
			chkeq(::zp::dot(lvec4,rvec4),0x64);

			auto const avec2 = ::zp::vadd(lvec2,rvec2);
			auto const svec2 = ::zp::vsub(lvec2,rvec2);
			chkeq(avec2.x,+0x3);
			chkeq(avec2.y,-0x7);
			chkeq(svec2.x,-0x1);
			chkeq(svec2.y,+0x1);*/
		}();

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

			::zp::i04 filbuf;

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

			chkeq(filbuf,0xFFFFFFFF7F7F3F1Fu);

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

			chkeq(cpybuf,filbuf);

			chkeq(::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::siz const len   = ::zp_strlen(  str);
			::zp::siz const wlen  = ::zp_wstrlen( wstr);
			::zp::siz const len02 = ::zp_utf32len(str02);

			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];

			chkeq(::zp_strcpy(  buf,  str),  len);
			chkeq(::zp_wstrcpy( wbuf, wstr), wlen);
			chkeq(::zp_utf32cpy(buf02,str02),len02);

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

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

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

			::zp::siz const buf8len = ::zp_utf8enclen(src);
			chkeq(buf8len,0x4u);
			
			::zp::c8 * buf8 = new ::zp::c8[buf8len+0x1];

			::zp_utf8enc(buf8,src);

			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);
			chkeq(buf02len,0x1u);

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

			::zp_utf8dec(buf02,buf8);

			chkeq(buf02[0x0],0x1F480u);
			chkeq(buf02[0x1],0x0u);

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

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

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

			::zp::siz const buf01len = ::zp_utf16enclen(src);
			chkeq(buf01len,0x3u);
			
			char16_t * buf01 = new char16_t[buf01len+0x1];

			::zp_utf16enc(buf01,src);

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

			::zp::siz const buf02len = ::zp_utf16declen(buf01);
			chkeq(buf02len,0x2u);

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

			::zp_utf16dec(buf02,buf01);

			chkeq(buf02[0x0],0x1F480u);
			chkeq(buf02[0x1],0xF0u);
			chkeq(buf02[0x2],0x0u);

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

		[&] {
			tst("arrays");

			auto arr0 = ::zp::arr<::zp::i8,::std::allocator<::zp::i8>>::alc(0x8u);

			chkeq(arr0.len(),0x8u);

			arr0[0x0] = 0x7Fu;
			arr0[0x1] = 0x3Fu;
			arr0[0x2] = 0x1Fu;
			arr0[0x3] = 0x0Fu;
			arr0[0x4] = 0x07u;
			arr0[0x5] = 0x03u;
			arr0[0x6] = 0x01u;
			arr0[0x7] = 0x00u;

			::zp::i8 prv = 0xFFu;
			for (::zp::i8 const val : arr0) {
				chkeq(val,static_cast<::zp::i8>(prv>>0x1u));
				prv = val;
			}

			arr0.fil(0xFFu);

			for (::zp::i8 const val : arr0) {
				chkeq(val,0xFFu);
			}
		}();
	}
	catch (::std::exception const&) {
		return EXIT_FAILURE;
	}

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

	return numerr ? EXIT_FAILURE : EXIT_SUCCESS;
}