summaryrefslogtreecommitdiffstats
path: root/test/cutest/include/cut_defs.h
blob: bd9a07168669ae118e097f2ad0c035cfb4f9ad00 (plain)
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

#ifndef CUT_DEFS_H
#define CUT_DEFS_H

#include <stdio.h>

#ifdef __cplusplus
extern "C"
{
#endif

#define CUT_LOG_LEVEL AXIS2_LOG_LEVEL_DEBUG
#define CUT_LOG_HEADER "************* testing %s **************"

#define CUT_ASSERT(value) \
  cut_assert((value), __LINE__, #value, __FILE__);

#define CUT_ASSERT_RETURN(value) \
  if (!cut_assert((value), __LINE__, #value, __FILE__)) return;

#define CUT_RETURN_ON_FAILURE(cr) \
    if (cut_nb_assert_failures) return cr;

#define CUT_ASSERT_PTR_EQUAL(ptr,value,bret) \
  if (!cut_ptr_equal((void*)(ptr), (void*)(value), 1, __LINE__, __FILE__) && bret) return;

#define CUT_ASSERT_PTR_NOT_EQUAL(ptr,value,bret) \
  if (!cut_ptr_equal((void*)(ptr), (void*)(value), 0, __LINE__, __FILE__) && bret) return;

#define CUT_ASSERT_INT_EQUAL(value,expected,bret) \
  if (!cut_int_equal((long)(value), (long)(expected), 1, __LINE__, __FILE__) && bret) return;

#define CUT_ASSERT_INT_NOT_EQUAL(value,expected,bret) \
  if (!cut_int_equal((long)(value), (long)(expected), 0, __LINE__, __FILE__) && bret) return;

#define CUT_ASSERT_STR_EQUAL(value,expected,bret) \
  if (!cut_str_equal(value, expected, 1, __LINE__, __FILE__) && bret) return;

#define CUT_ASSERT_STR_NOT_EQUAL(value,expected,bret) \
  if (!cut_str_equal(value, expected, 0, __LINE__, __FILE__) && bret) return;

static int cut_nb_asserts = 0;
static int cut_nb_assert_failures = 0;
static axutil_env_t *cut_axis2_env = NULL;

static int cut_strcmp(const char *str1, const char *str2)
{
    if (str1 == NULL || str2 == NULL)
	{
	    return str1 == str2;
	}
	return strcmp(str1, str2);
}

static int cut_assert(int bValue, 
               unsigned int uiLine,
               char strCondition[], 
               char strFile[])
{
    ++cut_nb_asserts;
    if (bValue) return bValue;
    ++cut_nb_assert_failures;
    printf("Assertion Failure %s %d: %s\n", strFile, uiLine, strCondition);
    AXIS2_LOG_ERROR(cut_axis2_env->log, strFile, uiLine, "Assertion Failure : %s", strCondition);
	return bValue;
}

static int cut_ptr_equal(void *ptr, void *value, int equal,
               unsigned int uiLine,
               char strFile[])
{
    ++cut_nb_asserts;
    if ( (ptr == value) == equal) return 1;
    ++cut_nb_assert_failures;
    printf("Assertion Failure %s %d: pointer %sexpected <%s>, found <%s>\n", strFile, uiLine, 
	    equal?"":"not ", (char *) value, (char*) ptr);
    AXIS2_LOG_ERROR(cut_axis2_env->log, strFile, uiLine, 
	    "Assertion Failure : pointer %sexpected <%p>, found <%p>", equal?"":"not ", value, ptr);
	return 0;
}

static int cut_int_equal(long value, long expected, int equal,
               unsigned int uiLine,
               char strFile[])
{
    ++cut_nb_asserts;
    if ((expected == value)== equal) return 1;
    ++cut_nb_assert_failures;
    printf("Assertion Failure %s %d: integer %sexpected <%ld>, found <%ld>\n", strFile, uiLine, 
	    equal?"":"not ", expected, value);
    AXIS2_LOG_ERROR(cut_axis2_env->log, strFile, uiLine, 
	    "Assertion Failure : integer %sexpected <%ld>, found <%ld>", equal?"":"not ", expected,value);
	return 0;
}

static int cut_str_equal(const char *value, const char *expected, int equal,
               unsigned int uiLine,
               char strFile[])
{
    ++cut_nb_asserts;
    if ( (cut_strcmp(expected,value) == 0) == equal) return 1;
    ++cut_nb_assert_failures;
    printf("Assertion Failure %s %d: string %sexpected <%s>, found <%s>\n", strFile, uiLine, 
	    equal?"":"not ", expected, value);
    AXIS2_LOG_ERROR(cut_axis2_env->log, strFile, uiLine, 
	    "Assertion Failure : string %sexpected <%s>, found <%s>", equal?"":"not ", expected,value);
	return 0;
}

axutil_env_t *cut_setup_env(char* testName)
{
    cut_axis2_env = axutil_env_create_all("unit_tests.log", CUT_LOG_LEVEL);
    if (testName != NULL)
    {
        printf("\n");
        printf(CUT_LOG_HEADER, testName);
        printf("\n\n");
        if (cut_axis2_env) 
        {
            AXIS2_LOG_INFO(cut_axis2_env->log, CUT_LOG_HEADER, testName);
        }
    }
    return cut_axis2_env;
}

#ifdef __cplusplus
}
#endif

#endif                          /* CUT_DEFS_H */