A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
nlohmann-json-test-suite.cc
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: GPL-2.0-only
3
*
4
* Author: Tom Henderson <tomh@tomh.org>
5
*/
6
7
#include "nlohmann/json.hpp"
8
9
#include "ns3/test.h"
10
11
#include <string>
12
13
/**
14
* @file
15
* @ingroup core-tests
16
* Smoke test for the vendored nlohmann/json third-party header.
17
*/
18
19
namespace
ns3
20
{
21
22
namespace
tests
23
{
24
25
/**
26
* @ingroup core-tests
27
* Parse a trivial JSON document and check that the expected fields
28
* round-trip correctly.
29
*/
30
class
NlohmannJsonParseTestCase
:
public
TestCase
31
{
32
public
:
33
NlohmannJsonParseTestCase
();
34
~NlohmannJsonParseTestCase
()
override
=
default
;
35
36
private
:
37
void
DoRun
()
override
;
38
};
39
40
NlohmannJsonParseTestCase::NlohmannJsonParseTestCase
()
41
:
TestCase
(
"Parse a trivial JSON document with nlohmann::json"
)
42
{
43
}
44
45
void
46
NlohmannJsonParseTestCase::DoRun
()
47
{
48
const
std::string input = R
"({
49
"name": "ns-3",
50
"version": 3,
51
"enabled": true,
52
"modules": ["core", "network", "internet"]
53
})";
54
55
nlohmann::json doc;
56
bool
parsed =
false
;
57
try
58
{
59
doc = nlohmann::json::parse(input);
60
parsed =
true
;
61
}
62
catch
(
const
nlohmann::json::parse_error&)
63
{
64
parsed =
false
;
65
}
66
NS_TEST_ASSERT_MSG_EQ
(parsed,
true
,
"nlohmann::json::parse threw on valid JSON"
);
67
68
NS_TEST_ASSERT_MSG_EQ
(doc.is_object(),
true
,
"Top-level value should be an object"
);
69
NS_TEST_ASSERT_MSG_EQ
(doc[
"name"
].get<std::string>(),
70
"ns-3"
,
71
"String field did not round-trip"
);
72
NS_TEST_ASSERT_MSG_EQ
(doc[
"version"
].get<int>(), 3,
"Integer field did not round-trip"
);
73
NS_TEST_ASSERT_MSG_EQ
(doc[
"enabled"
].get<bool>(),
true
,
"Boolean field did not round-trip"
);
74
NS_TEST_ASSERT_MSG_EQ
(doc[
"modules"
].is_array(),
true
,
"Array field should be an array"
);
75
NS_TEST_ASSERT_MSG_EQ
(doc[
"modules"
].size(),
76
static_cast<
std::size_t
>
(3),
77
"Array field has unexpected length"
);
78
NS_TEST_ASSERT_MSG_EQ
(doc[
"modules"
][0].get<std::string>(),
79
"core"
,
80
"Array element did not round-trip"
);
81
}
82
83
/**
84
* @ingroup core-tests
85
* nlohmann/json test suite.
86
*/
87
class
NlohmannJsonTestSuite
:
public
TestSuite
88
{
89
public
:
90
NlohmannJsonTestSuite
();
91
};
92
93
NlohmannJsonTestSuite::NlohmannJsonTestSuite
()
94
:
TestSuite
(
"nlohmann-json"
)
95
{
96
AddTestCase
(
new
NlohmannJsonParseTestCase
);
97
}
98
99
/**
100
* @ingroup core-tests
101
* NlohmannJsonTestSuite instance variable.
102
*/
103
static
NlohmannJsonTestSuite
g_nlohmannJsonTestSuite
;
104
105
}
// namespace tests
106
107
}
// namespace ns3
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:296
ns3::TestCase::TestCase
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1264
ns3::TestSuite::TestSuite
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition
test.cc:494
ns3::tests::NlohmannJsonParseTestCase
Parse a trivial JSON document and check that the expected fields round-trip correctly.
Definition
nlohmann-json-test-suite.cc:31
ns3::tests::NlohmannJsonParseTestCase::NlohmannJsonParseTestCase
NlohmannJsonParseTestCase()
Definition
nlohmann-json-test-suite.cc:40
ns3::tests::NlohmannJsonParseTestCase::~NlohmannJsonParseTestCase
~NlohmannJsonParseTestCase() override=default
ns3::tests::NlohmannJsonParseTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
nlohmann-json-test-suite.cc:46
ns3::tests::NlohmannJsonTestSuite
nlohmann/json test suite.
Definition
nlohmann-json-test-suite.cc:83
ns3::tests::NlohmannJsonTestSuite::NlohmannJsonTestSuite
NlohmannJsonTestSuite()
Definition
nlohmann-json-test-suite.cc:88
ns3::tests::g_nlohmannJsonTestSuite
static NlohmannJsonTestSuite g_nlohmannJsonTestSuite
NlohmannJsonTestSuite instance variable.
Definition
nlohmann-json-test-suite.cc:98
NS_TEST_ASSERT_MSG_EQ
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition
test.h:133
ns3::tests
Namespace for test files, TestCases and TestSuites.
Definition
environment-variable.h:32
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
test
nlohmann-json-test-suite.cc
Generated on
for ns-3 by
1.16.1