Add json support
[ric-plt/xapp-frame-cpp.git] / test / ut_support.cpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4     Copyright (c) 2020 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 /*
21     Mnemonic:   ut_support.c
22     Abstract:   Tools to make unit testing easier.
23
24     Date:       16 June 2020
25     Author:     E. Scott Daniels
26 */
27
28 #include <string>
29
30 static std::string test_name = "unknown";
31
32 /*
33         Set the name of the current tester
34 */
35 extern void set_test_name( std::string name ) {
36         test_name = name;
37 }
38
39 /*
40         Returns 1 if the condition is true (not zero)
41 */
42 extern int fail_if( int cond, std::string reason ) {
43         if( cond ) {
44                 fprintf( stderr, "<FAIL> %s: %s\n", test_name.c_str(), reason.c_str() );
45                 return 1;
46         }
47
48         return 0;
49 }
50
51 /*
52         Returns 1 if the condition is false.
53 */
54 extern int fail_if_false( int cond, std::string reason ) {
55         if( !cond ) {
56                 fprintf( stderr, "<FAIL> %s: %s\n", test_name.c_str(), reason.c_str() );
57                 return 1;
58         }
59
60         return 0;
61 }
62
63 extern void announce_results( int errors ) {
64         if( errors > 0 ) {
65                 fprintf( stderr, "<FAIL> %s: failed with %d errors\n", test_name.c_str(), errors );
66         } else {
67                 fprintf( stderr, "<PASS> %s: passed with\n", test_name.c_str() );
68         }
69 }