Initial commit of RMR Library
[ric-plt/lib/rmr.git] / test / test_support.c
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia 
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       test_tools.c
23         Abstract:       Functions for test applications to make their life a bit easier.
24                                 This file is probably compiled to a .o, and then included on
25                                 the cc command for the test.
26         Author:         E. Scott Daniels
27         Date:           6 January 2019
28 */
29
30 #include <signal.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #ifndef BAD
36 #define BAD 1                   // these are exit codes unless user overrides
37 #define GOOD 0
38 #endif
39
40 /*
41         Snag the optional positional parameter at pp, return defval if not there.
42 */
43 static char* snag_pp( int pp, int argc, char** argv, char* defval ) {
44
45         if( pp < argc ) {
46                 return argv[pp];
47         }
48
49         return defval;
50 }
51
52 /*
53         Signal handler -- inside of the tests we will exit cleanly for hup/temp/intr
54         signals so that the coverage stuff will generate the needed data files. If
55         we inter/term the process they don't drive.
56 */
57
58 void sig_clean_exit( int sign ) {
59         fprintf( stderr, "signal trapped for clean exit: %d\n", sign );
60         exit( 0 );
61 }
62
63 /*      
64         Setup all of the signal handling for signals that we want to force a clean exit:
65         term, intr, hup, quit, usr1/2 alarm, etc.  All others we'll let default.
66 */
67 static void set_signals( void ) {
68         struct sigaction sa;
69         int     sig_list[] = { SIGINT, SIGQUIT, SIGILL, SIGALRM, SIGTERM, SIGUSR1 , SIGUSR2 };
70         int i;
71         int nele;               // number of elements in the list
72         
73         nele = (int) ( sizeof( sig_list )/sizeof( int ) );              // convert raw size to the number of elements
74         for( i = 0; i < nele; i ++ ) {
75                 memset( &sa, 0, sizeof( sa ) );
76                 sa.sa_handler = sig_clean_exit;
77                 sigaction( sig_list[i], &sa, NULL );
78         }
79 }
80
81
82 static int fail_if_nil( void* p, char* what ) {
83         if( !p ) {
84                 fprintf( stderr, "[FAIL] pointer to '%s' was nil\n", what );
85         }
86         return p ? GOOD : BAD;
87 }
88
89 static int fail_not_nil( void* p, char* what ) {
90         if( p ) {
91                 fprintf( stderr, "[FAIL] pointer to '%s' was not nil\n", what );
92         }
93         return !p ? GOOD : BAD;
94 }
95
96 static int fail_if_false( int bv, char* what ) {
97         if( !bv ) {
98                 fprintf( stderr, "[FAIL] boolean was false (%d) %s\n", bv, what );
99         }
100
101         return bv ? GOOD : BAD;
102 }
103
104 static int fail_if_true( int bv, char* what ) {
105         if( bv ) {
106                 fprintf( stderr, "[FAIL] boolean was true (%d) %s\n", bv, what );
107         }
108         return bv ? BAD : GOOD;
109 }
110
111 /*
112         Same as fail_if_true(), but reads easier in the test code.
113 */
114 static int fail_if( int bv, char* what ) {
115
116         if( bv ) {
117                 fprintf( stderr, "[FAIL] boolean was true (%d) %s\n", bv, what );
118         }
119         return bv ? BAD : GOOD;
120 }
121
122 static int fail_not_equal( int a, int b, char* what ) {
123         if( a != b ) {
124                 fprintf( stderr, "[FAIL] %s values were not equal a=%d b=%d\n", what, a, b );
125         }
126         return a == b ? GOOD : BAD;                     // user may override good/bad so do NOT return a==b directly!
127 }
128
129 static int fail_if_equal( int a, int b, char* what ) {
130         if( a == b ) {
131                 fprintf( stderr, "[FAIL] %s values were equal a=%d b=%d\n", what, a, b );
132         }
133         return a != b ? GOOD : BAD;                     // user may override good/bad so do NOT return a==b directly!
134 }