1 // : vi ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
11 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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
36 #define BAD 1 // these are exit codes unless user overrides
41 Snag the optional positional parameter at pp, return defval if not there.
43 static char* snag_pp( int pp, int argc, char** argv, char* defval ) {
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.
58 void sig_clean_exit( int sign ) {
59 fprintf( stderr, "signal trapped for clean exit: %d\n", sign );
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.
67 static void set_signals( void ) {
69 int sig_list[] = { SIGINT, SIGQUIT, SIGILL, SIGALRM, SIGTERM, SIGUSR1 , SIGUSR2 };
71 int nele; // number of elements in the list
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 );
82 static int fail_if_nil( void* p, char* what ) {
84 fprintf( stderr, "[FAIL] pointer to '%s' was nil\n", what );
86 return p ? GOOD : BAD;
89 static int fail_not_nil( void* p, char* what ) {
91 fprintf( stderr, "[FAIL] pointer to '%s' was not nil\n", what );
93 return !p ? GOOD : BAD;
96 static int fail_if_false( int bv, char* what ) {
98 fprintf( stderr, "[FAIL] boolean was false (%d) %s\n", bv, what );
101 return bv ? GOOD : BAD;
104 static int fail_if_true( int bv, char* what ) {
106 fprintf( stderr, "[FAIL] boolean was true (%d) %s\n", bv, what );
108 return bv ? BAD : GOOD;
112 Same as fail_if_true(), but reads easier in the test code.
114 static int fail_if( int bv, char* what ) {
117 fprintf( stderr, "[FAIL] boolean was true (%d) %s\n", bv, what );
119 return bv ? BAD : GOOD;
122 static int fail_not_equal( int a, int b, char* what ) {
124 fprintf( stderr, "[FAIL] %s values were not equal a=%d b=%d\n", what, a, b );
126 return a == b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly!
129 static int fail_if_equal( int a, int b, char* what ) {
131 fprintf( stderr, "[FAIL] %s values were equal a=%d b=%d\n", what, a, b );
133 return a != b ? GOOD : BAD; // user may override good/bad so do NOT return a==b directly!