@@ -37,6 +37,7 @@ add_executable(test_libswan | |||
test/Tile.t.cc | |||
test/Timer.t.cc | |||
test/util.t.cc | |||
test/log.t.cc | |||
test/Vector2.t.cc | |||
test/Win.t.cc | |||
test/World.t.cc |
@@ -0,0 +1,44 @@ | |||
#pragma once | |||
#include <iostream> | |||
namespace Swan { | |||
class Logger { | |||
public: | |||
class NewlineStream { | |||
public: | |||
NewlineStream(std::ostream &os): os_(os) {} | |||
~NewlineStream() { | |||
os_ << '\n' << std::flush; | |||
} | |||
template<typename T> | |||
NewlineStream &operator<<(const T &val) { | |||
os_ << val; | |||
return *this; | |||
} | |||
private: | |||
std::ostream &os_; | |||
}; | |||
Logger(std::ostream &os, std::string name): os_(os), name_(std::move(name)) {} | |||
template<typename T> | |||
NewlineStream operator<<(const T &val) { | |||
os_ << name_ << ": " << val; | |||
return NewlineStream(os_); | |||
} | |||
private: | |||
std::ostream &os_; | |||
std::string name_; | |||
}; | |||
static Logger log(std::clog, "log"); | |||
static Logger info(std::clog, "info"); | |||
static Logger warning(std::clog, "warning"); | |||
static Logger panic(std::clog, "panic"); | |||
} |
@@ -11,7 +11,7 @@ class Deferred { | |||
public: | |||
Deferred(Func func): func_(func) {} | |||
Deferred(const Deferred &def) = delete; | |||
Deferred(const Deferred &&def) noexcept: func_(def.func_) { def.active = false; } | |||
Deferred(Deferred &&def) noexcept: func_(def.func_) { def.active_ = false; } | |||
~Deferred() { if (active_) func_(); } | |||
private: |
@@ -0,0 +1,12 @@ | |||
#include "log.h" | |||
#include <sstream> | |||
#include "lib/test.h" | |||
test("basic logging") { | |||
std::stringstream ostream; | |||
Swan::Logger log(ostream, "test"); | |||
log << "Hello World, a number: " << 100 << ", and a string"; | |||
expecteq(ostream.str(), "test: Hello World, a number: 100, and a string\n"); | |||
} |
@@ -1,3 +1,37 @@ | |||
#include "util.h" | |||
#include "lib/test.h" | |||
test("Deferred functions run") { | |||
int runs = 0; | |||
{ | |||
auto def = Swan::makeDeferred([&] { runs += 1; }); | |||
expecteq(runs, 0); | |||
} | |||
expecteq(runs, 1); | |||
} | |||
test("Moved deferreds don't run") { | |||
int runs = 0; | |||
{ | |||
auto def = Swan::makeDeferred([&] { runs += 1; }); | |||
auto def2 = std::move(def); | |||
expecteq(runs, 0); | |||
} | |||
expecteq(runs, 1); | |||
} | |||
test("Raii pointers") { | |||
int runs = 0; | |||
{ | |||
auto ptr = Swan::makeRaiiPtr(new int(10), [&](int *p) { | |||
runs += 1; | |||
delete p; | |||
}); | |||
expecteq(runs, 0); | |||
expectneq(ptr, nullptr); | |||
expecteq(*ptr, 10); | |||
} | |||
expecteq(runs, 1); | |||
} |