Skip to content

Commit 305e61c

Browse files
committed
[cjson] Bugfixes and GTest unittest
1 parent ae28d03 commit 305e61c

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

constexpr_json/include/constexpr_json/ext/printing.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ struct PrinterBase {
193193
constexpr char aDecSep = '.';
194194
using IntMantissaTy = size_t;
195195

196+
if (theNumber < 0) {
197+
aStream = (aStream << '-');
198+
theNumber *= -1.;
199+
}
200+
196201
// determine highest power of 10 fitting into theNumber
197202
IntMantissaTy aPowRadix = 1;
198203
while (theNumber >= aPowRadix)

constexpr_json/include/constexpr_json/impl/document_access.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ template <typename DocumentTy> struct EntityRefImpl {
155155
}
156156
return false;
157157
}
158+
template <typename OtherRefTy>
159+
constexpr bool operator!=(const OtherRefTy &theOther) const noexcept {
160+
return !(*this == theOther);
161+
}
158162

159163
constexpr Entity::KIND getType() const { return itsEntity->itsKind; }
160164
constexpr bool toBool() const { return itsEntity->itsPayload; }

constexpr_json/include/constexpr_json/impl/document_allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct DocumentAllocator : private DocumentInfo {
105105
case Entity::NUL:
106106
return Entity{Entity::NUL, 0};
107107
case Entity::BOOL:
108-
return Entity{Entity::NUL, theJson.toBool()};
108+
return Entity{Entity::BOOL, theJson.toBool()};
109109
case Entity::NUMBER:
110110
return allocateNumber(theDoc, theJson.toNumber());
111111
case Entity::STRING:

constexpr_json/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
add_executable(cjson_basic basic.cc)
2-
target_link_libraries(cjson_basic PRIVATE constexpr_json)
3-
add_test(NAME cjson_basic COMMAND cjson_basic)
2+
target_link_libraries(cjson_basic PRIVATE constexpr_json gtest_main)
3+
gtest_discover_tests(cjson_basic)
44

55
add_executable(cjson_static_unittests static_unittests.cc)
66
target_link_libraries(cjson_static_unittests PRIVATE constexpr_json)

constexpr_json/test/basic.cc

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,49 @@
22
#include "constexpr_json/dynamic_document.h"
33
#include "constexpr_json/static_document.h"
44

5-
#include <cassert>
5+
#include <gtest/gtest.h>
66

7-
int main() {
8-
using namespace cjson;
7+
using namespace cjson;
8+
9+
TEST(cjson_basic, static_doc) {
910
constexpr std::string_view aJsonStr{"1234"};
1011
constexpr auto aDocInfo = DocumentParser<>::computeDocInfo(aJsonStr);
11-
if (!aDocInfo)
12-
return 1;
12+
ASSERT_TRUE(aDocInfo);
1313
// Static json object creation
1414
using StaticDocTy = CJSON_STATIC_DOCTY(*aDocInfo);
1515
constexpr auto aDocStatic =
1616
DocumentParser<>::parseDocument<StaticDocTy>(aJsonStr, aDocInfo);
1717
static_assert(aDocStatic);
1818
static_assert(aDocStatic->getRoot().toNumber() == 1234.);
19+
}
1920

21+
TEST(cjson_basic, dynamic_doc) {
2022
// Dynamic json object creation
21-
using DynamicDocTy = DynamicDocument;
22-
auto aDocDynamic =
23-
DocumentParser<>::parseDocument<DynamicDocTy>(aJsonStr, aDocInfo);
24-
assert(aDocDynamic);
25-
assert(aDocDynamic->getRoot().toNumber() == 1234.);
23+
auto aDocDynamic = DynamicDocument::parseJson("1234");
24+
ASSERT_TRUE(aDocDynamic);
25+
ASSERT_TRUE(*aDocDynamic);
26+
EXPECT_EQ((*aDocDynamic)->getRoot().toNumber(), 1234.);
27+
}
2628

27-
return 0;
29+
TEST(cjson_basic, access_equal) {
30+
// Dynamic json object creation
31+
const std::string_view aJson(R"{"}""{"}(
32+
{
33+
"a": 1,
34+
"b": [123],
35+
"c": {
36+
"x": true,
37+
"y": false,
38+
"z": null
39+
}
40+
}
41+
){"}""{"}");
42+
auto aDoc1 = DynamicDocument::parseJson(aJson);
43+
auto aDoc2 = DynamicDocument::parseJson(aJson);
44+
ASSERT_TRUE(aDoc1 && *aDoc1);
45+
ASSERT_TRUE(aDoc2 && *aDoc2);
46+
const auto aRoot1 = (*aDoc1)->getRoot();
47+
const auto aRoot2 = (*aDoc2)->getRoot();
48+
EXPECT_EQ(aRoot1, aRoot2);
49+
EXPECT_FALSE(aRoot1 != aRoot2);
2850
}

0 commit comments

Comments
 (0)