From 06c1dc3a2956e84e06dc3f04c88e66a38af36507 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 13 Mar 2020 14:37:02 -0400 Subject: [PATCH] Adding NDEBUG to release (#557) * Adding NDEBUG to release * Asserts are deleted with NDEBUG. We want hard asserts. --- tests/basictests.cpp | 1 - tests/errortests.cpp | 1 - tests/integer_tests.cpp | 31 ++++++++++++++++++++---------- tests/jsoncheck.cpp | 1 - tests/numberparsingcheck.cpp | 1 - tests/parse_many_test.cpp | 1 - tests/pointercheck.cpp | 36 +++++++++++++++++++++++------------ tests/stringparsingcheck.cpp | 2 +- tools/cmake/FindOptions.cmake | 4 ++-- 9 files changed, 48 insertions(+), 30 deletions(-) diff --git a/tests/basictests.cpp b/tests/basictests.cpp index 54a898cb..188cd70b 100644 --- a/tests/basictests.cpp +++ b/tests/basictests.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/tests/errortests.cpp b/tests/errortests.cpp index ef951694..88eb49dd 100644 --- a/tests/errortests.cpp +++ b/tests/errortests.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/tests/integer_tests.cpp b/tests/integer_tests.cpp index ca329fa4..de0542f6 100644 --- a/tests/integer_tests.cpp +++ b/tests/integer_tests.cpp @@ -1,10 +1,21 @@ -#include #include #include #include "simdjson.h" +// we define our own asserts to get around NDEBUG +#ifndef ASSERT +#define ASSERT(x) \ +{ if (!(x)) { \ + char buf[4096]; \ + snprintf (buf, 4096, "Failure in \"%s\", line %d\n", \ + __FILE__, __LINE__); \ + abort (); \ + } \ +} +#endif + using namespace simdjson; static const std::string make_json(const std::string value) { @@ -23,10 +34,10 @@ static void parse_and_validate(const std::string src, T expected) { const padded_string pstr{src}; auto json = build_parsed_json(pstr); - assert(json.is_valid()); + ASSERT(json.is_valid()); ParsedJson::Iterator it{json}; - assert(it.down()); - assert(it.next()); + ASSERT(it.down()); + ASSERT(it.next()); bool result; if constexpr (std::is_same::value) { const auto actual = it.get_integer(); @@ -47,10 +58,10 @@ static bool parse_and_check_signed(const std::string src) { const padded_string pstr{src}; auto json = build_parsed_json(pstr); - assert(json.is_valid()); + ASSERT(json.is_valid()); document::iterator it{json}; - assert(it.down()); - assert(it.next()); + ASSERT(it.down()); + ASSERT(it.next()); return it.is_integer() && it.is_number(); } @@ -59,10 +70,10 @@ static bool parse_and_check_unsigned(const std::string src) { const padded_string pstr{src}; auto json = build_parsed_json(pstr); - assert(json.is_valid()); + ASSERT(json.is_valid()); document::iterator it{json}; - assert(it.down()); - assert(it.next()); + ASSERT(it.down()); + ASSERT(it.next()); return it.is_unsigned_integer() && it.is_number(); } diff --git a/tests/jsoncheck.cpp b/tests/jsoncheck.cpp index d96e97a6..b8a80b28 100644 --- a/tests/jsoncheck.cpp +++ b/tests/jsoncheck.cpp @@ -1,4 +1,3 @@ -#include #include #ifndef _MSC_VER #include diff --git a/tests/numberparsingcheck.cpp b/tests/numberparsingcheck.cpp index ee2c389e..8270ae0a 100644 --- a/tests/numberparsingcheck.cpp +++ b/tests/numberparsingcheck.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/tests/parse_many_test.cpp b/tests/parse_many_test.cpp index 0b70f85e..c7834cf8 100644 --- a/tests/parse_many_test.cpp +++ b/tests/parse_many_test.cpp @@ -1,4 +1,3 @@ -#include #include #ifndef _MSC_VER #include diff --git a/tests/pointercheck.cpp b/tests/pointercheck.cpp index 4d536594..b00e4c3f 100644 --- a/tests/pointercheck.cpp +++ b/tests/pointercheck.cpp @@ -2,35 +2,47 @@ #include "simdjson.h" +// we define our own asserts to get around NDEBUG +#ifndef ASSERT +#define ASSERT(x) \ +{ if (!(x)) { \ + char buf[4096]; \ + snprintf (buf, 4096, "Failure in \"%s\", line %d\n", \ + __FILE__, __LINE__); \ + abort (); \ + } \ +} +#endif + int main() { // {"/~01abc": [0, {"\\\" 0": ["value0", "value1"]}]}" std::string json = "{\"/~01abc\": [0, {\"\\\\\\\" 0\": [\"value0\", \"value1\"]}]}"; simdjson::ParsedJson pj; simdjson::json_parse(json.c_str(), json.length(), pj); - assert(pj.is_valid()); + ASSERT(pj.is_valid()); simdjson::ParsedJson::Iterator it(pj); // valid JSON String Representation pointer std::string pointer1("/~1~001abc/1/\\\\\\\" 0/0"); - assert(it.move_to(pointer1.c_str(), pointer1.length())); - assert(it.is_string()); - assert(it.get_string() == std::string("value0")); + ASSERT(it.move_to(pointer1.c_str(), pointer1.length())); + ASSERT(it.is_string()); + ASSERT(it.get_string() == std::string("value0")); // valid URI Fragment Identifier Representation pointer std::string pointer2("#/~1~001abc/1/%x5C%x22%x200/1"); - assert(it.move_to(pointer2.c_str(), pointer2.length())); - assert(it.is_string()); - assert(it.get_string() == std::string("value1")); + ASSERT(it.move_to(pointer2.c_str(), pointer2.length())); + ASSERT(it.is_string()); + ASSERT(it.get_string() == std::string("value1")); // invalid pointer with leading 0 in index std::string pointer3("#/~1~001abc/01"); - assert(!it.move_to(pointer3.c_str(), pointer3.length())); // failed - assert(it.is_string()); // has probably not moved - assert(it.get_string() == std::string("value1")); // has not move + ASSERT(!it.move_to(pointer3.c_str(), pointer3.length())); // failed + ASSERT(it.is_string()); // has probably not moved + ASSERT(it.get_string() == std::string("value1")); // has not move // "the (nonexistent) member after the last array element" std::string pointer4("/~1~001abc/-"); - assert(it.move_to(pointer4.c_str(), pointer4.length())); - assert(it.get_type() == ']'); + ASSERT(it.move_to(pointer4.c_str(), pointer4.length())); + ASSERT(it.get_type() == ']'); } diff --git a/tests/stringparsingcheck.cpp b/tests/stringparsingcheck.cpp index 90a6f6d5..648a4b36 100644 --- a/tests/stringparsingcheck.cpp +++ b/tests/stringparsingcheck.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/tools/cmake/FindOptions.cmake b/tools/cmake/FindOptions.cmake index f9854c1c..7a8edd00 100644 --- a/tools/cmake/FindOptions.cmake +++ b/tools/cmake/FindOptions.cmake @@ -43,14 +43,14 @@ set(WARNING_FLAGS "-Wall") if(NOT MSVC) set(WARNING_FLAGS "${WARNING_FLAGS} -Wextra -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self") set(CMAKE_C_FLAGS_DEBUG "-ggdb") -set(CMAKE_C_FLAGS_RELEASE "-O3") +set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${STD_FLAGS} ${OPT_FLAGS} ${INCLUDE_FLAGS} ${WARNING_FLAGS} ${SIMDJSON_SANITIZE_FLAGS} ") if(NOT MSVC) set(CMAKE_CXX_FLAGS_DEBUG "-ggdb") -set(CMAKE_CXX_FLAGS_RELEASE "-O3") +set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXXSTD_FLAGS} ${OPT_FLAGS} ${INCLUDE_FLAGS} ${WARNING_FLAGS} ${SIMDJSON_SANITIZE_FLAGS} ")