Prefix the JNI types from SquirrelJME with sjme_ so that they can easily be mixed...
[SquirrelJME.git] / nanocoat / tests / src / unit.c
bloba27de133fd1a8ffb70600fb91c86d18196b57996
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 #include <stdarg.h>
11 #include <string.h>
13 #include "unit.h"
15 /** Variable list declarations. */
16 #define SJME_VA_DEF va_list inputVa; \
17 va_list copyVa
19 /**
20 * Performs a short with the given type.
22 * @param type The type of return value.
23 * @since 2023/11/11
25 #define SJME_VA_SHORT(type) do {\
26 va_start(inputVa, format); \
27 sjme_unitShortingEmit(file, line, func, test, (type), format, inputVa); \
28 va_end(inputVa); \
29 } while(SJME_JNI_FALSE)
31 /**
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.
38 * @since 2023/11/20
40 typedef sjme_jboolean (*sjme_unitOperatorFunc)(size_t size, void* a, void* b);
42 /**
43 * Operator comparison information.
45 * @since 2023/11/20
47 typedef struct sjme_unitOperatorInfo
49 /** The operator symbol. */
50 const char* 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)
68 sjme_jlong *ja;
69 sjme_jlong *jb;
71 if (size == sizeof(sjme_jint))
72 return *((sjme_jint*)a) < *((sjme_jint*)b);
74 ja = a;
75 jb = b;
76 if (ja->hi <= jb->hi)
77 return ja->lo < jb->lo;
78 return SJME_JNI_FALSE;
81 static sjme_jboolean sjme_unitOperatorLessEqual(size_t size, void* a, void* b)
83 sjme_jlong *ja;
84 sjme_jlong *jb;
86 if (size == sizeof(sjme_jint))
87 return *((sjme_jint*)a) <= *((sjme_jint*)b);
89 ja = a;
90 jb = b;
91 if (ja->hi <= jb->hi)
92 return ja->lo <= jb->lo;
93 return SJME_JNI_FALSE;
96 static sjme_jboolean sjme_unitOperatorGreaterThan(size_t size, void* a, void* b)
98 sjme_jlong *ja;
99 sjme_jlong *jb;
101 if (size == sizeof(sjme_jint))
102 return *((sjme_jint*)a) > *((sjme_jint*)b);
104 ja = a;
105 jb = 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)
113 sjme_jlong *ja;
114 sjme_jlong *jb;
116 if (size == sizeof(sjme_jint))
117 return *((sjme_jint*)a) >= *((sjme_jint*)b);
119 ja = a;
120 jb = 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. */
131 "==",
132 sjme_unitOperatorEqual
135 /* SJME_UNIT_OPERATOR_NOT_EQUAL. */
137 "!=",
138 sjme_unitOperatorNotEqual
141 /* SJME_UNIT_OPERATOR_LESS_THAN. */
143 "<",
144 sjme_unitOperatorLessThan
147 /* SJME_UNIT_OPERATOR_LESS_EQUAL. */
149 "<=",
150 sjme_unitOperatorLessEqual
153 /* SJME_UNIT_OPERATOR_GREATER_THAN. */
155 ">",
156 sjme_unitOperatorGreaterThan
159 /* SJME_UNIT_OPERATOR_GREATER_EQUAL. */
161 ">=",
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)
172 if (test == NULL)
173 return sjme_die("No test structure?");
175 /* Emit message. */
176 sjme_messageV(file, line, func, format, vaArgs);
178 /* Hit abort for debugging. */
179 abort();
181 /* Jump back to the outer code. */
182 longjmp(test->jumpPoint, type);
184 /* Ignored. */
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, ...)
195 SJME_VA_DEF;
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, ...)
219 SJME_VA_DEF;
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, ...)
243 SJME_VA_DEF;
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, ...)
264 SJME_VA_DEF;
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, ...)
276 SJME_VA_DEF;
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;