libose
Loading...
Searching...
No Matches
ose_test_common.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020-23 John MacCallum Permission is hereby granted,
3 free of charge, to any person obtaining a copy of this software
4 and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the
6 rights to use, copy, modify, merge, publish, distribute,
7 sublicense, and/or sell copies of the Software, and to permit
8 persons to whom the Software is furnished to do so, subject to the
9 following conditions:
10
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22*/
23
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <stdint.h>
69#include <stdarg.h>
70#include <signal.h>
71#include <setjmp.h>
72#include <time.h>
73
74#include "ose.h"
75#include "ose_util.h"
76#include "ose_context.h"
77#include "ose_print.h"
78#include "ose_pprint.h"
79
80
85#define ANSI_COLOR_RED "\x1b[31m"
86#define ANSI_COLOR_GREEN "\x1b[32m"
87#define ANSI_COLOR_YELLOW "\x1b[33m"
88#define ANSI_COLOR_BLUE "\x1b[34m"
89#define ANSI_COLOR_MAGENTA "\x1b[35m"
90#define ANSI_COLOR_CYAN "\x1b[36m"
91#define ANSI_COLOR_RESET "\x1b[0m"
// DOXYGEN_SKIP
93
102const int32_t ASSERTION_FAILED = 0x66666666;
103
109const int32_t ASSERTION_PASSED = 0x33333333;
110
123jmp_buf env;
124
129int grand_total_failctr;
130
135int grand_total_testctr;
136
141int test_functions_skipped;
142
147int failctr;
148
153int testctr;
154
159int tests_skipped;
160
169int verbose = 0;
170
174#define MAX_BNDLSIZE 1024
175
179#define COMPARE_BUNDLES(refbndl, testbndl) \
180 memcmp(refbndl, testbndl, 4 + ntohl(*((int32_t *)refbndl)))
181
183void sighandler(int signo)
184{
185 if(signo == SIGABRT){
186 longjmp(env, ASSERTION_FAILED);
187 }
188}
// DOXYGEN_SKIP
190
195static char *align(char *p)
196{
197 while((long)p % 4){
198 p++;
199 }
200 return p;
201}
202
215static void pbndl(ose_bundle bundle, const char * const str)
216{
217 char buf[65536];
218 memset(buf, 0, 65536);
219 ose_pprintBundle(bundle, buf, 65536);
220 fprintf(stderr, "\n\r%s>>>>>\n\r%s\n\r%s<<<<<\n\r",
221 str, buf, str);
222}
223
237static void pbytes(ose_bundle bundle, int32_t start, int32_t end)
238{
239 char *b = ose_getBundlePtr(bundle);
240 for(int32_t i = start; i < end; i++){
241 fprintf(stderr, "%d: %c %d\n\r", i,
242 (unsigned char)b[i],
243 (unsigned char)b[i]);
244 }
245}
246
250static void print_passed(const char * const fname)
251{
252 printf(ANSI_COLOR_GREEN "%d / %d tests PASSED" ANSI_COLOR_RESET "\n",
253 testctr - tests_skipped, testctr);
254 if(tests_skipped){
255 printf(ANSI_COLOR_YELLOW "%d tests SKIPPED" ANSI_COLOR_RESET "\n",
256 tests_skipped);
257 }
258}
259
263static void print_failed(const char * const fname)
264{
265 printf(ANSI_COLOR_RED "...%d of %d tests FAILED" ANSI_COLOR_RESET "\n",
266 failctr, testctr);
267}
268
272static void print_results(const char * const fname)
273{
274 if(!testctr){
275 printf(ANSI_COLOR_RED
276 "NO TESTS PERFORMED for this unit"
277 ANSI_COLOR_RESET
278 "\n");
279 return;
280 }
281 if(!failctr){
282 print_passed(fname);
283 }else{
284 print_failed(fname);
285 }
286}
287
292static void print_startTest(const char * const fname)
293{
294 failctr = 0;
295 testctr = 0;
296 printf(ANSI_COLOR_CYAN
297 "Testing %s..."
298 ANSI_COLOR_RESET
299 "\n",
300 fname);
301}
302
314#define TEST_FUNCTION(pfx, fn) \
315 { \
316 tests_skipped = 0; \
317 grand_total_testctr++; \
318 print_startTest(#fn); \
319 int fc = failctr; \
320 pfx ## fn(); \
321 print_results(#fn); \
322 if(fc != failctr){ \
323 grand_total_failctr++; \
324 } \
325 }
326
337#define SKIP_TEST_FUNCTION(fn) \
338 test_functions_skipped++; \
339 grand_total_testctr++; \
340 printf(ANSI_COLOR_YELLOW \
341 "SKIPPING %s : " \
342 ANSI_COLOR_RESET \
343 "\n", \
344 #fn)
345
357#define SKIP_TEST(test, expected_result) \
358 { \
359 tests_skipped++; \
360 testctr++; \
361 printf(ANSI_COLOR_YELLOW \
362 "\nSKIPPING test %s" \
363 ANSI_COLOR_RESET \
364 "\n", \
365 #test); \
366 }
367
374static void init(void)
375{
376 signal(SIGABRT, sighandler);
377 /* memset(true256, OSETT_TRUE, 256); */
378 /* memset(false256, OSETT_FALSE, 256); */
379 srand(time(NULL));
380 grand_total_testctr = grand_total_failctr = 0;
381 test_functions_skipped = tests_skipped = 0;
382}
383
395static int finalize(void)
396{
397 printf("\n**************************************************\n");
398 if(!grand_total_failctr){
399 printf(ANSI_COLOR_GREEN
400 "%d / %d TESTS PASSED"
401 ANSI_COLOR_RESET
402 "\n",
403 grand_total_testctr - test_functions_skipped,
404 grand_total_testctr);
405 if(test_functions_skipped){
406 printf(ANSI_COLOR_YELLOW
407 "%d TESTS SKIPPED"
408 ANSI_COLOR_RESET
409 "\n",
410 test_functions_skipped);
411 }
412 }else{
413 printf(ANSI_COLOR_RED
414 "%d / %d TESTS FAILED"
415 ANSI_COLOR_RESET
416 "\n",
417 grand_total_failctr,
418 grand_total_testctr);
419 }
420 printf("**************************************************\n");
421 return grand_total_failctr ? 1 : 0;
422}
provides global definitions and types
contains functions that structure an OSC bundle for use with other parts of this library
Rendering a bundle for a human to read.
int verbose
Enable / disable verbosity.
Definition ose_test_common.h:169
static void init(void)
Initialize the testing system.
Definition ose_test_common.h:374
const int32_t ASSERTION_PASSED
The return code for a passed assertion.
Definition ose_test_common.h:109
static void print_failed(const char *const fname)
Print a message indicating that the current test failed.
Definition ose_test_common.h:263
static void pbndl(ose_bundle bundle, const char *const str)
Format and print the contents of a bundle to stderr.
Definition ose_test_common.h:215
static int finalize(void)
Finalize the testing system.
Definition ose_test_common.h:395
static void print_passed(const char *const fname)
Print a message indicating that the current test passed.
Definition ose_test_common.h:250
static void print_startTest(const char *const fname)
Initialize global variables and print a preamble for a test.
Definition ose_test_common.h:292
static void print_results(const char *const fname)
Print the results of the current test.
Definition ose_test_common.h:272
const int32_t ASSERTION_FAILED
The return code for a failed assertion.
Definition ose_test_common.h:102
static void pbytes(ose_bundle bundle, int32_t start, int32_t end)
Print each byte of a bundle on a separate line to stderr.
Definition ose_test_common.h:237
contains basic utilities for reading, writing, and querying an OSC bundle
Definition ose.h:374