diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index ea3f572d..a99a0942 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -11,7 +11,8 @@ set(PUBLIC_HEADERS include/join/utf16.hpp include/join/probe.hpp include/join/value.hpp - include/join/document.hpp + include/join/value_builder.hpp + include/join/sax_error.hpp include/join/sax.hpp include/join/pack.hpp include/join/json.hpp @@ -20,7 +21,8 @@ set(PUBLIC_HEADERS ) set(SOURCES - src/sax.cpp + src/value.cpp + src/sax_error.cpp src/json.cpp src/zstream.cpp ) diff --git a/data/include/join/json.hpp b/data/include/join/json.hpp index 9d0b5b65..5df2cfa7 100644 --- a/data/include/join/json.hpp +++ b/data/include/join/json.hpp @@ -26,6 +26,7 @@ #define JOIN_DATA_JSON_HPP // libjoin. +#include #include #include #include @@ -385,13 +386,18 @@ namespace join */ int setString (std::string_view value) { - array (); - append ('"'); - if (writeEscaped (value) == -1) + if constexpr (WriteMode == JsonWriteMode::Canonical) { - reset (); - return -1; + if (JOIN_UNLIKELY (utf8::validate (value.data (), value.data () + value.size ()) == -1)) + { + join::lastError = make_error_code (JsonErrc::InvalidEncoding); + reset (); + return -1; + } } + array (); + append ('"'); + writeEscaped (value); append ('"'); endValue (); return 0; @@ -497,11 +503,95 @@ namespace join } /** - * @brief set key. + * @brief set null key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] std::nullptr_t key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set boolean key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] bool key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set integer key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] int32_t key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set unsigned integer key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] uint32_t key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set 64 bits integer key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] int64_t key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set unsigned 64 bits integer key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] uint64_t key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set real key. + * @param key object key. + * @return -1. + */ + int setKey ([[maybe_unused]] double key) + { + join::lastError = make_error_code (SaxErrc::InvalidKey); + reset (); + return -1; + } + + /** + * @brief set string key. * @param key object key. * @return 0 on success, -1 otherwise. */ - int setKey (const Value& key) + int setKey (std::string_view key) { if (JOIN_UNLIKELY (_depth == 0)) { @@ -509,20 +599,19 @@ namespace join reset (); return -1; } - if (JOIN_UNLIKELY (!key.isString ())) + if constexpr (WriteMode == JsonWriteMode::Canonical) { - join::lastError = make_error_code (SaxErrc::InvalidKey); - reset (); - return -1; + if (JOIN_UNLIKELY (utf8::validate (key.data (), key.data () + key.size ()) == -1)) + { + join::lastError = make_error_code (JsonErrc::InvalidEncoding); + reset (); + return -1; + } } comma (); newLine (); append ('"'); - if (writeEscaped (key.getString ()) == -1) - { - reset (); - return -1; - } + writeEscaped (key); append ('"'); append (':'); space (); @@ -530,6 +619,16 @@ namespace join return 0; } + /** + * @brief set string key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (const char* key) + { + return setKey (std::string_view (key)); + } + /** * @brief stop object. * @return 0 on success, -1 otherwise. @@ -686,10 +785,10 @@ namespace join */ void writeDouble (double value) { - char buf[25]; - char* end = join::dtoa (buf, value); if constexpr (WriteMode == JsonWriteMode::Canonical) { + char buf[25]; + char* end = join::dtoa (buf, value); for (char* pos = buf; pos < end; ++pos) { append (*pos); @@ -701,18 +800,19 @@ namespace join } else { - append (buf, end - buf); + if (JOIN_UNLIKELY (sizeof (_buffer) - _size < 25)) + { + flush (); + } + _size = join::dtoa (_buffer + _size, value) - _buffer; } } /** * @brief escape string value. * @param value string value to escape. - * @return 0 on success, -1 otherwise. */ - template - int writeEscaped (std::string_view value) - requires (Mode == JsonWriteMode::None) + void writeEscaped (std::string_view value) { const char* cur = value.data (); const char* end = cur + value.size (); @@ -769,25 +869,6 @@ namespace join ++cur; } - - return 0; - } - - /** - * @brief validate and escape string value. - * @param value string value to escape. - * @return 0 on success, -1 otherwise. - */ - template - int writeEscaped (std::string_view value) - requires (Mode == JsonWriteMode::Canonical) - { - if (JOIN_UNLIKELY (utf8::validate (value.data (), value.data () + value.size ()) == -1)) - { - join::lastError = make_error_code (JsonErrc::InvalidEncoding); - return -1; - } - return writeEscaped (value); } /** @@ -964,7 +1045,7 @@ namespace join /// JSON default writer. using JsonWriter = BasicJsonWriter; - /// JSON canonicalizer, sorting object members when used with Document::serialize. + /// JSON canonicalizer, sorting object members when used with Value::serialize. using JsonCanonicalizer = BasicJsonWriter; /** @@ -1100,9 +1181,9 @@ namespace join * @return 0 on success, -1 otherwise. */ template - int deserialize (const std::string& document, Handler& handler) + int deserialize (std::string_view document, Handler& handler) { - StringProbe in (document.c_str (), document.size ()); + StringProbe in (document.data (), document.size ()); return read (in, handler); } @@ -1185,6 +1266,18 @@ namespace join } protected: + /** + * @brief parse a document into a value. + * @param document document to parse. + * @param value value receiving the document. + * @return 0 on success, -1 otherwise. + */ + template + int read (ProbeType& document, Value& value) + { + return read (document, ValueBuilder (value)); + } + /** * @brief parse a document, resetting the handler on failure. * @param document document to parse. @@ -1192,7 +1285,7 @@ namespace join * @return 0 on success, -1 otherwise. */ template - int read (ProbeType& document, Handler& handler) + int read (ProbeType& document, Handler&& handler) { try { @@ -1705,8 +1798,10 @@ namespace join return handler.setDouble (d); } - join::lastError = make_error_code (SaxErrc::InvalidValue); // LCOV_EXCL_LINE - return -1; // LCOV_EXCL_LINE + // LCOV_EXCL_START + join::lastError = make_error_code (SaxErrc::InvalidValue); + return -1; + // LCOV_EXCL_STOP } /** @@ -1868,7 +1963,7 @@ namespace join if (JOIN_LIKELY (document.peek () == '"')) { document.get (); - return isKey ? handler.setKey (Value (chunk)) : handler.setString (chunk); + return isKey ? handler.setKey (chunk) : handler.setString (chunk); } } @@ -1911,7 +2006,7 @@ namespace join } } - int result = isKey ? handler.setKey (Value (output)) : handler.setString (output); + int result = isKey ? handler.setKey (output) : handler.setString (output); cache = std::move (output); return result; } diff --git a/data/include/join/pack.hpp b/data/include/join/pack.hpp index 37bf518b..a56953ef 100644 --- a/data/include/join/pack.hpp +++ b/data/include/join/pack.hpp @@ -26,6 +26,7 @@ #define JOIN_DATA_PACK_HPP // libjoin. +#include #include #include #include @@ -413,11 +414,11 @@ namespace join } /** - * @brief set key. + * @brief set null key. * @param key object key. * @return 0 on success, -1 otherwise. */ - int setKey (const Value& key) + int setKey ([[maybe_unused]] std::nullptr_t key) { if (JOIN_UNLIKELY (_depth == 0)) { @@ -425,38 +426,129 @@ namespace join reset (); return -1; } - if (JOIN_UNLIKELY (key.isArray () || key.isObject ())) + return setNull (); + } + + /** + * @brief set boolean key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (bool key) + { + if (JOIN_UNLIKELY (_depth == 0)) { - join::lastError = make_error_code (SaxErrc::InvalidKey); + join::lastError = make_error_code (SaxErrc::InvalidParent); reset (); return -1; } - switch (key.index ()) - { - case Value::BooleanValue: - return setBool (key.getBool ()); - - case Value::IntegerValue: - return setInt (key.getInt ()); + return setBool (key); + } - case Value::UnsignedValue: - return setUint (key.getUint ()); + /** + * @brief set integer key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (int32_t key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + reset (); + return -1; + } + return setInt (key); + } - case Value::Integer64Value: - return setInt64 (key.getInt64 ()); + /** + * @brief set unsigned integer key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (uint32_t key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + reset (); + return -1; + } + return setUint (key); + } - case Value::Unsigned64Value: - return setUint64 (key.getUint64 ()); + /** + * @brief set 64 bits integer key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (int64_t key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + reset (); + return -1; + } + return setInt64 (key); + } - case Value::RealValue: - return setDouble (key.getDouble ()); + /** + * @brief set unsigned 64 bits integer key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (uint64_t key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + reset (); + return -1; + } + return setUint64 (key); + } - case Value::StringValue: - return setString (key.getString ()); + /** + * @brief set real key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (double key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + reset (); + return -1; + } + return setDouble (key); + } - default: - return setNull (); + /** + * @brief set string key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (std::string_view key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + reset (); + return -1; } + return setString (key); + } + + /** + * @brief set string key. + * @param key object key. + * @return 0 on success, -1 otherwise. + */ + int setKey (const char* key) + { + return setKey (std::string_view (key)); } /** @@ -642,9 +734,9 @@ namespace join * @return 0 on success, -1 otherwise. */ template - int deserialize (const std::string& document, Handler& handler) + int deserialize (std::string_view document, Handler& handler) { - StringProbe in (document.c_str (), document.size ()); + StringProbe in (document.data (), document.size ()); return read (in, handler); } @@ -727,6 +819,18 @@ namespace join } protected: + /** + * @brief parse a document into a value. + * @param document document to parse. + * @param value value receiving the document. + * @return 0 on success, -1 otherwise. + */ + template + int read (ProbeType& document, Value& value) + { + return read (document, ValueBuilder (value)); + } + /** * @brief parse a document, resetting the handler on failure. * @param document document to parse. @@ -734,7 +838,7 @@ namespace join * @return 0 on success, -1 otherwise. */ template - int read (ProbeType& document, Handler& handler) + int read (ProbeType& document, Handler&& handler) { try { @@ -826,7 +930,7 @@ namespace join int readNull (ProbeType& document, Handler& handler, bool isKey = false) { document.get (); - return (isKey) ? handler.setKey (Value (nullptr)) : handler.setNull (); + return (isKey) ? handler.setKey (nullptr) : handler.setNull (); } /** @@ -840,7 +944,7 @@ namespace join int readFalse (ProbeType& document, Handler& handler, bool isKey = false) { document.get (); - return (isKey) ? handler.setKey (Value (false)) : handler.setBool (false); + return (isKey) ? handler.setKey (false) : handler.setBool (false); } /** @@ -854,7 +958,7 @@ namespace join int readTrue (ProbeType& document, Handler& handler, bool isKey = false) { document.get (); - return (isKey) ? handler.setKey (Value (true)) : handler.setBool (true); + return (isKey) ? handler.setKey (true) : handler.setBool (true); } /** @@ -1063,7 +1167,7 @@ namespace join return -1; } - int result = isKey ? handler.setKey (Value (output)) : handler.setString (output); + int result = isKey ? handler.setKey (output) : handler.setString (output); cache = std::move (output); return result; } @@ -1129,12 +1233,12 @@ namespace join if (head <= 0x7f) { auto number = uint32_t (head); - return isKey ? handler.setKey (Value (number)) : handler.setUint (number); + return isKey ? handler.setKey (number) : handler.setUint (number); } else if (head >= 0xe0) { auto number = int32_t (static_cast (head)); - return isKey ? handler.setKey (Value (number)) : handler.setInt (number); + return isKey ? handler.setKey (number) : handler.setInt (number); } else if (head == 0xd3) { @@ -1143,7 +1247,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setInt64 (number); + return isKey ? handler.setKey (number) : handler.setInt64 (number); } else if (head == 0xcf) { @@ -1152,7 +1256,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setUint64 (number); + return isKey ? handler.setKey (number) : handler.setUint64 (number); } else if (head == 0xcb) { @@ -1161,7 +1265,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setDouble (number); + return isKey ? handler.setKey (number) : handler.setDouble (number); } else if (head == 0xd2) { @@ -1170,7 +1274,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setInt (number); + return isKey ? handler.setKey (number) : handler.setInt (number); } else if (head == 0xce) { @@ -1179,7 +1283,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setUint (number); + return isKey ? handler.setKey (number) : handler.setUint (number); } else if (head == 0xca) { @@ -1188,7 +1292,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setDouble (number); + return isKey ? handler.setKey (number) : handler.setDouble (number); } else if (head == 0xd1) { @@ -1197,7 +1301,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setInt (number); + return isKey ? handler.setKey (number) : handler.setInt (number); } else if (head == 0xcd) { @@ -1206,7 +1310,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setUint (number); + return isKey ? handler.setKey (number) : handler.setUint (number); } else if (head == 0xd0) { @@ -1215,7 +1319,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setInt (number); + return isKey ? handler.setKey (number) : handler.setInt (number); } else if (head == 0xcc) { @@ -1224,12 +1328,14 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (number)) : handler.setUint (number); + return isKey ? handler.setKey (number) : handler.setUint (number); } else { - join::lastError = make_error_code (SaxErrc::InvalidValue); // LCOV_EXCL_LINE - return -1; // LCOV_EXCL_LINE + // LCOV_EXCL_START + join::lastError = make_error_code (SaxErrc::InvalidValue); + return -1; + // LCOV_EXCL_STOP } } diff --git a/data/include/join/sax.hpp b/data/include/join/sax.hpp index 2d698e32..93ab323d 100644 --- a/data/include/join/sax.hpp +++ b/data/include/join/sax.hpp @@ -26,13 +26,11 @@ #define JOIN_DATA_SAX_HPP // libjoin. +#include #include -#include // C++. -#include #include -#include // C. #include @@ -43,59 +41,6 @@ namespace join /// max number of nested containers. constexpr size_t saxMaxDepth = 256; - /** - * @brief SAX API generic error codes. - */ - enum class SaxErrc - { - StackOverflow = 1, /**< Stack overflow. */ - InvalidParent, /**< Parent not an array nor an object. */ - InvalidKey, /**< Key is invalid. */ - InvalidValue, /**< Value is invalid. */ - ExtraData, /**< Extra data detected. */ - Truncated /**< Data is truncated. */ - }; - - /** - * @brief SAX API generic error category. - */ - class SaxCategory : public std::error_category - { - public: - /** - * @brief get SAX API generic error category name. - * @return deserializer generic error category name. - */ - virtual const char* name () const noexcept; - - /** - * @brief translate SAX API generic error code to human readable error string. - * @param code error code. - * @return human readable error string. - */ - virtual std::string message (int code) const; - }; - - /** - * @brief get error category. - * @return the created std::error_category object. - */ - const std::error_category& saxCategory () noexcept; - - /** - * @brief create an std::error_code object. - * @param code error code number. - * @return the created std::error_code object. - */ - std::error_code make_error_code (SaxErrc code) noexcept; - - /** - * @brief create an std::error_condition object. - * @param code error code number. - * @return the created std::error_condition object. - */ - std::error_condition make_error_condition (SaxErrc code) noexcept; - /** * @brief SAX handler ignoring every event, to derive from and use through the derived type. */ @@ -220,11 +165,91 @@ namespace join } /** - * @brief set key. + * @brief set null key. * @param key object key. * @return 0. */ - int setKey ([[maybe_unused]] const Value& key) noexcept + int setKey ([[maybe_unused]] std::nullptr_t key) noexcept + { + return 0; + } + + /** + * @brief set boolean key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] bool key) noexcept + { + return 0; + } + + /** + * @brief set integer key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] int32_t key) noexcept + { + return 0; + } + + /** + * @brief set unsigned integer key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] uint32_t key) noexcept + { + return 0; + } + + /** + * @brief set 64 bits integer key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] int64_t key) noexcept + { + return 0; + } + + /** + * @brief set unsigned 64 bits integer key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] uint64_t key) noexcept + { + return 0; + } + + /** + * @brief set real key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] double key) noexcept + { + return 0; + } + + /** + * @brief set string key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] std::string_view key) noexcept + { + return 0; + } + + /** + * @brief set string key. + * @param key object key. + * @return 0. + */ + int setKey ([[maybe_unused]] const char* key) noexcept { return 0; } @@ -248,13 +273,4 @@ namespace join }; } -namespace std -{ - /// SAX API generic error code specialization. - template <> - struct is_error_condition_enum : public true_type - { - }; -} - #endif diff --git a/data/include/join/sax_error.hpp b/data/include/join/sax_error.hpp new file mode 100644 index 00000000..304acab5 --- /dev/null +++ b/data/include/join/sax_error.hpp @@ -0,0 +1,100 @@ +/** + * MIT License + * + * Copyright (c) 2021 Mathieu Rabine + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * 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. + */ + +#ifndef JOIN_DATA_SAX_ERROR_HPP +#define JOIN_DATA_SAX_ERROR_HPP + +// libjoin. +#include + +// C++. +#include +#include + +namespace join +{ + /** + * @brief SAX API generic error codes. + */ + enum class SaxErrc + { + StackOverflow = 1, /**< Stack overflow. */ + InvalidParent, /**< Parent not an array nor an object. */ + InvalidKey, /**< Key is invalid. */ + InvalidValue, /**< Value is invalid. */ + ExtraData, /**< Extra data detected. */ + Truncated /**< Data is truncated. */ + }; + + /** + * @brief SAX API generic error category. + */ + class SaxCategory : public std::error_category + { + public: + /** + * @brief get SAX API generic error category name. + * @return deserializer generic error category name. + */ + virtual const char* name () const noexcept; + + /** + * @brief translate SAX API generic error code to human readable error string. + * @param code error code. + * @return human readable error string. + */ + virtual std::string message (int code) const; + }; + + /** + * @brief get error category. + * @return the created std::error_category object. + */ + const std::error_category& saxCategory () noexcept; + + /** + * @brief create an std::error_code object. + * @param code error code number. + * @return the created std::error_code object. + */ + std::error_code make_error_code (SaxErrc code) noexcept; + + /** + * @brief create an std::error_condition object. + * @param code error code number. + * @return the created std::error_condition object. + */ + std::error_condition make_error_condition (SaxErrc code) noexcept; +} + +namespace std +{ + /// SAX API generic error code specialization. + template <> + struct is_error_condition_enum : public true_type + { + }; +} + +#endif diff --git a/data/include/join/value.hpp b/data/include/join/value.hpp index 3d152225..5d4310de 100644 --- a/data/include/join/value.hpp +++ b/data/include/join/value.hpp @@ -26,12 +26,16 @@ #define JOIN_DATA_VALUE_HPP // libjoin. +#include #include -#include +#include // C++. #include +#include #include +#include +#include #include #include #include @@ -48,8 +52,35 @@ namespace join /// array. using Array = std::vector; + /** + * @brief member. + */ + template + struct BasicMember + { + /** + * @brief compare members. + * @param other member to compare to. + * @return true if equal. + */ + constexpr bool operator== (const BasicMember& other) const = default; + + /** + * @brief three-way compare members. + * @param other member to compare to. + * @return ordering of this member relative to other. + */ + constexpr std::partial_ordering operator<=> (const BasicMember& other) const = default; + + /// member key. + Type first; + + /// member value. + Type second; + }; + /// object member. - using Member = std::pair; + using Member = BasicMember; /// object. using Object = std::vector; @@ -69,16 +100,16 @@ namespace join */ enum Index : size_t { - NullValue = 0, /**< index of the std::nullptr_t alternative. */ + NullValue = 0, /**< index of the null alternative. */ BooleanValue = 1, /**< index of the boolean alternative. */ IntegerValue = 2, /**< index of the 32 bits integer alternative. */ UnsignedValue = 3, /**< index of the 32 bits unsigned integer alternative. */ Integer64Value = 4, /**< index of the 64 bits integer alternative. */ Unsigned64Value = 5, /**< index of the 64 bits unsigned integer alternative. */ RealValue = 6, /**< index of the real alternative. */ - StringValue = 7, /**< index of the std::string alternative. */ - ArrayValue = 8, /**< index of the Array alternative. */ - ObjectValue = 9, /**< index of the Object alternative. */ + StringValue = 7, /**< index of the string alternative. */ + ArrayValue = 8, /**< index of the array alternative. */ + ObjectValue = 9, /**< index of the object alternative. */ }; /** @@ -950,17 +981,7 @@ namespace join * @return variable held by value as a string value. * @throw std::bad_cast. */ - constexpr const std::string& getString () const - { - return _data.get (); - } - - /** - * @brief get variable held by value as a string value. - * @return variable held by value as a string value. - * @throw std::bad_cast. - */ - constexpr std::string& getString () + constexpr std::string_view getString () const { return _data.get (); } @@ -970,9 +991,9 @@ namespace join * @return converted string value. * @throw std::bad_cast. */ - explicit operator const char* () const + explicit operator std::string_view () const { - return getString ().c_str (); + return getString (); } /** @@ -1034,328 +1055,1387 @@ namespace join } /** - * @brief returns a reference to the value at position pos or mapped to key. - * @param key the position or key of the element to find. + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. * @return a reference to the requested element. * @throw std::bad_cast, std::out_of_range. */ - Value& at (const Value& key) + Value& at ([[maybe_unused]] std::nullptr_t key) { - if (_data.index () == ArrayValue) + for (auto& member : _data.get ()) { - return _data.get ().at (key.getUint64 ()); + if (member.first.isNull ()) + { + return member.second; + } } + throwException ("invalid key"); + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. + */ + Value& at (bool key) + { for (auto& member : _data.get ()) { - if (member.first == key) + if (member.first.isBool () && member.first.getBool () == key) { return member.second; } } - throw std::out_of_range ("invalid key"); + throwException ("invalid key"); } /** * @brief returns a reference to the value at position pos or mapped to key. - * @param key the position or key of the element to find. + * @param key position or key of the element to find. * @return a reference to the requested element. * @throw std::bad_cast, std::out_of_range. */ - const Value& at (const Value& key) const + Value& at (int32_t key) { if (_data.index () == ArrayValue) { - return _data.get ().at (key.getUint64 ()); + return _data.get ().at (key); } - for (auto const& member : _data.get ()) + for (auto& member : _data.get ()) { - if (member.first == key) + if (member.first.isInt () && member.first.getInt () == key) { return member.second; } } - throw std::out_of_range ("invalid key"); + throwException ("invalid key"); } /** * @brief returns a reference to the value at position pos or mapped to key. - * @param key the position or key of the element to find. + * @param key position or key of the element to find. * @return a reference to the requested element. - * @throw std::bad_cast. + * @throw std::bad_cast, std::out_of_range. */ - Value& operator[] (const Value& key) + Value& at (uint32_t key) { if (_data.index () == ArrayValue) { - return _data.get ()[key.getUint64 ()]; - } - - if (_data.index () == NullValue) - { - _data.set (); + return _data.get ().at (key); } for (auto& member : _data.get ()) { - if (member.first == key) + if (member.first.isUint () && member.first.getUint () == key) { return member.second; } } - _data.get ().emplace_back (key, nullptr); - - return _data.get ().back ().second; + throwException ("invalid key"); } /** - * @brief check if the nested container is empty. - * @return true if empty, false otherwise. - * @throw std::bad_cast. + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - bool empty () const + Value& at (int64_t key) { - switch (_data.index ()) + if (_data.index () == ArrayValue) { - case StringValue: - return _data.get ().empty (); - - case ArrayValue: - return _data.get ().empty (); - - case ObjectValue: - return _data.get ().empty (); + return _data.get ().at (key); + } - default: - break; + for (auto& member : _data.get ()) + { + if (member.first.isInt64 () && member.first.getInt64 () == key) + { + return member.second; + } } - throwException (); + throwException ("invalid key"); } /** - * @brief returns the number of elements in the nested container. - * @return the number of elements in the nested container. - * @throw std::bad_cast. + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - size_t size () const + Value& at (uint64_t key) { - switch (_data.index ()) + if (_data.index () == ArrayValue) { - case StringValue: - return _data.get ().size (); - - case ArrayValue: - return _data.get ().size (); - - case ObjectValue: - return _data.get ().size (); + return _data.get ().at (key); + } - default: - break; + for (auto& member : _data.get ()) + { + if (member.first.isUint64 () && member.first.getUint64 () == key) + { + return member.second; + } } - throwException (); + throwException ("invalid key"); } /** - * @brief increase the capacity of the nested container. - * @param cap new capacity. - * @throw std::bad_cast. + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - void reserve (size_t cap) + Value& at (double key) { - switch (_data.index ()) + for (auto& member : _data.get ()) { - case StringValue: - _data.get ().reserve (cap); - return; - - case ArrayValue: - _data.get ().reserve (cap); - return; - - case ObjectValue: - _data.get ().reserve (cap); - return; - - default: - break; + if (member.first.isDouble () && member.first.getDouble () == key) + { + return member.second; + } } - throwException (); + throwException ("invalid key"); } /** - * @brief erases all elements in the nested container. - * @throw std::bad_cast. + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - void clear () + Value& at (std::string_view key) { - switch (_data.index ()) + for (auto& member : _data.get ()) { - case StringValue: - _data.get ().clear (); - return; - - case ArrayValue: - _data.get ().clear (); - return; - - case ObjectValue: - _data.get ().clear (); - return; - - default: - break; + if (member.first.isString () && member.first.getString () == key) + { + return member.second; + } } - throwException (); - } - - /** - * @brief insert element in the nested container. - * @param member the element to insert in the nested container. - * @return a reference to the inserted element. - * @throw std::bad_cast. - */ - Value& insert (const Member& member) - { - _data.get ().push_back (member); - return _data.get ().back ().second; + throwException ("invalid key"); } /** - * @brief insert element in the nested container. - * @param member the element to insert in the nested container. - * @return a reference to the inserted element. - * @throw std::bad_cast. + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - Value& insert (Member&& member) + Value& at (const char* key) { - _data.get ().emplace_back (std::move (member)); - return _data.get ().back ().second; + return at (std::string_view (key)); } /** - * @brief removes member with the key equivalent to key. - * @param key key value of the member to remove. - * @return number of elements removed. - * @throw std::bad_cast. + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - size_t erase (const Value& key) + const Value& at ([[maybe_unused]] std::nullptr_t key) const { - auto beg = _data.get ().begin (); - auto end = _data.get ().end (); - - for (auto it = beg; it != end; ++it) + for (auto const& member : _data.get ()) { - if (it->first == key) + if (member.first.isNull ()) { - _data.get ().erase (it); - return 1; + return member.second; } } - return 0; + throwException ("invalid key"); } /** - * @brief constructs an element in place at the end of the nested container. - * @param args arguments forwarded to the element constructor. - * @return a reference to the constructed object. - * @throw std::bad_cast. + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - template - Value& emplaceBack (Args&&... args) + const Value& at (bool key) const { - if (_data.index () == NullValue) + for (auto const& member : _data.get ()) { - _data.set (); + if (member.first.isBool () && member.first.getBool () == key) + { + return member.second; + } } - _data.get ().emplace_back (std::forward (args)...); - return _data.get ().back (); + + throwException ("invalid key"); } /** - * @brief appends element at the end of the nested container. - * @param value the element to append. - * @return a reference to the pushed object. - * @throw std::bad_cast. + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - Value& pushBack (const Value& value) + const Value& at (int32_t key) const { - if (_data.index () == NullValue) + if (_data.index () == ArrayValue) { - _data.set (); + return _data.get ().at (key); } - _data.get ().push_back (value); - return _data.get ().back (); - } - /** - * @brief appends element at the end of the nested container. - * @param value the element to append. - * @return a reference to the pushed object. - * @throw std::bad_cast. - */ - Value& pushBack (Value&& value) - { - if (_data.index () == NullValue) + for (auto const& member : _data.get ()) { - _data.set (); + if (member.first.isInt () && member.first.getInt () == key) + { + return member.second; + } } - _data.get ().emplace_back (std::move (value)); - return _data.get ().back (); - } - /** - * @brief removes the last element of the nested container. - * @throw std::bad_cast. - */ - void popBack () - { - _data.get ().pop_back (); + throwException ("invalid key"); } /** - * @brief checks if the nested container contains an element that is mapped to key. - * @return true if the nested container contains an element mapped to key, false otherwise. - * @throw std::bad_cast. + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - bool contains (const Value& key) const + const Value& at (uint32_t key) const { if (_data.index () == ArrayValue) { - return _data.get ().size () > key.getUint64 (); + return _data.get ().at (key); } for (auto const& member : _data.get ()) { - if (member.first == key) + if (member.first.isUint () && member.first.getUint () == key) { - return true; + return member.second; } } - return false; + throwException ("invalid key"); } /** - * @brief exchanges the contents of the value with those of other. - * @param other value to exchange the contents with. + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. */ - void swap (Value& other) + const Value& at (int64_t key) const { - Value temp (std::move (*this)); - *this = std::move (other); + if (_data.index () == ArrayValue) + { + return _data.get ().at (key); + } + + for (auto const& member : _data.get ()) + { + if (member.first.isInt64 () && member.first.getInt64 () == key) + { + return member.second; + } + } + + throwException ("invalid key"); + } + + /** + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. + */ + const Value& at (uint64_t key) const + { + if (_data.index () == ArrayValue) + { + return _data.get ().at (key); + } + + for (auto const& member : _data.get ()) + { + if (member.first.isUint64 () && member.first.getUint64 () == key) + { + return member.second; + } + } + + throwException ("invalid key"); + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. + */ + const Value& at (double key) const + { + for (auto const& member : _data.get ()) + { + if (member.first.isDouble () && member.first.getDouble () == key) + { + return member.second; + } + } + + throwException ("invalid key"); + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. + */ + const Value& at (std::string_view key) const + { + for (auto const& member : _data.get ()) + { + if (member.first.isString () && member.first.getString () == key) + { + return member.second; + } + } + + throwException ("invalid key"); + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast, std::out_of_range. + */ + const Value& at (const char* key) const + { + return at (std::string_view (key)); + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (std::nullptr_t key) + { + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isNull ()) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (bool key) + { + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isBool () && member.first.getBool () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (int32_t key) + { + if (_data.index () == ArrayValue) + { + return _data.get ()[key]; + } + + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isInt () && member.first.getInt () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (uint32_t key) + { + if (_data.index () == ArrayValue) + { + return _data.get ()[key]; + } + + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isUint () && member.first.getUint () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (int64_t key) + { + if (_data.index () == ArrayValue) + { + return _data.get ()[key]; + } + + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isInt64 () && member.first.getInt64 () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value at position pos or mapped to key. + * @param key position or key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (uint64_t key) + { + if (_data.index () == ArrayValue) + { + return _data.get ()[key]; + } + + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isUint64 () && member.first.getUint64 () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (double key) + { + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isDouble () && member.first.getDouble () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (std::string_view key) + { + if (_data.index () == NullValue) + { + _data.set (); + } + + for (auto& member : _data.get ()) + { + if (member.first.isString () && member.first.getString () == key) + { + return member.second; + } + } + + _data.get ().emplace_back (key, nullptr); + + return _data.get ().back ().second; + } + + /** + * @brief returns a reference to the value mapped to key. + * @param key key of the element to find. + * @return a reference to the requested element. + * @throw std::bad_cast. + */ + Value& operator[] (const char* key) + { + return operator[] (std::string_view (key)); + } + + /** + * @brief check if the nested container is empty. + * @return true if empty, false otherwise. + * @throw std::bad_cast. + */ + bool empty () const + { + switch (_data.index ()) + { + case StringValue: + return _data.get ().empty (); + + case ArrayValue: + return _data.get ().empty (); + + case ObjectValue: + return _data.get ().empty (); + + default: + break; + } + + throwException (); + } + + /** + * @brief returns the number of elements in the nested container. + * @return the number of elements in the nested container. + * @throw std::bad_cast. + */ + size_t size () const + { + switch (_data.index ()) + { + case StringValue: + return _data.get ().size (); + + case ArrayValue: + return _data.get ().size (); + + case ObjectValue: + return _data.get ().size (); + + default: + break; + } + + throwException (); + } + + /** + * @brief increase the capacity of the nested container. + * @param cap new capacity. + * @throw std::bad_cast. + */ + void reserve (size_t cap) + { + switch (_data.index ()) + { + case StringValue: + _data.get ().reserve (cap); + return; + + case ArrayValue: + _data.get ().reserve (cap); + return; + + case ObjectValue: + _data.get ().reserve (cap); + return; + + default: + break; + } + + throwException (); + } + + /** + * @brief erases all elements in the nested container. + * @throw std::bad_cast. + */ + void clear () + { + switch (_data.index ()) + { + case StringValue: + _data.get ().clear (); + return; + + case ArrayValue: + _data.get ().clear (); + return; + + case ObjectValue: + _data.get ().clear (); + return; + + default: + break; + } + + throwException (); + } + + /** + * @brief insert element in the nested container. + * @param member the element to insert in the nested container. + * @return a reference to the inserted element. + * @throw std::bad_cast. + */ + Value& insert (const Member& member) + { + _data.get ().push_back (member); + return _data.get ().back ().second; + } + + /** + * @brief insert element in the nested container. + * @param member the element to insert in the nested container. + * @return a reference to the inserted element. + * @throw std::bad_cast. + */ + Value& insert (Member&& member) + { + _data.get ().emplace_back (std::move (member)); + return _data.get ().back ().second; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase ([[maybe_unused]] std::nullptr_t key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isNull ()) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (bool key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isBool () && it->first.getBool () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (int32_t key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isInt () && it->first.getInt () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (uint32_t key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isUint () && it->first.getUint () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (int64_t key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isInt64 () && it->first.getInt64 () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (uint64_t key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isUint64 () && it->first.getUint64 () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (double key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isDouble () && it->first.getDouble () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (std::string_view key) + { + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); + + for (auto it = beg; it != end; ++it) + { + if (it->first.isString () && it->first.getString () == key) + { + _data.get ().erase (it); + return 1; + } + } + + return 0; + } + + /** + * @brief removes member with the key equivalent to key. + * @param key key of the member to remove. + * @return number of elements removed. + * @throw std::bad_cast. + */ + size_t erase (const char* key) + { + return erase (std::string_view (key)); + } + + /** + * @brief constructs an element in place at the end of the nested container. + * @param args arguments forwarded to the element constructor. + * @return a reference to the constructed object. + * @throw std::bad_cast. + */ + template + Value& emplaceBack (Args&&... args) + { + if (_data.index () == NullValue) + { + _data.set (); + } + _data.get ().emplace_back (std::forward (args)...); + return _data.get ().back (); + } + + /** + * @brief appends element at the end of the nested container. + * @param value the element to append. + * @return a reference to the pushed object. + * @throw std::bad_cast. + */ + Value& pushBack (const Value& value) + { + if (_data.index () == NullValue) + { + _data.set (); + } + _data.get ().push_back (value); + return _data.get ().back (); + } + + /** + * @brief appends element at the end of the nested container. + * @param value the element to append. + * @return a reference to the pushed object. + * @throw std::bad_cast. + */ + Value& pushBack (Value&& value) + { + if (_data.index () == NullValue) + { + _data.set (); + } + _data.get ().emplace_back (std::move (value)); + return _data.get ().back (); + } + + /** + * @brief removes the last element of the nested container. + * @throw std::bad_cast. + */ + void popBack () + { + _data.get ().pop_back (); + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains ([[maybe_unused]] std::nullptr_t key) const + { + for (auto const& member : _data.get ()) + { + if (member.first.isNull ()) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (bool key) const + { + for (auto const& member : _data.get ()) + { + if (member.first.isBool () && member.first.getBool () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key position or key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (int32_t key) const + { + if (_data.index () == ArrayValue) + { + return key >= 0 && static_cast (key) < _data.get ().size (); + } + + for (auto const& member : _data.get ()) + { + if (member.first.isInt () && member.first.getInt () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key position or key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (uint32_t key) const + { + if (_data.index () == ArrayValue) + { + return key < _data.get ().size (); + } + + for (auto const& member : _data.get ()) + { + if (member.first.isUint () && member.first.getUint () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key position or key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (int64_t key) const + { + if (_data.index () == ArrayValue) + { + return key >= 0 && static_cast (key) < _data.get ().size (); + } + + for (auto const& member : _data.get ()) + { + if (member.first.isInt64 () && member.first.getInt64 () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key position or key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (uint64_t key) const + { + if (_data.index () == ArrayValue) + { + return key < _data.get ().size (); + } + + for (auto const& member : _data.get ()) + { + if (member.first.isUint64 () && member.first.getUint64 () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (double key) const + { + for (auto const& member : _data.get ()) + { + if (member.first.isDouble () && member.first.getDouble () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (std::string_view key) const + { + for (auto const& member : _data.get ()) + { + if (member.first.isString () && member.first.getString () == key) + { + return true; + } + } + + return false; + } + + /** + * @brief checks if the nested container contains an element that is mapped to key. + * @param key key of the element to find. + * @return true if the nested container contains an element mapped to key, false otherwise. + * @throw std::bad_cast. + */ + bool contains (const char* key) const + { + return contains (std::string_view (key)); + } + + /** + * @brief exchanges the contents of the value with those of other. + * @param other value to exchange the contents with. + */ + void swap (Value& other) + { + Value temp (std::move (*this)); + *this = std::move (other); other = std::move (temp); } + /** + * @brief serialize the value, resetting the handler on failure. + * @param handler handler receiving the value. + * @return 0 on success, -1 otherwise. + */ + template + int serialize (Handler& handler) const + { + try + { + if (JOIN_LIKELY (setValue (*this, handler) == 0)) + { + return 0; + } + } + catch (...) + { + handler.reset (); + throw; + } + handler.reset (); + return -1; + } + + /** + * @brief deserialize a document into the value. + * @param args document to deserialize, followed by any reader argument. + * @return 0 on success, -1 otherwise. + */ + template + int deserialize (Args&&... args) + { + Reader reader; + return reader.deserialize (std::forward (args)..., *this); + } + + /** + * @brief serialize the value to a stream. + * @param document stream receiving the value. + * @param args writer arguments. + * @return 0 on success, -1 otherwise. + */ + template + int serialize (std::ostream& document, Args&&... args) const + { + Writer writer (document, std::forward (args)...); + return serialize (writer); + } + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @param length the length of the json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (const char* document, size_t length); + + /** + * @brief deserialize a json document. + * @param first the first character of the json document to deserialize. + * @param last the last character of the document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (const char* first, const char* last); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::string_view document); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::stringstream& document); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::istringstream& document); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::fstream& document); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::ifstream& document); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::iostream& document); + + /** + * @brief deserialize a json document. + * @param document json document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int jsonRead (std::istream& document); + + /** + * @brief serialize json data. + * @param document Document where to serialize json data. + * @param indentation number of spaces per indentation level. + * @return 0 on success, -1 otherwise. + */ + int jsonWrite (std::ostream& document, size_t indentation = 0) const; + + /** + * @brief serialize canonicalized json data. + * @param document Document where to serialize canonicalized json data. + * @return 0 on success, -1 otherwise. + */ + int jsonCanonicalize (std::ostream& document) const; + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @param length the length of the msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (const char* document, size_t length); + + /** + * @brief deserialize a msgpack document. + * @param first the first character of the msgpack document to deserialize. + * @param last the last character of the document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (const char* first, const char* last); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::string_view document); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::stringstream& document); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::istringstream& document); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::fstream& document); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::ifstream& document); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::iostream& document); + + /** + * @brief deserialize a msgpack document. + * @param document msgpack document to deserialize. + * @return 0 on success, -1 otherwise. + */ + int packRead (std::istream& document); + + /** + * @brief serialize msgpack data. + * @param document Document where to serialize msgpack data. + * @return 0 on success, -1 otherwise. + */ + int packWrite (std::ostream& document) const; + // friendship with equal operator. friend constexpr bool operator== (const Value& lhs, const Value& rhs); @@ -1363,6 +2443,149 @@ namespace join friend constexpr std::partial_ordering operator<=> (const Value& lhs, const Value& rhs); private: + /** + * @brief set value. + * @param value value to write. + * @param handler handler receiving the value. + * @return 0 on success, -1 otherwise. + */ + template + static int setValue (const Value& value, Handler& handler) + { + switch (value.index ()) + { + case Value::BooleanValue: + return handler.setBool (value.getBool ()); + + case Value::IntegerValue: + return handler.setInt (value.getInt ()); + + case Value::UnsignedValue: + return handler.setUint (value.getUint ()); + + case Value::Integer64Value: + return handler.setInt64 (value.getInt64 ()); + + case Value::Unsigned64Value: + return handler.setUint64 (value.getUint64 ()); + + case Value::RealValue: + return handler.setDouble (value.getDouble ()); + + case Value::StringValue: + return handler.setString (value.getString ()); + + case Value::ArrayValue: + return setArray (value.getArray (), handler); + + case Value::ObjectValue: + return setObject (value.getObject (), handler); + + default: + return handler.setNull (); + } + } + + /** + * @brief set array value. + * @param array array value to set. + * @param handler handler receiving the value. + * @return 0 on success, -1 otherwise. + */ + template + static int setArray (const Array& array, Handler& handler) + { + if (handler.startArray (array.size ()) == -1) + { + return -1; + } + + for (auto const& element : array) + { + if (setValue (element, handler) == -1) + { + return -1; + } + } + + return handler.stopArray (); + } + + /** + * @brief set object value. + * @param object object value to set. + * @param handler handler receiving the value. + * @return 0 on success, -1 otherwise. + */ + template + static int setObject (const Object& object, Handler& handler) + { + if (handler.startObject (object.size ()) == -1) + { + return -1; + } + + for (const Member& member : handler.members (object)) + { + int result = -1; + + if (JOIN_LIKELY (member.first.isString ())) + { + result = handler.setKey (member.first.getString ()); + } + else + { + switch (member.first.index ()) + { + case Value::BooleanValue: + result = handler.setKey (member.first.getBool ()); + break; + + case Value::IntegerValue: + result = handler.setKey (member.first.getInt ()); + break; + + case Value::UnsignedValue: + result = handler.setKey (member.first.getUint ()); + break; + + case Value::Integer64Value: + result = handler.setKey (member.first.getInt64 ()); + break; + + case Value::Unsigned64Value: + result = handler.setKey (member.first.getUint64 ()); + break; + + case Value::RealValue: + result = handler.setKey (member.first.getDouble ()); + break; + + case Value::ArrayValue: + case Value::ObjectValue: + join::lastError = make_error_code (SaxErrc::InvalidKey); + break; + + default: + result = handler.setKey (nullptr); + break; + } + } + + if (result == -1) + { + return -1; + } + + if (setValue (member.second, handler) == -1) + { + return -1; + } + } + + return handler.stopObject (); + } + /** * @brief convert the value held by the variant to float. * @return converted value. diff --git a/data/include/join/document.hpp b/data/include/join/value_builder.hpp similarity index 68% rename from data/include/join/document.hpp rename to data/include/join/value_builder.hpp index e4f8991e..750ee7c6 100644 --- a/data/include/join/document.hpp +++ b/data/include/join/value_builder.hpp @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef JOIN_DATA_DOCUMENT_HPP -#define JOIN_DATA_DOCUMENT_HPP +#ifndef JOIN_DATA_VALUE_BUILDER_HPP +#define JOIN_DATA_VALUE_BUILDER_HPP // libjoin. #include @@ -32,7 +32,6 @@ // C++. #include -#include #include #include #include @@ -44,102 +43,54 @@ namespace join { /** - * @brief document class, filled by balanced SAX calls. + * @brief value builder class, filled by balanced SAX calls. */ - class Document + class ValueBuilder { public: /** - * @brief default constructor. + * @brief create instance. + * @param root value receiving the document. */ - Document () = default; + explicit ValueBuilder (Value& root) + : _stack (std::move (cache ())) + , _root (root) + { + } /** * @brief copy constructor. * @param other object to copy. */ - Document (const Document& other) = delete; + ValueBuilder (const ValueBuilder& other) = delete; /** * @brief copy assignment. * @param other object to copy. * @return a reference of the current object. */ - Document& operator= (const Document& other) = delete; + ValueBuilder& operator= (const ValueBuilder& other) = delete; /** * @brief move constructor. * @param other object to move. */ - Document (Document&& other) noexcept - : _depth (other._depth) - , _stack (std::move (other._stack)) - , _root (std::move (other._root)) - { - std::copy_n (other._starts, _depth, _starts); - other._depth = 0; - } + ValueBuilder (ValueBuilder&& other) = delete; /** * @brief move assignment. * @param other object to move. * @return a reference of the current object. */ - Document& operator= (Document&& other) noexcept - { - std::copy_n (other._starts, other._depth, _starts); - _depth = other._depth; - _stack = std::move (other._stack); - _root = std::move (other._root); - other._depth = 0; - return *this; - } + ValueBuilder& operator= (ValueBuilder&& other) = delete; /** * @brief destroy instance. */ - ~Document () = default; - - /** - * @brief get the root value. - * @return root value. - */ - Value& root () noexcept - { - return _root; - } - - /** - * @brief get the root value. - * @return root value. - */ - const Value& root () const noexcept - { - return _root; - } - - /** - * @brief serialize the document, resetting the handler on failure. - * @param handler handler receiving the document. - * @return 0 on success, -1 otherwise. - */ - template - int serialize (Handler& handler) const + ~ValueBuilder () { - try - { - if (JOIN_LIKELY (setValue (_root, handler) == 0)) - { - return 0; - } - } - catch (...) - { - handler.reset (); - throw; - } - handler.reset (); - return -1; + _stack.clear (); + cache () = std::move (_stack); } /** @@ -303,11 +254,11 @@ namespace join } /** - * @brief set key. + * @brief set null key. * @param key object key to set. * @return 0 on success, -1 otherwise. */ - int setKey (const Value& key) + int setKey (std::nullptr_t key) { if (JOIN_UNLIKELY (_depth == 0)) { @@ -316,15 +267,16 @@ namespace join } _stack.emplace_back (key); + return 0; } /** - * @brief set key. - * @param key object key to move. + * @brief set boolean key. + * @param key object key to set. * @return 0 on success, -1 otherwise. */ - int setKey (Value&& key) + int setKey (bool key) { if (JOIN_UNLIKELY (_depth == 0)) { @@ -332,15 +284,17 @@ namespace join return -1; } - _stack.emplace_back (std::move (key)); + _stack.emplace_back (key); + return 0; } /** - * @brief stop object. + * @brief set integer key. + * @param key object key to set. * @return 0 on success, -1 otherwise. */ - int stopObject () + int setKey (int32_t key) { if (JOIN_UNLIKELY (_depth == 0)) { @@ -348,140 +302,159 @@ namespace join return -1; } - if (JOIN_UNLIKELY ((_stack.size () - _starts[_depth - 1]) & 1)) - { - join::lastError = make_error_code (SaxErrc::InvalidKey); - return -1; - } + _stack.emplace_back (key); - auto first = _stack.begin () + _starts[--_depth]; - Object object; - object.reserve ((_stack.end () - first) / 2); - for (auto it = first; it != _stack.end (); it += 2) - { - object.emplace_back (std::move (it[0]), std::move (it[1])); - } - _stack.erase (first, _stack.end ()); + return 0; + } - if (_depth == 0) + /** + * @brief set unsigned integer key. + * @param key object key to set. + * @return 0 on success, -1 otherwise. + */ + int setKey (uint32_t key) + { + if (JOIN_UNLIKELY (_depth == 0)) { - _root = std::move (object); - return 0; + join::lastError = make_error_code (SaxErrc::InvalidParent); + return -1; } - _stack.emplace_back (std::move (object)); + _stack.emplace_back (key); return 0; } /** - * @brief reset the parsing state, leaving the root untouched. + * @brief set 64 bits integer key. + * @param key object key to set. + * @return 0 on success, -1 otherwise. */ - void reset () noexcept + int setKey (int64_t key) { - _depth = 0; - _stack.clear (); + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + return -1; + } + + _stack.emplace_back (key); + + return 0; } - private: /** - * @brief set value. - * @param value value to write. - * @param handler handler receiving the value. + * @brief set unsigned 64 bits integer key. + * @param key object key to set. * @return 0 on success, -1 otherwise. */ - template - static int setValue (const Value& value, Handler& handler) + int setKey (uint64_t key) { - switch (value.index ()) + if (JOIN_UNLIKELY (_depth == 0)) { - case Value::BooleanValue: - return handler.setBool (value.getBool ()); - - case Value::IntegerValue: - return handler.setInt (value.getInt ()); - - case Value::UnsignedValue: - return handler.setUint (value.getUint ()); - - case Value::Integer64Value: - return handler.setInt64 (value.getInt64 ()); - - case Value::Unsigned64Value: - return handler.setUint64 (value.getUint64 ()); + join::lastError = make_error_code (SaxErrc::InvalidParent); + return -1; + } - case Value::RealValue: - return handler.setDouble (value.getDouble ()); + _stack.emplace_back (key); - case Value::StringValue: - return handler.setString (value.getString ()); + return 0; + } - case Value::ArrayValue: - return setArray (value.getArray (), handler); + /** + * @brief set real key. + * @param key object key to set. + * @return 0 on success, -1 otherwise. + */ + int setKey (double key) + { + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + return -1; + } - case Value::ObjectValue: - return setObject (value.getObject (), handler); + _stack.emplace_back (key); - default: - return handler.setNull (); - } + return 0; } /** - * @brief set array value. - * @param array array value to set. - * @param handler handler receiving the value. + * @brief set string key. + * @param key object key to set. * @return 0 on success, -1 otherwise. */ - template - static int setArray (const Array& array, Handler& handler) + int setKey (std::string_view key) { - if (handler.startArray (array.size ()) == -1) + if (JOIN_UNLIKELY (_depth == 0)) { + join::lastError = make_error_code (SaxErrc::InvalidParent); return -1; } - for (auto const& element : array) - { - if (setValue (element, handler) == -1) - { - return -1; - } - } + _stack.emplace_back (key); - return handler.stopArray (); + return 0; } /** - * @brief set object value. - * @param object object value to set. - * @param handler handler receiving the value. + * @brief set string key. + * @param key object key to set. + * @return 0 on success, -1 otherwise. + */ + int setKey (const char* key) + { + return setKey (std::string_view (key)); + } + + /** + * @brief stop object. * @return 0 on success, -1 otherwise. */ - template - static int setObject (const Object& object, Handler& handler) + int stopObject () { - if (handler.startObject (object.size ()) == -1) + if (JOIN_UNLIKELY (_depth == 0)) + { + join::lastError = make_error_code (SaxErrc::InvalidParent); + return -1; + } + + if (JOIN_UNLIKELY ((_stack.size () - _starts[_depth - 1]) & 1)) { + join::lastError = make_error_code (SaxErrc::InvalidKey); return -1; } - for (const Member& member : handler.members (object)) + auto first = _stack.begin () + _starts[--_depth]; + Object object; + object.reserve ((_stack.end () - first) / 2); + for (auto it = first; it != _stack.end (); it += 2) { - if (handler.setKey (member.first) == -1) - { - return -1; - } - - if (setValue (member.second, handler) == -1) - { - return -1; - } + object.emplace_back (std::move (it[0]), std::move (it[1])); } + _stack.erase (first, _stack.end ()); - return handler.stopObject (); + if (_depth == 0) + { + _root = std::move (object); + return 0; + } + + _stack.emplace_back (std::move (object)); + + return 0; } + /** + * @brief reset the parsing state, leaving the root untouched. + */ + void reset () noexcept + { + _depth = 0; + _stack.clear (); + } + + private: /** * @brief construct a value in place in the current container, or as root. * @param value value of the alternative to construct. @@ -501,6 +474,16 @@ namespace join return 0; } + /** + * @brief get the stack kept by the calling thread between documents. + * @return stack. + */ + static std::vector& cache () noexcept + { + thread_local std::vector stack; + return stack; + } + /// start of each open container in _stack. size_t _starts[saxMaxDepth] = {}; @@ -510,8 +493,8 @@ namespace join /// stack of the values of the open containers. std::vector _stack; - /// root. - Value _root; + /// value receiving the document. + Value& _root; }; } diff --git a/data/src/sax.cpp b/data/src/sax_error.cpp similarity index 99% rename from data/src/sax.cpp rename to data/src/sax_error.cpp index 15b5de5a..1b255fd9 100644 --- a/data/src/sax.cpp +++ b/data/src/sax_error.cpp @@ -23,7 +23,7 @@ */ // libjoin. -#include +#include using join::SaxErrc; using join::SaxCategory; diff --git a/data/src/value.cpp b/data/src/value.cpp new file mode 100644 index 00000000..189ae683 --- /dev/null +++ b/data/src/value.cpp @@ -0,0 +1,225 @@ +/** + * MIT License + * + * Copyright (c) 2021 Mathieu Rabine + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * 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 +#include +#include + +using join::Value; + +using join::JsonReader; +using join::JsonWriter; +using join::JsonCanonicalizer; + +using join::PackReader; +using join::PackWriter; + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (const char* document, size_t length) +{ + return deserialize (document, length); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (const char* first, const char* last) +{ + return deserialize (first, last); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::string_view document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::stringstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::istringstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::fstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::ifstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::iostream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonRead +// ========================================================================= +int Value::jsonRead (std::istream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonWrite +// ========================================================================= +int Value::jsonWrite (std::ostream& document, size_t indentation) const +{ + return serialize (document, indentation); +} + +// ========================================================================= +// CLASS : Value +// METHOD : jsonCanonicalize +// ========================================================================= +int Value::jsonCanonicalize (std::ostream& document) const +{ + return serialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (const char* document, size_t length) +{ + return deserialize (document, length); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (const char* first, const char* last) +{ + return deserialize (first, last); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::string_view document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::stringstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::istringstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::fstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::ifstream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::iostream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packRead +// ========================================================================= +int Value::packRead (std::istream& document) +{ + return deserialize (document); +} + +// ========================================================================= +// CLASS : Value +// METHOD : packWrite +// ========================================================================= +int Value::packWrite (std::ostream& document) const +{ + return serialize (document); +} diff --git a/data/tests/CMakeLists.txt b/data/tests/CMakeLists.txt index c54f6c36..9e6e1582 100644 --- a/data/tests/CMakeLists.txt +++ b/data/tests/CMakeLists.txt @@ -32,10 +32,10 @@ target_link_libraries(value.gtest ${JOIN_DATA} GTest::gtest_main) add_test(NAME value.gtest COMMAND value.gtest) install(TARGETS value.gtest RUNTIME DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/test) -add_executable(document.gtest document_test.cpp) -target_link_libraries(document.gtest ${JOIN_DATA} GTest::gtest_main) -add_test(NAME document.gtest COMMAND document.gtest) -install(TARGETS document.gtest RUNTIME DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/test) +add_executable(value_builder.gtest value_builder_test.cpp) +target_link_libraries(value_builder.gtest ${JOIN_DATA} GTest::gtest_main) +add_test(NAME value_builder.gtest COMMAND value_builder.gtest) +install(TARGETS value_builder.gtest RUNTIME DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/test) add_executable(sax_error.gtest sax_error_test.cpp) target_link_libraries(sax_error.gtest ${JOIN_DATA} GTest::gtest_main) diff --git a/data/tests/document_test.cpp b/data/tests/document_test.cpp deleted file mode 100644 index 977a63bc..00000000 --- a/data/tests/document_test.cpp +++ /dev/null @@ -1,560 +0,0 @@ -/** - * MIT License - * - * Copyright (c) 2021 Mathieu Rabine - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * 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. - */ - - -// libjoin. -#include -#include -#include - -// Libraries. -#include - -// C++. -#include -#include - -using join::Array; -using join::Object; -using join::Value; -using join::Document; -using join::JsonReader; -using join::JsonWriter; -using join::JsonCanonicalizer; -using join::PackReader; -using join::PackWriter; -using join::SaxErrc; - -/** - * @brief stream buffer failing on its first write. - */ -struct FailingBuffer : public std::streambuf -{ - std::streamsize xsputn (const char* data, std::streamsize size) override - { - if (!failed) - { - failed = true; - throw std::runtime_error ("write failed"); - } - written.append (data, size); - return size; - } - - bool failed = false; - std::string written; -}; - -/** - * @brief document derived by a user. - */ -struct Derived : public Document -{ -}; - -/** - * @brief handler counting the values it receives. - */ -struct Counter : public join::SaxHandler -{ - int setNull () - { - return count (); - } - - int setBool (bool) - { - return count (); - } - - int setInt (int32_t) - { - return count (); - } - - int setUint (uint32_t) - { - return count (); - } - - int setInt64 (int64_t) - { - return count (); - } - - int setUint64 (uint64_t) - { - return count (); - } - - int setDouble (double) - { - return count (); - } - - int setString (std::string_view) - { - return count (); - } - - int count () - { - ++values; - return 0; - } - - size_t values = 0; -}; - -/** - * @brief Test move constructor. - */ -TEST (Document, moveConstruct) -{ - Document document; - ASSERT_EQ (JsonReader ().deserialize (std::string ("[1,{\"a\":2}]"), document), 0) << join::lastError.message (); - Document moved (std::move (document)); - ASSERT_EQ (moved.root (), (Array{1, Object{{"a", 2}}})); - - Document partial; - ASSERT_EQ (partial.startArray (), 0); - ASSERT_EQ (partial.setInt (1), 0); - ASSERT_EQ (partial.startObject (), 0); - ASSERT_EQ (partial.setKey (Value ("a")), 0); - Document resumed (std::move (partial)); - ASSERT_EQ (resumed.setInt (2), 0); - ASSERT_EQ (resumed.stopObject (), 0); - ASSERT_EQ (resumed.stopArray (), 0); - ASSERT_EQ (resumed.root (), (Array{1, Object{{"a", 2}}})); - ASSERT_EQ (partial.stopArray (), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); -} - -/** - * @brief Test move assignment. - */ -TEST (Document, moveAssign) -{ - Document document; - ASSERT_EQ (JsonReader ().deserialize (std::string ("[1,{\"a\":2}]"), document), 0) << join::lastError.message (); - Document moved; - moved = std::move (document); - ASSERT_EQ (moved.root (), (Array{1, Object{{"a", 2}}})); - - Document partial; - ASSERT_EQ (partial.startArray (), 0); - ASSERT_EQ (partial.setInt (1), 0); - ASSERT_EQ (partial.startObject (), 0); - ASSERT_EQ (partial.setKey (Value ("a")), 0); - Document resumed; - ASSERT_EQ (resumed.startArray (), 0); - resumed = std::move (partial); - ASSERT_EQ (resumed.setInt (2), 0); - ASSERT_EQ (resumed.stopObject (), 0); - ASSERT_EQ (resumed.stopArray (), 0); - ASSERT_EQ (resumed.root (), (Array{1, Object{{"a", 2}}})); - ASSERT_EQ (partial.stopArray (), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); -} - -/** - * @brief Test root method. - */ -TEST (Document, root) -{ - Document document; - ASSERT_TRUE (document.root ().isNull ()); - - document.root () = Array{1, 2}; - const Document& constant = document; - ASSERT_TRUE (constant.root ().isArray ()); - ASSERT_EQ (constant.root ().size (), 2U); -} - -/** - * @brief Test serialize method. - */ -TEST (Document, serialize) -{ - Document document; - Value& value = document.root (); - value.pushBack (1); - value.pushBack (nullptr); - value.pushBack (true); - value.pushBack ("test"); - value.pushBack (1.23456789e-13); - - std::stringstream out; - JsonWriter jsonWriter (out); - ASSERT_EQ (document.serialize (jsonWriter), 0) << join::lastError.message (); - EXPECT_EQ (out.str (), "[1,null,true,\"test\",1.23456789e-13]"); - - out.str (""); - PackWriter packWriter (out); - ASSERT_EQ (document.serialize (packWriter), 0) << join::lastError.message (); - EXPECT_EQ (out.str (), std::string ({'\x95', '\x01', '\xc0', '\xc3', '\xa4', '\x74', '\x65', '\x73', '\x74', '\xcb', - '\x3d', '\x41', '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'})); - - Counter counter; - ASSERT_EQ (document.serialize (counter), 0) << join::lastError.message (); - EXPECT_EQ (counter.values, 5U); - - value = Object{}; - value["numbers"] = Array{333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001}; - value["string"] = "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"/"; - value["literals"] = Array{nullptr, true, false}; - - out.str (""); - JsonCanonicalizer canonicalizer (out); - ASSERT_EQ (document.serialize (canonicalizer), 0) << join::lastError.message (); - EXPECT_EQ (out.str (), - "{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"€$" - "\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}"); - - value = Object{{Array{1}, 1}}; - out.str (""); - ASSERT_EQ (document.serialize (packWriter), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidKey); - ASSERT_EQ (out.str (), ""); - - value = Array{7}; - ASSERT_EQ (document.serialize (packWriter), 0) << join::lastError.message (); - ASSERT_EQ (out.str (), std::string ({'\x91', '\x07'})); - - FailingBuffer buffer; - std::ostream failing (&buffer); - JsonWriter failingWriter (failing); - value = Array{std::string (2000, 'a')}; - ASSERT_THROW (document.serialize (failingWriter), std::runtime_error); - value = Array{1, 2}; - ASSERT_EQ (document.serialize (failingWriter), 0) << join::lastError.message (); - ASSERT_EQ (buffer.written, "[1,2]"); -} - -/** - * @brief Test setNull method. - */ -TEST (Document, setNull) -{ - Document document; - document.root () = Array{}; - ASSERT_EQ (document.setNull (), 0); - ASSERT_TRUE (document.root ().isNull ()); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setNull (), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root (), Array{nullptr}); -} - -/** - * @brief Test setBool method. - */ -TEST (Document, setBool) -{ - Document document; - ASSERT_EQ (document.setBool (true), 0); - ASSERT_EQ (document.root (), true); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setBool (true), 0); - ASSERT_EQ (document.setBool (false), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root (), (Array{true, false})); -} - -/** - * @brief Test setInt method. - */ -TEST (Document, setInt) -{ - Document document; - ASSERT_EQ (document.setInt (-1), 0); - ASSERT_EQ (document.root ().index (), Value::IntegerValue); - ASSERT_EQ (document.root (), -1); - ASSERT_EQ (JsonReader ().deserialize (std::string ("42"), document), 0) << join::lastError.message (); - ASSERT_EQ (document.root (), 42); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setInt (-1), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root ()[0].index (), Value::IntegerValue); - ASSERT_EQ (document.root ()[0], -1); -} - -/** - * @brief Test setUint method. - */ -TEST (Document, setUint) -{ - Document document; - ASSERT_EQ (document.setUint (1), 0); - ASSERT_EQ (document.root ().index (), Value::UnsignedValue); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setUint (1), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root ()[0].index (), Value::UnsignedValue); - ASSERT_EQ (document.root ()[0], 1U); -} - -/** - * @brief Test setInt64 method. - */ -TEST (Document, setInt64) -{ - Document document; - ASSERT_EQ (document.setInt64 (-1), 0); - ASSERT_EQ (document.root ().index (), Value::Integer64Value); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setInt64 (-1), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root ()[0].index (), Value::Integer64Value); - ASSERT_EQ (document.root ()[0], int64_t (-1)); -} - -/** - * @brief Test setUint64 method. - */ -TEST (Document, setUint64) -{ - Document document; - ASSERT_EQ (document.setUint64 (1), 0); - ASSERT_EQ (document.root ().index (), Value::Unsigned64Value); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setUint64 (1), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root ()[0].index (), Value::Unsigned64Value); - ASSERT_EQ (document.root ()[0], uint64_t (1)); -} - -/** - * @brief Test setDouble method. - */ -TEST (Document, setDouble) -{ - Document document; - ASSERT_EQ (document.setDouble (1.5), 0); - ASSERT_EQ (document.root (), 1.5); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setDouble (1.5), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root (), Array{1.5}); -} - -/** - * @brief Test setString method. - */ -TEST (Document, setString) -{ - Document document; - ASSERT_EQ (document.setString ("a"), 0); - ASSERT_EQ (document.root (), "a"); - - const std::string b ("b"); - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setString ("a"), 0); - ASSERT_EQ (document.setString (b), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root (), (Array{"a", "b"})); -} - -/** - * @brief Test startArray method. - */ -TEST (Document, startArray) -{ - Document document; - for (size_t i = 0; i < join::saxMaxDepth; ++i) - { - ASSERT_EQ (document.startArray (), 0); - } - ASSERT_EQ (document.startArray (), -1); - ASSERT_EQ (join::lastError, SaxErrc::StackOverflow); - - document.reset (); - ASSERT_EQ (document.startArray (2), 0); - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root (), Array{Value (Array{})}); -} - -/** - * @brief Test stopArray method. - */ -TEST (Document, stopArray) -{ - Document document; - ASSERT_EQ (document.stopArray (), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); - - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setInt (1), 0); - ASSERT_EQ (document.stopArray (), 0); - ASSERT_EQ (document.root (), Array{1}); -} - -/** - * @brief Test startObject method. - */ -TEST (Document, startObject) -{ - Document document; - for (size_t i = 0; i < join::saxMaxDepth; ++i) - { - ASSERT_EQ (document.startObject (), 0); - ASSERT_EQ (document.setKey ("a"), 0); - } - ASSERT_EQ (document.startObject (), -1); - ASSERT_EQ (join::lastError, SaxErrc::StackOverflow); - - document.reset (); - ASSERT_EQ (document.startObject (1), 0); - ASSERT_EQ (document.setKey ("a"), 0); - ASSERT_EQ (document.startObject (), 0); - ASSERT_EQ (document.stopObject (), 0); - ASSERT_EQ (document.stopObject (), 0); - ASSERT_EQ (document.root (), (Object{{"a", Object{}}})); -} - -/** - * @brief Test members method. - */ -TEST (Document, members) -{ - Document document; - Object object{{"b", 1}, {"a", 2}}; - EXPECT_EQ (&document.members (object), &object); - - Document copy; - document.root () = Object{{"b", 1}, {"a", Array{2}}}; - ASSERT_EQ (document.serialize (copy), 0) << join::lastError.message (); - EXPECT_EQ (copy.root (), document.root ()); - - document.root () = "scalar"; - ASSERT_EQ (document.serialize (copy), 0) << join::lastError.message (); - EXPECT_EQ (copy.root (), "scalar"); -} - -/** - * @brief Test setKey method. - */ -TEST (Document, setKey) -{ - Document document; - ASSERT_EQ (document.setKey ("a"), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); - const Value a ("a"); - ASSERT_EQ (document.setKey (a), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); - - ASSERT_EQ (document.startObject (), 0); - ASSERT_EQ (document.setKey ("a"), 0); - ASSERT_EQ (document.setInt (1), 0); - ASSERT_EQ (document.setKey (Value (2)), 0); - ASSERT_EQ (document.setInt (3), 0); - const Value c ("c"); - ASSERT_EQ (document.setKey (c), 0); - ASSERT_EQ (document.setInt (4), 0); - ASSERT_EQ (document.stopObject (), 0); - ASSERT_EQ (document.root ()["a"], 1); - ASSERT_EQ (document.root ().getObject ()[1].first, 2); - ASSERT_EQ (document.root ()["c"], 4); -} - -/** - * @brief Test stopObject method. - */ -TEST (Document, stopObject) -{ - Document document; - ASSERT_EQ (document.stopObject (), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); - - ASSERT_EQ (document.startObject (), 0); - ASSERT_EQ (document.setInt (1), 0); - ASSERT_EQ (document.stopObject (), -1); - ASSERT_EQ (join::lastError, SaxErrc::InvalidKey); - - document.reset (); - ASSERT_EQ (document.startObject (), 0); - ASSERT_EQ (document.setKey ("a"), 0); - ASSERT_EQ (document.setInt (1), 0); - ASSERT_EQ (document.stopObject (), 0); - ASSERT_EQ (document.root (), (Object{{"a", 1}})); - - std::string pack; - { - std::stringstream out; - PackWriter writer (out); - Document source; - source.root () = Object{{"a", Array{1, 2}}, {"b", nullptr}}; - ASSERT_EQ (source.serialize (writer), 0) << join::lastError.message (); - pack = out.str (); - } - ASSERT_EQ (PackReader ().deserialize (pack, document), 0) << join::lastError.message (); - ASSERT_EQ (document.root ()["a"][1], 2); - ASSERT_TRUE (document.root ()["b"].isNull ()); -} - -/** - * @brief Test reset method. - */ -TEST (Document, reset) -{ - Document document; - ASSERT_EQ (document.startArray (), 0); - ASSERT_EQ (document.setInt (1), 0); - document.reset (); - ASSERT_EQ (document.startObject (), 0); - ASSERT_EQ (document.stopObject (), 0); - ASSERT_TRUE (document.root ().isObject ()); - ASSERT_TRUE (document.root ().empty ()); - - ASSERT_EQ (JsonReader ().deserialize (std::string ("[1, [2, "), document), -1); - ASSERT_TRUE (document.root ().isObject ()); - ASSERT_EQ (JsonReader ().deserialize (std::string ("{\"a\":3}"), document), 0) << join::lastError.message (); - ASSERT_TRUE (document.root ().isObject ()); - ASSERT_EQ (document.root ()["a"], 3); - - Derived derived; - ASSERT_EQ (JsonReader ().deserialize (std::string ("[1, [2, "), derived), -1); - ASSERT_EQ (JsonReader ().deserialize (std::string ("[3]"), derived), 0) << join::lastError.message (); - ASSERT_TRUE (derived.root ().isArray ()); - ASSERT_EQ (derived.root ().size (), 1U); - ASSERT_EQ (derived.root ()[0], 3); -} - -/** - * @brief main function. - */ -int main (int argc, char** argv) -{ - testing::InitGoogleTest (&argc, argv); - return RUN_ALL_TESTS (); -} diff --git a/data/tests/json_canonicalizer_test.cpp b/data/tests/json_canonicalizer_test.cpp index fe3fda6f..637b8424 100644 --- a/data/tests/json_canonicalizer_test.cpp +++ b/data/tests/json_canonicalizer_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include // libraries. @@ -35,7 +34,6 @@ using join::Object; using join::JsonCanonicalizer; using join::JsonErrc; using join::SaxErrc; -using join::Document; /** * @brief Test members method. @@ -53,12 +51,12 @@ TEST (JsonCanonicalizer, members) EXPECT_EQ (sorted[2].get ().first, "\u20ac"); EXPECT_EQ (sorted[3].get ().first, "\U0001F600"); - Document document; - document.root () = Object{{"b", 1}, {"a", 2}}; - ASSERT_EQ (document.serialize (writer), 0) << join::lastError.message (); + Value value; + value = Object{{"b", 1}, {"a", 2}}; + ASSERT_EQ (value.serialize (writer), 0) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"a\":2,\"b\":1}"); - EXPECT_EQ (document.root ().getObject ()[0].first, "b"); - EXPECT_EQ (document.root ().getObject ()[1].first, "a"); + EXPECT_EQ (value.getObject ()[0].first, "b"); + EXPECT_EQ (value.getObject ()[1].first, "a"); } /** @@ -100,14 +98,13 @@ TEST (JsonCanonicalizer, reset) */ TEST (JsonCanonicalizer, nan) { - Document document; - Value& value = document.root (); + Value value; value.pushBack (std::numeric_limits::quiet_NaN ()); value.pushBack (-std::numeric_limits::quiet_NaN ()); std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "[null,null]"); } @@ -116,14 +113,13 @@ TEST (JsonCanonicalizer, nan) */ TEST (JsonCanonicalizer, infinity) { - Document document; - Value& value = document.root (); + Value value; value.pushBack (std::numeric_limits::infinity ()); value.pushBack (-std::numeric_limits::infinity ()); std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "[null,null]"); } @@ -132,15 +128,14 @@ TEST (JsonCanonicalizer, infinity) */ TEST (JsonCanonicalizer, array) { - Document document; - Value& value = document.root (); + Value value; value.pushBack (56); value.pushBack (Object{{"d", true}, {"10", nullptr}, {"1", Array{}}}); value.pushBack (-53.0); std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "[56,{\"1\":[],\"10\":null,\"d\":true},-53]"); } @@ -149,8 +144,7 @@ TEST (JsonCanonicalizer, array) */ TEST (JsonCanonicalizer, french) { - Document document; - Value& value = document.root (); + Value value; value["peach"] = "This sorting order"; value["péché"] = "is wrong according to French"; value["pêche"] = "but canonicalization MUST"; @@ -158,7 +152,7 @@ TEST (JsonCanonicalizer, french) std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"peach\":\"This sorting order\",\"péché\":\"is wrong according to French\",\"pêche\":\"but " "canonicalization MUST\",\"sin\":\"ignore locale\"}"); @@ -169,8 +163,7 @@ TEST (JsonCanonicalizer, french) */ TEST (JsonCanonicalizer, structures) { - Document document; - Value& value = document.root (); + Value value; value["1"] = Object{{"f", Object{{"f", "hi"}, {"F", 5}}}, {"\n", 56.0}}; value["10"] = Object{}; value[""] = "empty"; @@ -180,7 +173,7 @@ TEST (JsonCanonicalizer, structures) std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"\":\"empty\",\"1\":{\"\\n\":56,\"f\":{\"F\":5,\"f\":\"hi\"}},\"10\":{},\"111\":[{\"E\":\"no\",\"e\":" "\"yes\"}],\"A\":{},\"a\":{}}"); @@ -191,13 +184,12 @@ TEST (JsonCanonicalizer, structures) */ TEST (JsonCanonicalizer, unicode) { - Document document; - Value& value = document.root (); + Value value; value["Unnormalized Unicode"] = "A\u030a"; std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"Unnormalized Unicode\":\"Å\"}"); } @@ -206,15 +198,14 @@ TEST (JsonCanonicalizer, unicode) */ TEST (JsonCanonicalizer, values) { - Document document; - Value& value = document.root (); + Value value; value["numbers"] = Array{333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001}; value["string"] = "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"/"; value["literals"] = Array{nullptr, true, false}; std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"€$" "\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}"); @@ -225,8 +216,7 @@ TEST (JsonCanonicalizer, values) */ TEST (JsonCanonicalizer, weird) { - Document document; - Value& value = document.root (); + Value value; value["\u20ac"] = "Euro Sign"; value["\r"] = "Carriage Return"; value["\u000a"] = "Newline"; @@ -239,7 +229,7 @@ TEST (JsonCanonicalizer, weird) std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"\\n\":\"Newline\",\"\\r\":\"Carriage Return\",\"1\":\"One\",\"\":\"Browser " "Challenge\",\"€\":\"Control\",\"ö\":\"Latin Small Letter O With Diaeresis\",\"€\":\"Euro " @@ -251,24 +241,23 @@ TEST (JsonCanonicalizer, weird) */ TEST (JsonCanonicalizer, invalid) { - Document document; - Value& value = document.root (); + Value value; value["\xFF"] = "Invalid"; value["a"] = "Ascii"; std::stringstream out; JsonCanonicalizer writer (out); - ASSERT_EQ (document.serialize (writer), -1); + ASSERT_EQ (value.serialize (writer), -1); ASSERT_EQ (join::lastError, JsonErrc::InvalidEncoding); value = Object{{"a", "\xF0"}}; out.str (""); - ASSERT_EQ (document.serialize (writer), -1); + ASSERT_EQ (value.serialize (writer), -1); ASSERT_EQ (join::lastError, JsonErrc::InvalidEncoding); value = Array{"\xFF"}; out.str (""); - ASSERT_EQ (document.serialize (writer), -1); + ASSERT_EQ (value.serialize (writer), -1); ASSERT_EQ (join::lastError, JsonErrc::InvalidEncoding); } @@ -277,13 +266,12 @@ TEST (JsonCanonicalizer, invalid) */ TEST (JsonCanonicalizer, document) { - Document document; - Value& value = document.root (); + Value value; value = Object{{Value (1), "One"}, {"a", "Ascii"}}; std::stringstream out; JsonCanonicalizer writer (out); - EXPECT_EQ (document.serialize (writer), -1); + EXPECT_EQ (value.serialize (writer), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); EXPECT_EQ (out.str (), ""); @@ -294,7 +282,7 @@ TEST (JsonCanonicalizer, document) value = Object{{"b", 1}, {"a", Array{1}}}; out.str (""); JsonCanonicalizer indented (out, 2); - EXPECT_EQ (document.serialize (indented), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (indented), 0) << join::lastError.message (); EXPECT_EQ (out.str (), "{\"a\":[1],\"b\":1}"); } diff --git a/data/tests/json_reader_test.cpp b/data/tests/json_reader_test.cpp index 42c6bed8..8f7ca652 100644 --- a/data/tests/json_reader_test.cpp +++ b/data/tests/json_reader_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include // libraries. @@ -49,7 +48,6 @@ using join::JsonReadMode; using join::JsonReader; using join::BasicJsonReader; using join::JsonWriter; -using join::Document; /** * @brief stream buffer failing on its first write. @@ -100,44 +98,47 @@ struct Nested : public join::SaxHandler TEST (JsonReader, deserialize) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str ("[]"); std::string str ("[]"); char data[] = {'\x5b', '\x5d', '\x00'}; - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (JsonReader ().deserialize (str, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (str, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (JsonReader ().deserialize (data, sizeof (data) - 1, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (data, sizeof (data) - 1, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (JsonReader ().deserialize (&data[0], &data[sizeof (data) - 1], document), 0) - << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (&data[0], &data[sizeof (data) - 1], value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (JsonReader ().deserialize (&data[0], &data[sizeof (data)], document), -1); + ASSERT_EQ (JsonReader ().deserialize (&data[0], &data[sizeof (data)], value), -1); ASSERT_EQ (join::lastError, SaxErrc::ExtraData); JsonReader reader; - ASSERT_EQ (reader.deserialize (std::string ("[1, [2, "), document), -1); + ASSERT_EQ (reader.deserialize (std::string ("[1, [2, "), value), -1); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (reader.deserialize (std::string ("[3]"), document), 0) << join::lastError.message (); + ASSERT_EQ (reader.deserialize (std::string ("[3]"), value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_EQ (value.size (), 1); ASSERT_EQ (value[0], 3); + std::stringbuf buf ("[1.00000000000000000000001]"); + std::istream istream (&buf); + ASSERT_EQ (JsonReader ().deserialize (istream, value), 0) << join::lastError.message (); + ASSERT_EQ (value[0], 1.0); + std::stringstream out; JsonWriter writer (out); std::string nested = std::string (256, '[') + std::string (256, ']'); @@ -177,18 +178,17 @@ TEST (JsonReader, deserialize) TEST (JsonReader, pass) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str ("[]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); stream.clear (); stream.str ("[1234567890]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isInt ()); @@ -196,7 +196,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[-9876.543210]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -204,7 +204,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[0.123456789e-12]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -212,7 +212,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[1.234567890E+34]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -220,7 +220,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[NaN]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -228,7 +228,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[-NaN]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -236,7 +236,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[Inf]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -244,7 +244,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[-Inf]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -252,7 +252,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[Infinity]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -260,7 +260,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[-Infinity]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -268,7 +268,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[true]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isBool ()); @@ -276,7 +276,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[false]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isBool ()); @@ -284,7 +284,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[null]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isNull ()); @@ -292,7 +292,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("[0.5 ,98.6\n,\n99.44\n,\n1066,\n1e1\n,0.1e1\n,1e-1\n,1e00\n,2e+00\n,2e-00\n,\"rosebud\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_EQ (value.size (), 11); @@ -317,28 +317,28 @@ TEST (JsonReader, pass) ASSERT_TRUE (value[9].isDouble ()); ASSERT_DOUBLE_EQ (value[9].getDouble (), 2e-00); ASSERT_TRUE (value[10].isString ()); - ASSERT_STREQ (value[10].getString ().c_str (), "rosebud"); + ASSERT_EQ (value[10].getString (), "rosebud"); stream.clear (); stream.str ("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); stream.clear (); stream.str ("[{}]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isObject ()); stream.clear (); stream.str ("{}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.empty ()); stream.clear (); stream.str ("{\"integer\": 1234567890}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["integer"].isInt ()); @@ -346,7 +346,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"real\": -9876.543210}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["real"].isDouble ()); @@ -354,7 +354,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"e\": 0.123456789e-12}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["e"].isDouble ()); @@ -362,7 +362,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"E\": 1.234567890E+34}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["E"].isDouble ()); @@ -370,7 +370,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"\": 23456789012E66}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[""].isDouble ()); @@ -378,7 +378,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"zero\": 0}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["zero"].isInt ()); @@ -386,7 +386,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"one\": 1}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["one"].isInt ()); @@ -394,7 +394,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"space\": \" \"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["space"].isString ()); @@ -402,7 +402,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"quote\": \"\\\"\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["quote"].isString ()); @@ -410,7 +410,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"backslash\": \"\\\\\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["backslash"].isString ()); @@ -418,7 +418,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"controls\": \"\\b\\f\\n\\r\\t\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["controls"].isString ()); @@ -426,7 +426,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"slash\": \"/ & \\\\/\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["slash"].isString ()); @@ -434,7 +434,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"alpha\": \"abcdefghijklmnopqrstuvwyz\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["alpha"].isString ()); @@ -442,7 +442,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"ALPHA\": \"ABCDEFGHIJKLMNOPQRSTUVWYZ\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["ALPHA"].isString ()); @@ -450,7 +450,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"digit\": \"0123456789\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["digit"].isString ()); @@ -458,7 +458,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"0123456789\": \"digit\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["0123456789"].isString ()); @@ -466,7 +466,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"special\": \"`1~!@#$%^&*()_+-={':[,]}|;.?\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["special"].isString ()); @@ -474,7 +474,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"hex\": \"\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["hex"].isString ()); @@ -483,7 +483,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"true\": true}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["true"].isBool ()); @@ -491,7 +491,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"false\": false}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["false"].isBool ()); @@ -499,7 +499,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"null\": null}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["null"].isNull ()); @@ -507,7 +507,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"array\":[ ]}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["array"].isArray ()); @@ -515,7 +515,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"object\":{ }}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["object"].isObject ()); @@ -523,7 +523,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"address\": \"50 St. James Street\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["address"].isString ()); @@ -531,7 +531,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"url\": \"https://www.joinframework.net/\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["url"].isString ()); @@ -539,7 +539,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"comment\": \"// /* */\": \" \"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["comment"].isString ()); @@ -551,7 +551,7 @@ TEST (JsonReader, pass) stream.str ( "{\" s p a c e d \" :[1,2 , 3\n\n,\n4 , 5 , 6 ,7 " "],\"compact\":[1,2,3,4,5,6,7]}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[" s p a c e d "].isArray ()); @@ -573,7 +573,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"object with 1 member\":[\"array with 1 element\"]}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["object with 1 member"].isArray ()); @@ -581,7 +581,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"quotes\": \"" \\u0022 %22 0x22 034 "\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["quotes"].isString ()); @@ -589,7 +589,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{\"\\u0022\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?\"\n: \"A key can be any string\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["\"\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"].isString ()); @@ -597,7 +597,7 @@ TEST (JsonReader, pass) stream.clear (); stream.str ("{} extra"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), 0) + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.empty ()); @@ -609,185 +609,185 @@ TEST (JsonReader, pass) TEST (JsonReader, fail) { std::stringstream stream; - Document document; + Value value; stream.clear (); stream.str ("\"payload should be an object or array, not a string\""); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); - ASSERT_EQ (document.root (), "payload should be an object or array, not a string"); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); + ASSERT_EQ (value, "payload should be an object or array, not a string"); stream.clear (); stream.str ("[N]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[In]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[Infinit]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[nul]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[tru]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[fals]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Unclosed array\""); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{unquoted_key: \"keys must be quoted\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"extra comma\",]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"double extra comma\",,]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[ , \"<-- missing value\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Comma after the close\"],"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Extra close\"]]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Missing quote]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Extra comma\": true,}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Extra value after close\": true} \"misplaced quoted value\""); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Illegal expression\": 1 + 2}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Illegal invocation\": alert()}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Numbers cannot have leading zeroes\": 013}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Numbers cannot be hex\": 0x14}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Illegal backslash escape: \\x15\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\\naked]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Illegal backslash escape: \\017\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); stream.clear (); stream.str (std::string (join::saxMaxDepth + 1, '[') + std::string (join::saxMaxDepth + 1, ']')); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); ASSERT_EQ (join::lastError, SaxErrc::StackOverflow); stream.clear (); stream.str ("[[[[[[[[[[[[[[[[[[[{\"Not too deep\":0}]]]]]]]]]]]]]]]]]]]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); stream.clear (); stream.str ("{\"Missing colon\" null}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Missing quote: null}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Double colon\":: null}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Comma instead of colon\", null}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Colon instead of comma\": false]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Bad value\", truth]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("['single quote']"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"\ttab\tcharacter\tin\tstring\t\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"tab\\\t character\\\t in\\\t string\\\t \"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"line\nbreak\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"line\\\nbreak\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[0e]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[0e+]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[0e+-1]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\"Comma instead of closing brace\": true,"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[\"Mismatch\"}"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{} extra"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); } /** @@ -796,12 +796,11 @@ TEST (JsonReader, fail) TEST (JsonReader, dbl) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str ("[0.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -809,7 +808,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-0.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -817,7 +816,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -825,7 +824,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -833,7 +832,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -841,7 +840,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.0e46]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -849,7 +848,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1.5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -857,7 +856,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[3.1416]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -865,7 +864,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1E10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -873,7 +872,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1e10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -881,7 +880,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1E+10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -889,7 +888,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1E-10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -897,7 +896,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1E10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -905,7 +904,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1e10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -913,7 +912,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1E+10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -921,7 +920,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1E-10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -929,7 +928,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1e-200]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -937,7 +936,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.234E+10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -945,7 +944,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.234E-10]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -953,7 +952,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.79769e+308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -961,7 +960,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[2.22507e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -969,7 +968,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-1.79769e+308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -977,7 +976,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-2.22507e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -985,7 +984,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-4.9406564584124654e-324]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -993,7 +992,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[2.2250738585072009e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1001,7 +1000,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[2.2250738585072014e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1009,7 +1008,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.7976931348623157e+308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1017,7 +1016,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1e-10000]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1025,7 +1024,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[18446744073709551616]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1033,7 +1032,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[-9223372036854775809]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1041,7 +1040,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.9868011474609375]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1049,7 +1048,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[123e34]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1057,7 +1056,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[45913141877270640000.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1065,7 +1064,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[2.2250738585072011e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1073,7 +1072,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1e-214748363]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1081,7 +1080,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1e-214748364]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1089,7 +1088,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.017976931348623157e+310]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1097,7 +1096,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[2.2250738585072012e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1105,7 +1104,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.999999999999999944488848768742172978818416595458984375]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1113,7 +1112,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.999999999999999944488848768742172978818416595458984374]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1121,7 +1120,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.999999999999999944488848768742172978818416595458984376]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1129,7 +1128,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.00000000000000011102230246251565404236316680908203125]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1137,7 +1136,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.00000000000000011102230246251565404236316680908203124]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1145,7 +1144,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1.00000000000000011102230246251565404236316680908203126]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1153,7 +1152,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[72057594037927928.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1161,7 +1160,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[72057594037927936.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1169,7 +1168,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[72057594037927932.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1177,7 +1176,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[7205759403792793199999e-5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1185,7 +1184,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[7205759403792793200001e-5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1193,7 +1192,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[9223372036854774784.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1201,7 +1200,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[9223372036854775808.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1209,7 +1208,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[9223372036854775296.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1217,7 +1216,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[922337203685477529599999e-5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1225,7 +1224,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[922337203685477529600001e-5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1233,7 +1232,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[10141204801825834086073718800384]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1241,7 +1240,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[10141204801825835211973625643008]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1249,7 +1248,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[10141204801825834649023672221696]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1257,7 +1256,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1014120480182583464902367222169599999e-5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1265,7 +1264,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1014120480182583464902367222169600001e-5]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1273,7 +1272,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[5708990770823838890407843763683279797179383808]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1281,7 +1280,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[5708990770823839524233143877797980545530986496]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1289,7 +1288,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[5708990770823839207320493820740630171355185152]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1297,7 +1296,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[5708990770823839207320493820740630171355185151999e-3]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1305,7 +1304,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[5708990770823839207320493820740630171355185152001e-3]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1317,7 +1316,7 @@ TEST (JsonReader, dbl) "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" "0000000000]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1333,7 +1332,7 @@ TEST (JsonReader, dbl) "89880258182545180325707018860872113128079512233426288368622321503775666622503982534335974568884423900" "26549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042756718" "6443383770486037861622771738545623065874679014086723327636718751234567890123456789012345678901e-308]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1341,7 +1340,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[57507.7647e38]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1349,7 +1348,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[0.759154e41]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1357,7 +1356,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[1e126]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1365,7 +1364,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[2e126]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1373,7 +1372,7 @@ TEST (JsonReader, dbl) stream.clear (); stream.str ("[9007199254740993.0]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1386,99 +1385,98 @@ TEST (JsonReader, dbl) TEST (JsonReader, str) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str ("[\"\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), ""); + EXPECT_EQ (value[0].getString (), ""); stream.clear (); stream.str ("[\"Hello\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello"); + EXPECT_EQ (value[0].getString (), "Hello"); stream.clear (); stream.str ("[\"Hello\\nWorld\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello\nWorld"); + EXPECT_EQ (value[0].getString (), "Hello\nWorld"); stream.clear (); stream.str ("[\"Hello\\u0000World\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello\0World"); + EXPECT_EQ (value[0].getString (), std::string_view ("Hello\0World", 11)); stream.clear (); stream.str ("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\"\\/\b\f\n\r\t"); + EXPECT_EQ (value[0].getString (), "\"\\/\b\f\n\r\t"); stream.clear (); stream.str ("[\"\\u0024\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\x24"); + EXPECT_EQ (value[0].getString (), "\x24"); stream.clear (); stream.str ("[\"\\u00A2\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xC2\xA2"); + EXPECT_EQ (value[0].getString (), "\xC2\xA2"); stream.clear (); stream.str ("[\"\\u20AC\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xE2\x82\xAC"); + EXPECT_EQ (value[0].getString (), "\xE2\x82\xAC"); stream.clear (); stream.str ("[\"\\uD834\\uDD1E\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xF0\x9D\x84\x9E"); + EXPECT_EQ (value[0].getString (), "\xF0\x9D\x84\x9E"); stream.clear (); stream.str ("[\"\\/\"]"); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "/"); + EXPECT_EQ (value[0].getString (), "/"); - ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"Hello\"]"), document), 0) << join::lastError.message (); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello"); + ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"Hello\"]"), value), 0) << join::lastError.message (); + EXPECT_EQ (value[0].getString (), "Hello"); - ASSERT_EQ (JsonReader ().deserialize (std::string ("{\"key\":\"Hello\\nWorld\"}"), document), 0) + ASSERT_EQ (JsonReader ().deserialize (std::string ("{\"key\":\"Hello\\nWorld\"}"), value), 0) << join::lastError.message (); - EXPECT_STREQ (value["key"].getString ().c_str (), "Hello\nWorld"); + EXPECT_EQ (value["key"].getString (), "Hello\nWorld"); - ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"Hello"), document), -1); + ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"Hello"), value), -1); - ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"Hello\x01World\"]"), document), -1); + ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"Hello\x01World\"]"), value), -1); EXPECT_EQ (join::lastError, JsonErrc::IllegalCharacter); Nested nested; @@ -1494,52 +1492,51 @@ TEST (JsonReader, str) TEST (JsonReader, comments) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (" / this is an invalid comment\n"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[ / this is an invalid comment\n 1,2\n]"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[ 1, / this is an invalid comment\n2\n]"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("[ 1,2\n / this is an invalid comment\n]"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{ / this is an invalid comment\n \"a\":1,\"b\":2\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{ \"a\" / this is an invalid comment\n:1,\"b\":2\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{ \"a\": / this is an invalid comment\n1,\"b\":2\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{ \"a\":1 / this is an invalid comment,\"b\":2\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{ \"a\":1, / this is an invalid comment\"b\":2\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{} / invalid extra comment"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); stream.clear (); stream.str ("{\n // this is a comment\n \"comment\":true\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), 0) + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); @@ -1548,7 +1545,7 @@ TEST (JsonReader, comments) stream.clear (); stream.str ("{\n /*this is another comment*/\n \"comment\":false\n}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), 0) + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); @@ -1562,33 +1559,32 @@ TEST (JsonReader, comments) TEST (JsonReader, encoding) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.str ("[\"caf\xC3\xA9 \xE2\x82\xAC \xF0\x9F\x98\x82\"]"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), 0) + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); - EXPECT_STREQ (value[0].getString ().c_str (), "caf\xC3\xA9 \xE2\x82\xAC \xF0\x9F\x98\x82"); + EXPECT_EQ (value[0].getString (), "caf\xC3\xA9 \xE2\x82\xAC \xF0\x9F\x98\x82"); stream.clear (); stream.str ("{\"\xC3\xB6\\u00e9\xC3\xB6\":\"\xE2\x82\xAC\\n\xE2\x82\xAC\"}"); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), 0) + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); - EXPECT_STREQ (value["\xC3\xB6\xC3\xA9\xC3\xB6"].getString ().c_str (), "\xE2\x82\xAC\n\xE2\x82\xAC"); + EXPECT_EQ (value["\xC3\xB6\xC3\xA9\xC3\xB6"].getString (), "\xE2\x82\xAC\n\xE2\x82\xAC"); for (auto const& doc : {"[\"\xC0\xAF\"]", "[\"\xED\xA0\x80\"]", "[\"\xF5\x80\x80\x80\"]", "[\"\xE2\x28\xA1\"]", "[\"\xE2\x82\"]", "[\"\xE2\x82\\n\"]", "{\"\xFF\":1}"}) { stream.clear (); stream.str (doc); - ASSERT_EQ (BasicJsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (stream, value), -1); EXPECT_EQ (join::lastError, JsonErrc::InvalidEncoding); stream.clear (); stream.str (doc); - ASSERT_EQ (JsonReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (JsonReader ().deserialize (stream, value), 0) << join::lastError.message (); } for (auto const& doc : @@ -1596,17 +1592,17 @@ TEST (JsonReader, encoding) { stream.clear (); stream.str (doc); - ASSERT_EQ (JsonReader ().deserialize (stream, document), -1); + ASSERT_EQ (JsonReader ().deserialize (stream, value), -1); EXPECT_EQ (join::lastError, JsonErrc::InvalidEncoding); } std::string str ("[\"caf\xC3\xA9\", \"\xE2\x82\"]"); - ASSERT_EQ (BasicJsonReader ().deserialize (str, document), -1); + ASSERT_EQ (BasicJsonReader ().deserialize (str, value), -1); EXPECT_EQ (join::lastError, JsonErrc::InvalidEncoding); - ASSERT_EQ (BasicJsonReader ().deserialize (str.substr (0, 8) + "]", document), 0) + ASSERT_EQ (BasicJsonReader ().deserialize (str.substr (0, 8) + "]", value), 0) << join::lastError.message (); - ASSERT_EQ (BasicJsonReader ().deserialize (std::string ("{\"\xFF\":1}"), document), + ASSERT_EQ (BasicJsonReader ().deserialize (std::string ("{\"\xFF\":1}"), value), -1); EXPECT_EQ (join::lastError, JsonErrc::InvalidEncoding); } diff --git a/data/tests/json_writer_test.cpp b/data/tests/json_writer_test.cpp index 92b99e0c..82fd61b1 100644 --- a/data/tests/json_writer_test.cpp +++ b/data/tests/json_writer_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include // libraries. @@ -33,7 +32,6 @@ #include #include -using join::Document; using join::JsonWriter; using join::Value; using join::Array; @@ -205,6 +203,18 @@ TEST (JsonWriter, setDouble) stream.str (""); EXPECT_EQ (jsonWriter.setDouble (std::numeric_limits::quiet_NaN ()), 0); EXPECT_EQ (stream.str (), "NaN"); + + stream.str (""); + std::string expected ("[98.6"); + EXPECT_EQ (jsonWriter.startArray (), 0); + EXPECT_EQ (jsonWriter.setDouble (98.6), 0); + for (int i = 1; i < 300; ++i) + { + EXPECT_EQ (jsonWriter.setDouble (98.6), 0); + expected += ",98.6"; + } + EXPECT_EQ (jsonWriter.stopArray (), 0); + EXPECT_EQ (stream.str (), expected + "]"); } /** @@ -324,11 +334,45 @@ TEST (JsonWriter, setKey) EXPECT_EQ (jsonWriter.setKey ("foo"), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + auto invalid = [&] (auto key) { + stream.str (""); + jsonWriter.reset (); + jsonWriter.startObject (); + int result = jsonWriter.setKey (key); + jsonWriter.flush (); + return result; + }; + + EXPECT_EQ (invalid (nullptr), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (invalid (true), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (invalid (int32_t (-2)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (invalid (uint32_t (3)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (invalid (int64_t (-4)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (invalid (uint64_t (5)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (invalid (6.5), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); + EXPECT_EQ (stream.str (), ""); + + jsonWriter.reset (); + stream.str (""); EXPECT_EQ (jsonWriter.startObject (), 0); EXPECT_EQ (jsonWriter.setKey ("foo"), 0); jsonWriter.flush (); EXPECT_EQ (stream.str (), "{\"foo\":"); + jsonWriter.reset (); + stream.str (""); + EXPECT_EQ (jsonWriter.startObject (), 0); + EXPECT_EQ (jsonWriter.setKey (std::string_view ("bar")), 0); + jsonWriter.flush (); + EXPECT_EQ (stream.str (), "{\"bar\":"); + jsonWriter.reset (); stream.str (""); EXPECT_EQ (jsonWriter.startObject (), 0); @@ -395,8 +439,7 @@ TEST (JsonWriter, reset) */ TEST (JsonWriter, document) { - Document document; - Value& value = document.root (); + Value value; value.pushBack (nullptr); value.pushBack (true); value.pushBack (int32_t (1)); @@ -410,7 +453,7 @@ TEST (JsonWriter, document) JsonWriter jsonWriter (stream); stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (jsonWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), "[null,true,1,2,3,4,1.23456789e-13,\"test\"]"); value = Array{}; @@ -424,23 +467,23 @@ TEST (JsonWriter, document) expected += "\"" + std::string (2000, 'a') + "\"]"; stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (jsonWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), expected); value = Object{{Value (1), "One"}}; stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), -1); + EXPECT_EQ (value.serialize (jsonWriter), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); EXPECT_EQ (stream.str (), ""); value = Object{{"a", Object{{Value (1), "One"}}}}; stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), -1); + EXPECT_EQ (value.serialize (jsonWriter), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); value = Array{1, 2}; stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (jsonWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), "[1,2]"); value = Array{}; @@ -449,7 +492,7 @@ TEST (JsonWriter, document) value = Array{value}; } stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), -1); + EXPECT_EQ (value.serialize (jsonWriter), -1); EXPECT_EQ (join::lastError, SaxErrc::StackOverflow); value = Object{}; @@ -458,21 +501,21 @@ TEST (JsonWriter, document) value = Object{{"a", value}}; } stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), -1); + EXPECT_EQ (value.serialize (jsonWriter), -1); EXPECT_EQ (join::lastError, SaxErrc::StackOverflow); value = Array{1, 2}; stream.str (""); - EXPECT_EQ (document.serialize (jsonWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (jsonWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), "[1,2]"); FailingBuffer buffer; std::ostream failing (&buffer); JsonWriter failingWriter (failing); value = Array{std::string (2000, 'a')}; - EXPECT_THROW (document.serialize (failingWriter), std::runtime_error); + EXPECT_THROW (value.serialize (failingWriter), std::runtime_error); value = Array{1, 2}; - EXPECT_EQ (document.serialize (failingWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (failingWriter), 0) << join::lastError.message (); EXPECT_EQ (buffer.written, "[1,2]"); } diff --git a/data/tests/pack_reader_test.cpp b/data/tests/pack_reader_test.cpp index 052f69a2..abf74043 100644 --- a/data/tests/pack_reader_test.cpp +++ b/data/tests/pack_reader_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include #include @@ -48,7 +47,6 @@ using join::SaxErrc; using join::PackReader; using join::PackWriter; using join::JsonWriter; -using join::Document; /** * @brief handler throwing on strings. @@ -110,32 +108,30 @@ struct FailingBuffer : public std::streambuf TEST (PackReader, deserialize) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x00'})); std::string str ({'\xdd', '\x00', '\x00', '\x00', '\x00'}); char data[] = {'\xdd', '\x00', '\x00', '\x00', '\x00', '\x00'}; - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (PackReader ().deserialize (str, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (str, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (PackReader ().deserialize (data, sizeof (data) - 1, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (data, sizeof (data) - 1, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (PackReader ().deserialize (&data[0], &data[sizeof (data) - 1], document), 0) - << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (&data[0], &data[sizeof (data) - 1], value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (PackReader ().deserialize (&data[0], &data[sizeof (data)], document), -1); + ASSERT_EQ (PackReader ().deserialize (&data[0], &data[sizeof (data)], value), -1); ASSERT_EQ (join::lastError, SaxErrc::ExtraData); std::stringstream out; @@ -173,17 +169,17 @@ TEST (PackReader, deserialize) << join::lastError.message (); ASSERT_EQ (buffer.written, "[2]"); - ASSERT_EQ (PackReader ().deserialize (std::string ({'\xDB', '\xFF', '\xFF', '\xFF', '\xFF'}), document), -1); + ASSERT_EQ (PackReader ().deserialize (std::string ({'\xDB', '\xFF', '\xFF', '\xFF', '\xFF'}), value), -1); ASSERT_EQ (join::lastError, SaxErrc::Truncated); - ASSERT_EQ (PackReader ().deserialize (std::string ({'\xC6', '\xFF', '\xFF', '\xFF', '\xFF'}), document), -1); + ASSERT_EQ (PackReader ().deserialize (std::string ({'\xC6', '\xFF', '\xFF', '\xFF', '\xFF'}), value), -1); ASSERT_EQ (join::lastError, SaxErrc::Truncated); - ASSERT_EQ (PackReader ().deserialize (std::string ({'\xDA', '\x00'}), document), -1); + ASSERT_EQ (PackReader ().deserialize (std::string ({'\xDA', '\x00'}), value), -1); ASSERT_EQ (join::lastError, SaxErrc::Truncated); for (char head : {'\xDD', '\xDC', '\xDF', '\xDE', '\xDB', '\xDA', '\xD9', '\xC6', '\xC5', '\xC4', '\xD3', '\xCF', '\xCB', '\xD2', '\xCE', '\xCA', '\xD1', '\xCD', '\xD0', '\xCC'}) { - ASSERT_EQ (PackReader ().deserialize (std::string (1, head), document), -1) << int (head & 0xFF); + ASSERT_EQ (PackReader ().deserialize (std::string (1, head), value), -1) << int (head & 0xFF); ASSERT_EQ (join::lastError, SaxErrc::Truncated) << int (head & 0xFF); } @@ -191,11 +187,11 @@ TEST (PackReader, deserialize) ASSERT_THROW (PackReader ().deserialize (std::string ({'\x91', '\xA1', 'a'}), throwing), std::range_error); PackReader reader; - ASSERT_EQ (reader.deserialize (std::string ({'\x92', '\x01'}), document), -1); + ASSERT_EQ (reader.deserialize (std::string ({'\x92', '\x01'}), value), -1); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); - ASSERT_EQ (reader.deserialize (std::string ({'\x91', '\x03'}), document), 0) << join::lastError.message (); + ASSERT_EQ (reader.deserialize (std::string ({'\x91', '\x03'}), value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_EQ (value.size (), 1); ASSERT_EQ (value[0], 3); @@ -207,46 +203,45 @@ TEST (PackReader, deserialize) TEST (PackReader, pass) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (std::string ({'\x90'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value.empty ()); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xcc', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isUint8 ()); ASSERT_EQ (value[0], 1); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xd0', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isInt8 ()); ASSERT_EQ (value[0], 1); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xd1', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isUint16 ()); ASSERT_EQ (value[0], 1); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xd1', '\xfe', '\xd4'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isInt16 ()); ASSERT_EQ (value[0], -300); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xd2', '\x00', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isInt ()); ASSERT_EQ (value[0], 1); @@ -254,7 +249,7 @@ TEST (PackReader, pass) stream.clear (); stream.str ( std::string ({'\xdc', '\x00', '\x01', '\xcf', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isUint64 ()); ASSERT_EQ (value[0], 1); @@ -262,14 +257,14 @@ TEST (PackReader, pass) stream.clear (); stream.str ( std::string ({'\xdc', '\x00', '\x01', '\xd3', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (value[0].isInt64 ()); ASSERT_EQ (value[0], 1); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xce', '\x49', '\x96', '\x02', '\xd2'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isInt ()); @@ -277,7 +272,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xca', '\xc0', '\xc3', '\x4a', '\x45'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isFloat ()); @@ -286,7 +281,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\xc0', '\xc3', '\x4a', '\x45', '\x87', '\xe7', '\xc0', '\x6e'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -294,7 +289,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\x91', '\xcb', '\x3d', '\x41', '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -303,7 +298,7 @@ TEST (PackReader, pass) stream.clear (); stream.str ( std::string ({'\xdc', '\x00', '\x01', '\xcb', '\x47', '\x03', '\x05', '\x82', '\xff', '\xd7', '\x14', '\x75'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -311,7 +306,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc3'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isBool ()); @@ -319,7 +314,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\x91', '\xc2'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isBool ()); @@ -327,7 +322,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xc0'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isNull ()); @@ -342,7 +337,7 @@ TEST (PackReader, pass) '\x99', '\x99', '\x99', '\x99', '\x99', '\x9a', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xcb', '\x40', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xcb', '\x40', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xa7', '\x72', '\x6f', '\x73', '\x65', '\x62', '\x75', '\x64'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_EQ (value.size (), 11); @@ -379,18 +374,18 @@ TEST (PackReader, pass) '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xac', '\x4e', '\x6f', '\x74', '\x20', '\x74', '\x6f', '\x6f', '\x20', '\x64', '\x65', '\x65', '\x70'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); stream.clear (); stream.str (std::string ({'\x80'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.empty ()); stream.clear (); stream.str (std::string ({'\xde', '\x00', '\x01', '\xa7', '\x69', '\x6e', '\x74', '\x65', '\x67', '\x65', '\x72', '\xce', '\x49', '\x96', '\x02', '\xd2'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["integer"].isInt ()); @@ -399,7 +394,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa4', '\x72', '\x65', '\x61', '\x6c', '\xcb', '\xc0', '\xc3', '\x4a', '\x45', '\x87', '\xe7', '\xc0', '\x6e'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["real"].isDouble ()); @@ -408,7 +403,7 @@ TEST (PackReader, pass) stream.clear (); stream.str ( std::string ({'\x81', '\xa1', '\x65', '\xcb', '\x3d', '\x41', '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["e"].isDouble ()); @@ -417,7 +412,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xde', '\x00', '\x01', '\xa1', '\x45', '\xcb', '\x47', '\x03', '\x05', '\x82', '\xff', '\xd7', '\x14', '\x75'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["E"].isDouble ()); @@ -426,7 +421,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa0', '\xcb', '\x4f', '\xc9', '\xee', '\x09', '\x3a', '\x64', '\xb8', '\x54'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[""].isDouble ()); @@ -434,7 +429,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\x81', '\xa4', '\x7a', '\x65', '\x72', '\x6f', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["zero"].isInt ()); @@ -442,7 +437,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xde', '\x00', '\x01', '\xa3', '\x6f', '\x6e', '\x65', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["one"].isInt ()); @@ -451,7 +446,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ( {'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa5', '\x73', '\x70', '\x61', '\x63', '\x65', '\xa1', '\x20'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["space"].isString ()); @@ -459,7 +454,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\x81', '\xa5', '\x71', '\x75', '\x6f', '\x74', '\x65', '\xa1', '\x22'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["quote"].isString ()); @@ -468,7 +463,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xde', '\x00', '\x01', '\xa9', '\x62', '\x61', '\x63', '\x6b', '\x73', '\x6c', '\x61', '\x73', '\x68', '\xa1', '\x5c'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["backslash"].isString ()); @@ -477,7 +472,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa8', '\x63', '\x6f', '\x6e', '\x74', '\x72', '\x6f', '\x6c', '\x73', '\xa5', '\x08', '\x0c', '\x0a', '\x0d', '\x09'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["controls"].isString ()); @@ -486,7 +481,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\x81', '\xa5', '\x73', '\x6c', '\x61', '\x73', '\x68', '\xa6', '\x2f', '\x20', '\x26', '\x20', '\x5c', '\x2f'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["slash"].isString ()); @@ -497,7 +492,7 @@ TEST (PackReader, pass) std::string ({'\xde', '\x00', '\x01', '\xa5', '\x61', '\x6c', '\x70', '\x68', '\x61', '\xb9', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', '\x79', '\x7a'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["alpha"].isString ()); @@ -508,7 +503,7 @@ TEST (PackReader, pass) '\x41', '\xb9', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', '\x59', '\x5a'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["ALPHA"].isString ()); @@ -517,7 +512,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa5', '\x64', '\x69', '\x67', '\x69', '\x74', '\xaa', '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', '\x38', '\x39'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["digit"].isString ()); @@ -526,7 +521,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xaa', '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', '\x38', '\x39', '\xa5', '\x64', '\x69', '\x67', '\x69', '\x74'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["0123456789"].isString ()); @@ -538,7 +533,7 @@ TEST (PackReader, pass) '\x6c', '\xbf', '\x60', '\x31', '\x7e', '\x21', '\x40', '\x23', '\x24', '\x25', '\x5e', '\x26', '\x2a', '\x28', '\x29', '\x5f', '\x2b', '\x2d', '\x3d', '\x7b', '\x27', '\x3a', '\x5b', '\x2c', '\x5d', '\x7d', '\x7c', '\x3b', '\x2e', '\x3c', '\x2f', '\x3e', '\x3f'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["special"].isString ()); @@ -548,7 +543,7 @@ TEST (PackReader, pass) stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa3', '\x68', '\x65', '\x78', '\xb1', '\xc4', '\xa3', '\xe4', '\x95', '\xa7', '\xe8', '\xa6', '\xab', '\xec', '\xb7', '\xaf', '\xea', '\xaf', '\x8d', '\xee', '\xbd', '\x8a'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["hex"].isString ()); @@ -558,7 +553,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa4', '\x74', '\x72', '\x75', '\x65', '\xc3'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["true"].isBool ()); @@ -567,7 +562,7 @@ TEST (PackReader, pass) stream.clear (); stream.str ( std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa5', '\x66', '\x61', '\x6c', '\x73', '\x65', '\xc2'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["false"].isBool ()); @@ -575,7 +570,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa4', '\x6e', '\x75', '\x6c', '\x6c', '\xc0'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["null"].isNull ()); @@ -584,7 +579,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa5', '\x61', '\x72', '\x72', '\x61', '\x79', '\xdd', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["array"].isArray ()); @@ -593,7 +588,7 @@ TEST (PackReader, pass) stream.clear (); stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa6', '\x6f', '\x62', '\x6a', '\x65', '\x63', '\x74', '\xdf', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["object"].isObject ()); @@ -603,7 +598,7 @@ TEST (PackReader, pass) stream.str (std::string ({'\xdf', '\x00', '\x00', '\x00', '\x01', '\xa7', '\x61', '\x64', '\x64', '\x72', '\x65', '\x73', '\x73', '\xb3', '\x35', '\x30', '\x20', '\x53', '\x74', '\x2e', '\x20', '\x4a', '\x61', '\x6d', '\x65', '\x73', '\x20', '\x53', '\x74', '\x72', '\x65', '\x65', '\x74'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["address"].isString ()); @@ -614,7 +609,7 @@ TEST (PackReader, pass) '\x68', '\x74', '\x74', '\x70', '\x73', '\x3a', '\x2f', '\x2f', '\x77', '\x77', '\x77', '\x2e', '\x6a', '\x6f', '\x69', '\x6e', '\x66', '\x72', '\x61', '\x6d', '\x65', '\x77', '\x6f', '\x72', '\x6b', '\x2e', '\x6e', '\x65', '\x74', '\x2f'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["url"].isString ()); @@ -625,7 +620,7 @@ TEST (PackReader, pass) '\x6e', '\x74', '\xad', '\x2f', '\x2f', '\x20', '\x2f', '\x2a', '\x20', '\x3c', '\x21', '\x2d', '\x2d', '\x20', '\x2d', '\x2d', '\xab', '\x23', '\x20', '\x2d', '\x2d', '\x20', '\x2d', '\x2d', '\x3e', '\x20', '\x2a', '\x2f', '\xa1', '\x20'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["comment"].isString ()); @@ -639,7 +634,7 @@ TEST (PackReader, pass) '\x00', '\x07', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\xa7', '\x63', '\x6f', '\x6d', '\x70', '\x61', '\x63', '\x74', '\xdd', '\x00', '\x00', '\x00', '\x07', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[" s p a c e d "].isArray ()); @@ -665,7 +660,7 @@ TEST (PackReader, pass) '\x6d', '\x62', '\x65', '\x72', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xb4', '\x61', '\x72', '\x72', '\x61', '\x79', '\x20', '\x77', '\x69', '\x74', '\x68', '\x20', '\x31', '\x20', '\x65', '\x6c', '\x65', '\x6d', '\x65', '\x6e', '\x74'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["object with 1 member"].isArray ()); @@ -676,7 +671,7 @@ TEST (PackReader, pass) '\x65', '\x73', '\xbb', '\x26', '\x23', '\x33', '\x34', '\x3b', '\x20', '\x22', '\x20', '\x25', '\x32', '\x32', '\x20', '\x30', '\x78', '\x32', '\x32', '\x20', '\x30', '\x33', '\x34', '\x20', '\x26', '\x23', '\x78', '\x32', '\x32', '\x3b'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["quotes"].isString ()); @@ -689,7 +684,7 @@ TEST (PackReader, pass) '\x2d', '\x3d', '\x5b', '\x5d', '\x7b', '\x7d', '\x7c', '\x3b', '\x3a', '\x27', '\x2c', '\x2e', '\x2f', '\x3c', '\x3e', '\x3f', '\xb7', '\x41', '\x20', '\x6b', '\x65', '\x79', '\x20', '\x63', '\x61', '\x6e', '\x20', '\x62', '\x65', '\x20', '\x61', '\x6e', '\x79', '\x20', '\x73', '\x74', '\x72', '\x69', '\x6e', '\x67'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value["\"\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"].isString ()); @@ -702,7 +697,7 @@ TEST (PackReader, pass) TEST (PackReader, fail) { std::stringstream stream; - Document document; + Value value; stream.clear (); stream.str (std::string ({'\xd9', '\x32', '\x70', '\x61', '\x79', '\x6c', '\x6f', '\x61', '\x64', '\x20', '\x73', @@ -710,8 +705,8 @@ TEST (PackReader, fail) '\x20', '\x6f', '\x62', '\x6a', '\x65', '\x63', '\x74', '\x20', '\x6f', '\x72', '\x20', '\x61', '\x72', '\x72', '\x61', '\x79', '\x2c', '\x20', '\x6e', '\x6f', '\x74', '\x20', '\x61', '\x20', '\x73', '\x74', '\x72', '\x69', '\x6e', '\x67'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); - ASSERT_EQ (document.root (), "payload should be an object or array, not a string"); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); + ASSERT_EQ (value, "payload should be an object or array, not a string"); stream.clear (); stream.str (std::string ( @@ -723,11 +718,11 @@ TEST (PackReader, fail) '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xa8', '\x54', '\x6f', '\x6f', '\x20', '\x64', '\x65', '\x65', '\x70'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); stream.clear (); stream.str (std::string (join::saxMaxDepth + 1, '\x91') + '\xc0'); - ASSERT_EQ (PackReader ().deserialize (stream, document), -1); + ASSERT_EQ (PackReader ().deserialize (stream, value), -1); ASSERT_EQ (join::lastError, SaxErrc::StackOverflow); stream.clear (); @@ -740,41 +735,41 @@ TEST (PackReader, fail) '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdd', '\x00', '\x00', '\x00', '\x01', '\xdf', '\x00', '\x00', '\x00', '\x01', '\xA8', '\x54', '\x6F', '\x6F', '\x20', '\x64', '\x65', '\x65', '\x70', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); stream.clear (); stream.str (std::string ({'\xdc', '\x00', '\x01', '\xce', '\x49', '\x96', '\x02'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str ( std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xda', '\x00', '\x05', '\x48', '\x65', '\x6c', '\x6c'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str ( std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc5', '\x00', '\x05', '\x48', '\x65', '\x6c', '\x6c'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str (std::string ({'\x81', '\xa1'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str (std::string ({'\x81', '\xa1', '\x31', '\xcc'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str (std::string ({'\xd7'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str (std::string ({'\x92', '\x01'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); stream.clear (); stream.str (std::string ({'\x81'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); } /** @@ -783,13 +778,12 @@ TEST (PackReader, fail) TEST (PackReader, dbl) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -798,7 +792,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -807,7 +801,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -816,7 +810,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\xbf', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -825,7 +819,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf8', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -834,7 +828,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\xbf', '\xf8', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -843,7 +837,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x40', '\x09', '\x21', '\xff', '\x2e', '\x48', '\xe8', '\xa7'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -852,7 +846,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x42', '\x02', '\xa0', '\x5f', '\x20', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -861,7 +855,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3d', '\xdb', '\x7C', '\xdf', '\xd9', '\xd7', '\xbd', '\xbb'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -870,7 +864,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\xc2', '\x02', '\xa0', '\x5f', '\x20', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -879,7 +873,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\xbd', '\xdb', '\x7c', '\xdf', '\xd9', '\xd7', '\xbd', '\xbb'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -888,7 +882,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x42', '\x06', '\xfc', '\x2b', '\xa8', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -897,7 +891,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3d', '\xe0', '\xf5', '\xc0', '\x63', '\x56', '\x43', '\xa8'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -906,7 +900,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x7f', '\xef', '\xff', '\xfc', '\x57', '\xca', '\x82', '\xae'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -915,7 +909,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x0f', '\xff', '\xfe', '\x2e', '\x81', '\x59', '\xd0'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -924,7 +918,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\xff', '\xef', '\xff', '\xfc', '\x57', '\xca', '\x82', '\xae'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -933,7 +927,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x80', '\x0f', '\xff', '\xfe', '\x2e', '\x81', '\x59', '\xd0'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -942,7 +936,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -951,7 +945,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x0f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -960,7 +954,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -969,7 +963,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x7f', '\xef', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -978,7 +972,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -987,7 +981,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xef', '\x93', '\xe0', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -996,7 +990,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x47', '\x6d', '\x9c', '\x75', '\xd3', '\xac', '\x07', '\x2b'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1005,7 +999,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x44', '\x03', '\xe9', '\x61', '\xfa', '\x3b', '\xa6', '\xa0'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1014,7 +1008,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x0f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1023,7 +1017,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1032,7 +1026,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1041,7 +1035,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x7f', '\xef', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1050,7 +1044,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1059,7 +1053,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1068,7 +1062,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xef', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1077,7 +1071,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1086,7 +1080,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1095,7 +1089,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1104,7 +1098,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1113,7 +1107,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\x6f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1122,7 +1116,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\x70', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1131,7 +1125,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\x70', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1140,7 +1134,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\x6f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1149,7 +1143,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\x70', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1158,7 +1152,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\xdf', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1167,7 +1161,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\xe0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1176,7 +1170,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\xe0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1185,7 +1179,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\xdf', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1194,7 +1188,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x43', '\xe0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1203,7 +1197,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x46', '\x5f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1212,7 +1206,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x46', '\x60', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1221,7 +1215,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x49', '\x6f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1230,7 +1224,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x49', '\x70', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1239,7 +1233,7 @@ TEST (PackReader, dbl) stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xcb', '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isDouble ()); @@ -1252,85 +1246,84 @@ TEST (PackReader, dbl) TEST (PackReader, str) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xa0'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), ""); + EXPECT_EQ (value[0].getString (), ""); stream.clear (); stream.str (std::string ( {'\xdd', '\x00', '\x00', '\x00', '\x01', '\xda', '\x00', '\x05', '\x48', '\x65', '\x6c', '\x6c', '\x6f'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello"); + EXPECT_EQ (value[0].getString (), "Hello"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xdb', '\x00', '\x00', '\x00', '\x0b', '\x48', '\x65', '\x6c', '\x6c', '\x6f', '\x0a', '\x57', '\x6f', '\x72', '\x6c', '\x64'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello\nWorld"); + EXPECT_EQ (value[0].getString (), "Hello\nWorld"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xab', '\x48', '\x65', '\x6c', '\x6c', '\x6f', '\x00', '\x57', '\x6f', '\x72', '\x6c', '\x64'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello\0World"); + EXPECT_EQ (value[0].getString (), std::string_view ("Hello\0World", 11)); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xda', '\x00', '\x08', '\x22', '\x5c', '\x2f', '\x08', '\x0c', '\x0a', '\x0d', '\x09'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\"\\/\b\f\n\r\t"); + EXPECT_EQ (value[0].getString (), "\"\\/\b\f\n\r\t"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xdb', '\x00', '\x00', '\x00', '\x01', '\x24'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\x24"); + EXPECT_EQ (value[0].getString (), "\x24"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xa2', '\xc2', '\xa2'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xC2\xA2"); + EXPECT_EQ (value[0].getString (), "\xC2\xA2"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xda', '\x00', '\x03', '\xe2', '\x82', '\xac'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xE2\x82\xAC"); + EXPECT_EQ (value[0].getString (), "\xE2\x82\xAC"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xdb', '\x00', '\x00', '\x00', '\x04', '\xf0', '\x9d', '\x84', '\x9e'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xF0\x9D\x84\x9E"); + EXPECT_EQ (value[0].getString (), "\xF0\x9D\x84\x9E"); Nested nested; ASSERT_EQ (PackReader ().deserialize (std::string ({'\x91', '\xa4', '\x91', '\xa2', 'a', 'b'}), nested), 0) @@ -1346,85 +1339,84 @@ TEST (PackReader, str) TEST (PackReader, bin) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc4', '\x00'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), ""); + EXPECT_EQ (value[0].getString (), ""); stream.clear (); stream.str (std::string ( {'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc5', '\x00', '\x05', '\x48', '\x65', '\x6c', '\x6c', '\x6f'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello"); + EXPECT_EQ (value[0].getString (), "Hello"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc6', '\x00', '\x00', '\x00', '\x0b', '\x48', '\x65', '\x6c', '\x6c', '\x6f', '\x0a', '\x57', '\x6f', '\x72', '\x6c', '\x64'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello\nWorld"); + EXPECT_EQ (value[0].getString (), "Hello\nWorld"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc4', '\x0b', '\x48', '\x65', '\x6c', '\x6c', '\x6f', '\x00', '\x57', '\x6f', '\x72', '\x6c', '\x64'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "Hello\0World"); + EXPECT_EQ (value[0].getString (), std::string_view ("Hello\0World", 11)); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc5', '\x00', '\x08', '\x22', '\x5c', '\x2f', '\x08', '\x0c', '\x0a', '\x0d', '\x09'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\"\\/\b\f\n\r\t"); + EXPECT_EQ (value[0].getString (), "\"\\/\b\f\n\r\t"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc6', '\x00', '\x00', '\x00', '\x01', '\x24'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\x24"); + EXPECT_EQ (value[0].getString (), "\x24"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc4', '\x02', '\xc2', '\xa2'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xC2\xA2"); + EXPECT_EQ (value[0].getString (), "\xC2\xA2"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc5', '\x00', '\x03', '\xe2', '\x82', '\xac'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xE2\x82\xAC"); + EXPECT_EQ (value[0].getString (), "\xE2\x82\xAC"); stream.clear (); stream.str (std::string ({'\xdd', '\x00', '\x00', '\x00', '\x01', '\xc6', '\x00', '\x00', '\x00', '\x04', '\xf0', '\x9d', '\x84', '\x9e'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isArray ()); ASSERT_FALSE (value.empty ()); ASSERT_TRUE (value[0].isString ()); - EXPECT_STREQ (value[0].getString ().c_str (), "\xF0\x9D\x84\x9E"); + EXPECT_EQ (value[0].getString (), "\xF0\x9D\x84\x9E"); Nested nested; ASSERT_EQ ( @@ -1441,89 +1433,88 @@ TEST (PackReader, bin) TEST (PackReader, key) { std::stringstream stream; - Document document; - Value& value = document.root (); + Value value; stream.clear (); stream.str (std::string ({'\x81', '\xc0', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (nullptr)); stream.clear (); stream.str (std::string ({'\x81', '\xc2', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (false)); stream.clear (); stream.str (std::string ({'\x81', '\xc3', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (true)); stream.clear (); stream.str (std::string ({'\x81', '\xa3', '\x6b', '\x65', '\x79', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains ("key")); stream.clear (); stream.str (std::string ({'\x81', '\x2a', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (42)); stream.clear (); stream.str (std::string ({'\x81', '\xcc', '\xff', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (255)); stream.clear (); stream.str (std::string ({'\x81', '\xd3', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (int64_t (1))); stream.clear (); stream.str (std::string ({'\x81', '\xcf', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (uint64_t (1))); stream.clear (); stream.str (std::string ({'\x81', '\xca', '\x3f', '\x80', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (1.0)); stream.clear (); stream.str (std::string ({'\x81', '\xcb', '\x3f', '\xf0', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); ASSERT_TRUE (value.contains (1.0)); stream.clear (); stream.str (std::string ({'\x81', '\x91', '\x01', '\x01'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); ASSERT_EQ (join::lastError, SaxErrc::InvalidValue); stream.clear (); stream.str (std::string ({'\x81', '\x81', '\xa1', '\x61', '\x01', '\x01'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); ASSERT_EQ (join::lastError, SaxErrc::InvalidValue); stream.clear (); stream.str (std::string ({'\x81', '\xcc'})); - ASSERT_NE (PackReader ().deserialize (stream, document), 0); + ASSERT_NE (PackReader ().deserialize (stream, value), 0); ASSERT_EQ (join::lastError, SaxErrc::Truncated); stream.clear (); stream.str (std::string ({'\x81', '\xf6', '\x01'})); - ASSERT_EQ (PackReader ().deserialize (stream, document), 0) << join::lastError.message (); + ASSERT_EQ (PackReader ().deserialize (stream, value), 0) << join::lastError.message (); ASSERT_TRUE (value.isObject ()); - ASSERT_TRUE (value.contains (Value (-10))); + ASSERT_TRUE (value.contains (-10)); } /** diff --git a/data/tests/pack_writer_test.cpp b/data/tests/pack_writer_test.cpp index 2aa1d131..c63bcc1d 100644 --- a/data/tests/pack_writer_test.cpp +++ b/data/tests/pack_writer_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include #include @@ -33,7 +32,6 @@ // C++. #include -using join::Document; using join::PackWriter; using join::Value; using join::Array; @@ -483,7 +481,7 @@ TEST (PackWriter, setKey) std::stringstream stream; PackWriter packWriter (stream); - auto key = [&] (const Value& value) { + auto key = [&] (auto value) { stream.str (""); packWriter.reset (); packWriter.startObject (1); @@ -492,7 +490,23 @@ TEST (PackWriter, setKey) return result; }; - EXPECT_EQ (packWriter.setKey ("a"), -1); + EXPECT_EQ (packWriter.setKey (nullptr), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (true), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (int32_t (-2)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (uint32_t (3)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (int64_t (-4)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (uint64_t (5)), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (6.5), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey (std::string_view ("seven")), -1); + EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); + EXPECT_EQ (packWriter.setKey ("eight"), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidParent); EXPECT_EQ (key (nullptr), 0); @@ -547,16 +561,8 @@ TEST (PackWriter, setKey) EXPECT_EQ (key (std::string (65536, 'x')), 0); EXPECT_EQ (stream.str ().compare (1, 6, std::string ({'\xDB', '\x00', '\x01', '\x00', '\x00', '\x78'})), 0); - EXPECT_EQ (key (Value (Array{})), -1); - - EXPECT_EQ (key (Value (Object{})), -1); - - stream.str (""); - EXPECT_EQ (packWriter.startObject (1), 0); - EXPECT_EQ (packWriter.setKey (Value (Array{})), -1); - EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); - EXPECT_EQ (packWriter.setNull (), 0); - EXPECT_EQ (stream.str (), std::string ({'\xC0'})); + EXPECT_EQ (key ("a"), 0); + EXPECT_EQ (stream.str ().substr (1), std::string ({'\xA1', 'a'})); } /** @@ -616,8 +622,7 @@ TEST (PackWriter, reset) */ TEST (PackWriter, document) { - Document document; - Value& value = document.root (); + Value value; value.pushBack (nullptr); value.pushBack (true); value.pushBack (int32_t (1)); @@ -631,24 +636,24 @@ TEST (PackWriter, document) PackWriter packWriter (stream); stream.str (""); - EXPECT_EQ (document.serialize (packWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (packWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), std::string ({'\x98', '\xC0', '\xC3', '\x01', '\x02', '\x03', '\x04', '\xCB', '\x3D', '\x41', '\x5F', '\xFF', '\xE5', '\x3A', '\x68', '\x5D', '\xA4', '\x74', '\x65', '\x73', '\x74'})); value = Object{{"a", 1}}; stream.str (""); - EXPECT_EQ (document.serialize (packWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (packWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), std::string ({'\x81', '\xA1', 'a', '\x01'})); value = Object{{Array{1}, 1}}; stream.str (""); - EXPECT_EQ (document.serialize (packWriter), -1); + EXPECT_EQ (value.serialize (packWriter), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); EXPECT_EQ (stream.str (), ""); value = Array{7}; - EXPECT_EQ (document.serialize (packWriter), 0) << join::lastError.message (); + EXPECT_EQ (value.serialize (packWriter), 0) << join::lastError.message (); EXPECT_EQ (stream.str (), std::string ({'\x91', '\x07'})); } diff --git a/data/tests/parse_test.cpp b/data/tests/parse_test.cpp index e991710f..e7db2ffa 100644 --- a/data/tests/parse_test.cpp +++ b/data/tests/parse_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include // libraries. @@ -33,7 +32,6 @@ #include using join::Value; -using join::Document; using join::JsonReader; /** @@ -44,8 +42,8 @@ TEST (Parse, canada) std::ifstream fs ("data/canada.json"); ASSERT_TRUE (fs.is_open ()); - Document document; - ASSERT_EQ (JsonReader ().deserialize (fs, document), 0) << join::lastError.message (); + Value value; + ASSERT_EQ (JsonReader ().deserialize (fs, value), 0) << join::lastError.message (); } /** @@ -60,8 +58,8 @@ TEST (Parse, citm_catalog) buffer << fs.rdbuf (); std::string json = buffer.str (); - Document document; - ASSERT_EQ (JsonReader ().deserialize (json, document), 0) << join::lastError.message (); + Value value; + ASSERT_EQ (JsonReader ().deserialize (json, value), 0) << join::lastError.message (); } /** @@ -72,8 +70,8 @@ TEST (Parse, twitter) std::ifstream fs ("data/twitter.json"); ASSERT_TRUE (fs.is_open ()); - Document document; - ASSERT_EQ (JsonReader ().deserialize (fs, document), 0) << join::lastError.message (); + Value value; + ASSERT_EQ (JsonReader ().deserialize (fs, value), 0) << join::lastError.message (); } /** diff --git a/data/tests/roundtrip_test.cpp b/data/tests/roundtrip_test.cpp index 4abc0ad3..695a5c6d 100644 --- a/data/tests/roundtrip_test.cpp +++ b/data/tests/roundtrip_test.cpp @@ -23,7 +23,6 @@ */ // libjoin. -#include #include // libraries. @@ -38,14 +37,13 @@ using join::SaxErrc; using join::JsonErrc; using join::JsonWriter; using join::JsonReader; -using join::Document; /** * @brief Test JSON round trip. */ TEST (JsonReader, roundtrip) { - Document document; + Value value; JsonReader reader; std::stringstream stream; @@ -53,218 +51,218 @@ TEST (JsonReader, roundtrip) stream.clear (); stream.str ("[null]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[null]"); stream.clear (); stream.str ("[true]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[true]"); stream.clear (); stream.str ("[false]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[false]"); stream.clear (); stream.str ("[0]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[0]"); stream.clear (); stream.str ("[\"foo\"]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[\"foo\"]"); stream.clear (); stream.str ("[]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[]"); stream.clear (); stream.str ("{}"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "{}"); stream.clear (); stream.str ("[0,1]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[0,1]"); stream.clear (); stream.str ("{\"foo\":\"bar\"}"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "{\"foo\":\"bar\"}"); stream.clear (); stream.str ("{\"a\":null,\"foo\":\"bar\"}"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "{\"a\":null,\"foo\":\"bar\"}"); stream.clear (); stream.str ("[-1]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[-1]"); stream.clear (); stream.str ("[-2147483648]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[-2147483648]"); stream.clear (); stream.str ("[-1234567890123456789]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[-1234567890123456789]"); stream.clear (); stream.str ("[-9223372036854775808]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[-9223372036854775808]"); stream.clear (); stream.str ("[1]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[1]"); stream.clear (); stream.str ("[2147483647]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[2147483647]"); stream.clear (); stream.str ("[4294967295]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[4294967295]"); stream.clear (); stream.str ("[1234567890123456789]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[1234567890123456789]"); stream.clear (); stream.str ("[9223372036854775807]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[9223372036854775807]"); stream.clear (); stream.str ("[0.0]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[0.0]"); stream.clear (); stream.str ("[-0.0]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[-0.0]"); stream.clear (); stream.str ("[1.2345]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[1.2345]"); stream.clear (); stream.str ("[-1.2345]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[-1.2345]"); stream.clear (); stream.str ("[5e-324]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[5e-324]"); stream.clear (); stream.str ("[2.225073858507201e-308]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[2.225073858507201e-308]"); stream.clear (); stream.str ("[2.2250738585072014e-308]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[2.2250738585072014e-308]"); stream.clear (); stream.str ("[1.7976931348623157e308]"); - ASSERT_NE (reader.deserialize (stream, document), -1) << join::lastError.message (); + ASSERT_NE (reader.deserialize (stream, value), -1) << join::lastError.message (); stream.str (""); stream.clear (); - ASSERT_NE (document.serialize (writer), -1) << join::lastError.message (); + ASSERT_NE (value.serialize (writer), -1) << join::lastError.message (); EXPECT_EQ (stream.str (), "[1.7976931348623157e308]"); } diff --git a/data/tests/sax_error_test.cpp b/data/tests/sax_error_test.cpp index cafe3edd..fa007dac 100644 --- a/data/tests/sax_error_test.cpp +++ b/data/tests/sax_error_test.cpp @@ -23,7 +23,7 @@ */ // libjoin. -#include +#include // Libraries. #include diff --git a/data/tests/sax_handler_test.cpp b/data/tests/sax_handler_test.cpp index e21e14d8..5d57a7dc 100644 --- a/data/tests/sax_handler_test.cpp +++ b/data/tests/sax_handler_test.cpp @@ -178,7 +178,15 @@ TEST (SaxHandler, members) TEST (SaxHandler, setKey) { SaxHandler handler; - EXPECT_EQ (handler.setKey (Value ("a")), 0); + EXPECT_EQ (handler.setKey (nullptr), 0); + EXPECT_EQ (handler.setKey (true), 0); + EXPECT_EQ (handler.setKey (int32_t (-2)), 0); + EXPECT_EQ (handler.setKey (uint32_t (3)), 0); + EXPECT_EQ (handler.setKey (int64_t (-4)), 0); + EXPECT_EQ (handler.setKey (uint64_t (5)), 0); + EXPECT_EQ (handler.setKey (6.5), 0); + EXPECT_EQ (handler.setKey (std::string_view ("seven")), 0); + EXPECT_EQ (handler.setKey ("eight"), 0); } /** diff --git a/data/tests/value_builder_test.cpp b/data/tests/value_builder_test.cpp new file mode 100644 index 00000000..e8cc27f5 --- /dev/null +++ b/data/tests/value_builder_test.cpp @@ -0,0 +1,454 @@ +/** + * MIT License + * + * Copyright (c) 2021 Mathieu Rabine + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * 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. + */ + + +// libjoin. +#include +#include +#include + +// Libraries. +#include + +// C++. +#include +#include +#include +#include + +using join::Array; +using join::Object; +using join::Value; +using join::ValueBuilder; +using join::JsonReader; +using join::PackReader; +using join::PackWriter; +using join::SaxErrc; + +/** + * @brief value builder derived by a user. + */ +struct Derived : public ValueBuilder +{ + using ValueBuilder::ValueBuilder; +}; + +/** + * @brief handler parsing each string it receives into its own value. + */ +struct Nested : public join::SaxHandler +{ + int setString (std::string_view json) + { + values.emplace_back (); + return JsonReader ().deserialize (json.data (), json.size (), values.back ()); + } + + std::vector values; +}; + +/** + * @brief Test create method. + */ +TEST (ValueBuilder, create) +{ + Value value; + { + ValueBuilder builder (value); + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setInt (1), 0); + + Value inner; + { + ValueBuilder innerBuilder (inner); + ASSERT_EQ (innerBuilder.startArray (), 0); + ASSERT_EQ (innerBuilder.setInt (2), 0); + ASSERT_EQ (innerBuilder.stopArray (), 0); + } + ASSERT_EQ (inner, Array{2}); + + ASSERT_EQ (builder.setInt (3), 0); + ASSERT_EQ (builder.stopArray (), 0); + } + ASSERT_EQ (value, (Array{1, 3})); + + Nested nested; + ASSERT_EQ (JsonReader ().deserialize (std::string ("[\"[1,{\\\"a\\\":2}]\", \"3\"]"), nested), 0) + << join::lastError.message (); + ASSERT_EQ (nested.values.size (), 2U); + ASSERT_EQ (nested.values[0], (Array{1, Object{{"a", 2}}})); + ASSERT_EQ (nested.values[1], 3); +} + +/** + * @brief Test setNull method. + */ +TEST (ValueBuilder, setNull) +{ + Value value; + ValueBuilder builder (value); + value = Array{}; + ASSERT_EQ (builder.setNull (), 0); + ASSERT_TRUE (value.isNull ()); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setNull (), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value, Array{nullptr}); +} + +/** + * @brief Test setBool method. + */ +TEST (ValueBuilder, setBool) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setBool (true), 0); + ASSERT_EQ (value, true); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setBool (true), 0); + ASSERT_EQ (builder.setBool (false), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value, (Array{true, false})); +} + +/** + * @brief Test setInt method. + */ +TEST (ValueBuilder, setInt) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setInt (-1), 0); + ASSERT_EQ (value.index (), Value::IntegerValue); + ASSERT_EQ (value, -1); + ASSERT_EQ (JsonReader ().deserialize (std::string ("42"), value), 0) << join::lastError.message (); + ASSERT_EQ (value, 42); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setInt (-1), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value[0].index (), Value::IntegerValue); + ASSERT_EQ (value[0], -1); +} + +/** + * @brief Test setUint method. + */ +TEST (ValueBuilder, setUint) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setUint (1), 0); + ASSERT_EQ (value.index (), Value::UnsignedValue); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setUint (1), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value[0].index (), Value::UnsignedValue); + ASSERT_EQ (value[0], 1U); +} + +/** + * @brief Test setInt64 method. + */ +TEST (ValueBuilder, setInt64) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setInt64 (-1), 0); + ASSERT_EQ (value.index (), Value::Integer64Value); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setInt64 (-1), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value[0].index (), Value::Integer64Value); + ASSERT_EQ (value[0], int64_t (-1)); +} + +/** + * @brief Test setUint64 method. + */ +TEST (ValueBuilder, setUint64) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setUint64 (1), 0); + ASSERT_EQ (value.index (), Value::Unsigned64Value); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setUint64 (1), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value[0].index (), Value::Unsigned64Value); + ASSERT_EQ (value[0], uint64_t (1)); +} + +/** + * @brief Test setDouble method. + */ +TEST (ValueBuilder, setDouble) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setDouble (1.5), 0); + ASSERT_EQ (value, 1.5); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setDouble (1.5), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value, Array{1.5}); +} + +/** + * @brief Test setString method. + */ +TEST (ValueBuilder, setString) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setString ("a"), 0); + ASSERT_EQ (value, "a"); + + const std::string b ("b"); + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setString ("a"), 0); + ASSERT_EQ (builder.setString (b), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value, (Array{"a", "b"})); +} + +/** + * @brief Test startArray method. + */ +TEST (ValueBuilder, startArray) +{ + Value value; + ValueBuilder builder (value); + for (size_t i = 0; i < join::saxMaxDepth; ++i) + { + ASSERT_EQ (builder.startArray (), 0); + } + ASSERT_EQ (builder.startArray (), -1); + ASSERT_EQ (join::lastError, SaxErrc::StackOverflow); + + builder.reset (); + ASSERT_EQ (builder.startArray (2), 0); + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value, Array{Value (Array{})}); +} + +/** + * @brief Test stopArray method. + */ +TEST (ValueBuilder, stopArray) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.stopArray (), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setInt (1), 0); + ASSERT_EQ (builder.stopArray (), 0); + ASSERT_EQ (value, Array{1}); +} + +/** + * @brief Test startObject method. + */ +TEST (ValueBuilder, startObject) +{ + Value value; + ValueBuilder builder (value); + for (size_t i = 0; i < join::saxMaxDepth; ++i) + { + ASSERT_EQ (builder.startObject (), 0); + ASSERT_EQ (builder.setKey ("a"), 0); + } + ASSERT_EQ (builder.startObject (), -1); + ASSERT_EQ (join::lastError, SaxErrc::StackOverflow); + + builder.reset (); + ASSERT_EQ (builder.startObject (1), 0); + ASSERT_EQ (builder.setKey ("a"), 0); + ASSERT_EQ (builder.startObject (), 0); + ASSERT_EQ (builder.stopObject (), 0); + ASSERT_EQ (builder.stopObject (), 0); + ASSERT_EQ (value, (Object{{"a", Object{}}})); +} + +/** + * @brief Test members method. + */ +TEST (ValueBuilder, members) +{ + Value value; + ValueBuilder builder (value); + Object object{{"b", 1}, {"a", 2}}; + EXPECT_EQ (&builder.members (object), &object); + + Value copy; + ValueBuilder copyBuilder (copy); + value = Object{{"b", 1}, {"a", Array{2}}}; + ASSERT_EQ (value.serialize (copyBuilder), 0) << join::lastError.message (); + EXPECT_EQ (copy, value); + + value = "scalar"; + ASSERT_EQ (value.serialize (copyBuilder), 0) << join::lastError.message (); + EXPECT_EQ (copy, "scalar"); +} + +/** + * @brief Test setKey method. + */ +TEST (ValueBuilder, setKey) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.setKey ("a"), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (nullptr), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (true), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (int32_t (-2)), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (uint32_t (3)), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (int64_t (-4)), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (uint64_t (5)), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + ASSERT_EQ (builder.setKey (6.5), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + + ASSERT_EQ (builder.startObject (), 0); + ASSERT_EQ (builder.setKey (nullptr), 0); + ASSERT_EQ (builder.setInt (0), 0); + ASSERT_EQ (builder.setKey (true), 0); + ASSERT_EQ (builder.setInt (1), 0); + ASSERT_EQ (builder.setKey (int32_t (-2)), 0); + ASSERT_EQ (builder.setInt (2), 0); + ASSERT_EQ (builder.setKey (uint32_t (3)), 0); + ASSERT_EQ (builder.setInt (3), 0); + ASSERT_EQ (builder.setKey (int64_t (-4)), 0); + ASSERT_EQ (builder.setInt (4), 0); + ASSERT_EQ (builder.setKey (uint64_t (5)), 0); + ASSERT_EQ (builder.setInt (5), 0); + ASSERT_EQ (builder.setKey (6.5), 0); + ASSERT_EQ (builder.setInt (6), 0); + ASSERT_EQ (builder.setKey (std::string_view ("seven")), 0); + ASSERT_EQ (builder.setInt (7), 0); + ASSERT_EQ (builder.setKey ("eight"), 0); + ASSERT_EQ (builder.setInt (8), 0); + ASSERT_EQ (builder.stopObject (), 0); + ASSERT_EQ (value.size (), 9U); + ASSERT_TRUE (value.getObject ()[0].first.isNull ()); + ASSERT_TRUE (value.getObject ()[1].first.isBool ()); + ASSERT_TRUE (value.getObject ()[2].first.isInt ()); + ASSERT_TRUE (value.getObject ()[3].first.isUint ()); + ASSERT_TRUE (value.getObject ()[4].first.isInt64 ()); + ASSERT_TRUE (value.getObject ()[5].first.isUint64 ()); + ASSERT_TRUE (value.getObject ()[6].first.isDouble ()); + ASSERT_EQ (value["seven"], 7); + ASSERT_EQ (value["eight"], 8); +} + +/** + * @brief Test stopObject method. + */ +TEST (ValueBuilder, stopObject) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.stopObject (), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidParent); + + ASSERT_EQ (builder.startObject (), 0); + ASSERT_EQ (builder.setInt (1), 0); + ASSERT_EQ (builder.stopObject (), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidKey); + + builder.reset (); + ASSERT_EQ (builder.startObject (), 0); + ASSERT_EQ (builder.setKey ("a"), 0); + ASSERT_EQ (builder.setInt (1), 0); + ASSERT_EQ (builder.stopObject (), 0); + ASSERT_EQ (value, (Object{{"a", 1}})); + + std::string pack; + { + std::stringstream out; + PackWriter writer (out); + Value source = Object{{"a", Array{1, 2}}, {"b", nullptr}}; + ASSERT_EQ (source.serialize (writer), 0) << join::lastError.message (); + pack = out.str (); + } + ASSERT_EQ (PackReader ().deserialize (pack, value), 0) << join::lastError.message (); + ASSERT_EQ (value["a"][1], 2); + ASSERT_TRUE (value["b"].isNull ()); +} + +/** + * @brief Test reset method. + */ +TEST (ValueBuilder, reset) +{ + Value value; + ValueBuilder builder (value); + ASSERT_EQ (builder.startArray (), 0); + ASSERT_EQ (builder.setInt (1), 0); + builder.reset (); + ASSERT_EQ (builder.startObject (), 0); + ASSERT_EQ (builder.stopObject (), 0); + ASSERT_TRUE (value.isObject ()); + ASSERT_TRUE (value.empty ()); + + ASSERT_EQ (JsonReader ().deserialize (std::string ("[1, [2, "), builder), -1); + ASSERT_TRUE (value.isObject ()); + ASSERT_EQ (JsonReader ().deserialize (std::string ("{\"a\":3}"), builder), 0) << join::lastError.message (); + ASSERT_TRUE (value.isObject ()); + ASSERT_EQ (value["a"], 3); + + Value derivedValue; + Derived derived (derivedValue); + ASSERT_EQ (JsonReader ().deserialize (std::string ("[1, [2, "), derived), -1); + ASSERT_EQ (JsonReader ().deserialize (std::string ("[3]"), derived), 0) << join::lastError.message (); + ASSERT_TRUE (derivedValue.isArray ()); + ASSERT_EQ (derivedValue.size (), 1U); + ASSERT_EQ (derivedValue[0], 3); +} + +/** + * @brief main function. + */ +int main (int argc, char** argv) +{ + testing::InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/data/tests/value_test.cpp b/data/tests/value_test.cpp index a7db9d96..a4600c52 100644 --- a/data/tests/value_test.cpp +++ b/data/tests/value_test.cpp @@ -25,17 +25,104 @@ // libjoin. #include #include +#include +#include // Libraries. #include // C++. +#include +#include +#include +#include #include using join::Array; using join::Member; using join::Object; using join::Value; +using join::JsonReader; +using join::JsonReadMode; +using join::JsonWriter; +using join::JsonCanonicalizer; +using join::PackReader; +using join::PackWriter; +using join::SaxErrc; + +/** + * @brief stream buffer failing on its first write. + */ +struct FailingBuffer : public std::streambuf +{ + std::streamsize xsputn (const char* data, std::streamsize size) override + { + if (!failed) + { + failed = true; + throw std::runtime_error ("write failed"); + } + written.append (data, size); + return size; + } + + bool failed = false; + std::string written; +}; + +/** + * @brief handler counting the values it receives. + */ +struct Counter : public join::SaxHandler +{ + int setNull () + { + return count (); + } + + int setBool (bool) + { + return count (); + } + + int setInt (int32_t) + { + return count (); + } + + int setUint (uint32_t) + { + return count (); + } + + int setInt64 (int64_t) + { + return count (); + } + + int setUint64 (uint64_t) + { + return count (); + } + + int setDouble (double) + { + return count (); + } + + int setString (std::string_view) + { + return count (); + } + + int count () + { + ++values; + return 0; + } + + size_t values = 0; +}; /** * @brief Test create method. @@ -3561,128 +3648,128 @@ TEST (Value, getDouble) */ TEST (Value, getString) { - const char* tmp = nullptr; + std::string_view tmp; (void)tmp; Value value; ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = nullptr; ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = true; ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = false; ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::min (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = std::numeric_limits::max (); ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = ""; ASSERT_EQ (value.getString (), ""); - ASSERT_STREQ (static_cast (value), ""); + ASSERT_EQ (static_cast (value), ""); value = "foo"; ASSERT_EQ (value.getString (), "foo"); - ASSERT_STREQ (static_cast (value), "foo"); + ASSERT_EQ (static_cast (value), "foo"); value = "02:42:64:2f:6a:d0"; - ASSERT_STREQ (value.getString ().c_str (), "02:42:64:2f:6a:d0"); - ASSERT_STREQ (static_cast (value), "02:42:64:2f:6a:d0"); + ASSERT_EQ (value.getString (), "02:42:64:2f:6a:d0"); + ASSERT_EQ (static_cast (value), "02:42:64:2f:6a:d0"); value = "127.0.0.1"; - ASSERT_STREQ (value.getString ().c_str (), "127.0.0.1"); - ASSERT_STREQ (static_cast (value), "127.0.0.1"); + ASSERT_EQ (value.getString (), "127.0.0.1"); + ASSERT_EQ (static_cast (value), "127.0.0.1"); value = Array{1}; ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); value = Object{{"i", 1}}; ASSERT_THROW (value.getString (), std::bad_cast); - ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); + ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } /** @@ -4059,6 +4146,132 @@ TEST (Value, at) ASSERT_TRUE (value.at ("object").isObject ()); ASSERT_THROW (value.at ("non existing path"), std::out_of_range); + + value = Object{{nullptr, 0}, {true, 1}, {int32_t (-2), 2}, {uint32_t (3), 3}, + {int64_t (-4), 4}, {uint64_t (5), 5}, {6.5, 6}, {"seven", 7}}; + const Value& constant = value; + ASSERT_EQ (value.at (nullptr), 0); + ASSERT_EQ (constant.at (nullptr), 0); + ASSERT_EQ (value.at (true), 1); + ASSERT_EQ (constant.at (true), 1); + ASSERT_EQ (value.at (int32_t (-2)), 2); + ASSERT_EQ (constant.at (int32_t (-2)), 2); + ASSERT_EQ (value.at (uint32_t (3)), 3); + ASSERT_EQ (constant.at (uint32_t (3)), 3); + ASSERT_EQ (value.at (int64_t (-4)), 4); + ASSERT_EQ (constant.at (int64_t (-4)), 4); + ASSERT_EQ (value.at (uint64_t (5)), 5); + ASSERT_EQ (constant.at (uint64_t (5)), 5); + ASSERT_EQ (value.at (6.5), 6); + ASSERT_EQ (constant.at (6.5), 6); + ASSERT_EQ (value.at (std::string_view ("seven")), 7); + ASSERT_EQ (constant.at (std::string_view ("seven")), 7); + ASSERT_EQ (value.at ("seven"), 7); + ASSERT_EQ (constant.at ("seven"), 7); + ASSERT_EQ (value.at (std::string ("seven")), 7); + ASSERT_EQ (constant.at (std::string ("seven")), 7); + ASSERT_THROW (value.at (8), std::out_of_range); + ASSERT_THROW (constant.at (8), std::out_of_range); + ASSERT_THROW (constant.at ("eight"), std::out_of_range); + + value = Object{{"seven", 7}}; + ASSERT_THROW (value.at (nullptr), std::out_of_range); + ASSERT_THROW (constant.at (nullptr), std::out_of_range); + ASSERT_THROW (value.at (true), std::out_of_range); + ASSERT_THROW (constant.at (true), std::out_of_range); + ASSERT_THROW (value.at (uint32_t (3)), std::out_of_range); + ASSERT_THROW (constant.at (uint32_t (3)), std::out_of_range); + ASSERT_THROW (value.at (int64_t (-4)), std::out_of_range); + ASSERT_THROW (constant.at (int64_t (-4)), std::out_of_range); + ASSERT_THROW (value.at (uint64_t (5)), std::out_of_range); + ASSERT_THROW (constant.at (uint64_t (5)), std::out_of_range); + ASSERT_THROW (value.at (6.5), std::out_of_range); + ASSERT_THROW (constant.at (6.5), std::out_of_range); + + value = Array{0, 1, 2, 3}; + ASSERT_EQ (value.at (int32_t (0)), 0); + ASSERT_EQ (constant.at (int32_t (0)), 0); + ASSERT_EQ (value.at (uint32_t (1)), 1); + ASSERT_EQ (constant.at (uint32_t (1)), 1); + ASSERT_EQ (value.at (int64_t (2)), 2); + ASSERT_EQ (constant.at (int64_t (2)), 2); + ASSERT_EQ (value.at (uint64_t (3)), 3); + ASSERT_EQ (constant.at (uint64_t (3)), 3); + ASSERT_THROW (value.at (-1), std::out_of_range); + ASSERT_THROW (constant.at (-1), std::out_of_range); + ASSERT_THROW (value.at (4), std::out_of_range); + ASSERT_THROW (constant.at (4), std::out_of_range); + ASSERT_THROW (value.at (nullptr), std::bad_cast); + ASSERT_THROW (constant.at (nullptr), std::bad_cast); + ASSERT_THROW (value.at (true), std::bad_cast); + ASSERT_THROW (constant.at (true), std::bad_cast); + ASSERT_THROW (value.at (1.0), std::bad_cast); + ASSERT_THROW (constant.at (1.0), std::bad_cast); + ASSERT_THROW (value.at ("seven"), std::bad_cast); + ASSERT_THROW (constant.at ("seven"), std::bad_cast); +} + +/** + * @brief Test subscript operator. + */ +TEST (Value, subscript) +{ + Value value; + value[nullptr] = 0; + value[true] = 1; + value[int32_t (-2)] = 2; + value[uint32_t (3)] = 3; + value[int64_t (-4)] = 4; + value[uint64_t (5)] = 5; + value[6.5] = 6; + value[std::string_view ("seven")] = 7; + value["eight"] = 8; + value[std::string ("nine")] = 9; + ASSERT_EQ (value.size (), 10U); + ASSERT_EQ (value[nullptr], 0); + ASSERT_EQ (value[true], 1); + ASSERT_EQ (value[int32_t (-2)], 2); + ASSERT_EQ (value[uint32_t (3)], 3); + ASSERT_EQ (value[int64_t (-4)], 4); + ASSERT_EQ (value[uint64_t (5)], 5); + ASSERT_EQ (value[6.5], 6); + ASSERT_EQ (value[std::string_view ("seven")], 7); + ASSERT_EQ (value["eight"], 8); + ASSERT_EQ (value[std::string ("nine")], 9); + ASSERT_EQ (value.size (), 10U); + + value = nullptr; + value[true] = 1; + ASSERT_TRUE (value.isObject ()); + value = nullptr; + value[int32_t (-2)] = 2; + ASSERT_TRUE (value.isObject ()); + value = nullptr; + value[uint32_t (3)] = 3; + ASSERT_TRUE (value.isObject ()); + value = nullptr; + value[int64_t (-4)] = 4; + ASSERT_TRUE (value.isObject ()); + value = nullptr; + value[uint64_t (5)] = 5; + ASSERT_TRUE (value.isObject ()); + value = nullptr; + value[6.5] = 6; + ASSERT_TRUE (value.isObject ()); + + value = Array{0, 1, 2, 3}; + ASSERT_EQ (value[int32_t (0)], 0); + ASSERT_EQ (value[uint32_t (1)], 1); + ASSERT_EQ (value[int64_t (2)], 2); + ASSERT_EQ (value[uint64_t (3)], 3); + ASSERT_THROW (value[nullptr], std::bad_cast); + ASSERT_THROW (value[true], std::bad_cast); + ASSERT_THROW (value[1.0], std::bad_cast); + ASSERT_THROW (value["seven"], std::bad_cast); + + value = true; + ASSERT_THROW (value[0], std::bad_cast); + ASSERT_THROW (value["seven"], std::bad_cast); } /** @@ -4508,94 +4721,94 @@ TEST (Value, clear) TEST (Value, insert) { Value value; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = nullptr; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = true; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = false; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::min (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = std::numeric_limits::max (); - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = ""; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = "foo"; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = "02:42:64:2f:6a:d0"; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = "127.0.0.1"; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = Array{1}; - ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); + ASSERT_THROW (value.insert (Member{"i", 1}), std::bad_cast); value = Object{{"i", 1}}; - ASSERT_NO_THROW (value.insert (std::make_pair ("i", 1))); + ASSERT_NO_THROW (value.insert (Member{"i", 1})); } /** @@ -4693,6 +4906,30 @@ TEST (Value, erase) value = Object{{"i", 1}}; ASSERT_EQ (value.erase ("i"), 1); ASSERT_EQ (value.erase ("j"), 0); + + value = Object{{nullptr, 0}, {true, 1}, {int32_t (-2), 2}, {uint32_t (3), 3}, {int64_t (-4), 4}, + {uint64_t (5), 5}, {6.5, 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}}; + ASSERT_EQ (value.erase (nullptr), 1); + ASSERT_EQ (value.erase (nullptr), 0); + ASSERT_EQ (value.erase (true), 1); + ASSERT_EQ (value.erase (true), 0); + ASSERT_EQ (value.erase (int32_t (-2)), 1); + ASSERT_EQ (value.erase (int32_t (-2)), 0); + ASSERT_EQ (value.erase (uint32_t (3)), 1); + ASSERT_EQ (value.erase (uint32_t (3)), 0); + ASSERT_EQ (value.erase (int64_t (-4)), 1); + ASSERT_EQ (value.erase (int64_t (-4)), 0); + ASSERT_EQ (value.erase (uint64_t (5)), 1); + ASSERT_EQ (value.erase (uint64_t (5)), 0); + ASSERT_EQ (value.erase (6.5), 1); + ASSERT_EQ (value.erase (6.5), 0); + ASSERT_EQ (value.erase (std::string_view ("seven")), 1); + ASSERT_EQ (value.erase (std::string_view ("seven")), 0); + ASSERT_EQ (value.erase ("eight"), 1); + ASSERT_EQ (value.erase ("eight"), 0); + ASSERT_EQ (value.erase (std::string ("nine")), 1); + ASSERT_EQ (value.erase (std::string ("nine")), 0); + ASSERT_TRUE (value.empty ()); } /** @@ -5034,6 +5271,43 @@ TEST (Value, contains) value = Object{{"i", 1}}; ASSERT_FALSE (value.contains (0)); ASSERT_TRUE (value.contains ("i")); + + value = Object{{nullptr, 0}, {true, 1}, {int32_t (-2), 2}, {uint32_t (3), 3}, + {int64_t (-4), 4}, {uint64_t (5), 5}, {6.5, 6}, {"seven", 7}}; + ASSERT_TRUE (value.contains (nullptr)); + ASSERT_TRUE (value.contains (true)); + ASSERT_TRUE (value.contains (int32_t (-2))); + ASSERT_TRUE (value.contains (uint32_t (3))); + ASSERT_TRUE (value.contains (int64_t (-4))); + ASSERT_TRUE (value.contains (uint64_t (5))); + ASSERT_TRUE (value.contains (6.5)); + ASSERT_TRUE (value.contains (std::string_view ("seven"))); + ASSERT_TRUE (value.contains ("seven")); + ASSERT_TRUE (value.contains (std::string ("seven"))); + ASSERT_FALSE (value.contains (false)); + ASSERT_FALSE (value.contains (std::string_view ("eight"))); + + value = Object{{"seven", 7}}; + ASSERT_FALSE (value.contains (nullptr)); + ASSERT_FALSE (value.contains (uint32_t (3))); + ASSERT_FALSE (value.contains (int64_t (-4))); + ASSERT_FALSE (value.contains (uint64_t (5))); + ASSERT_FALSE (value.contains (6.5)); + + value = Array{0, 1}; + ASSERT_TRUE (value.contains (int32_t (1))); + ASSERT_FALSE (value.contains (int32_t (2))); + ASSERT_FALSE (value.contains (int32_t (-1))); + ASSERT_TRUE (value.contains (uint32_t (1))); + ASSERT_FALSE (value.contains (uint32_t (2))); + ASSERT_TRUE (value.contains (int64_t (1))); + ASSERT_FALSE (value.contains (int64_t (2))); + ASSERT_FALSE (value.contains (int64_t (-1))); + ASSERT_TRUE (value.contains (uint64_t (1))); + ASSERT_FALSE (value.contains (uint64_t (2))); + ASSERT_THROW (value.contains (nullptr), std::bad_cast); + ASSERT_THROW (value.contains (true), std::bad_cast); + ASSERT_THROW (value.contains (1.0), std::bad_cast); } /** @@ -5121,6 +5395,457 @@ TEST (Value, swap) ASSERT_TRUE (other.isArray ()); } +/** + * @brief Test serialize method. + */ +TEST (Value, serialize) +{ + Value keys = Object{{nullptr, 0}, {true, 1}, {int32_t (-2), 2}, {uint32_t (3), 3}, + {int64_t (-4), 4}, {uint64_t (5), 5}, {6.5, 6}, {"seven", 7}}; + Value copy; + join::ValueBuilder builder (copy); + ASSERT_EQ (keys.serialize (builder), 0) << join::lastError.message (); + EXPECT_EQ (copy, keys); + + Value value; + value.pushBack (1); + value.pushBack (nullptr); + value.pushBack (true); + value.pushBack ("test"); + value.pushBack (1.23456789e-13); + + std::stringstream out; + JsonWriter jsonWriter (out); + ASSERT_EQ (value.serialize (jsonWriter), 0) << join::lastError.message (); + EXPECT_EQ (out.str (), "[1,null,true,\"test\",1.23456789e-13]"); + + out.str (""); + ASSERT_EQ (value.serialize (out), 0) << join::lastError.message (); + EXPECT_EQ (out.str (), "[1,null,true,\"test\",1.23456789e-13]"); + + std::stringstream indented; + JsonWriter indentedWriter (indented, 4); + ASSERT_EQ (value.serialize (indentedWriter), 0) << join::lastError.message (); + out.str (""); + ASSERT_EQ (value.serialize (out, 4), 0) << join::lastError.message (); + EXPECT_EQ (out.str (), indented.str ()); + + out.str (""); + PackWriter packWriter (out); + ASSERT_EQ (value.serialize (packWriter), 0) << join::lastError.message (); + EXPECT_EQ (out.str (), std::string ({'\x95', '\x01', '\xc0', '\xc3', '\xa4', '\x74', '\x65', '\x73', '\x74', '\xcb', + '\x3d', '\x41', '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'})); + + out.str (""); + ASSERT_EQ (value.serialize (out), 0) << join::lastError.message (); + EXPECT_EQ (out.str (), std::string ({'\x95', '\x01', '\xc0', '\xc3', '\xa4', '\x74', '\x65', '\x73', '\x74', '\xcb', + '\x3d', '\x41', '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'})); + + Counter counter; + ASSERT_EQ (value.serialize (counter), 0) << join::lastError.message (); + EXPECT_EQ (counter.values, 5U); + + value = Object{}; + value["numbers"] = Array{333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001}; + value["string"] = "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"/"; + value["literals"] = Array{nullptr, true, false}; + + out.str (""); + ASSERT_EQ (value.serialize (out), 0) << join::lastError.message (); + EXPECT_EQ (out.str (), + "{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"€$" + "\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}"); + + value = Object{{Array{1}, 1}}; + out.str (""); + ASSERT_EQ (value.serialize (packWriter), -1); + ASSERT_EQ (join::lastError, SaxErrc::InvalidKey); + ASSERT_EQ (out.str (), ""); + + value = Array{7}; + ASSERT_EQ (value.serialize (packWriter), 0) << join::lastError.message (); + ASSERT_EQ (out.str (), std::string ({'\x91', '\x07'})); + + FailingBuffer buffer; + std::ostream failing (&buffer); + JsonWriter failingWriter (failing); + value = Array{std::string (2000, 'a')}; + ASSERT_THROW (value.serialize (failingWriter), std::runtime_error); + value = Array{1, 2}; + ASSERT_EQ (value.serialize (failingWriter), 0) << join::lastError.message (); + ASSERT_EQ (buffer.written, "[1,2]"); +} + +/** + * @brief Test deserialize method. + */ +TEST (Value, deserialize) +{ + Value value; + ASSERT_EQ (value.deserialize (std::string ("[1,{\"a\":\"b\"}]")), 0) << join::lastError.message (); + ASSERT_EQ (value, (Array{1, Object{{"a", "b"}}})); + + const char json[] = "{\"c\":[true,null]}"; + ASSERT_EQ (value.deserialize (json, sizeof (json) - 1), 0) << join::lastError.message (); + ASSERT_EQ (value, (Object{{"c", Array{true, nullptr}}})); + + ASSERT_EQ (value.deserialize (&json[5], &json[sizeof (json) - 2]), 0) << join::lastError.message (); + ASSERT_EQ (value, (Array{true, nullptr})); + + std::stringstream stream ("42"); + ASSERT_EQ (value.deserialize (stream), 0) << join::lastError.message (); + ASSERT_EQ (value, 42); + + ASSERT_EQ (value.deserialize> (std::string ("/* c */ 1.5")), 0) + << join::lastError.message (); + ASSERT_EQ (value, 1.5); + + ASSERT_EQ (value.deserialize (std::string ("[1, [2, ")), -1); + ASSERT_EQ (value, 1.5); + + ASSERT_EQ (value.deserialize (std::string ({'\x92', '\x01', '\xa1', '\x61'})), 0) + << join::lastError.message (); + ASSERT_EQ (value, (Array{1, "a"})); + + ASSERT_EQ (value.deserialize (std::string ({'\x92', '\x01'})), -1); + ASSERT_EQ (join::lastError, SaxErrc::Truncated); + ASSERT_EQ (value, (Array{1, "a"})); +} + +/** + * @brief packRead method. + */ +TEST (Value, packRead) +{ + Value value; + + std::string str ({'\x95', '\x01', '\xc0', '\xc3', '\xa4', '\x74', '\x65', '\x73', '\x74', '\xcb', '\x3d', '\x41', + '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'}); + + ASSERT_NE (value.packRead (str), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + ASSERT_NE (value.packRead (str.c_str (), str.size ()), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + ASSERT_NE (value.packRead (str.data (), str.data () + str.size ()), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::stringstream sstream (str); + ASSERT_NE (value.packRead (sstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::istringstream isstream (str); + ASSERT_NE (value.packRead (isstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::ofstream out ("/tmp/fstream.pack", std::ios::binary); + out << str; + out.close (); + + std::fstream fstream ("/tmp/fstream.pack", std::ios::in | std::ios::binary); + ASSERT_NE (value.packRead (fstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + fstream.close (); + + std::ifstream ifstream ("/tmp/fstream.pack", std::ios::in | std::ios::binary); + ASSERT_NE (value.packRead (ifstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + ifstream.close (); + + std::stringbuf iosbuf (str, std::ios::in); + std::iostream iostream (&iosbuf); + ASSERT_NE (value.packRead (iostream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::stringbuf isbuf (str); + std::istream istream (&isbuf); + ASSERT_NE (value.packRead (istream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); +} + +/** + * @brief packWrite method. + */ +TEST (Value, packWrite) +{ + Value value; + value.pushBack (1); + value.pushBack (nullptr); + value.pushBack (true); + value.pushBack ("test"); + value.pushBack (1.23456789e-13); + + std::stringstream out; + ASSERT_NE (value.packWrite (out), -1) << join::lastError.message (); + EXPECT_EQ (out.str (), std::string ({'\x95', '\x01', '\xc0', '\xc3', '\xa4', '\x74', '\x65', '\x73', '\x74', '\xcb', + '\x3d', '\x41', '\x5f', '\xff', '\xe5', '\x3a', '\x68', '\x5d'})); +} + +/** + * @brief jsonRead method. + */ +TEST (Value, jsonRead) +{ + Value value; + + std::string str = "[1,null,true,\"test\",1.23456789e-13]"; + ASSERT_NE (value.jsonRead (str), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + ASSERT_NE (value.jsonRead (str.c_str (), str.size ()), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + ASSERT_NE (value.jsonRead (str.data (), str.data () + str.size ()), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::stringstream sstream (str); + ASSERT_NE (value.jsonRead (sstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::istringstream isstream (str); + ASSERT_NE (value.jsonRead (isstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::ofstream out ("/tmp/fstream.json"); + out << str; + out.close (); + + std::fstream fstream ("/tmp/fstream.json", std::ios::in); + ASSERT_NE (value.jsonRead (fstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + fstream.close (); + + std::ifstream ifstream ("/tmp/fstream.json"); + ASSERT_NE (value.jsonRead (ifstream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + ifstream.close (); + + std::stringbuf iosbuf (str, std::ios::in); + std::iostream iostream (&iosbuf); + ASSERT_NE (value.jsonRead (iostream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); + + std::stringbuf isbuf (str); + std::istream istream (&isbuf); + ASSERT_NE (value.jsonRead (istream), -1) << join::lastError.message (); + ASSERT_TRUE (value.isArray ()); + ASSERT_TRUE (value[0].isInt ()); + ASSERT_EQ (value[0], 1); + ASSERT_TRUE (value[1].isNull ()); + ASSERT_EQ (value[1], nullptr); + ASSERT_TRUE (value[2].isBool ()); + ASSERT_EQ (value[2], true); + ASSERT_TRUE (value[3].isString ()); + ASSERT_EQ (value[3], "test"); + ASSERT_TRUE (value[4].isDouble ()); + ASSERT_EQ (value[4], 1.23456789e-13); +} + +/** + * @brief jsonWrite method. + */ +TEST (Value, jsonWrite) +{ + Value value; + value.pushBack (1); + value.pushBack (nullptr); + value.pushBack (true); + value.pushBack ("test"); + value.pushBack (1.23456789e-13); + + std::stringstream out; + ASSERT_NE (value.jsonWrite (out), -1) << join::lastError.message (); + EXPECT_EQ (out.str (), "[1,null,true,\"test\",1.23456789e-13]"); +} + +/** + * @brief jsonCanonicalize method. + */ +TEST (Value, jsonCanonicalize) +{ + Value value; + value["numbers"] = Array{333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001}; + value["string"] = "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"/"; + value["literals"] = Array{nullptr, true, false}; + + std::stringstream out; + ASSERT_NE (value.jsonCanonicalize (out), -1) << join::lastError.message (); + EXPECT_EQ (out.str (), + "{\"literals\":[null,true,false],\"numbers\":[333333333.3333333,1e+30,4.5,0.002,1e-27],\"string\":\"€$" + "\\u000f\\nA'B\\\"\\\\\\\\\\\"/\"}"); +} + /** * @brief equal operators. */ diff --git a/fabric/include/join/dhcp.hpp b/fabric/include/join/dhcp.hpp index 24094e90..fa0ccc1c 100644 --- a/fabric/include/join/dhcp.hpp +++ b/fabric/include/join/dhcp.hpp @@ -260,8 +260,10 @@ namespace join if (::sendmsg (_socket.handle (), &message, 0) == -1) { - lastError = std::error_code (errno, std::generic_category ()); // LCOV_EXCL_LINE - return -1; // LCOV_EXCL_LINE + // LCOV_EXCL_START + lastError = std::error_code (errno, std::generic_category ()); + return -1; + // LCOV_EXCL_STOP } return 0; @@ -292,14 +294,18 @@ namespace join ssize_t size = ::recvmsg (_socket.handle (), &message, 0); if (size == -1) { - lastError = std::error_code (errno, std::generic_category ()); // LCOV_EXCL_LINE - return -1; // LCOV_EXCL_LINE + // LCOV_EXCL_START + lastError = std::error_code (errno, std::generic_category ()); + return -1; + // LCOV_EXCL_STOP } if (message.msg_flags & MSG_TRUNC) { - lastError = make_error_code (Errc::MessageTooLong); // LCOV_EXCL_LINE - return -1; // LCOV_EXCL_LINE + // LCOV_EXCL_START + lastError = make_error_code (Errc::MessageTooLong); + return -1; + // LCOV_EXCL_STOP } if (size < static_cast (sizeof (Frame) + DhcpMessage::headerSize)) diff --git a/fabric/src/arp.cpp b/fabric/src/arp.cpp index 1992c460..589bbd75 100644 --- a/fabric/src/arp.cpp +++ b/fabric/src/arp.cpp @@ -152,8 +152,10 @@ MacAddress Arp::cache (const IpAddress& ip) if (!(neighbor->state () & NUD_VALID)) { - lastError = std::make_error_code (std::errc::no_such_device_or_address); // LCOV_EXCL_LINE - return {}; // LCOV_EXCL_LINE + // LCOV_EXCL_START + lastError = std::make_error_code (std::errc::no_such_device_or_address); + return {}; + // LCOV_EXCL_STOP } return neighbor->mac ();