1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
15 /** Variable list declarations. */
16 #define SJME_VA_DEF va_list inputVa; \
20 * Performs a short with the given type.
22 * @param type The type of return value.
25 #define SJME_VA_SHORT(type) do {\
26 va_start(inputVa, format); \
27 sjme_unitShortingEmit(file, line, func, test, (type), format, inputVa); \
29 } while(SJME_JNI_FALSE)
32 * Operator comparison function.
34 * @param size The size of the values.
35 * @param a The first value.
36 * @param b The second value.
37 * @return Returns what the comparison is.
40 typedef sjme_jboolean (*sjme_unitOperatorFunc
)(size_t size
, void* a
, void* b
);
43 * Operator comparison information.
47 typedef struct sjme_unitOperatorInfo
49 /** The operator symbol. */
52 /** The function for comparison. */
53 sjme_unitOperatorFunc function
;
54 } sjme_unitOperatorInfo
;
56 static sjme_jboolean
sjme_unitOperatorEqual(size_t size
, void* a
, void* b
)
58 return memcmp(a
, b
, size
) == 0;
61 static sjme_jboolean
sjme_unitOperatorNotEqual(size_t size
, void* a
, void* b
)
63 return memcmp(a
, b
, size
) != 0;
66 static sjme_jboolean
sjme_unitOperatorLessThan(size_t size
, void* a
, void* b
)
71 if (size
== sizeof(sjme_jint
))
72 return *((sjme_jint
*)a
) < *((sjme_jint
*)b
);
77 return ja
->lo
< jb
->lo
;
78 return SJME_JNI_FALSE
;
81 static sjme_jboolean
sjme_unitOperatorLessEqual(size_t size
, void* a
, void* b
)
86 if (size
== sizeof(sjme_jint
))
87 return *((sjme_jint
*)a
) <= *((sjme_jint
*)b
);
92 return ja
->lo
<= jb
->lo
;
93 return SJME_JNI_FALSE
;
96 static sjme_jboolean
sjme_unitOperatorGreaterThan(size_t size
, void* a
, void* b
)
101 if (size
== sizeof(sjme_jint
))
102 return *((sjme_jint
*)a
) > *((sjme_jint
*)b
);
106 if (ja
->hi
>= jb
->hi
)
107 return ja
->lo
> jb
->lo
;
108 return SJME_JNI_FALSE
;
111 static sjme_jboolean
sjme_unitOperatorGreaterEqual(size_t size
, void* a
, void* b
)
116 if (size
== sizeof(sjme_jint
))
117 return *((sjme_jint
*)a
) >= *((sjme_jint
*)b
);
121 if (ja
->hi
>= jb
->hi
)
122 return ja
->lo
>= jb
->lo
;
123 return SJME_JNI_FALSE
;
126 /** Operator information. */
127 const sjme_unitOperatorInfo sjme_unitOperatorInfos
[SJME_NUM_UNIT_OPERATORS
] =
129 /* SJME_UNIT_OPERATOR_EQUAL. */
132 sjme_unitOperatorEqual
135 /* SJME_UNIT_OPERATOR_NOT_EQUAL. */
138 sjme_unitOperatorNotEqual
141 /* SJME_UNIT_OPERATOR_LESS_THAN. */
144 sjme_unitOperatorLessThan
147 /* SJME_UNIT_OPERATOR_LESS_EQUAL. */
150 sjme_unitOperatorLessEqual
153 /* SJME_UNIT_OPERATOR_GREATER_THAN. */
156 sjme_unitOperatorGreaterThan
159 /* SJME_UNIT_OPERATOR_GREATER_EQUAL. */
162 sjme_unitOperatorGreaterEqual
166 static sjme_jboolean
sjme_unitShortingEmit(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
167 sjme_attrInNotNull sjme_test
* test
,
168 sjme_attrInRange(SJME_TEST_RESULT_PASS
, SJME_TEST_RESULT_FAIL
)
169 sjme_testResult type
,
170 sjme_attrInNullable
const char* format
, va_list vaArgs
)
173 return sjme_die("No test structure?");
176 sjme_messageV(file
, line
, func
, format
, vaArgs
);
178 /* Hit abort for debugging. */
181 /* Jump back to the outer code. */
182 longjmp(test
->jumpPoint
, type
);
185 return (type
== SJME_TEST_RESULT_PASS
);
188 sjme_testResult
sjme_unitOperatorIR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
189 sjme_attrInValue sjme_unitOperator
operator,
190 sjme_attrInNotNull sjme_test
* test
,
191 sjme_attrInValue sjme_jint a
,
192 sjme_attrInValue sjme_jint b
,
193 sjme_attrInNullable sjme_attrFormatArg
const char* format
, ...)
196 const sjme_unitOperatorInfo
* opInfo
;
198 if (operator < 0 || operator >= SJME_NUM_UNIT_OPERATORS
)
199 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
200 opInfo
= &sjme_unitOperatorInfos
[operator];
202 if (!opInfo
->function(sizeof(sjme_jint
), &a
, &b
))
204 sjme_messageR(file
, line
, func
, "ASSERT: %d %s %d",
205 a
, opInfo
->symbol
, b
);
206 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
209 return SJME_TEST_RESULT_PASS
;
212 sjme_testResult
sjme_unitOperatorLR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
213 sjme_attrInValue sjme_unitOperator
operator,
214 sjme_attrInNotNull sjme_test
* test
,
215 sjme_attrInNullable sjme_jobject a
,
216 sjme_attrInNullable sjme_jobject b
,
217 sjme_attrInNullable sjme_attrFormatArg
const char* format
, ...)
220 const sjme_unitOperatorInfo
* opInfo
;
222 if (operator < 0 || operator >= SJME_NUM_UNIT_OPERATORS
)
223 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
224 opInfo
= &sjme_unitOperatorInfos
[operator];
226 if (!opInfo
->function(sizeof(sjme_jobject
), &a
, &b
))
228 sjme_messageR(file
, line
, func
, "ASSERT: %p %s %p",
229 a
, opInfo
->symbol
, b
);
230 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
233 return SJME_TEST_RESULT_PASS
;
236 sjme_testResult
sjme_unitOperatorPR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
237 sjme_attrInValue sjme_unitOperator
operator,
238 sjme_attrInNotNull sjme_test
* test
,
239 sjme_attrInNullable
void* a
,
240 sjme_attrInNullable
void* b
,
241 sjme_attrInNullable sjme_attrFormatArg
const char* format
, ...)
244 const sjme_unitOperatorInfo
* opInfo
;
246 if (operator < 0 || operator >= SJME_NUM_UNIT_OPERATORS
)
247 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
248 opInfo
= &sjme_unitOperatorInfos
[operator];
250 if (!opInfo
->function(sizeof(void*), &a
, &b
))
252 sjme_messageR(file
, line
, func
, "ASSERT: %p %s %p",
253 a
, opInfo
->symbol
, b
);
254 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
257 return SJME_TEST_RESULT_PASS
;
260 sjme_testResult
sjme_unitFailR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
261 sjme_attrInNotNull sjme_test
* test
,
262 sjme_attrInNullable sjme_attrFormatArg
const char* format
, ...)
266 /* Test always fails. */
267 sjme_messageR(file
, line
, func
, "ASSERT: Fail");
268 SJME_VA_SHORT(SJME_TEST_RESULT_FAIL
);
269 return SJME_TEST_RESULT_FAIL
;
272 sjme_testResult
sjme_unitSkipR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
273 sjme_attrInNotNull sjme_test
* test
,
274 sjme_attrInNullable sjme_attrFormatArg
const char* format
, ...)
278 /* Test always fails. */
279 sjme_messageR(file
, line
, func
, "Skipped test.");
280 SJME_VA_SHORT(SJME_TEST_RESULT_SKIP
);
281 return SJME_TEST_RESULT_SKIP
;