diff options
Diffstat (limited to 'dux-demo')
-rw-r--r-- | dux-demo/CMakeLists.txt | 45 | ||||
-rw-r--r-- | dux-demo/ctest.c | 46 | ||||
-rw-r--r-- | dux-demo/main.cc | 251 |
3 files changed, 306 insertions, 36 deletions
diff --git a/dux-demo/CMakeLists.txt b/dux-demo/CMakeLists.txt new file mode 100644 index 0000000..7c799d8 --- /dev/null +++ b/dux-demo/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required( + VERSION + 3.19 +) + +# Language Standards: +set( + CMAKE_C_STANDARD + 11 +) +set( + CMAKE_C_STANDARD_REQUIRED + TRUE +) +set( + CMAKE_CXX_STANDARD + 20 +) +set( + CMAKE_CXX_STANDARD_REQUIRED + TRUE +) + +# Project: +project( + dux-demo + LANGUAGES + C + CXX + VERSION + ${DUX_VERSION} +) + +# Target: +add_executable( + dux-demo + "ctest.c" + "main.cc" +) + +# Dependencies: +target_link_libraries( + dux-demo + dux +)
\ No newline at end of file diff --git a/dux-demo/ctest.c b/dux-demo/ctest.c new file mode 100644 index 0000000..8869c00 --- /dev/null +++ b/dux-demo/ctest.c @@ -0,0 +1,46 @@ +# include <dux/io> +# include <dux/math> +# include <dux/media> +# include <dux/mem> +# include <dux/seq> +# include <dux/sig> +# include <dux/str> +# include <dux/thrd> +# include <dux/time> + +#include <stdio.h> + +bool dux_local_gotsig = false; + +static bool dux_local_sighandl(enum dux_sig const _sig) { + dux_ass("",_sig == dux_sig_intr); + dux_local_gotsig = true; + return false; +} + +void ctest(void const * const restrict _params) { + dux_dbglog("dux :: demo :: Performing C-tests!\n"); + if (dux_getnummainparams(_params) == dux_uwrdl(0x0)) {dux_dbglog("dux :: demo :: No parameters!\n");} + else {for (dux_uwrd n = dux_uwrdl(0x0);n < dux_getnummainparams(_params);++n) {dux_dbglog("dux :: demo :: Parameter %zu: %s\n",n + dux_uwrdl(0x1),dux_getmainparam(_params,n));}} + { + dux_setsighandl(dux_sig_intr,dux_local_sighandl); + dux_raise(dux_sig_intr); + dux_setsighandl(dux_sig_intr,dux_dflsighandl); + dux_ass("",dux_local_gotsig); + } + /*{ + int arr0[] = { + 0xF, + 0xFF, + 0xFFF, + 0xFFFF, + }; + int * const restrict arr1 = dux_alloc(sizeof (arr0)); + dux_cpy(arr0,sizeof (arr0),arr1,sizeof (arr0[dux_uwrdl(0x0)])); + dux_ass("",arr1[dux_uwrdl(0x0)] == 0xF); + dux_ass("",arr1[dux_uwrdl(0x1)] == 0xFF); + dux_ass("",arr1[dux_uwrdl(0x2)] == 0xFFF); + dux_ass("",arr1[dux_uwrdl(0x3)] == 0xFFFF); + dux_free(arr1); + }*/ +} diff --git a/dux-demo/main.cc b/dux-demo/main.cc index fea721e..c67d5de 100644 --- a/dux-demo/main.cc +++ b/dux-demo/main.cc @@ -1,40 +1,219 @@ -#include <dux/io> -#include <dux/media> -#include <dux/thrd> - -auto thrdfn0(int const _x,int const _y,int const _z,int const _w) -> int { - return _x * _y * _z * _w; -}; -auto thrdfn1(int const _x) -> int { - return _x * _x * _x; -}; - -auto main() -> int { - _Pragma("GCC diagnostic ignored \"-Wunreachable-code\""); - ::dux::dobj dobj; - dobj.link("libdux.so"); - dobj.call<void>("goatmaster",0xC); - ::dux::thrd thrd; - auto val{thrd(thrdfn0,0xC,0xC,0xC,0xC)}; - thrd.join(&val); - ::dux::print(U"Returned value: $\n",val); - val = thrd(thrdfn1,0x4); - thrd.join(&val); - ::dux::print(U"Returned value: $\n",val); - ::dux::initgfx(); - auto win = ::dux::win::crt("dux demo",::dux::uint32v(0x400),::dux::uint32v(0x300)); - for (auto run{true};run;) { - while (auto const evt = win.pollevt()) { - switch (evt->typ) { - default: - goto skipevt; - case ::dux::evttyp::quit: - run = false; - break; +# include <dux/arr> +# include <dux/io> +# include <dux/math> +# include <dux/media> +# include <dux/mem> +# include <dux/seq> +# include <dux/sig> +# include <dux/str> +# include <dux/thrd> +# include <dux/time> + +# include <signal.h> + +static_assert(sizeof (::dux::uwrd) >= sizeof (void *),"::dux::uwrd must be capable of holding a pointer value"); + +static_assert(::dux::abs(0x10) == 0x10); +static_assert(::dux::abs(-0x10) == 0x10); + +static_assert(::dux::fac(0x1) == 0x1); +static_assert(::dux::fac(0x2) == 0x2); +static_assert(::dux::fac(0x3) == 0x6); +static_assert(::dux::fac(0x4) == 0x18); + +static_assert(::dux::msb(0b1) == 0b1); +static_assert(::dux::msb(0b10) == 0b10); +static_assert(::dux::msb(0b101) == 0b100); +static_assert(::dux::msb(0b1010) == 0b1000); +static_assert(::dux::msb(0b10101) == 0b10000); +static_assert(::dux::msb(0b101010) == 0b100000); + +namespace dux::local { + static auto gotsig {false}; +} + +static_assert([] { + auto chk {[] (::dux::sig const _sig0,int const _sig1) { + return static_cast<int>(_sig0) == _sig1; + }}; + static_assert(chk(::dux::sig::abrt, SIGABRT)); + static_assert(chk(::dux::sig::alrm, SIGALRM)); + static_assert(chk(::dux::sig::bus, SIGBUS)); + static_assert(chk(::dux::sig::chld, SIGCHLD)); + static_assert(chk(::dux::sig::cont, SIGCONT)); + static_assert(chk(::dux::sig::fpe, SIGFPE)); + static_assert(chk(::dux::sig::hup, SIGHUP)); + static_assert(chk(::dux::sig::ill, SIGILL)); + static_assert(chk(::dux::sig::io, SIGIO)); + static_assert(chk(::dux::sig::intr, SIGINT)); + static_assert(chk(::dux::sig::kill, SIGKILL)); + static_assert(chk(::dux::sig::pipe, SIGPIPE)); + static_assert(chk(::dux::sig::prof, SIGPROF)); + static_assert(chk(::dux::sig::quit, SIGQUIT)); + static_assert(chk(::dux::sig::segv, SIGSEGV)); + static_assert(chk(::dux::sig::stop, SIGSTOP)); + static_assert(chk(::dux::sig::sys, SIGSYS)); + static_assert(chk(::dux::sig::term, SIGTERM)); + static_assert(chk(::dux::sig::trap, SIGTRAP)); + static_assert(chk(::dux::sig::tstp, SIGTSTP)); + static_assert(chk(::dux::sig::ttin, SIGTTIN)); + static_assert(chk(::dux::sig::ttou, SIGTTOU)); + static_assert(chk(::dux::sig::urg, SIGURG)); + static_assert(chk(::dux::sig::usr1, SIGUSR1)); + static_assert(chk(::dux::sig::usr2, SIGUSR2)); + static_assert(chk(::dux::sig::vtalrm,SIGVTALRM)); + static_assert(chk(::dux::sig::winch, SIGWINCH)); + static_assert(chk(::dux::sig::xcpu, SIGXCPU)); + static_assert(chk(::dux::sig::xfsz, SIGXFSZ)); +# if defined(SIGPOLL) + static_assert(chk(::dux::sig::poll, SIGPOLL)); +# endif +# if defined(SIGPWR) + static_assert(chk(::dux::sig::pwr, SIGPWR)); +# endif + return true; +}()); + +extern "C" auto ctest(void const * params) -> void; + +extern "C" auto dux_main(::dux::mainparams const * const restrict _params) -> ::dux::stat { + if (_params->num() == dux_uwrdl(0x0)) {::dux::dbglog("dux :: demo :: No parameters!\n");} + else {for (auto n {dux_uwrdl(0x0)};n < _params->num();++n) {::dux::dbglog("dux :: demo :: Parameter #%zu: %s\n",n + dux_uwrdl(0x1),_params->param(n).ptr);}} + { + constexpr auto estim {dux_uint64l(0x100)}; + ::dux::dbglog("Trying to roll two identical 8-bit numbers in a row. Estimated number of tries: " dux_printfuint64 ".\n",estim); + auto num0 {::dux::rnd<::dux::uint8>()}; + auto num1 {::dux::rnd<::dux::uint8>()}; + for (auto n = dux_uint64l(0x1);;++n) { + if (num0 == num1) [[unlikely]] { + ::dux::dbglog("Done! Took " dux_printfuint64 " attempts (estimated: " dux_printfuint64 ") to roll the same 8-bit number (" dux_printfuint8 ") two times in a row.\n",n,estim,num0); + ::dux::dbglog("Odds: %f%%",((double)n / (double)estim) * (double)100.0); + if (n == dux_uint64l(0x45)) {::dux::dbglog(" - Nice!");} /* 69 */ + else if (n == dux_uint64l(0x1)) {::dux::dbglog(" - bruh");} + ::dux::dbglog("\n\nContinuing to the testing stage...\n\n"); + goto test; } + num0 = ::dux::rnd<::dux::uint8>(); + num1 = ::dux::rnd<::dux::uint8>(); + } + } +test:; + auto const time = ::dux::gettime(); + ::dux::dbglog("Current time: " dux_printfsint64 "\n",time); + dux_ass("",time >= dux_sint64l(0x5C10DFA4)); + { + ::dux::reseterr(); + auto const ptr {::dux::alloc<char>()}; + dux_ass("Memory allocation failed!",!::dux::haserr()); + ::dux::free(ptr); + } + { + constexpr auto val {dux_uint8l(0xC4)}; + dux_ass("",::dux::getbit(val,0x0) == false); + dux_ass("",::dux::getbit(val,0x1) == false); + dux_ass("",::dux::getbit(val,0x2) == true); + dux_ass("",::dux::getbit(val,0x3) == false); + dux_ass("",::dux::getbit(val,0x4) == false); + dux_ass("",::dux::getbit(val,0x5) == false); + dux_ass("",::dux::getbit(val,0x6) == true); + dux_ass("",::dux::getbit(val,0x7) == true); + } + { + constexpr auto arrsz {dux_uwrdl(0x5)}; + constexpr ::dux::uint16 arr0[] { + dux_uint16l(0xF), + dux_uint16l(0x1F), + dux_uint16l(0x3F), + dux_uint16l(0x7F), + dux_uint16l(0xFF), + }; + auto const arr1 {::dux::alloc<::dux::uint16>(arrsz)}; + ::dux::cpy(arr0,arrsz,arr1); + dux_ass("",arr1[dux_uwrdl(0x0)] == dux_uint16l(0xF)); + dux_ass("",arr1[dux_uwrdl(0x1)] == dux_uint16l(0x1F)); + dux_ass("",arr1[dux_uwrdl(0x2)] == dux_uint16l(0x3F)); + dux_ass("",arr1[dux_uwrdl(0x3)] == dux_uint16l(0x7F)); + dux_ass("",arr1[dux_uwrdl(0x4)] == dux_uint16l(0xFF)); + ::dux::free(arr1); + } + /*{ + auto * restrict ptr = ::dux::alloc<::dux::uint16>(dux_uwrdl(0x1)); + *ptr = dux_uint16l(0x10); + dux_ass("",*ptr == dux_uint16l(0x10)); + ptr = ::dux::realloc(ptr,dux_uwrdl(0x2)); + ptr[dux_uwrdl(0x1)] = dux_uint16l(0x20); + dux_ass("",ptr[dux_uwrdl(0x0)] == dux_uint16l(0x10)); + dux_ass("",ptr[dux_uwrdl(0x1)] == dux_uint16l(0x20)); + ::dux::free(ptr); + }*/ /* Does not work as realloc currently does not support reallocating aligned allocations. */ + { + constexpr auto arrsz {dux_uwrdl(0x2)}; + ::dux::uint64 arr[arrsz]; + ::dux::bytefill(arr,arrsz,dux_uint8l(0xFF)); + dux_ass("",arr[dux_uwrdl(0x0)] >= dux_uint64l(0xFFFFFFFFFFFFFFFF)); + ::dux::bytefill(arr,arrsz,dux_uint8l(0x7F)); + dux_ass("",arr[dux_uwrdl(0x1)] >= dux_uint64l(0x7F7F7F7F7F7F7F7F)); + } + { + ::dux::seterr(::dux::errcd::test); + dux_ass("Testing if the test error was set",::dux::geterr() >::dux::errcd::noerr); + ::dux::reseterr(); + } + { + auto x = 0x45; + auto y = 0x1A4; + ::dux::swp(x,y); + dux_ass("",x == 0x1A4); + dux_ass("",y == 0x45); + } + { + ::dux::setsighandl(::dux::sig::intr,[](::dux::sig const _sig) { + dux_ass("",_sig == ::dux::sig::intr); + ::dux::local::gotsig = true; + return false; + }); + ::dux::raise(::dux::sig::intr); + dux_ass("",::dux::local::gotsig); + ::dux::setsighandl(::dux::sig::intr,::dux::sighandl::dfl()); + } + { + auto handl {[](::dux::sig) {return false;}}; + ::dux::setsighandl(::dux::sig::abrt,handl); + auto goterr {false}; + if (::dux::haserr()) [[unlikely]] { + goterr = true; + ::dux::reseterr(); } - skipevt:; + dux_ass("",goterr); + } + { + auto const start {::dux::gettime()}; + ::dux::sleep(dux_uint64l(0x2)); + auto const stop {::dux::gettime()}; + dux_ass("",stop - start == dux_uint64l(0x2)); + } + { + constexpr auto arrsz {dux_uwrdl(0x8)}; + bool arr[arrsz]; + ::dux::plot(arr,arrsz,[](::dux::uwrd const _n) {return ::dux::isprime(_n);}); + dux_ass("",arr[dux_uwrdl(0x0)] == false); + dux_ass("",arr[dux_uwrdl(0x1)] == false); + dux_ass("",arr[dux_uwrdl(0x2)] == true); + dux_ass("",arr[dux_uwrdl(0x3)] == true); + dux_ass("",arr[dux_uwrdl(0x4)] == false); + dux_ass("",arr[dux_uwrdl(0x5)] == true); + dux_ass("",arr[dux_uwrdl(0x6)] == false); + dux_ass("",arr[dux_uwrdl(0x7)] == true); + } + if (::dux::haserr()) [[unlikely]] { + ::dux::dbglog("dux :: \x1B[91mdemo\x1B[0m :: Got error %S!\n",::dux::errcdnm(::dux::geterr())); + ::dux::exit(::dux::stat::err); } - win.destr(); - ::dux::endgfx(); + ::ctest(_params); + ::dux::onexit([](::dux::stat) { + ::dux::onexit([](::dux::stat) {}); + dux_ass("",::dux::haserr()); + ::dux::dbglog("dux :: demo :: All tests passed!\n"); + }); + ::dux::exit(::dux::stat::ok); } |