aboutsummaryrefslogtreecommitdiff
path: root/tests/utils_tests.cpp
diff options
context:
space:
mode:
authorPaweł Bylica <chfast@gmail.com>2016-02-08 12:34:21 +0100
committerPaweł Bylica <chfast@gmail.com>2016-02-08 12:38:06 +0100
commit6a4f2512b78b4de4003b38f3e67b66c7161ffcee (patch)
tree4460eae4aba3cc4b8b31c0c17aeb833a7fc0a86d /tests/utils_tests.cpp
parent0be53d99ef9b6ac0c7d6d995cb94c0dce14fe1f8 (diff)
downloadMicrosoft-GSL-6a4f2512b78b4de4003b38f3e67b66c7161ffcee.tar.gz
narrow: Also check if a value has changed sign after cast.
Fixes https://github.com/Microsoft/GSL/issues/222.
Diffstat (limited to 'tests/utils_tests.cpp')
-rw-r--r--tests/utils_tests.cpp45
1 files changed, 29 insertions, 16 deletions
diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp
index 9406e6b..a46d6e4 100644
--- a/tests/utils_tests.cpp
+++ b/tests/utils_tests.cpp
@@ -1,20 +1,20 @@
-///////////////////////////////////////////////////////////////////////////////
-//
-// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
-//
-// This code is licensed under the MIT License (MIT).
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-//
+///////////////////////////////////////////////////////////////////////////////
+//
+// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
+//
+// This code is licensed under the MIT License (MIT).
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
///////////////////////////////////////////////////////////////////////////////
-#include <UnitTest++/UnitTest++.h>
+#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
#include <functional>
@@ -95,8 +95,21 @@ SUITE(utils_tests)
char c = narrow<char>(n);
CHECK(c == 120);
- n = 300;
+ 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) == 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);
}
}