diff --git a/octa/type_traits.h b/octa/type_traits.h index 869bc4f..f545748 100644 --- a/octa/type_traits.h +++ b/octa/type_traits.h @@ -11,7 +11,7 @@ #include "octa/types.h" namespace octa { - /* forward declarations */ +/* forward declarations */ namespace detail { template struct RemoveCvBase; @@ -1172,6 +1172,6 @@ namespace detail { template using UnderlyingType = typename octa::detail::UnderlyingTypeBase::Type; -} +} /* namespace octa */ #endif \ No newline at end of file diff --git a/tests/utility.cpp b/tests/utility.cpp new file mode 100644 index 0000000..a293914 --- /dev/null +++ b/tests/utility.cpp @@ -0,0 +1,91 @@ +#include +#include +#include "octa/utility.h" +#include "octa/string.h" + +using namespace octa; + +struct Foo { + int x; + Foo(): x(5) {} + Foo(int x): x(x) {} + Foo(const Foo &x): x(x.x) {} + Foo(Foo &&x): x(x.x) { x.x = 0; } + Foo &operator=(int _x) { + x = _x; + return *this; + } +}; + +struct NotSwappable { + int i; + NotSwappable(int i): i(i) {} +}; + +struct Swappable { + int i; + bool swapped; + Swappable(int i): i(i), swapped(false) {} + void swap(Swappable &v) { + auto j = i; + i = v.i; + v.i = j; + swapped = v.swapped = true; + } +}; + +int main() { + Foo bar(150); + Foo baz(move(bar)); + + assert(bar.x == 0); + assert(baz.x == 150); + + Foo cp(baz); + + assert(baz.x == 150); + assert(cp.x == 150); + + auto i = exchange(baz, 20); + assert(baz.x == 20); + assert(i.x == 150); + + NotSwappable nsx(10); + NotSwappable nsy(20); + + swap(nsx, nsy); + assert(nsx.i == 20); + assert(nsy.i == 10); + + Swappable sx(10); + Swappable sy(20); + + assert(!sx.swapped); + assert(!sy.swapped); + + swap(sx, sy); + assert(sx.swapped); + assert(sy.swapped); + assert(sx.i == 20); + assert(sy.i == 10); + + int ai[3] = { 5, 10, 15 }; + int bi[3] = { 6, 11, 16 }; + swap(ai, bi); + + assert(ai[0] == 6); + assert(bi[2] == 15); + + auto x = make_pair(5, 3.14f); + + assert((IsSame::value)); + assert((IsSame::value)); + + assert(x.first == 5); + assert(x.second == 3.14f); + + auto st = make_pair(5, 10); + assert(!strcmp(to_string(st).data(), "{5, 10}")); + + return 0; +} \ No newline at end of file