add tests for castSafely

This commit is contained in:
falsycat 2023-09-11 06:36:33 +09:00
parent 9210dec86d
commit 0123434f4b
2 changed files with 25 additions and 0 deletions

View File

@ -15,6 +15,7 @@ target_sources(nf7_iface
common/future.hh common/future.hh
common/leak_detector.hh common/leak_detector.hh
common/mutex.hh common/mutex.hh
common/numeric.hh
common/observer.hh common/observer.hh
common/observer.hh common/observer.hh
common/sql.hh common/sql.hh
@ -42,6 +43,7 @@ target_sources(nf7_iface_test
common/future_test.cc common/future_test.cc
common/leak_detector_test.cc common/leak_detector_test.cc
common/mutex_test.cc common/mutex_test.cc
common/numeric_test.cc
common/observer_test.cc common/observer_test.cc
common/observer_test.hh common/observer_test.hh
common/task_test.cc common/task_test.cc

View File

@ -0,0 +1,23 @@
// No copyright
#include "iface/common/numeric.hh"
#include <gtest/gtest.h>
#include "iface/common/exception.hh"
TEST(Numeric, CastSafelyShrink) {
EXPECT_EQ(nf7::castSafely<uint8_t>(uint32_t {0}), 0);
}
TEST(Numeric, CastSafelyShrinkWithOverflow) {
EXPECT_THROW(nf7::castSafely<uint8_t>(uint32_t {1000}), nf7::Exception);
}
TEST(Numeric, CastSafelyShrinkWithUnderflow) {
EXPECT_THROW(nf7::castSafely<int8_t>(int32_t {-1000}), nf7::Exception);
}
TEST(Numeric, CastSafelyShrinkWithSignDrop) {
EXPECT_THROW(nf7::castSafely<uint8_t>(int32_t {-1}), nf7::Exception);
}
TEST(Numeric, CastSafelyExpand) {
EXPECT_EQ(nf7::castSafely<uint32_t>(uint8_t {255}), 255);
}