tests for utility.h

master
Daniel Kolesa 2015-06-06 18:22:10 +01:00
parent 3f56700e19
commit d6d0ff3e8d
2 changed files with 93 additions and 2 deletions

View File

@ -11,7 +11,7 @@
#include "octa/types.h"
namespace octa {
/* forward declarations */
/* forward declarations */
namespace detail {
template<typename> struct RemoveCvBase;
@ -1172,6 +1172,6 @@ namespace detail {
template<typename T>
using UnderlyingType = typename octa::detail::UnderlyingTypeBase<T>::Type;
}
} /* namespace octa */
#endif

91
tests/utility.cpp 100644
View File

@ -0,0 +1,91 @@
#include <assert.h>
#include <string.h>
#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<decltype(x.first), int>::value));
assert((IsSame<decltype(x.second), float>::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;
}