00001 #ifndef PS_ASSERT_H
00002 #define PS_ASSERT_H
00003
00004
00005
00006
00007 #include <assert.h>
00008 #include <inttypes.h>
00009 #include <math.h>
00010
00011 #include "psMemory.h"
00012 #include "psError.h"
00013 #include "psLogMsg.h"
00014
00015
00016 #define PS_ASSERT_PTR(NAME, RVAL) \
00017 { \
00018 if (NAME == NULL) return(RVAL); \
00019 psMemBlock *mb = (psMemBlock*)(NAME) - 1; \
00020 if (mb->startblock != P_PS_MEMMAGIC || mb->endblock != P_PS_MEMMAGIC || \
00021 *(psU32 *)((char *)(mb + 1) + mb->userMemorySize) != P_PS_MEMMAGIC) { \
00022 psError(PS_ERR_MEMORY_CORRUPTION, false, \
00023 "Error: Pointer %s is corrupted or not on the PS memory system.", \
00024 #NAME); \
00025 return (RVAL); \
00026 } \
00027 }
00028
00029 #define PS_ASSERT_INT_UNEQUAL(NAME1, NAME2, RVAL) \
00030 if ((NAME1) == (NAME2)) { \
00031 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00032 "Error: %s and %s are equal.", \
00033 #NAME1, #NAME2); \
00034 return(RVAL); \
00035 }
00036
00037 #define PS_ASSERT_INT_EQUAL(NAME1, NAME2, RVAL) \
00038 if ((NAME1) != (NAME2)) { \
00039 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00040 "Error: %s and %s are not equal.", \
00041 #NAME1, #NAME2); \
00042 return(RVAL); \
00043 }
00044
00045 #define PS_ASSERT_INT_NONNEGATIVE(NAME, RVAL) \
00046 if ((NAME) < 0) { \
00047 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: %s is less than 0.", #NAME); \
00048 return(RVAL); \
00049 }
00050
00051 #define PS_ASSERT_INT_POSITIVE(NAME, RVAL) \
00052 if ((NAME) < 1) { \
00053 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00054 "Error: %s is 0 or less.", #NAME); \
00055 return(RVAL); \
00056 }
00057
00058 #define PS_ASSERT_INT_ZERO(NAME, RVAL) \
00059 if ((NAME) != 0) { \
00060 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00061 "Error: %s is 0.", #NAME); \
00062 return(RVAL); \
00063 }
00064
00065 #define PS_ASSERT_INT_NONZERO(NAME, RVAL) \
00066 if ((NAME) == 0) { \
00067 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00068 "Error: %s is 0.", #NAME); \
00069 return(RVAL); \
00070 }
00071
00072
00073 #define PS_ASSERT_INT_WITHIN_RANGE(NAME, LOWER, UPPER, RVAL) \
00074 if ((int)(NAME) < LOWER || (int)(NAME) > UPPER) { \
00075 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00076 "Error: %s, %d, is out of range. Must be between %d and %d.", \
00077 #NAME,(int)NAME,LOWER,UPPER); \
00078 return RVAL; \
00079 }
00080
00081 #define PS_ASSERT_INT_LESS_THAN(VAR1, VAR2, RVAL) \
00082 if (!(VAR1 < VAR2)) { \
00083 psError(PS_ERR_UNKNOWN, true, \
00084 "Error: %s is not less than %s (%d, %d)", #VAR1, #VAR2, VAR1, VAR2); \
00085 return(RVAL); \
00086 }
00087
00088 #define PS_ASSERT_INT_LESS_THAN_OR_EQUAL(VAR1, VAR2, RVAL) \
00089 if (!(VAR1 <= VAR2)) { \
00090 psError(PS_ERR_UNKNOWN, true, \
00091 "Error: %s is not less than %s (%d, %d)", #VAR1, #VAR2, VAR1, VAR2); \
00092 return(RVAL); \
00093 }
00094
00095 #define PS_ASSERT_INT_LARGER_THAN(NAME1, NAME2, RVAL) \
00096 if (!((NAME1) > (NAME2))) { \
00097 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00098 "Error: !(%s > %s) (%d %d).", \
00099 #NAME1, #NAME2, NAME1, NAME2); \
00100 return(RVAL); \
00101 }
00102
00103 #define PS_ASSERT_INT_LARGER_THAN_OR_EQUAL(NAME1, NAME2, RVAL) \
00104 if (!((NAME1) >= (NAME2))) { \
00105 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00106 "Error: !(%s >= %s) (%d %d).", \
00107 #NAME1, #NAME2, NAME1, NAME2); \
00108 return(RVAL); \
00109 }
00110 #define PS_ASSERT_FLOAT_LARGER_THAN(NAME1, NAME2, RVAL) \
00111 if (!((NAME1) > (NAME2))) { \
00112 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00113 "Error: !(%s > %s) (%f %f).", \
00114 #NAME1, #NAME2, NAME1, NAME2); \
00115 return(RVAL); \
00116 }
00117
00118 #define PS_ASSERT_FLOAT_LARGER_THAN_OR_EQUAL(NAME1, NAME2, RVAL) \
00119 if (!((NAME1) >= (NAME2))) { \
00120 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00121 "Error: !(%s >= %s) (%f %f).", \
00122 #NAME1, #NAME2, NAME1, NAME2); \
00123 return(RVAL); \
00124 }
00125
00126 #define PS_ASSERT_FLOAT_LESS_THAN(NAME1, NAME2, RVAL) \
00127 if (!((NAME1) < (NAME2))) { \
00128 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00129 "Error: !(%s < %s) (%f %f).", \
00130 #NAME1, #NAME2, NAME1, NAME2); \
00131 return(RVAL); \
00132 }
00133
00134 #define PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(NAME1, NAME2, RVAL) \
00135 if (!((NAME1) <= (NAME2))) { \
00136 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00137 "Error: !(%s <= %s) (%f %f).", \
00138 #NAME1, #NAME2, NAME1, NAME2); \
00139 return(RVAL); \
00140 }
00141
00142 #define PS_ASSERT_FLOAT_NON_EQUAL(NAME1, NAME2, RVAL) \
00143 if (fabs((NAME2) - (NAME1)) < FLT_EPSILON) { \
00144 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00145 "Error: %s and %s are equal.", \
00146 #NAME1, #NAME2); \
00147 return(RVAL); \
00148 }
00149
00150 #define PS_ASSERT_FLOAT_EQUAL(NAME1, NAME2, RVAL) \
00151 if (fabs((NAME2) - (NAME1)) > FLT_EPSILON) { \
00152 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00153 "Error: %s and %s are not equal.", \
00154 #NAME1, #NAME2); \
00155 return(RVAL); \
00156 }
00157
00158
00159 #define PS_ASSERT_FLOAT_WITHIN_RANGE(NAME, LOWER, UPPER, RVAL) \
00160 if ((NAME) < (LOWER) || (NAME) > (UPPER)) { \
00161 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00162 "Error: %s, %f, is out of range. Must be between %f and %f.", \
00163 #NAME, NAME, LOWER, UPPER); \
00164 return RVAL; \
00165 }
00166
00167 #define PS_ASSERT_FLOAT_REAL(NAME, RVAL) \
00168 if (!isfinite(NAME)) { \
00169 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00170 "Error: %s=%f is not a real value.\n", \
00171 #NAME, NAME); \
00172 return RVAL; \
00173 }
00174
00175 #define PS_ASSERT_DOUBLE_WITHIN_RANGE(NAME, LOWER, UPPER, RVAL) \
00176 if ((NAME) < (LOWER) || (NAME) > (UPPER)) { \
00177 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00178 "Error: %s, %lf, is out of range. Must be between %lf and %lf.", \
00179 #NAME, NAME, LOWER, UPPER); \
00180 return RVAL; \
00181 }
00182
00183 #define PS_ASSERT_LONG_WITHIN_RANGE(NAME, LOWER, UPPER, RVAL) \
00184 if ((NAME) < (LOWER) || (NAME) > (UPPER)) { \
00185 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00186 "Error: %s, %ld, is out of range. Must be between %ld and %ld.", \
00187 #NAME, NAME, LOWER, UPPER); \
00188 return RVAL; \
00189 }
00190
00191 #define PS_ASSERT_LONG_LARGER_THAN_OR_EQUAL(NAME1, NAME2, RVAL) \
00192 if (!((NAME1) >= (NAME2))) { \
00193 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: !(%s >= %s) (%ld %ld).",\
00194 #NAME1, #NAME2, NAME1, NAME2); \
00195 return(RVAL); \
00196 }
00197
00198 #define PS_ASSERT_S64_WITHIN_RANGE(NAME, LOWER, UPPER, RVAL) \
00199 if ((NAME) < (LOWER) || (NAME) > (UPPER)) { \
00200 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \
00201 "Error: %s, %" PRId64 ", is out of range. Must be between %" PRId64 " and %" PRId64 ".", \
00202 #NAME, NAME, LOWER, UPPER); \
00203 return RVAL; \
00204 }
00205
00206
00207
00208
00209
00210 #define PS_WARN_PTR_NON_NULL(NAME) \
00211 if ((NAME) == NULL) { \
00212 psLogMsg(__func__, PS_LOG_WARN, "WARNING: %s is NULL.", #NAME); \
00213 } \
00214
00215 #define PS_ASSERT_PTR_NON_NULL(NAME, RVAL) PS_ASSERT_GENERAL_PTR_NON_NULL(NAME, return RVAL)
00216 #define PS_ASSERT_GENERAL_PTR_NON_NULL(NAME, CLEANUP) \
00217 if ((NAME) == NULL) { \
00218 psError(PS_ERR_BAD_PARAMETER_NULL, true, \
00219 "Unallowable operation: %s is NULL.", \
00220 #NAME); \
00221 CLEANUP; \
00222 }
00223
00224 #define PS_ASSERT_PTR_TYPE(NAME, TYPE, RVAL) \
00225 if ((NAME)->type.type != TYPE) { \
00226 psError(PS_ERR_BAD_PARAMETER_TYPE, true, \
00227 "Unallowable operation: %s has incorrect type.", \
00228 #NAME); \
00229 return(RVAL); \
00230 }
00231
00232 #define PS_ASSERT_PTR_DIMEN(NAME, DIMEN, RVAL) PS_ASSERT_GENERAL_PTR_DIMEN(NAME, DIMEN, return RVAL)
00233 #define PS_ASSERT_GENERAL_PTR_DIMEN(NAME, DIMEN, CLEANUP) \
00234 if ((NAME)->type.dimen != DIMEN) { \
00235 psError(PS_ERR_BAD_PARAMETER_TYPE, true, \
00236 "Unallowable operation: %s has incorrect dimensionality.", \
00237 #NAME); \
00238 CLEANUP; \
00239 }
00240
00241 #define PS_ASSERT_PTR_DIMEN_GENERAL_NOT(NAME, DIMEN, CLEANUP) \
00242 if ((NAME)->type.dimen == DIMEN) { \
00243 psError(PS_ERR_BAD_PARAMETER_TYPE, true, \
00244 "Unallowable operation: %s has incorrect dimensionality.", \
00245 #NAME); \
00246 CLEANUP; \
00247 }
00248
00249
00250 #define PS_ASSERT_PTRS_SIZE_EQUAL(PTR1, PTR2, RVAL) \
00251 if (PTR1->n != PTR2->n) { \
00252 psError(PS_ERR_BAD_PARAMETER_SIZE, true, \
00253 "ptr %s has size %d, ptr %s has size %d.", \
00254 #PTR1, PTR1->n, #PTR2, PTR2->n); \
00255 return(RVAL); \
00256 }
00257
00258 #define PS_ASSERT_PTR_TYPE_EQUAL(PTR1, PTR2, RVAL) PS_ASSERT_PTRS_TYPE_EQUAL_GENERAL(PTR1, PTR2, return RVAL)
00259 #define PS_ASSERT_PTRS_TYPE_EQUAL_GENERAL(PTR1, PTR2, CLEANUP) \
00260 if (PTR1->type.type != PTR2->type.type) { \
00261 psError(PS_ERR_BAD_PARAMETER_TYPE, true, \
00262 "ptr %s has type %d, ptr %s has type %d.", \
00263 #PTR1, PTR1->type.type, #PTR2, PTR2->type.type); \
00264 CLEANUP; \
00265 }
00266
00267
00268
00269 #endif