forked from microsoft/GSL
Move from unittest-cpp to catch for unit testing. (#533)
Many thanks to @rianquinn. This should fix #495, #494 and #529.
This commit is contained in:
+82
-87
@@ -14,7 +14,7 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
#include <catch/catch.hpp>
|
||||
|
||||
#include <gsl/gsl>
|
||||
|
||||
@@ -22,94 +22,89 @@
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
SUITE(utils_tests)
|
||||
void f(int& i) { i += 1; }
|
||||
|
||||
TEST_CASE("finally_lambda")
|
||||
{
|
||||
void f(int& i) { i += 1; }
|
||||
|
||||
TEST(finally_lambda)
|
||||
int i = 0;
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally([&]() { f(i); });
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
|
||||
TEST(finally_lambda_move)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _1 = finally([&]() { f(i); });
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 1);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
|
||||
TEST(finally_function_with_bind)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally(std::bind(&f, std::ref(i)));
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
void g() { j += 1; }
|
||||
TEST(finally_function_ptr)
|
||||
{
|
||||
j = 0;
|
||||
{
|
||||
auto _ = finally(&g);
|
||||
CHECK(j == 0);
|
||||
}
|
||||
CHECK(j == 1);
|
||||
}
|
||||
|
||||
TEST(narrow_cast)
|
||||
{
|
||||
int n = 120;
|
||||
char c = narrow_cast<char>(n);
|
||||
CHECK(c == 120);
|
||||
|
||||
n = 300;
|
||||
unsigned char uc = narrow_cast<unsigned char>(n);
|
||||
CHECK(uc == 44);
|
||||
}
|
||||
|
||||
TEST(narrow)
|
||||
{
|
||||
int n = 120;
|
||||
const char c = narrow<char>(n);
|
||||
CHECK(c == 120);
|
||||
|
||||
n = 300;
|
||||
CHECK_THROW(narrow<char>(n), narrowing_error);
|
||||
|
||||
const auto int32_max = std::numeric_limits<int32_t>::max();
|
||||
const auto int32_min = std::numeric_limits<int32_t>::min();
|
||||
|
||||
CHECK(narrow<uint32_t>(int32_t(0)) == 0);
|
||||
CHECK(narrow<uint32_t>(int32_t(1)) == 1);
|
||||
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
|
||||
|
||||
CHECK_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
|
||||
CHECK_THROW(narrow<uint32_t>(int32_min), narrowing_error);
|
||||
|
||||
n = -42;
|
||||
CHECK_THROW(narrow<unsigned>(n), narrowing_error);
|
||||
auto _ = finally([&]() { f(i); });
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
TEST_CASE("finally_lambda_move")
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _1 = finally([&]() { f(i); });
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 1);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("finally_function_with_bind")
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally(std::bind(&f, std::ref(i)));
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
void g() { j += 1; }
|
||||
TEST_CASE("finally_function_ptr")
|
||||
{
|
||||
j = 0;
|
||||
{
|
||||
auto _ = finally(&g);
|
||||
CHECK(j == 0);
|
||||
}
|
||||
CHECK(j == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("narrow_cast")
|
||||
{
|
||||
int n = 120;
|
||||
char c = narrow_cast<char>(n);
|
||||
CHECK(c == 120);
|
||||
|
||||
n = 300;
|
||||
unsigned char uc = narrow_cast<unsigned char>(n);
|
||||
CHECK(uc == 44);
|
||||
}
|
||||
|
||||
TEST_CASE("narrow")
|
||||
{
|
||||
int n = 120;
|
||||
const char c = narrow<char>(n);
|
||||
CHECK(c == 120);
|
||||
|
||||
n = 300;
|
||||
CHECK_THROWS_AS(narrow<char>(n), narrowing_error);
|
||||
|
||||
const auto int32_max = std::numeric_limits<int32_t>::max();
|
||||
const auto int32_min = std::numeric_limits<int32_t>::min();
|
||||
|
||||
CHECK(narrow<uint32_t>(int32_t(0)) == 0);
|
||||
CHECK(narrow<uint32_t>(int32_t(1)) == 1);
|
||||
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
|
||||
|
||||
CHECK_THROWS_AS(narrow<uint32_t>(int32_t(-1)), narrowing_error);
|
||||
CHECK_THROWS_AS(narrow<uint32_t>(int32_min), narrowing_error);
|
||||
|
||||
n = -42;
|
||||
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user