Implementation, tests and first use of rangechecked_cast

This commit is contained in:
eelke 2019-11-04 18:02:48 +01:00
parent c5f6da48ce
commit f875f0f012
8 changed files with 61 additions and 5 deletions

View file

@ -20,6 +20,7 @@ SOURCES += main.cpp \
tst_escapeConnectionStringValue.cpp \
tst_expected.cpp \
tst_SqlLexer.cpp \
tst_rangechecked_cast.cpp \
tst_scopeguard.cpp \
tst_CsvWriter.cpp \
tst_PasswordManager.cpp \

View file

@ -0,0 +1,35 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "rangechecked_cast.h"
#include "PrintTo_Qt.h"
using namespace testing;
TEST(rangechecked_cast, in_range)
{
ASSERT_NO_THROW({
int8_t in = 42;
int expected = 42;
auto output = rangechecked_cast<int>(in);
ASSERT_EQ(output, expected);
});
}
TEST(rangechecked_cast, above_max)
{
int64_t in = std::numeric_limits<int>::max() + static_cast<int64_t>(1);
ASSERT_THROW({
rangechecked_cast<int>(in);
},
std::runtime_error);
}
TEST(rangechecked_cast, below_lowest)
{
int64_t in = std::numeric_limits<int>::lowest() - static_cast<int64_t>(1);
ASSERT_THROW({
rangechecked_cast<int>(in);
},
std::runtime_error);
}