-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathtest_examples_common_utility.cpp
598 lines (554 loc) · 22.9 KB
/
test_examples_common_utility.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
597
598
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#if __TBB_TEST_USE_WSUGGEST_OVERRIDE
// __TBB_override may not be used in the tested header file
#pragma GCC diagnostic ignored "-Wsuggest-override"
#undef __TBB_TEST_USE_WSUGGEST_OVERRIDE
#endif
#include "harness_defs.h" // for suppress_unused_warning
#if TBB_USE_EXCEPTIONS
#include "harness_assert.h"
#include "../../examples/common/utility/utility.h"
#include <sstream>
namespace implementation_unit_tests {
namespace argument_dest_test_suite{
void test_type_impl_parse_and_store_simple_parse(){
int a=0;
utility::internal::type_impl<int> a_("","",a);
a_.parse_and_store("9");
ASSERT(a==9,"");
}
void test_default_value_of_is_matched(){
//Testing for result of is_matched() for arguments not yet tried to be parsed.
//I.e. values were set up by argument::constructor.
using utility::internal::argument;
int i;
argument b("","",i);
ASSERT(!b.is_matched(),"");
argument c = b;
ASSERT(!c.is_matched(),"");
argument d = b;
d = c;
ASSERT(!d.is_matched(),"");
}
}
//TODO: test cases for argument type management
namespace compile_only{
//TODO: enhance these to actually do checks by a negative test, or (if possible)
//by a positive test that at compile time selects between two alternatives,
//depending on whether operators exist or not (yes, SFINAE :)) -
//as non_pod class does provide the operators, and test do not check that compiler
//will reject types which don't have those.
using utility::cli_argument_pack;
void arg_chain(){
cli_argument_pack p;
int size=0;
p.arg(size,"size","size");
}
namespace tc_helper{
struct non_pod{
std::string s;
friend std::ostream& operator<<(std::ostream& o, non_pod){ return o;}
friend std::istream& operator>>(std::istream& i, non_pod){ return i;}
};
}
void non_pod_dest_type(){
cli_argument_pack p;
tc_helper::non_pod np;
p.arg(np,"","");
}
}
namespace cli_argument_pack_suite{
void test_copy_assign(){
using utility::cli_argument_pack;
int i=9;
std::stringstream expected_output; using std::endl;
expected_output
<< " Program usage is:" << endl
<< " the_binary_name [i=value]"
<< endl << endl
<< " where:" << endl
<< " i - i desc (9)" << endl
;
cli_argument_pack copy(cli_argument_pack().arg(i,"i","i desc"));
ASSERT(copy.usage_string("the_binary_name") == expected_output.str(),"usage string is not as expected");
cli_argument_pack assignee; assignee = copy;
ASSERT(assignee.usage_string("the_binary_name") == expected_output.str(),"Copying of cli_argument_pack breaks generation of usage string?");
}
}
}
#include <utility>
namespace high_level_api_tests {
using utility::cli_argument_pack;
using utility::internal::array_length;
static const char * wrong_exception = "wrong exception thrown";
static const char * wrong_exception_description = "caught exception has wrong description";
void test_parse_basic(){
char const* argv[]={"some.exe","1","a"};
cli_argument_pack p;
int i=0; char a=' ';
p.positional_arg(i,"int","").positional_arg(a,"char","");
p.parse(array_length(argv),argv);
ASSERT(i==1,"");
ASSERT(a=='a',"");
}
//helper function for test of named flag parsing
template<typename T, size_t N>
bool parse_silent_flag( T(& argv)[N]){
cli_argument_pack p;
bool silent=false;
p.arg(silent,"silent","is extra info needed");
p.parse(array_length(argv),argv);
return silent;
}
void test_named_flags_success(){
char const* argv[]={"some.exe","silent"};
ASSERT(true == parse_silent_flag(argv),"");
}
void test_named_flags_failure(){
try {
char const* argv[]={"some.exe","1"};
parse_silent_flag(argv);
ASSERT(false,"exception was expected due to invalid argument, but not caught");
}
catch(std::invalid_argument& e){
ASSERT(e.what()==std::string("unknown parameter starting at:'1'"),wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
}
//helper function for test of named flag parsing
template<typename T, size_t N>
std::pair<bool,int> parse_silent_flag_and_int( T(& argv)[N]){
cli_argument_pack p;
bool silent=false;
int i=125;
p
.arg(silent,"silent","is extra info needed")
.positional_arg(i,"int","");
p.parse(array_length(argv),argv);
return std::make_pair(silent,i);
}
void test_named_flags_failure_and_other_arg(){
char const* argv[]={"some.exe","1"};
ASSERT(std::make_pair(false,1) == parse_silent_flag_and_int(argv),"");
}
void test_named_flags_and_other_arg(){
char const* argv[]={"some.exe","silent","7"};
ASSERT(std::make_pair(true,7) == parse_silent_flag_and_int(argv),"");
}
void test_named_flags_and_other_arg_different_order(){
char const* argv[]={"some.exe","7","silent"};
ASSERT(std::make_pair(true,7) == parse_silent_flag_and_int(argv),"");
}
void test_flags_only_others_default(){
char const* argv[]={"some.exe","silent"};
ASSERT(std::make_pair(true,125) == parse_silent_flag_and_int(argv),"");
}
namespace parameters_validation_test_suite{
namespace test_validation_function_called_helpers{
struct validator{
static bool called;
static bool accept(const int & ){
called = true;
return true;
}
};
bool validator::called =false;
}
void test_validation_function_called(){
using test_validation_function_called_helpers::validator;
char const* argv[]={"some.exe","7"};
cli_argument_pack p;
int size =0;
p.positional_arg(size,"size","",validator::accept);
p.parse(array_length(argv),argv);
ASSERT((validator::called),"validation function has not been called");
}
void test_validation_failed(){
struct validator{
static bool reject(const int &){
return false;
}
};
char const* argv[]={"some.exe","7"};
cli_argument_pack p;
int size =0;
p.positional_arg(size,"size","",validator::reject);
try {
p.parse(array_length(argv),argv);
ASSERT((false),"An exception was expected due to failed argument validation, "
"but no exception thrown");
}
catch(std::invalid_argument& e){
std::string error_msg("'7' is invalid value for argument 'size'");
ASSERT(e.what()==error_msg , wrong_exception_description);
}
catch(...){ASSERT((false),wrong_exception);}
}
}
namespace error_handling {
void test_wrong_input(){
char const* argv[]={"some.exe","silent"};
cli_argument_pack p;
int size =0;
p.positional_arg(size,"size","");
try{
p.parse(array_length(argv),argv);
ASSERT(false,"An exception was expected due to wrong input, but no exception thrown");
}
catch(std::invalid_argument & e){
std::string error_msg("'silent' is incorrect input for argument 'size' (error converting string 'silent')");
ASSERT(e.what()==error_msg, wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
}
void test_duplicate_arg_names(){
cli_argument_pack p;
int a=0;
p.arg(a,"a","");
try{
int dup_a=0;
p.arg(dup_a,"a","");
ASSERT(false, "An exception was expected due adding duplicate parameter name, but not thrown");
}
catch(std::invalid_argument& e){
ASSERT(e.what()==std::string("argument with name: 'a' already registered"),wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
}
void test_duplicate_positional_arg_names(){
cli_argument_pack p;
int a=0;
p.positional_arg(a,"a","");
try{
int dup_a=0;
p.positional_arg(dup_a,"a","");
ASSERT(false, "An exception was expected due adding duplicate parameter name, but not thrown");
}
catch(std::invalid_argument& e){
ASSERT(e.what()==std::string("argument with name: 'a' already registered"),wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
}
}
namespace usage_string {
void test_one_arg(){
cli_argument_pack p;
int size =9;
p.arg(size,"size","size of problem domain");
std::string const binary_name = "binary.exe";
std::stringstream expected_output;
using std::endl;
expected_output << " Program usage is:" << endl
<< " " << binary_name << " [size=value]"
<< endl << endl
<< " where:" << endl
<< " size - size of problem domain (9)" << endl
;
std::string usage= p.usage_string(binary_name);
ASSERT(usage==expected_output.str(),"");
}
void test_named_and_postional_args(){
cli_argument_pack p;
int size =9;
int length =8;
int stride = 7;
p
.arg(size,"size","")
.positional_arg(length,"length","")
.positional_arg(stride,"stride","");
std::string const binary_name = "binary.exe";
std::stringstream expected_output;
using std::endl;
expected_output << " Program usage is:" << endl
<< " " << binary_name << " [size=value] [length=value] [stride=value] [length [stride]]"
<< endl << endl
<< " where:" << endl
<< " size - (9)" << endl
<< " length - (8)" << endl
<< " stride - (7)" << endl
;
std::string usage= p.usage_string(binary_name);
ASSERT(usage==expected_output.str(),"");
}
void test_bool_flag(){
bool flag=false;
cli_argument_pack p;
p.arg(flag,"flag","");
std::string const binary_name = "binary.exe";
std::stringstream expected_output;
using std::endl;
expected_output << " Program usage is:" << endl
<< " " << binary_name << " [flag]"
<< endl << endl
<< " where:" << endl
<< " flag - (0)" << endl
;
std::string usage= p.usage_string(binary_name);
ASSERT(usage==expected_output.str(),"");
}
}
namespace name_positional_syntax {
void test_basic(){
cli_argument_pack p;
int size =0;
int time = 0;
p
.positional_arg(size,"size","")
.positional_arg(time,"time","");
char const* argv[]={"some.exe","1","2"};
p.parse(array_length(argv),argv);
ASSERT(size==1,"");
ASSERT(time==2,"");
}
void test_positional_args_explicitly_named(){
const char* no_or_wrong_exception_error_msg = "exception was expected but not thrown, or wrong exception caught";
//TODO: Similar functionality is used all over the test. Generalize this helper further, and use as wide within the test as possible?
struct failed_with_exception{
static bool _(cli_argument_pack & p, std::size_t argc, char const* argv[]){
try{
p.parse(argc,argv);
return false;
}
catch(std::exception &){
return true;
}
catch(...){
return false;
}
}
};
{
cli_argument_pack p;
int a,b,c,d;
p
.positional_arg(a,"a","")
.positional_arg(b,"b","")
.positional_arg(c,"c","")
.positional_arg(d,"d","");
char const* argv[]={"some.exe","a=7","0","1","2","4"};
ASSERT(failed_with_exception::_(p,array_length(argv),argv),no_or_wrong_exception_error_msg);
}
{
cli_argument_pack p;
int a,b,c,d;
p
.positional_arg(a,"a","")
.positional_arg(b,"b","")
.positional_arg(c,"c","")
.positional_arg(d,"d","");
char const* argv[]={"some.exe","a=7","0","1","2"};
ASSERT(failed_with_exception::_(p,array_length(argv),argv),no_or_wrong_exception_error_msg);
}
{
cli_argument_pack p;
int a=-1,b=-1,c = -1,d=-1;
p
.positional_arg(a,"a","")
.positional_arg(b,"b","")
.positional_arg(c,"c","")
.positional_arg(d,"d","");
char const* argv[]={"some.exe","0","1","d=7",};
ASSERT(!failed_with_exception::_(p,array_length(argv),argv),"unexpected exception");
ASSERT(a==0,""); ASSERT(b==1,""); ASSERT(c==-1,"");ASSERT(d==7,"");
}
}
}
namespace name_value_syntax {
void test_basic(){
cli_argument_pack p;
int size =0;
p.arg(size,"size","size of problem domain");
char const* argv[]={"some.exe","size=7"};
p.parse(array_length(argv),argv);
ASSERT(size==7,"");
}
void test_relaxed_order(){
cli_argument_pack p;
int size =0;
int time=0;
p
.arg(size,"size","")
.arg(time,"time","");
char const* argv[]={"some.exe","time=1","size=2"};
p.parse(array_length(argv),argv);
ASSERT(size==2,"");
ASSERT(time==1,"");
}
}
namespace number_of_argument_value{
void test_only_single_values_allowed(){
cli_argument_pack p;
int a=0;
p.arg(a,"a","");
const char* argv[] = {"","a=7","a=8"};
try {
p.parse(array_length(argv),argv);
ASSERT(false,"exception was expected due to duplicated values provided in input, but not thrown");
}
catch(std::invalid_argument& e){
//TODO: use patterns (regexp ?) to generate /validate exception descriptions
ASSERT(e.what() == std::string("several values specified for: 'a' argument"),wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
}
}
namespace thread_range_tests{
using utility::thread_number_range;
using utility::internal::thread_range_step;
using utility::internal::step_function_multiply;
using utility::internal::step_function_plus;
using utility::internal::step_function_power2_ladder;
int auto_value(){
return 100;
}
bool operator ==(thread_range_step const& left, utility::internal::thread_range_step const& right){
return (left.step_function == right.step_function)
&& (left.step_function_argument == right.step_function_argument)
;
}
bool operator ==(thread_number_range const& left, thread_number_range const& right){
return (left.auto_number_of_threads==right.auto_number_of_threads)
&& (left.first == right.first)
&& (left.last == right.last)
&& (left.step == right.step)
;
}
void constructor_default_values(){
thread_number_range r(auto_value);
const int default_num_threads = auto_value();
ASSERT((r.first==1)&&(r.last==default_num_threads),"");
}
void validation(){
try{
thread_number_range range(auto_value,12,6);
Harness::suppress_unused_warning(range);
ASSERT(false,"exception was expected due to invalid range specified, but not thrown");
}
catch(std::invalid_argument& e){
ASSERT(e.what() == std::string("decreasing sequence not allowed"), wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
}
thread_number_range thread_number_range_from_string(std::string const& string_to_parse){
thread_number_range r(auto_value,0,0);
std::stringstream str(string_to_parse); str>>r;
return r;
}
static const char* thread_range_parse_failed = "error parsing thread range string";
void post_process_single_value(){
ASSERT(thread_number_range_from_string("auto") ==
thread_number_range(auto_value,auto_value(),auto_value())
,thread_range_parse_failed
);
}
void post_process_pair_value(){
ASSERT(thread_number_range_from_string("1:auto") ==
thread_number_range(auto_value,1,auto_value())
,thread_range_parse_failed
);
ASSERT(thread_number_range_from_string("auto:auto") ==
thread_number_range(auto_value,auto_value(),auto_value())
,thread_range_parse_failed
);
}
void post_process_troika_value_with_plus_step(){
ASSERT(thread_number_range_from_string("1:auto:+2") ==
thread_number_range(auto_value,1,auto_value(),thread_range_step(step_function_plus,2))
,thread_range_parse_failed
);
}
void post_process_troika_value_with_multiply_step(){
ASSERT(thread_number_range_from_string("1:auto:*2.6") ==
thread_number_range(auto_value,1,auto_value(),thread_range_step(step_function_multiply,2.6))
,thread_range_parse_failed
);
}
void post_process_troika_value_with_ladder_step(){
try{
thread_number_range range = thread_number_range_from_string("1:16:#3");
Harness::suppress_unused_warning(range);
ASSERT(false,"exception was expected due to invalid range specified, but not thrown");
}
catch(std::invalid_argument& e){
ASSERT(e.what() == std::string("the argument of # should be a power of 2"), wrong_exception_description);
}
catch(...){ASSERT(false,wrong_exception);}
ASSERT(thread_number_range_from_string("1:32:#4") ==
thread_number_range(auto_value,1,32,thread_range_step(step_function_power2_ladder,4))
,thread_range_parse_failed
);
}
void test_print_content(){
std::stringstream str;
str<<thread_number_range(auto_value,1,8,thread_range_step(step_function_multiply,2));
ASSERT(str.str() == "1:8:*2","Unexpected string");
}
}
}
void run_implementation_unit_tests(){
using namespace implementation_unit_tests;
argument_dest_test_suite::test_type_impl_parse_and_store_simple_parse();
argument_dest_test_suite::test_default_value_of_is_matched();
cli_argument_pack_suite::test_copy_assign();
}
void run_high_level_api_tests(){
using namespace high_level_api_tests;
test_parse_basic();
test_named_flags_success();
test_named_flags_failure();
test_named_flags_failure_and_other_arg();
test_named_flags_and_other_arg();
test_flags_only_others_default();
test_named_flags_and_other_arg_different_order();
usage_string::test_one_arg();
usage_string::test_named_and_postional_args();
usage_string::test_bool_flag();
parameters_validation_test_suite::test_validation_function_called();
parameters_validation_test_suite::test_validation_failed();
name_value_syntax::test_basic();
name_value_syntax::test_relaxed_order();
number_of_argument_value::test_only_single_values_allowed();
name_positional_syntax::test_basic();
name_positional_syntax::test_positional_args_explicitly_named();
error_handling::test_wrong_input();
error_handling::test_duplicate_arg_names();
error_handling::test_duplicate_positional_arg_names();
thread_range_tests::constructor_default_values();
thread_range_tests::validation();
thread_range_tests::post_process_single_value();
thread_range_tests::post_process_pair_value();
thread_range_tests::post_process_troika_value_with_plus_step();
thread_range_tests::post_process_troika_value_with_multiply_step();
thread_range_tests::post_process_troika_value_with_ladder_step();
thread_range_tests::test_print_content();
}
#endif // TBB_USE_EXCEPTIONS
#include "harness.h"
int TestMain(){
#if TBB_USE_EXCEPTIONS
Harness::suppress_unused_warning(utility::thread_number_range_desc);
try{
run_implementation_unit_tests();
run_high_level_api_tests();
}catch(std::exception& e){
//something went wrong , dump any possible details
std::stringstream str; str<< "run time error: " << e.what()<<std::endl;
ASSERT(false,str.str().c_str());
}
return Harness::Done;
#else
REPORT("Known issue: the test cannot work with exceptions disabled\n");
return Harness::Done;
#endif
}