-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
596 lines (518 loc) · 20 KB
/
main.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <iostream>
//#include "adapters/stl.h"
#define CPPDATALIB_FAST_IO
//#define CPPDATALIB_DISABLE_WRITE_CHECKS
#define CPPDATALIB_DISABLE_FAST_IO_GCOUNT
#define CPPDATALIB_OPTIMIZE_FOR_NUMERIC_SPACE
#include "cppdatalib.h"
struct vt100
{
const char * const reset_term = "\033c";
const char * const enable_lwrap = "\033[7h";
const char * const disable_lwrap = "\033[7l";
const char * const default_font = "\033(";
const char * const alternate_font = "\033)";
const char * const move_cursor_screen_home = "\033[H"; // Moves to upper-left of screen, not beginning of line
const char * const move_cursor_home = "\r"; // Moves to beginning of line, not beginning of screen
std::string move_cursor(int row, int col)
{
return "\033[" + std::to_string(row) + ';' + std::to_string(col) + 'H';
}
const char * const force_move_cursor_home = "\033[f";
std::string force_move_cursor(int row, int col)
{
return "\033[" + std::to_string(row) + ';' + std::to_string(col) + 'f';
}
const char * const move_cursor_up = "\033[A";
std::string move_cursor_up_by(int rows)
{
return "\033[" + std::to_string(rows) + 'A';
}
const char * const move_cursor_down = "\033[B";
std::string move_cursor_down_by(int rows)
{
return "\033[" + std::to_string(rows) + 'B';
}
const char * const move_cursor_right = "\033[C";
std::string move_cursor_right_by(int cols)
{
return "\033[" + std::to_string(cols) + 'C';
}
const char * const move_cursor_left = "\033[D";
std::string move_cursor_left_by(int cols)
{
return "\033[" + std::to_string(cols) + 'D';
}
const char * const save_cursor = "\033[s";
const char * const restore_cursor = "\033[u";
const char * const save_cursor_and_attrs = "\0337";
const char * const restore_cursor_and_attrs = "\0338";
const char * const enable_scroll = "\033[r";
const char * const scroll_screen_down = "\033D";
const char * const scroll_screen_up = "\033M";
std::string scroll_range(int from, int to)
{
return "\033[" + std::to_string(from) + ';' + std::to_string(to) + 'r';
}
const char * const set_tab = "\033H";
const char * const unset_tab = "\033[g";
const char * const unset_all_tabs = "\033[3g";
const char * const erase_to_end_of_line = "\033[K";
const char * const erase_to_start_of_line = "\033[1K";
const char * const erase_line = "\033[2K";
const char * const erase_screen_down = "\033[J";
const char * const erase_screen_up = "\033[1J";
const char * const erase_screen = "\033[2J";
const char * const attr_reset = "\033[0m";
const char * const attr_bright = "\033[1m";
const char * const attr_dim = "\033[2m";
const char * const attr_underscore = "\033[4m";
const char * const attr_blink = "\033[5m";
const char * const attr_reverse = "\033[7m";
const char * const attr_hidden = "\033[8m";
const char * const black = "\033[30m";
const char * const red = "\033[31m";
const char * const green = "\033[32m";
const char * const yellow = "\033[33m";
const char * const blue = "\033[34m";
const char * const magenta = "\033[35m";
const char * const cyan = "\033[36m";
const char * const white = "\033[37m";
const char * const bg_black = "\033[40m";
const char * const bg_red = "\033[41m";
const char * const bg_green = "\033[42m";
const char * const bg_yellow = "\033[43m";
const char * const bg_blue = "\033[44m";
const char * const bg_magenta = "\033[45m";
const char * const bg_cyan = "\033[46m";
const char * const bg_white = "\033[47m";
};
template<typename T>
std::ostream &operator<<(std::ostream &strm, const std::vector<T> &value)
{
strm << "[";
for (auto i = value.begin(); i != value.end(); ++i)
{
if (i != value.begin())
strm << " ";
strm << *i;
}
return strm << "]" << std::flush;
}
template<typename F, typename S = F>
struct TestData : public std::vector<std::pair<F, S>>
{
using std::vector<std::pair<F, S>>::vector;
};
// `tests` is a vector of test cases. The test cases will be normal (first item in tuple is parameter, second is result)
// ResultCode is a functor that should return the actual test result (whether valid or not)
template<typename F, typename S, typename ResultCode, typename Compare = std::not_equal_to<S>>
bool Test(const char *name, const TestData<F, S> &tests, ResultCode actual, bool bail_early = true, Compare compare = Compare())
{
vt100 vt;
size_t percent = 0;
size_t current = 0;
size_t failed = 0;
std::cout << "Testing " << name << "... " << vt.yellow << "0%" << std::flush;
std::cout << vt.attr_reset;
for (const auto &test: tests)
{
if (compare(test.second, actual(test.first)))
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << std::flush;
std::cout << vt.red;
std::cout << "Test " << (current+1) << " FAILED!\n";
std::cout << "\tInput: " << test.first << "\n";
std::cout << "\tExpected output: " << test.second << "\n";
std::cout << "\tActual output: " << actual(test.first) << std::endl;
std::cout << vt.attr_reset;
if (bail_early)
return false;
++failed;
}
if (current * 100 / tests.size() > percent)
{
percent = current * 100 / tests.size();
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.yellow << percent << "%" << std::flush;
std::cout << vt.attr_reset;
}
++current;
}
if (!failed)
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.green << "done." << std::endl;
std::cout << vt.attr_reset;
}
else
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.red << "done. (" << failed << " failed out of " << tests.size() << ")" << std::endl;
std::cout << vt.attr_reset;
}
return failed;
}
// `tests` is a vector of test cases. The test cases will be reversed (first item in tuple is result, second is parameter)
// ResultCode is a functor that should return the actual test result (whether valid or not)
template<typename F, typename S, typename ResultCode, typename Compare = std::not_equal_to<S>>
bool ReverseTest(const char *name, const TestData<F, S> &tests, ResultCode actual, bool bail_early = true, Compare compare = Compare())
{
vt100 vt;
size_t percent = 0;
size_t current = 0;
uintmax_t failed = 0;
std::cout << "Testing " << name << "... " << vt.yellow << "0%" << std::flush;
std::cout << vt.attr_reset;
for (const auto &test: tests)
{
if (compare(test.first, actual(test.second)))
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << std::flush;
std::cout << vt.red;
std::cout << "Test " << (current+1) << " FAILED!\n";
std::cout << "\tInput: " << test.second << "\n";
std::cout << "\tExpected output: " << test.first << "\n";
std::cout << "\tActual output: " << actual(test.second) << std::endl;
std::cout << vt.attr_reset;
if (bail_early)
return false;
++failed;
}
if (current * 100 / tests.size() > percent)
{
percent = current * 100 / tests.size();
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.yellow << percent << "%" << std::flush;
std::cout << vt.attr_reset;
}
++current;
}
if (!failed)
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.green << "done." << std::endl;
std::cout << vt.attr_reset;
}
else
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.red << "done. (" << failed << " failed out of " << tests.size() << ")" << std::endl;
std::cout << vt.attr_reset;
}
return failed;
}
// `tests` is the maximum of a range, 0 -> tests, each element of which is passed to the test code
// ExpectedCode is a functor that should return the expected, valid, test result
// ResultCode is a functor that should return the actual test result (whether valid or not)
template<typename ExpectedCode, typename ResultCode, typename Compare>
bool TestRange(const char *name, uintmax_t tests, ExpectedCode expected, ResultCode actual, bool bail_early = true, Compare compare = Compare())
{
vt100 vt;
size_t percent = 0;
uintmax_t failed = 0;
std::cout << "Testing " << name << "... " << vt.yellow << "0%" << std::flush;
std::cout << vt.attr_reset;
for (uintmax_t test = 0; test < tests; ++test)
{
if (compare(expected(test), actual(test)))
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << std::flush;
std::cout << vt.red;
std::cout << "Test " << (test+1) << " FAILED!\n";
std::cout << "\tInput: " << test << "\n";
std::cout << "\tExpected output: " << expected(test) << "\n";
std::cout << "\tActual output: " << actual(test) << std::endl;
std::cout << vt.attr_reset;
if (bail_early)
return false;
++failed;
}
if (test * 100 / tests > percent)
{
percent = test * 100 / tests;
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.yellow << percent << "%" << std::flush;
std::cout << vt.attr_reset;
}
}
if (!failed)
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.green << "done." << std::endl;
std::cout << vt.attr_reset;
}
else
{
std::cout << vt.erase_line << vt.move_cursor_home << vt.attr_reset;
std::cout << "Testing " << name << "... " << vt.red << "done. (" << failed << " failed out of " << tests << ")" << std::endl;
std::cout << vt.attr_reset;
}
return failed;
}
TestData<std::string> debug_hex_encode_tests = {
{"Hello World!", "H e l l o W o r l d ! "},
{"A", "A "},
{"1234", "1 2 3 4 "},
{"..\1\n", ". . 01 0A "},
{"", ""}
};
TestData<std::string> hex_encode_tests = {
{"Hello World!", "48656C6C6F20576F726C6421"},
{"A", "41"},
{"1234", "31323334"},
{"..\1\n", "2E2E010A"},
{"", ""}
};
TestData<std::string> base64_encode_tests = {
{"Hello World!", "SGVsbG8gV29ybGQh"},
{"A", "QQ=="},
{"1234", "MTIzNA=="},
{"..\1\n", "Li4BCg=="},
{"", ""}
};
TestData<std::string> json_tests = {
{"{}", "{}"},
{"[]", "[]"},
{"null", "null"},
{"0", "0"},
{"-123", "-123"},
{"7655555", "7655555"},
{"76555556666666666", "76555556666666666"},
{"-76555556666666666021", "-76555556666666666021"},
{"true", "true"},
{"false", "false"},
{"0.5", "0.5"},
{"-5.0e-1", "-0.5"},
{"{\"key\":\"value\",\"key2\":null}", "{\"key\":\"value\",\"key2\":null}"},
{"[null,true,false,-5,\"\"]", "[null,true,false,-5,\"\"]"},
{"\"Hello World!\"", "\"Hello World!\""}
};
TestData<std::string> bencode_tests = {
{"de", "de"},
{"le", "le"},
{"i0e", "i0e"},
{"i-123e", "i-123e"},
{"i7655555e", "i7655555e"},
{"i76555556666666666e", "i76555556666666666e"},
{"17:76555556666666666", "17:76555556666666666"},
{"d3:key5:value4:key2i0ee", "d3:key5:value4:key2i0ee"},
{"li0e4:true5:falsei-5e0:e", "li0e4:true5:falsei-5e0:e"}
};
struct hex_string : public std::string
{
hex_string() {}
hex_string(const char *s) : std::string(s) {}
hex_string(const char *s, size_t size) : std::string(s, size) {}
hex_string(const std::string &str) : std::string(str) {}
};
std::ostream &operator<<(std::ostream &ostr, const hex_string &str)
{
cppdatalib::core::ostream_handle wrap(ostr);
cppdatalib::hex::debug_write(wrap, str);
return ostr;
}
TestData<hex_string> message_pack_tests = {
{"\x80", "\x80"},
{"\x90", "\x90"},
{"\x01", "\x01"},
{"\xff", "\xff"},
{"\xd0\x85", "\xd0\x85"},
{"\xcc\x85", "\xcc\x85"},
{"\x81\x01\x01", "\x81\x01\x01"},
{"\x92\x01\x01", "\x92\x01\x01"}
};
int readme_simple_test()
{
using namespace cppdatalib; // Parent namespace
using namespace json; // Format namespace
core::value my_value; // Global cross-format value class
try {
json::parser p(std::cin);
json::stream_writer w(std::cout);
p >> my_value; // Read in to core::value from STDIN as JSON
w << my_value; // Write core::value out to STDOUT as JSON
} catch (core::error e) {
std::cerr << e.what() << std::endl; // Catch any errors that might have occured (syntax or logical)
}
return 0;
}
int readme_simple_test2()
{
using namespace cppdatalib; // Parent namespace
using namespace json; // Format namespace
try {
json::parser(std::cin) >> json::stream_writer(std::cout); // Write core::value out to STDOUT as JSON
} catch (core::error e) {
std::cerr << e.what() << std::endl; // Catch any errors that might have occured (syntax or logical)
}
return 0;
}
int readme_simple_test3()
{
using namespace cppdatalib; // Parent namespace
using namespace json; // Format namespace
try {
json::stream_writer(std::cout) << from_json(std::cin); // Write core::value out to STDOUT as JSON
} catch (core::error e) {
std::cerr << e.what() << std::endl; // Catch any errors that might have occured (syntax or logical)
}
return 0;
}
struct point
{
int x, y;
};
#include "adapters/stl.h"
template<>
class cast_to_cppdatalib<point>
{
const point &bind;
public:
cast_to_cppdatalib(const point &bind) : bind(bind) {}
operator cppdatalib::core::value() const {return cppdatalib::core::object_t{{"x", bind.x}, {"y", bind.y}};}
};
template<>
struct cast_from_cppdatalib<point>
{
const cppdatalib::core::value &bind;
public:
cast_from_cppdatalib(const cppdatalib::core::value &bind) : bind(bind) {}
operator point() const
{
point p;
p.x = bind["x"];
p.y = bind["y"];
return p;
}
};
int readme_simple_test4()
{
using namespace cppdatalib; // Parent namespace
using namespace json; // Format namespace
core::value my_value, m2; // Global cross-format value class
core::value_builder builder(my_value);
m2 = std::array<int, 3>{0, 1, 4};
std::array<int, 2> axx = m2;
m2 = axx;
std::stack<int, std::vector<int>> stack;
stack.push(0);
stack.push(1);
try {
json::parser p(std::cin);
json::stream_writer w(std::cout);
p.begin(builder);
do
p.write_one(); // Read in to core::value from STDIN as JSON
while (p.busy());
p.end();
p >> my_value >> m2;
core::value_parser vp(my_value);
vp.begin(w);
do
vp.write_one();
while (vp.busy());
vp.end();
std::multimap<const char *, int> map = {{"x", 1}, {"y", 2}};
m2 = stack;
m2 = map;
point p2 = point();
p2.x = 9;
m2 = p2;
p2 = m2;
p2.x += 1000;
m2 = p2;
w << m2; // Write core::value out to STDOUT as JSON
} catch (core::error e) {
std::cerr << e.what() << std::endl; // Catch any errors that might have occured (syntax or logical)
}
return 0;
}
#ifdef CPPDATALIB_ENABLE_BOOST_COMPUTE
#include "adapters/boost_compute.h"
#include <boost/compute.hpp>
void test_boost_compute()
{
boost::compute::device device = boost::compute::system::default_device();
boost::compute::context ctx(device);
boost::compute::command_queue queue(ctx, device);
boost::compute::vector<float> vec(3100000, ctx);
cppdatalib::core::value compute_v;
compute_v = cppdatalib::core::array_t();
for (size_t i = 0; i < vec.size(); ++i)
compute_v.push_back(i);
vec = cppdatalib::core::userdata_cast(compute_v, queue);
boost::compute::transform(vec.begin(),
vec.end(),
vec.begin(),
boost::compute::sqrt<float>(),
queue);
compute_v = cppdatalib::core::userdata_cast(vec, queue);
//std::cout << compute_v << std::endl;
}
#endif
#ifdef CPPDATALIB_ENABLE_QT
void test_qt()
{
std::tuple<int, int, std::string, QVector<QString>> tuple;
QStringList value = { "value", "" };
xyz = std::make_tuple(0, 1.5, "hello", value, "stranger");
std::cout << xyz << std::endl;
tuple = xyz;
xyz = tuple;
std::cout << xyz << std::endl;
std::array<int, 3> stdarr = xyz;
xyz = stdarr;
std::cout << xyz << std::endl;
}
#endif
int main()
{
cppdatalib::core::value xyz;
std::cout << sizeof(cppdatalib::core::value) << std::endl;
cppdatalib::core::dump::stream_writer dummy(std::cout, 2);
cppdatalib::core::median_filter<cppdatalib::core::uinteger> median(dummy);
cppdatalib::core::mode_filter<cppdatalib::core::uinteger> mode(median);
cppdatalib::core::range_filter<cppdatalib::core::uinteger> range(mode);
cppdatalib::core::dispersion_filter<cppdatalib::core::uinteger> dispersal(range);
cppdatalib::core::array_sort_filter<> sorter(dispersal);
sorter << cppdatalib::core::array_t{2, 0, 1, 2, 3, 4, 4};
std::cout << median.get_median() << std::endl;
std::cout << mode.get_modes() << std::endl;
std::cout << range.get_max() << std::endl;
std::cout << dispersal.get_arithmetic_mean() << std::endl << dispersal.get_standard_deviation() << std::endl;
//return readme_simple_test4();
vt100 vt;
std::cout << vt.attr_bright;
#if 0
Test("base64_encode", base64_encode_tests, cppdatalib::base64::encode);
ReverseTest("base64_decode", base64_encode_tests, cppdatalib::base64::decode);
Test("debug_hex_encode", debug_hex_encode_tests, cppdatalib::hex::debug_encode);
Test("hex_encode", hex_encode_tests, cppdatalib::hex::encode);
#if 0
TestRange("float_from_ieee_754", UINT32_MAX, cppdatalib::core::float_cast_from_ieee_754,
cppdatalib::core::float_from_ieee_754, true, [](const auto &f, const auto &s){return f != s && !isnan(f) && !isnan(s);});
TestRange("float_to_ieee_754", UINT32_MAX, [](const auto &f){return f;},
[](const auto &f){return core::float_to_ieee_754(cppdatalib::core::float_cast_from_ieee_754(f));}, true,
[](const auto &f, const auto &s){return f != s && !isnan(cppdatalib::core::float_from_ieee_754(f)) && !isnan(cppdatalib::core::float_from_ieee_754(s));});
#endif
try
{
Test("JSON", json_tests, [](const auto &test){return cppdatalib::json::to_json(cppdatalib::json::from_json(test));}, false);
Test("Bencode", bencode_tests, [](const auto &test){return cppdatalib::bencode::to_bencode(cppdatalib::bencode::from_bencode(test));}, false);
Test("MessagePack", message_pack_tests, [](const auto &test) -> hex_string {return hex_string(cppdatalib::message_pack::to_message_pack(cppdatalib::message_pack::from_message_pack(test)));}, false);
}
catch (cppdatalib::core::error e)
{
std::cout << e.what() << std::endl;
}
#endif
std::cout << vt.attr_reset;
#ifdef CPPDATALIB_MSVC
system("pause");
#endif
return 0;
}