aboutsummaryrefslogtreecommitdiffstats
path: root/host/tests
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2018-01-10 14:45:30 -0800
committerMartin Braun <martin.braun@ettus.com>2018-01-12 13:18:03 -0800
commitc48c57073d618df5a023b5daec372aec5770b471 (patch)
treec736e31a4ededb430141703055f1bfa53f21b65f /host/tests
parentebfd551c355cd28f720b6b77a9d0470353b2e21c (diff)
downloaduhd-c48c57073d618df5a023b5daec372aec5770b471.tar.gz
uhd-c48c57073d618df5a023b5daec372aec5770b471.tar.bz2
uhd-c48c57073d618df5a023b5daec372aec5770b471.zip
uhd: Add narrow_cast and narrow
Reviewed-by: Ashish Chaudhari <ashish.chaudhari@ettus.com>
Diffstat (limited to 'host/tests')
-rw-r--r--host/tests/CMakeLists.txt1
-rw-r--r--host/tests/narrow_cast_test.cpp21
2 files changed, 22 insertions, 0 deletions
diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt
index 7fcf1652c..6f78f8d3e 100644
--- a/host/tests/CMakeLists.txt
+++ b/host/tests/CMakeLists.txt
@@ -27,6 +27,7 @@ SET(test_sources
gain_group_test.cpp
log_test.cpp
math_test.cpp
+ narrow_cast_test.cpp
property_test.cpp
ranges_test.cpp
sid_t_test.cpp
diff --git a/host/tests/narrow_cast_test.cpp b/host/tests/narrow_cast_test.cpp
new file mode 100644
index 000000000..2f784bfe3
--- /dev/null
+++ b/host/tests/narrow_cast_test.cpp
@@ -0,0 +1,21 @@
+//
+// Copyright 2018 Ettus Research, a National Instruments Company
+//
+// SPDX-License-Identifier: GPL-3.0
+//
+
+#include <uhd/exception.hpp>
+#include <../lib/include/uhdlib/utils/narrow.hpp>
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+using namespace uhd;
+
+BOOST_AUTO_TEST_CASE(test_narrow){
+ uint16_t x = 5;
+ uint8_t y = narrow_cast<uint8_t>(x);
+ BOOST_CHECK_EQUAL(x, y);
+
+ BOOST_CHECK_THROW(narrow<uint8_t>(uint16_t(1<<10)), narrowing_error);
+ BOOST_CHECK_THROW(narrow<uint8_t>(int8_t(-1)), narrowing_error);
+}