From b2ce86ef8aef9a9caa8bcc99e54591b92d7b2426 Mon Sep 17 00:00:00 2001 From: Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> Date: Tue, 2 Apr 2019 02:03:51 +0700 Subject: [PATCH] Add compile-time checking of numerical limits in C++ code. (#5270) * Add checking of numerical_limits in C++ code. * Add integer suffixes (LL/ULL) to int64 values in the IntegerBoundaryTest --- tests/test.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/tests/test.cpp b/tests/test.cpp index 2f60ca811..6da1dff9a 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1508,6 +1508,27 @@ void IntegerOutOfRangeTest() { } void IntegerBoundaryTest() { + // Check numerical compatibility with non-C++ languages. + // By the C++ standard, std::numerical_limits::min() == -9223372036854775807 (-2^63+1) or less* + // The Flatbuffers grammar and most of the languages (C#, Java, Rust) expect + // that minimum values are: -128, -32768,.., -9223372036854775808. + // Since C++20, static_cast(0x8000000000000000ULL) is well-defined two's complement cast. + // Therefore -9223372036854775808 should be valid negative value. + TEST_EQ(flatbuffers::numeric_limits::min(), -128); + TEST_EQ(flatbuffers::numeric_limits::max(), 127); + TEST_EQ(flatbuffers::numeric_limits::min(), -32768); + TEST_EQ(flatbuffers::numeric_limits::max(), 32767); + TEST_EQ(flatbuffers::numeric_limits::min() + 1, -2147483647); + TEST_EQ(flatbuffers::numeric_limits::max(), 2147483647ULL); + TEST_EQ(flatbuffers::numeric_limits::min() + 1LL, + -9223372036854775807LL); + TEST_EQ(flatbuffers::numeric_limits::max(), 9223372036854775807ULL); + TEST_EQ(flatbuffers::numeric_limits::max(), 255); + TEST_EQ(flatbuffers::numeric_limits::max(), 65535); + TEST_EQ(flatbuffers::numeric_limits::max(), 4294967295ULL); + TEST_EQ(flatbuffers::numeric_limits::max(), + 18446744073709551615ULL); + TEST_EQ(TestValue("{ Y:127 }", "byte"), 127); TEST_EQ(TestValue("{ Y:-128 }", "byte"), -128); TEST_EQ(TestValue("{ Y:255 }", "ubyte"), 255); @@ -1517,15 +1538,15 @@ void IntegerBoundaryTest() { TEST_EQ(TestValue("{ Y:65535 }", "ushort"), 65535); TEST_EQ(TestValue("{ Y:0 }", "ushort"), 0); TEST_EQ(TestValue("{ Y:2147483647 }", "int"), 2147483647); - TEST_EQ(TestValue("{ Y:-2147483648 }", "int"), (-2147483647 - 1)); + TEST_EQ(TestValue("{ Y:-2147483648 }", "int") + 1, -2147483647); TEST_EQ(TestValue("{ Y:4294967295 }", "uint"), 4294967295); TEST_EQ(TestValue("{ Y:0 }", "uint"), 0); TEST_EQ(TestValue("{ Y:9223372036854775807 }", "long"), - 9223372036854775807); - TEST_EQ(TestValue("{ Y:-9223372036854775808 }", "long"), - (-9223372036854775807 - 1)); + 9223372036854775807LL); + TEST_EQ(TestValue("{ Y:-9223372036854775808 }", "long") + 1LL, + -9223372036854775807LL); TEST_EQ(TestValue("{ Y:18446744073709551615 }", "ulong"), - 18446744073709551615U); + 18446744073709551615ULL); TEST_EQ(TestValue("{ Y:0 }", "ulong"), 0); TEST_EQ(TestValue("{ Y: 18446744073709551615 }", "uint64"), 18446744073709551615ULL);