-
Notifications
You must be signed in to change notification settings - Fork 5
/
unittest.h
1564 lines (1354 loc) · 36.3 KB
/
unittest.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file unittest.h
*
* @brief single-header unittesting framework for pure C99 codes
*
* @author Hajime Suzuki
* @date 2016/3/1
* @license MIT
*
* @detail
* latest version is found at https://github.com/ocxtal/unittest.h.
* see README.md for the details.
*/
#pragma once /* instead of include guard */
#ifndef UNITTEST
#define UNITTEST 1
#endif
#ifndef UNITTEST_ALIAS_MAIN
#define UNITTEST_ALIAS_MAIN 0
#endif
/* for compatibility with -std=c99 (2016/4/26 by Hajime Suzuki) */
#if defined(__linux__) && !defined(_POSIX_C_SOURCE)
# define _POSIX_C_SOURCE 200112L
#endif
#if defined(__darwin__) && !defined(_BSD_SOURCE)
# define _BSD_SOURCE
#endif
/* end */
#include <alloca.h>
#include <ctype.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h> /* offsetof */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#ifndef UNITTEST_UNIQUE_ID
#define UNITTEST_UNIQUE_ID 0
#endif
/**
* from kvec.h in klib (https://github.com/attractivechaos/klib)
* a little bit modified from the original
*/
#define utkv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
#define UNITTEST_KV_INIT ( 64 )
/**
* output coloring
*/
#define UT_RED "\x1b[31m"
#define UT_GREEN "\x1b[32m"
#define UT_YELLOW "\x1b[33m"
#define UT_BLUE "\x1b[34m"
#define UT_MAGENTA "\x1b[35m"
#define UT_CYAN "\x1b[36m"
#define UT_WHITE "\x1b[37m"
#define UT_DEFAULT_COLOR "\x1b[39m"
#define ut_color(c, x) c x UT_DEFAULT_COLOR
/* static assertion macros */
#define ut_sa_cat_intl(x, y) x##y
#define ut_sa_cat(x, y) ut_sa_cat_intl(x, y)
#define ut_static_assert(expr) typedef char ut_sa_cat(ut_st, __LINE__)[(expr) ? 1 : -1]
/**
* basic vectors (utkv_*)
*/
#define utkvec_t(type) struct { uint64_t n, m; type *a; }
#define utkv_init(v) ( (v).n = 0, (v).m = UNITTEST_KV_INIT, (v).a = (__typeof__((v).a))calloc((v).m, sizeof(*(v).a)) )
#define utkv_destroy(v) { free((v).a); (v).a = NULL; }
// #define utkv_A(v, i) ( (v).a[(i)] )
#define utkv_pop(v) ( (v).a[--(v).n] )
#define utkv_size(v) ( (v).n )
#define utkv_max(v) ( (v).m )
#define utkv_clear(v) ( (v).n = 0 )
#define utkv_resize(v, s) ( \
(v).m = (s), (v).a = (__typeof__((v).a))realloc((v).a, sizeof(*(v).a) * (v).m) )
#define utkv_reserve(v, s) ( \
(v).m > (s) ? 0 : ((v).m = (s), (v).a = (__typeof__((v).a))realloc((v).a, sizeof(*(v).a) * (v).m), 0) )
#define utkv_copy(v1, v0) do { \
if ((v1).m < (v0).n) utkv_resize(v1, (v0).n); \
(v1).n = (v0).n; \
memcpy((v1).a, (v0).a, sizeof(*(v).a) * (v0).n); \
} while (0) \
#define utkv_push(v, x) do { \
if ((v).n == (v).m) { \
(v).m = (v).m * 2; \
(v).a = (__typeof__((v).a))realloc((v).a, sizeof(*(v).a) * (v).m); \
} \
(v).a[(v).n++] = (x); \
} while (0)
#define utkv_pushp(v) ( \
((v).n == (v).m) ? \
((v).m = (v).m * 2, \
(v).a = realloc((v).a, sizeof(*(v).a) * (v).m), 0) \
: 0), ( (v).a + ((v).n++) )
#define utkv_a(v, i) ( \
((v).m <= (size_t)(i) ? \
((v).m = (v).n = (i) + 1, utkv_roundup32((v).m), \
(v).a = realloc((v).a, sizeof(*(v).a) * (v).m), 0) \
: (v).n <= (size_t)(i) ? (v).n = (i) + 1 \
: 0), (v).a[(i)])
/** bound-unchecked accessor */
#define utkv_at(v, i) ( (v).a[(i)] )
#define utkv_ptr(v) ( (v).a )
/**
* forward declaration of the main function
*/
int main(int argc, char *argv[]);
/**
* @struct ut_result_s
*/
struct ut_result_s {
int64_t cnt;
int64_t succ;
int64_t fail;
};
struct ut_global_config_s;
struct ut_group_config_s;
struct ut_s;
struct ut_result_s;
struct ut_printer_s {
/* printers */
void (*failed)(
struct ut_s const *info,
struct ut_global_config_s const *gconf,
struct ut_group_config_s const *config,
int64_t line,
char const *func,
char const *expr,
char const *fmt,
...);
void (*result)(
struct ut_global_config_s const *gconf,
struct ut_group_config_s const *config,
struct ut_result_s const *result,
int64_t file_cnt);
};
/**
* @struct ut_global_config_s
*/
struct ut_global_config_s {
FILE *fp;
struct ut_printer_s printer;
uint64_t threads;
};
/**
* @struct ut_group_config_s
*
* @brief unittest scope config
*/
struct ut_group_config_s {
/* internal use */
char const *file;
int64_t unique_id;
uint64_t line;
int64_t exec;
uint64_t _pad[4];
/* dependency resolution */
char const *name;
char const *depends_on[16];
/* environment setup and cleanup */
void *(*init)(void *params);
void (*clean)(void *context);
void *params;
};
/**
* @struct ut_s
*
* @brief unittest function config
*/
struct ut_s {
/* for internal use */
char const *file;
int64_t unique_id;
uint64_t line;
int64_t exec;
/* per-function config */
void (*fn)(
void *ctx,
void *gctx,
struct ut_s *info,
struct ut_global_config_s const *ut_gconf,
struct ut_group_config_s const *config);
/* internal use (cont'd) */
uint64_t index;
uint64_t succ, fail; /* result: 1 when succeeded, 2 when failed */
/* dependency resolution */
char const *name;
char const *depends_on[16];
/* environment setup and cleanup */
void *(*init)(void *params);
void (*clean)(void *context);
void *params;
};
/* the two structs must be castable */
ut_static_assert(offsetof(struct ut_group_config_s, file) == offsetof(struct ut_s, file));
ut_static_assert(offsetof(struct ut_group_config_s, unique_id) == offsetof(struct ut_s, unique_id));
ut_static_assert(offsetof(struct ut_group_config_s, line) == offsetof(struct ut_s, line));
ut_static_assert(offsetof(struct ut_group_config_s, exec) == offsetof(struct ut_s, exec));
ut_static_assert(offsetof(struct ut_group_config_s, name) == offsetof(struct ut_s, name));
ut_static_assert(offsetof(struct ut_group_config_s, depends_on) == offsetof(struct ut_s, depends_on));
ut_static_assert(offsetof(struct ut_group_config_s, init) == offsetof(struct ut_s, init));
ut_static_assert(offsetof(struct ut_group_config_s, clean) == offsetof(struct ut_s, clean));
ut_static_assert(offsetof(struct ut_group_config_s, params) == offsetof(struct ut_s, params));
/**
* @macro ut_unused
* @brief declare the variable is unused in the function
*/
#define ut_unused(x) (void)(x);
/**
* @macro ut_null_replace
* @brief replace pointer with "(null)"
*/
#define ut_null_replace(ptr, str) ( (ptr) == NULL ? (str) : (ptr) )
/**
* @macro ut_build_name
*
* @brief an utility macro to make unique name
*/
#define ut_join_name(a, b, c) a##b##_##c
#define ut_build_name(prefix, num, id) ut_join_name(prefix, num, id)
/**
* @macro unittest
*
* @brief instanciate a unittest object
*/
#define UNITTEST_ARG_DECL \
void *ctx, \
void *gctx, \
struct ut_s *ut_info, \
struct ut_global_config_s const *ut_gconf, \
struct ut_group_config_s const *ut_config
#define UNITTEST_ARG_LIST ctx, gctx, ut_info, ut_gconf, ut_config
#if UNITTEST != 0
#define unittest(...) \
static void ut_build_name(ut_body_, UNITTEST_UNIQUE_ID, __LINE__)(UNITTEST_ARG_DECL); \
static struct ut_s const ut_build_name(ut_info_, UNITTEST_UNIQUE_ID, __LINE__) = { \
/* file, unique_id, line, exec, fn, name, depends_on */ \
__FILE__, UNITTEST_UNIQUE_ID, __LINE__, 1, ut_build_name(ut_body_, UNITTEST_UNIQUE_ID, __LINE__), \
0, 0, 0, __VA_ARGS__ \
}; \
struct ut_s ut_build_name(ut_get_info_, UNITTEST_UNIQUE_ID, __LINE__)(void) \
{ \
return(ut_build_name(ut_info_, UNITTEST_UNIQUE_ID, __LINE__)); \
} \
static void ut_build_name(ut_body_, UNITTEST_UNIQUE_ID, __LINE__)(UNITTEST_ARG_DECL)
#else /* UNITTEST != 0 */
#define unittest(...) \
static void ut_build_name(ut_body_, UNITTEST_UNIQUE_ID, __LINE__)(UNITTEST_ARG_DECL)
#endif /* UNITTEST != 0 */
/**
* @macro unittest_config
*
* @brief scope configuration
*/
#if UNITTEST != 0
#define unittest_config(...) \
static struct ut_group_config_s const ut_build_name(ut_config_, UNITTEST_UNIQUE_ID, __LINE__) = { \
.file = __FILE__, \
.line = __LINE__, \
.unique_id = UNITTEST_UNIQUE_ID, \
__VA_ARGS__ \
}; \
struct ut_group_config_s ut_build_name(ut_get_config_, UNITTEST_UNIQUE_ID, 0)(void) \
{ \
return(ut_build_name(ut_config_, UNITTEST_UNIQUE_ID, __LINE__)); \
}
#else /* UNITTEST != 0 */
#define unittest_config(...)
#endif /* UNITTEST != 0 */
/* assertion failed message printers */
static
void ut_print_assertion_failed(
struct ut_s const *info,
struct ut_global_config_s const *gconf,
struct ut_group_config_s const *config,
int64_t line,
char const *func,
char const *expr,
char const *fmt,
...)
{
ut_unused(func);
va_list l;
va_start(l, fmt);
fprintf(gconf->fp,
ut_color(UT_YELLOW, "assertion failed") ": [%s] %s:" ut_color(UT_BLUE, "%" PRId64 "") " (%s) `" ut_color(UT_MAGENTA, "%s") "'",
ut_null_replace(config->name, "no name"),
ut_null_replace(info->file, "(unknown filename)"),
line,
ut_null_replace(info->name, "no name"),
expr);
if(strlen(fmt) != 0) {
fprintf(gconf->fp, ", ");
vfprintf(gconf->fp, fmt, l);
}
fprintf(gconf->fp, "\n");
va_end(l);
return;
}
static
void ut_print_assertion_failed_json(
struct ut_s const *info,
struct ut_global_config_s const *gconf,
struct ut_group_config_s const *config,
int64_t line,
char const *func,
char const *expr,
char const *fmt,
...)
{
ut_unused(config);
ut_unused(func);
va_list l;
va_start(l, fmt);
fprintf(gconf->fp, "{ \"tag\": \"fail\", ");
if(config->name != NULL) {
fprintf(gconf->fp, "\"group\": \"%s\", ", config->name);
}
if(config->file != NULL) {
fprintf(gconf->fp, "\"filename\": \"%s\", ", config->file);
}
fprintf(gconf->fp, "\"line\": %" PRId64 ", ", line);
if(info->name != NULL) {
fprintf(gconf->fp, "\"name\": \"%s\", ", info->name);
}
fprintf(gconf->fp, "\"expr\": \"%s\", ", expr);
if(strlen(fmt) != 0) {
fprintf(gconf->fp, "\"debugprint\": \"");
vfprintf(gconf->fp, fmt, l); /* FIXME: escape */
fprintf(gconf->fp, "\", ");
}
fprintf(gconf->fp, "},\n");
va_end(l);
return;
}
/* summary printers */
static
void ut_print_results(
struct ut_global_config_s const *gconf,
struct ut_group_config_s const *config,
struct ut_result_s const *result,
int64_t file_cnt)
{
int64_t cnt = 0;
int64_t succ = 0;
int64_t fail = 0;
for(int64_t i = 0, j = 0; i < file_cnt; i++) {
if(config[i].exec == 0) { continue; }
fprintf(gconf->fp, "%sGroup %s: %" PRId64 " succeeded, %" PRId64 " failed in total %" PRId64 " assertions in %" PRId64 " tests.%s\n",
(result[j].fail == 0) ? UT_GREEN : UT_RED,
ut_null_replace(config[i].name, "(no name)"),
result[j].succ,
result[j].fail,
result[j].succ + result[j].fail,
result[j].cnt,
UT_DEFAULT_COLOR);
cnt += result[j].cnt;
succ += result[j].succ;
fail += result[j].fail;
j++;
}
fprintf(gconf->fp, "%sSummary: %" PRId64 " succeeded, %" PRId64 " failed in total %" PRId64 " assertions in %" PRId64 " tests.%s\n",
(fail == 0) ? UT_GREEN : UT_RED,
succ, fail, succ + fail, cnt,
UT_DEFAULT_COLOR);
return;
}
static
void ut_print_results_json(
struct ut_global_config_s const *gconf,
struct ut_group_config_s const *config,
struct ut_result_s const *result,
int64_t file_cnt)
{
int64_t cnt = 0;
int64_t succ = 0;
int64_t fail = 0;
fprintf(gconf->fp, "{ \"tag\": \"results\", [ ");
for(int64_t i = 0, j = 0; i < file_cnt; i++) {
if(config[i].exec == 0) { continue; }
if(config->name != NULL) {
fprintf(gconf->fp, "{ \"group\": \"%s\", ", config->name);
}
if(config->file != NULL) {
fprintf(gconf->fp, "\"filename\": \"%s\", ", config->file);
}
fprintf(gconf->fp, "\"succeeded\": %" PRId64 ", ", result[j].succ);
fprintf(gconf->fp, "\"failed\": %" PRId64 ", ", result[j].fail);
fprintf(gconf->fp, "\"assertioncount\": %" PRId64 ", ", result[j].succ + result[j].fail);
fprintf(gconf->fp, "\"testcount\": %" PRId64 ", ", result[j].cnt);
fprintf(gconf->fp, "}, ");
cnt += result[j].cnt;
succ += result[j].succ;
fail += result[j].fail;
j++;
}
fprintf(gconf->fp, "] },\n");
fprintf(gconf->fp, "{ \"tag\": \"summary\", ");
fprintf(gconf->fp, "\"succeeded\": %" PRId64 ", ", succ);
fprintf(gconf->fp, "\"failed\": %" PRId64 ", ", fail);
fprintf(gconf->fp, "\"assertioncount\": %" PRId64 ", ", succ + fail);
fprintf(gconf->fp, "\"testcount\": %" PRId64 ", ", cnt);
fprintf(gconf->fp, "},\n");
return;
}
static
struct ut_printer_s ut_default_printer = {
.failed = ut_print_assertion_failed,
.result = ut_print_results
};
static
struct ut_printer_s ut_json_printer = {
.failed = ut_print_assertion_failed_json,
.result = ut_print_results_json
};
/**
* memory dump macro
*/
#define ut_dump(ptr, len) ({ \
uint64_t _size = (((len) + 15) / 16 + 1) * \
(strlen("0x0123456789abcdef:") + 16 * strlen(" 00a") + strlen(" \n+ margin")) \
+ strlen(#ptr) + strlen("\n`' len: 100000000"); \
uint8_t *_ptr = (uint8_t *)(ptr); \
char *_str = alloca(_size); \
char *_s = _str; \
/* make header */ \
_s += sprintf(_s, "\n`%s' len: %" PRIu64 "\n", #ptr, (uint64_t)len); \
_s += sprintf(_s, " "); \
for(uint64_t i = 0; i < 16; i++) { \
_s += sprintf(_s, " %02x", (uint8_t)i); \
} \
_s += sprintf(_s, "\n"); \
for(uint64_t i = 0; i < ((len) + 15) / 16; i++) { \
_s += sprintf(_s, "0x%016" PRIx64 ":", (uint64_t)_ptr); \
for(uint64_t j = 0; j < 16; j++) { \
_s += sprintf(_s, " %02x", (uint8_t)_ptr[j]); \
} \
_s += sprintf(_s, " "); \
for(uint64_t j = 0; j < 16; j++) { \
_s += sprintf(_s, "%c", isprint(_ptr[j]) ? _ptr[j] : ' '); \
} \
_s += sprintf(_s, "\n"); \
_ptr += 16; \
} \
(char const *)_str; \
})
#ifndef dump
// #define dump ut_dump
#endif
/**
* assertion macro
*/
#define ut_assert(expr, ...) { \
if(expr) { \
ut_info->succ++; \
} else { \
ut_info->fail++; \
/* dump debug information */ \
ut_gconf->printer.failed(ut_info, ut_gconf, ut_config, __LINE__, __func__, #expr, "" __VA_ARGS__); \
} \
}
#ifndef assert
#define assert ut_assert
#endif
/**
* @struct ut_nm_result_s
* @brief parsed result container
*/
struct ut_nm_result_s {
void *ptr;
char type;
char name[255];
};
static inline
int ut_strcmp(
char const *a,
char const *b)
{
/* if both are NULL */
if(a == NULL && b == NULL) {
return(0);
}
if(b == NULL) {
return(1);
}
if(a == NULL) {
return(-1);
}
return(strcmp(a, b));
}
static inline
char *ut_build_nm_cmd(
char const *filename)
{
uint64_t const filename_len_limit = 1024;
char const *cmd_base = "nm ";
/* check the length of the filename */
if(strlen(filename) > filename_len_limit) {
return(NULL);
}
/* cat name */
char *cmd = (char *)malloc(strlen(cmd_base) + strlen(filename) + 1);
strcpy(cmd, cmd_base);
strcat(cmd, filename);
return(cmd);
}
static inline
char *ut_dump_file(
FILE *fp)
{
int c;
utkvec_t(char) buf;
utkv_init(buf);
while((c = getc(fp)) != EOF) {
utkv_push(buf, c);
}
/* push terminator */
utkv_push(buf, '\0');
return(utkv_ptr(buf));
}
static inline
char *ut_dump_nm_output(
char const *filename)
{
char *cmd = NULL;
FILE *fp = NULL;
char *res = NULL;
/* build command */
if((cmd = ut_build_nm_cmd(filename)) == NULL) {
goto _ut_nm_error_handler;
}
/* open */
if((fp = popen(cmd, "r")) == NULL) {
fprintf(stderr, ut_color(UT_RED, "ERROR") ": failed to open pipe.\n");
goto _ut_nm_error_handler;
}
/* dump */
if((res = ut_dump_file(fp)) == NULL) {
fprintf(stderr, ut_color(UT_RED, "ERROR") ": failed to read nm output.\n");
goto _ut_nm_error_handler;
}
/* close file */
if(pclose(fp) != 0) {
fprintf(stderr, ut_color(UT_RED, "ERROR") ": failed to close pipe.\n");
goto _ut_nm_error_handler;
}
free(cmd); cmd = NULL;
return(res);
_ut_nm_error_handler:
if(cmd != NULL) { free(cmd); }
if(fp != NULL) { pclose(fp); }
if(res != NULL) { free(res); }
return(NULL);
}
static inline
struct ut_nm_result_s *ut_parse_nm_output(
char const *str)
{
char const *p = str;
utkvec_t(struct ut_nm_result_s) buf;
utkv_init(buf);
while(*p != '\0') {
struct ut_nm_result_s r;
/* check the sanity of the line */
char const *sp = p;
while(*sp != '\r' && *sp != '\n' && *sp != '\0') { sp++; }
// printf("%p, %p, %" PRId64 "\n", p, sp, (int64_t)(sp - p));
if((int64_t)(sp - p) < 1) { break; }
/* if the first character of the line is space, pass the PTR state */
if(!isspace(*p)) {
char *np;
r.ptr = (void *)((uint64_t)strtoll(p, &np, 16));
/* advance pointer */
p = np;
// printf("ptr field found: %p\n", r.ptr);
} else {
r.ptr = NULL;
// printf("ptr field not found\n");
}
/* parse type */
while(isspace(*p)) { p++; }
r.type = *p++;
// printf("type found %c\n", r.type);
/* parse name */
while(isspace(*p)) { p++; }
if(*p == '_') { p++; }
int name_idx = 0;
while(*p != '\n' && name_idx < 254) {
r.name[name_idx++] = *p++;
}
r.name[name_idx] = '\0';
// printf("name found %s\n", r.name);
/* adjust the pointer to the head of the next line */
while(*p == '\r' || *p == '\n') { p++; }
/* push */
utkv_push(buf, r);
}
/* push terminator */
utkv_push(buf, (struct ut_nm_result_s){ 0 });
return(utkv_ptr(buf));
}
static inline
struct ut_nm_result_s *ut_nm(
char const *filename)
{
char const *str = NULL;
struct ut_nm_result_s *res = NULL;
if((str = ut_dump_nm_output(filename)) == NULL) {
return(NULL);
}
res = ut_parse_nm_output(str);
free((void *)str);
return(res);
}
static inline
int ut_startswith(char const *str, char const *prefix)
{
while(*str != '\0' && *prefix != '\0') {
if(*str++ != *prefix++) { return(1); }
}
return(*prefix == '\0' ? 0 : 1);
}
static inline
struct ut_s *ut_get_unittest(
struct ut_nm_result_s const *res)
{
/* extract offset */
uintptr_t offset = (uintptr_t)-1LL;
struct ut_nm_result_s const *r = res;
while(r->type != (char)0) {
if(ut_strcmp("main", r->name) == 0) {
offset = (uintptr_t)main - (uintptr_t)r->ptr;
}
r++;
}
if(offset == (uintptr_t)-1LL) {
return(NULL);
}
#define ut_get_info_call_func(_ptr, _offset) ( \
(struct ut_s (*)(void))((uintptr_t)(_ptr) + (uintptr_t)(_offset)) \
)
/* get info */
utkvec_t(struct ut_s) buf;
r = res;
utkv_init(buf);
while(r->type != (char)0) {
if(ut_startswith(r->name, "ut_get_info_") == 0) {
struct ut_s i = ut_get_info_call_func(r->ptr, offset)();
utkv_push(buf, i);
}
r++;
}
/* push terminator */
utkv_push(buf, (struct ut_s){ 0 });
return(utkv_ptr(buf));
}
static inline
struct ut_group_config_s *ut_get_ut_config(
struct ut_nm_result_s const *res)
{
/* extract offset */
uintptr_t offset = (uintptr_t)-1LL;
struct ut_nm_result_s const *r = res;
while(r->type != (char)0) {
if(ut_strcmp("main", r->name) == 0) {
offset = (uintptr_t)main - (uintptr_t)r->ptr;
}
r++;
}
if(offset == (uintptr_t)-1LL) {
return(NULL);
}
#define ut_get_config_call_func(_ptr, _offset) ( \
(struct ut_group_config_s (*)(void))((uintptr_t)(_ptr) + (uintptr_t)(_offset)) \
)
/* get info */
utkvec_t(struct ut_group_config_s) buf;
r = res;
utkv_init(buf);
while(r->type != (char)0) {
if(ut_startswith(r->name, "ut_get_config_") == 0) {
struct ut_group_config_s i = ut_get_config_call_func(r->ptr, offset)();
utkv_push(buf, i);
}
r++;
}
/* push terminator */
utkv_push(buf, (struct ut_group_config_s){ 0 });
return(utkv_ptr(buf));
}
static inline
void ut_dump_test(
struct ut_s const *test)
{
struct ut_s const *t = test;
while(t->file != NULL) {
printf("%s, %" PRIu64 ", %" PRId64 ", %s, %s, %p, %p\n",
t->file,
t->line,
t->unique_id,
t->name,
t->depends_on[0],
(void *)t->init,
(void *)t->clean);
t++;
}
return;
}
static inline
void ut_dump_config(
struct ut_group_config_s const *config)
{
struct ut_group_config_s const *c = config;
while(c->file != NULL) {
printf("%s, %" PRId64 ", %s, %s, %p, %p\n",
c->file,
c->unique_id,
c->name,
c->depends_on[0],
(void *)c->init,
(void *)c->clean);
c++;
}
return;
}
static
int ut_compare(
void const *_a,
void const *_b)
{
struct ut_s const *a = (struct ut_s const *)_a;
struct ut_s const *b = (struct ut_s const *)_b;
int64_t comp_res = 0;
/* first sort by file name */
if((comp_res = ut_strcmp(a->file, b->file)) != 0) {
return(comp_res);
}
#define sat(a) ( ((a) > INT32_MAX) ? INT32_MAX : (((a) < INT32_MIN) ? INT32_MIN : (a)) )
if((comp_res = (a->unique_id - b->unique_id)) != 0) {
return(sat(comp_res));
}
#undef sat
/* third sort by name */
if((comp_res = ut_strcmp(a->name, b->name)) != 0) {
return(comp_res);
}
/* last, sort by line number */
return((int)(a->line - b->line));
}
static inline
int ut_match(
void const *_a,
void const *_b)
{
struct ut_s const *a = (struct ut_s *)_a;
struct ut_s const *b = (struct ut_s *)_b;
return((ut_strcmp(a->file, b->file) == 0 && a->unique_id == b->unique_id) ? 0 : 1);
}
static inline
uint64_t ut_get_total_test_count(
struct ut_s const *test)
{
uint64_t cnt = 0;
struct ut_s const *t = test;
while(t->file != NULL) { t++; cnt++; }
return(cnt);
}
static inline
uint64_t ut_get_total_config_count(
struct ut_group_config_s const *config)
{
uint64_t cnt = 0;
struct ut_group_config_s const *c = config;
while(c->file != NULL) { c++; cnt++; }
return(cnt);
}
static inline
uint64_t ut_get_total_file_count(
struct ut_s const *sorted_test)
{
uint64_t cnt = 1;
struct ut_s const *t = sorted_test;
if(t++->file == NULL) {
return(0);
}
while(t->file != NULL) {
// printf("comp %s and %s\n", (t - 1)->file, t->file);
if(ut_match((void *)(t - 1), (void *)t) != 0) {
cnt++;
}
t++;
}
return(cnt);
}
static inline
void ut_sort(
struct ut_s *test,
struct ut_group_config_s *config)
{
// ut_dump_test(test);
qsort(test,
ut_get_total_test_count(test),
sizeof(struct ut_s),
ut_compare);
qsort(config,
ut_get_total_config_count(config),
sizeof(struct ut_group_config_s),
ut_compare);
// ut_dump_test(test);
// printf("%" PRId64 "\n", ut_get_total_file_count(test));
return;
}
static inline
uint64_t *ut_build_file_index(
struct ut_s const *sorted_test)
{
uint64_t cnt = 1;
utkvec_t(uint64_t) idx;
struct ut_s const *t = sorted_test;
utkv_init(idx);
if(t++->file == NULL) {
utkv_push(idx, 0);
utkv_push(idx, -1);
return(utkv_ptr(idx));
}
utkv_push(idx, 0);
while(t->file != NULL) {
// printf("comp %s and %s\n", (t - 1)->name, t->name);
if(ut_match((void *)(t - 1), (void *)t) != 0) {
utkv_push(idx, cnt);
}
cnt++;
t++;
}
utkv_push(idx, cnt);
utkv_push(idx, -1);
return(utkv_ptr(idx));
}
static inline
struct ut_group_config_s *ut_compensate_config(
struct ut_s *sorted_test,
struct ut_group_config_s *sorted_config)
{
// printf("compensate config\n");
// ut_dump_test(sorted_test);
// ut_dump_config(sorted_config);
uint64_t *file_idx = ut_build_file_index(sorted_test);
utkvec_t(struct ut_group_config_s) compd_config;
utkv_init(compd_config);
int64_t i = 0;
// printf("%" PRId64 "\n", file_idx[i]);
struct ut_s const *t = &sorted_test[file_idx[i]];
struct ut_group_config_s const *c = sorted_config;
while(c->file != NULL && t->file != NULL) {