-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
133 lines (113 loc) · 3.9 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "leptjsoncpp.h"
#include <iostream>
#include <string>
using namespace lept;
using namespace std;
#define TEST_NUMBER(expect, json) \
do {\
LeptValue v;\
LeptContext j(json);\
REQUIRE(PARSE_OK == LeptParser::Parse(v, j));\
REQUIRE(LeptValue::LEPT_NUMBER == v.GetType());\
REQUIRE(expect == v.GetNumber());\
}while(0)
#define TEST_ERROR(error, json) \
do {\
LeptValue v;\
LeptContext j(json);\
REQUIRE(error == LeptParser::Parse(v, j));\
REQUIRE(LeptValue::LEPT_NULL == v.GetType());\
}while(0)
#define TEST_INVALID(json) \
TEST_ERROR(PARSE_INVALID_VALUE, json)
#define TEST_EXPECT(json) \
TEST_ERROR(PARSE_EXPECT_VALUE, json)
#define TEST_NOT_SINGULAR(json) \
TEST_ERROR(PARSE_ROOT_NOT_SINGULAR, json)
TEST_CASE("TestParse") {
SECTION("parse literal") {
LeptValue v(LeptValue::LEPT_FALSE);
LeptContext json("null");
REQUIRE(PARSE_OK == LeptParser::Parse(v, json));
REQUIRE(LeptValue::LEPT_NULL == v.GetType());
json = "true";
REQUIRE(PARSE_OK == LeptParser::Parse(v, json));
REQUIRE(LeptValue::LEPT_TRUE == v.GetType());
json = "false";
REQUIRE(PARSE_OK == LeptParser::Parse(v, json));
REQUIRE(LeptValue::LEPT_FALSE == v.GetType());
}
SECTION("parse expect value") {
TEST_EXPECT("");
TEST_EXPECT(" ");
}
SECTION("parse invalid value") {
TEST_INVALID("nul");
TEST_INVALID("?");
/* invalid number */
TEST_INVALID("+0");
TEST_INVALID("+1");
TEST_INVALID(".123");
TEST_INVALID("1.");
TEST_INVALID("INF");
TEST_INVALID("inf");
TEST_INVALID("NAN");
TEST_INVALID("nan");
}
SECTION("parse root not singular") {
TEST_NOT_SINGULAR("null x");
/* invalid number */
TEST_NOT_SINGULAR("0123");
TEST_NOT_SINGULAR("0x0");
TEST_NOT_SINGULAR("0x123");
}
}
TEST_CASE("TestNumber") {
SECTION("simple number"){
TEST_NUMBER(0.0, "0");
TEST_NUMBER(0.0, "-0");
TEST_NUMBER(0.0, "-0.0");
TEST_NUMBER(1.0, "1");
TEST_NUMBER(-1.0, "-1");
TEST_NUMBER(1.5, "1.5");
TEST_NUMBER(-1.5, "-1.5");
TEST_NUMBER(3.1416, "3.1416");
TEST_NUMBER(1E10, "1E10");
TEST_NUMBER(1e10, "1e10");
TEST_NUMBER(1E+10, "1E+10");
TEST_NUMBER(1E-10, "1E-10");
TEST_NUMBER(-1E10, "-1E10");
TEST_NUMBER(-1e10, "-1e10");
TEST_NUMBER(-1E+10, "-1E+10");
TEST_NUMBER(-1E-10, "-1E-10");
TEST_NUMBER(1.234E+10, "1.234E+10");
TEST_NUMBER(1.234E-10, "1.234E-10");
TEST_NUMBER(0.0, "1e-10000"); /* must underflow */
TEST_NUMBER(1.0000000000000002, "1.0000000000000002"); /* the smallest number > 1 */
TEST_NUMBER( 4.9406564584124654e-324, "4.9406564584124654e-324"); /* minimum denormal */
TEST_NUMBER(-4.9406564584124654e-324, "-4.9406564584124654e-324");
TEST_NUMBER( 2.2250738585072009e-308, "2.2250738585072009e-308"); /* Max subnormal double */
TEST_NUMBER(-2.2250738585072009e-308, "-2.2250738585072009e-308");
TEST_NUMBER( 2.2250738585072014e-308, "2.2250738585072014e-308"); /* Min normal positive double */
TEST_NUMBER(-2.2250738585072014e-308, "-2.2250738585072014e-308");
TEST_NUMBER( 1.7976931348623157e+308, "1.7976931348623157e+308"); /* Max double */
TEST_NUMBER(-1.7976931348623157e+308, "-1.7976931348623157e+308");
}
}
TEST_CASE("LeptStack")
{
SECTION("basic")
{
LeptStack s;
*s.Push<int>() = 1;
*s.Push<string>() = string("hello");
*s.Push<char>() = 'a';
REQUIRE(*s.Pop<char>() == 'a');
string *str = s.Pop<string>();
REQUIRE(*str == string("hello"));
str->~basic_string();
REQUIRE(*s.Pop<int>() == 1);
}
}