Skip to content

Commit f0e7a7c

Browse files
committed
Add testing
1 parent feadf0e commit f0e7a7c

File tree

13 files changed

+441
-58
lines changed

13 files changed

+441
-58
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ obj*
66
bin*
77
*.zip
88
*.exe
9-
.bash_history
9+
.bash_history
10+
json.hpp

cpp/makefile

Lines changed: 0 additions & 17 deletions
This file was deleted.

cpp/test.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

cpp/testing/beautifier.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <jsonlite2.hpp>
2+
3+
#include <fstream>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
int main(int argc, char ** argv)
8+
{
9+
if (argc < 2)
10+
{
11+
std::cerr << "Filename not given!\n";
12+
return 1;
13+
}
14+
15+
std::ifstream inp(argv[1]);
16+
if (!inp.good())
17+
{
18+
std::cerr << "File error!\n";
19+
return 1;
20+
}
21+
22+
std::stringstream ss;
23+
ss << inp.rdbuf();
24+
inp.close();
25+
26+
std::cout << jsonlite2::json::parse(ss.str()).dump() << std::flush;
27+
28+
return 0;
29+
}

cpp/testing/makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export CPLUS_INCLUDE_PATH=../include
2+
3+
CXX=g++
4+
5+
CDEFFLAGS=-std=c++11 -Wall -Wextra -Wpedantic
6+
CFLAGS=-O3 -Wl,--strip-all,--build-id=none,--gc-sections -fno-ident
7+
CDEBFLAGS=-g -O0
8+
9+
TARGET=test
10+
11+
default: debug
12+
13+
debug: test.cpp
14+
$(CXX) $^ -o deb$(TARGET).exe $(CDEFFLAGS) $(CDEBFLAGS)
15+
16+
release: test.cpp
17+
$(CXX) $^ -o $(TARGET).exe $(CDEFFLAGS) $(CFLAGS)
18+
19+
beau: beautifier.cpp
20+
$(CXX) $^ -o beautifier.exe $(CDEFFLAGS) $(CFLAGS)

cpp/testing/test.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <jsonlite2.hpp>
2+
#include "json.hpp"
3+
4+
#include <iostream>
5+
#include <fstream>
6+
#include <sstream>
7+
8+
#include <cstring>
9+
#include <ctime>
10+
11+
template<typename T, typename Func>
12+
void test(const std::string & contents, const std::string & what, std::size_t maxIter, T, Func function)
13+
{
14+
T obj;
15+
auto start = std::clock();
16+
for (std::size_t i = 0; i < maxIter; ++i)
17+
{
18+
obj = function(contents);
19+
}
20+
auto stop = std::clock();
21+
std::cout << "Time elapsed (" << what << "):\t" << double(stop - start) / double(CLOCKS_PER_SEC) << " seconds.\n";
22+
}
23+
24+
int main(int argc, char ** argv)
25+
{
26+
if (argc < 2)
27+
{
28+
std::cerr << "Filename not given!\n";
29+
return 1;
30+
}
31+
else if (argc < 3)
32+
{
33+
std::cerr << "Iteration count not given!\n";
34+
return 1;
35+
}
36+
37+
std::vector<const char *> filenames;
38+
for (int i = 1; i < (argc - 1); ++i)
39+
{
40+
filenames.emplace_back(argv[i]);
41+
}
42+
43+
auto MAX_ITER = std::strtoul(argv[argc - 1], nullptr, 10);
44+
45+
for (auto i : filenames)
46+
{
47+
std::cout << "Testing file \"" << i << "\":\n";
48+
std::ifstream inp(i);
49+
if (!inp.good())
50+
{
51+
std::cerr << "Error opening file!" << std::endl;
52+
return 1;
53+
}
54+
55+
std::stringstream ss;
56+
ss << inp.rdbuf();
57+
58+
inp.close();
59+
60+
std::string contents = ss.str();
61+
62+
std::cout << "Contents length: " << contents.length() << " bytes\n";
63+
64+
test(contents, "jsonlite2", MAX_ITER, jsonlite2::json(), [](const std::string & cont){ return jsonlite2::json::parse(cont); });
65+
test(contents, "nlohmann::json", MAX_ITER, nlohmann::json(), [](const std::string & cont){ return nlohmann::json(cont); });
66+
67+
std::cout << '\n';
68+
}
69+
70+
return 0;
71+
}
File renamed without changes.

cpp/testing/test2.json

Whitespace-only changes.

cpp/testing/test3.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"tabsToSpaces": true,
3+
"tabWidth": 4,
4+
"autoindent": true
5+
}

cpp/testing/test4.json

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
{
2+
"tabsToSpaces": true,
3+
"tabWidth": 4,
4+
"autoindent": true
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+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+

0 commit comments

Comments
 (0)