Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_json.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <fc/io/json.hpp>
#include <fc/exception/exception.hpp>
Include dependency graph for test_json.cpp:

Go to the source code of this file.

Namespaces

namespace  json_test_util
 

Macros

#define BOOST_TEST_MODULE   io_json
 

Functions

const std::string json_test_util::repeat_chars (repeat_char_num, 'a')
 
 BOOST_AUTO_TEST_CASE (to_string_test)
 
 BOOST_AUTO_TEST_CASE (to_pretty_string_test)
 
 BOOST_AUTO_TEST_CASE (escape_string_test)
 

Variables

constexpr size_t json_test_util::exception_limit_size = 250
 
const json::yield_function_t json_test_util::yield_deadline_exception_at_start
 
const json::yield_function_t json_test_util::yield_deadline_exception_in_mid
 
const json::yield_function_t json_test_util::yield_length_exception
 
const json::yield_function_t json_test_util::yield_no_limitation
 
const auto json_test_util::time_except_verf_func
 
const auto json_test_util::length_limit_except_verf_func
 
constexpr size_t json_test_util::repeat_char_num = 512
 
const string json_test_util::escape_input_str
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   io_json

Definition at line 1 of file test_json.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/3]

BOOST_AUTO_TEST_CASE ( escape_string_test )

Definition at line 199 of file test_json.cpp.

200{
201 std::string escape_out_str;
204 BOOST_CHECK_LT(json_test_util::escape_input_str.size() - json_test_util::repeat_char_num, json_test_util::exception_limit_size); // by using size_different to calculate expected string
205 {
206 // simulate exceed time exception at the beginning of processing
208 fc::timeout_exception,
210 }
211 {
212 // simulate exceed time exception in the middle of processing
214 fc::timeout_exception,
216 }
217 {
218 // length limitation exception in the middle of processing
220 fc::assert_exception,
222 }
223}
std::string escape_string(const std::string_view &str, const json::yield_function_t &yield, bool escape_control_chars=true)
Definition json.cpp:495
const json::yield_function_t yield_deadline_exception_in_mid
Definition test_json.cpp:18
const json::yield_function_t yield_deadline_exception_at_start
Definition test_json.cpp:14
const json::yield_function_t yield_no_limitation
Definition test_json.cpp:28
const json::yield_function_t yield_length_exception
Definition test_json.cpp:24
const auto length_limit_except_verf_func
Definition test_json.cpp:36
constexpr size_t exception_limit_size
Definition test_json.cpp:12
const auto time_except_verf_func
Definition test_json.cpp:32
const string escape_input_str
Definition test_json.cpp:42
constexpr size_t repeat_char_num
Definition test_json.cpp:40
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/3]

BOOST_AUTO_TEST_CASE ( to_pretty_string_test )

Definition at line 123 of file test_json.cpp.

124{
125 { // to_pretty_string( const variant& v, const fc::time_point& deadline, const uint64_t max_len = max_length_limit, output_formatting format = stringify_large_ints_and_doubles );
126 {
128 std::string deadline_exception_at_start_str;
129 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_pretty_string( v, fc::time_point::min(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit),
130 fc::timeout_exception,
132 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
133 }
134 {
135 constexpr size_t max_len = 10;
137 std::string deadline_exception_at_start_str;
138 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
139 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_pretty_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, max_len),
140 fc::assert_exception,
142 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
143 }
144 {
146 std::string length_exception_in_mid_str;
147 BOOST_CHECK_NO_THROW(length_exception_in_mid_str = json::to_pretty_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit));
148 BOOST_CHECK_EQUAL(length_exception_in_mid_str, "\"" + json_test_util::repeat_chars + "\"");
149 }
150 }
151 { // to_pretty_string( const variant& v, const yield_function_t& yield, output_formatting format = stringify_large_ints_and_doubles );
154 {
155 std::string deadline_exception_at_start_str;
156 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_pretty_string( v, json_test_util::yield_deadline_exception_at_start),
157 fc::timeout_exception,
159 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
160 }
161 {
162 std::string deadline_exception_in_mid_str;
163 BOOST_CHECK_EXCEPTION(deadline_exception_in_mid_str = json::to_pretty_string( v, json_test_util::yield_deadline_exception_in_mid),
164 fc::timeout_exception,
166 BOOST_CHECK_EQUAL(deadline_exception_in_mid_str.empty(), true);
167 }
168 {
169 std::string length_exception_in_mid_str;
170 BOOST_CHECK_EXCEPTION(length_exception_in_mid_str = json::to_pretty_string( v, json_test_util::yield_length_exception),
171 fc::assert_exception,
173 BOOST_CHECK_EQUAL(length_exception_in_mid_str.empty(), true);
174 }
175 {
176 std::string no_exception_str;
177 BOOST_CHECK_NO_THROW(no_exception_str = json::to_pretty_string( v, json_test_util::yield_no_limitation));
178 BOOST_CHECK_EQUAL(no_exception_str, "\"" + json_test_util::repeat_chars + "\"");
179 }
180 }
181 { // to_pretty_string template call
182 const uint16_t id = 1000;
183 const uint64_t len = 3;
184 const std::string id_ret_1 = json::to_pretty_string( id, fc::time_point::maximum());
185 BOOST_CHECK_EQUAL(std::to_string(id), id_ret_1);
186
187 // exceed length
188 std::string id_ret_2;
189 BOOST_REQUIRE_THROW(id_ret_2 = json::to_pretty_string( id, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, len), fc::assert_exception);
190 BOOST_CHECK_EQUAL(id_ret_2.empty(), true);
191
192 // time_out
193 std::string id_ret_3;
194 BOOST_REQUIRE_THROW(id_ret_3 = json::to_pretty_string( id, fc::time_point::now() - fc::milliseconds(1) ), fc::timeout_exception);
195 BOOST_CHECK_EQUAL(id_ret_3.empty(), true);
196 }
197}
static constexpr time_point min()
Definition time.hpp:47
static time_point now()
Definition time.cpp:14
static constexpr time_point maximum()
Definition time.hpp:46
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
constexpr microseconds milliseconds(int64_t s)
Definition time.hpp:33
const std::string repeat_chars(repeat_char_num, 'a')
unsigned short uint16_t
Definition stdint.h:125
unsigned __int64 uint64_t
Definition stdint.h:136
size_t len
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/3]

BOOST_AUTO_TEST_CASE ( to_string_test )

Definition at line 47 of file test_json.cpp.

48{
49 { // to_string( const variant& v, const fc::time_point& deadline, const uint64_t max_len = max_length_limit, output_formatting format = stringify_large_ints_and_doubles);
50 {
52 std::string deadline_exception_at_start_str;
53 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_string( v, fc::time_point::min(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit),
54 fc::timeout_exception,
56 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
57 }
58 {
59 constexpr size_t max_len = 10;
61 std::string deadline_exception_at_start_str;
62 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
63 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, max_len),
64 fc::assert_exception,
66 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
67 }
68 {
70 std::string length_exception_in_mid_str;
71 BOOST_CHECK_NO_THROW(length_exception_in_mid_str = json::to_string( v, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, json::max_length_limit));
72 BOOST_CHECK_EQUAL(length_exception_in_mid_str, "\"" + json_test_util::repeat_chars + "\"");
73 }
74 }
75 { // to_string( const variant& v, const yield_function_t& yield, output_formatting format = stringify_large_ints_and_doubles);
78 {
79 std::string deadline_exception_at_start_str;
80 BOOST_CHECK_EXCEPTION(deadline_exception_at_start_str = json::to_string( v, json_test_util::yield_deadline_exception_at_start),
81 fc::timeout_exception,
83 BOOST_CHECK_EQUAL(deadline_exception_at_start_str.empty(), true);
84 }
85 {
86 std::string deadline_exception_in_mid_str;
87 BOOST_CHECK_EXCEPTION(deadline_exception_in_mid_str = json::to_string( v, json_test_util::yield_deadline_exception_in_mid),
88 fc::timeout_exception,
90 BOOST_CHECK_EQUAL(deadline_exception_in_mid_str.empty(), true);
91 }
92 {
93 std::string length_exception_in_mid_str;
94 BOOST_CHECK_EXCEPTION(length_exception_in_mid_str = json::to_string( v, json_test_util::yield_length_exception),
95 fc::assert_exception,
97 BOOST_CHECK_EQUAL(length_exception_in_mid_str.empty(), true);
98 }
99 {
100 std::string no_exception_str;
101 BOOST_CHECK_NO_THROW(no_exception_str = json::to_string( v, json_test_util::yield_no_limitation));
102 BOOST_CHECK_EQUAL(no_exception_str, "\"" + json_test_util::repeat_chars + "\"");
103 }
104 }
105 { // to_string template call
106 const uint16_t id = 1000;
107 const uint64_t len = 3;
108 const std::string id_ret_1 = json::to_string( id, fc::time_point::maximum());
109 BOOST_CHECK_EQUAL(std::to_string(id), id_ret_1);
110
111 // exceed length
112 std::string id_ret_2;
113 BOOST_REQUIRE_THROW(id_ret_2 = json::to_string( id, fc::time_point::maximum(), json::output_formatting::stringify_large_ints_and_doubles, len), fc::assert_exception);
114 BOOST_CHECK_EQUAL(id_ret_2.empty(), true);
115
116 // time_out
117 std::string id_ret_3;
118 BOOST_REQUIRE_THROW(id_ret_3 = json::to_string( id, fc::time_point::now() - fc::milliseconds(1) ), fc::timeout_exception);
119 BOOST_CHECK_EQUAL(id_ret_3.empty(), true);
120 }
121}
Here is the call graph for this function: