-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgidunit.h
2668 lines (2377 loc) · 102 KB
/
gidunit.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 gidunit.h - Header file for a C testing framework that is designed to
make writing thorough tests for C code as simple as possible.
@author - Aaron Leggett
@repo - https://github.com/a-leggett/GIDUnit
@license - This software is available under two licenses:
License 1 - The Unlicense (fulltext below)
License 2 - The MIT license (fulltext below)
You may pick whichever of these two licenses you prefer.
============================ License 1 ============================
The Unlicense
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
============================ License 2 ============================
MIT License
Copyright (c) 2021 Aaron Leggett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@example
#include "gidunit.h"
BEGIN_TEST_SUITE(MyFirstTestSuite)
SetUp()
{
fixture = malloc(sizeof(int));
assert_not_null(fixture);
*(int*)fixture = 1;
}
TearDown()
{
free(fixture);
}
Test(MyFirstTest)
{
assert_not_null(fixture);
assert_int_eq(1, *(int*)fixture);
}
Test(MyParameterizedTest,
IntRow(1, 2, 4, 8, 16)
IntRow(0, 0, 0, 0, 0)
IntRow(1, 1, 1, 1, 2)
EnumParam(i, 3, 6, 9, 12)
StringEnumParam(word, "Alpha", "Bravo", "Charlie", "Delta", "Echo"))
{
//This test will run 3*4*5=60 times
assert_int_eq(1, *(int*)fixture);
assert_int_not_eq(0, int_row[0]);
assert_string_not_eq("Alpha", word);
assert_int_not_eq(12, i);
}
END_TEST_SUITE()
BEGIN_TEST_SUITE(MySecondTestSuite)
Test(MyOtherTest,
StringRow("Alpha", "Bravo", "Charlie")
StringRow("alpha", "bravo", "charlie")
StringRow("ALPHA", "BRAVO", "CHARLIE"))
{
assert_null(fixture);
assert_int_eq(1, 1);
assert_string_not_eq("alpha", string_row[0]);
}
END_TEST_SUITE()
int main()
{
ADD_TEST_SUITE(MyFirstTestSuite);
ADD_TEST_SUITE(MySecondTestSuite);
return gidunit();
}
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#ifndef GIDUNIT_H
#define GIDUNIT_H
/* The maximum length of an assert failure message. */
#define GID_MAX_MESSAGE_LENGTH (256)
/* The maximum length of the string representation of a test configuration
* (that is, the string that shows the values of each variable in a test). */
#define GID_MAX_CONFIGURATION_STRING_LENGTH (256)
/* The amount of corruption-detection padding to add to the ends of memory
* that is allocated by the gid_malloc function. */
#define GID_MALLOC_PADDING (32)
/* Checks how many bytes of memory are equivalent.
* @param a - Pointer to the first memory object.
* @param b - Pointer to the second memory object.
* @param size - The number of bytes to scan.
* @returns - The number of bytes that were equal in both 'a' and 'b'. If the
* returned value is equal to 'size', then both 'a' and 'b' are equal. */
size_t _gid_cmp_memory(const uint8_t* a, const uint8_t* b, size_t size)
{
size_t match = 0;
for(size_t i = 0; i < size; i++)
{
if(a[i] == b[i])
match++;
else
break;
}
return match;
}
/* Clones a string.
* @param src - The null-terminated source string.
* @returns - A newly allocated clone of the 'src' string. */
const char* _gid_strclone(const char* src)
{
if(src == NULL)
return NULL;
size_t len = strlen(src) + 1;//+1 for null terminator
const char* ret = malloc(sizeof(char) * len);
memcpy((char*)ret, (char*)src, len);
return ret;
}
/* Base structure for a test parameter. Each test can have multiple parameters,
* and each test will run with all combinations of parameter values. Parameter
* values are abstractly defined in this structure, and concretely defined in
* various structures that will be stored in the 'data' field. */
typedef struct GIDParamBase
{
/* Stores the implementation-specific data for the parameter, such as the set
* of possible values and the current value. */
void* data;
/* The name of the parameter. */
const char* name;
/* Pointer to a function that counts the number of stored values. The 'data'
* field in this GIDParamBase structure will be passed as the only
* argument. The function returns the number of values. */
size_t (*value_count)(const void* data);
/* Pointer to a function that gets a pointer to the current value.
* The 'data' field of this GIDParamBase structure will be passed as the
* only argument. The function returns a pointer to the current value. */
void* (*current_value)(const void* data);
/* Pointer to a function that generates a string representation of the
* current value. The 'data' field in this GIDParamBase structure will be
* passed as the first argument. The second argument is the destination
* buffer into which the string will be written (it may be NULL for
* counting the length of the string), and the third argument is the size
* of the destination buffer. The function returns the size of the string
* regardless of how many characters could fit in the destination buffer. */
size_t (*current_value_string)(const void* data, char* dst, size_t dstSize);
/* Pointer to a function that changes the value of this parameter to the 'next
* value', if possible. The 'data' field in this GIDParamBase structure will
* be the only argment, and the function returns non-zero if there was a 'next
* value', otherwise zero. */
int (*next_value)(void* data);
/* Pointer to a function that resets the value of this parameter to the
* initial value. The 'data' field in this GIDParamBase structure will be
* the only argument, and the function does not have a return value. */
void (*reset_value)(void* data);
/* Pointer to a function that frees any memory that was allocated by the
* implementation. The 'data' parameter in this GIDParamBase structure will
* be the only argument, and the implementation is required to free it. */
void (*free_data)(void* data);
/* Pointer to the next linked parameter, or NULL. */
struct GIDParamBase* next;
} GIDParamBase;
/* Gets the number of values in a parameter.
* @param param - Pointer to the GIDParamBase from which to get the number of
* values.
* @returns - The number of values. May be zero, in which case the parameter
* should be considered unused (this is the case with 'row'
* parameters). */
size_t _gid_param_value_count(const GIDParamBase* param)
{
return param->value_count(param->data);
}
/* Gets the current value of a parameter.
* @param param - Pointer to the GIDParamBase from which to get the value.
* @returns - Pointer to the value. */
void* _gid_param_get_value(const GIDParamBase* param)
{
return param->current_value(param->data);
}
/* Generates a string representation of the current value of a parameter.
* @param param - Pointer to the GIDParamBase from which to get the value
* string.
* @param dst - Destination buffer, may be NULL to count the length of the
* string.
* @param dstSize - The size of the destination buffer.
* @returns - The size of the value string, regardless of how many bytes
* could fit in the dst buffer, excluding the null terminator. */
size_t _gid_param_get_value_string(const GIDParamBase* param, char* dst, size_t dstSize)
{
return param->current_value_string(param->data, dst, dstSize);
}
/* Changes the value of a parameter to the 'next value', if any.
* @param param - Pointer to the GIDParamBase on which to change
* the value.
* @returns - Non-zero if the value was changed. Zero means that
* there are no more values. */
int _gid_param_next_value(GIDParamBase* param)
{
return param->next_value(param->data);
}
/* Resets a parameter to its initial value.
* @param param - Pointer to the GIDParamBase on which to reset the value. */
void _gid_param_reset_value(GIDParamBase* param)
{
param->reset_value(param->data);
}
/* Gets a string representation of a linked list of parameters.
* @param root - The root GIDParamBase to stringify. All GIDParamBases
* that are linked to this root will be stringified.
* @param dst - The destination buffer.
* @param dstSize - The size of the destination buffer.
* @returns - The total size of the required string, regardless of how
* many bytes were actually written to the destination buffer. */
int64_t _gid_get_params_string(GIDParamBase* root, char* dst, int64_t dstSize)
{
int64_t pos = 0;
int first = 1;
while(root != NULL)
{
if(_gid_param_value_count(root) == 0)
{
root = root->next;
continue;//Don't print params with no values
}
int64_t valueLen = _gid_param_get_value_string(root, NULL, 0);
if(!first)
{
if(dst != NULL && pos + 2 < dstSize)
{
dst[pos + 0] = ',';
dst[pos + 1] = ' ';
}
pos += 2;
}
else
{
first = 0;
}
if(dst != NULL && dstSize > pos)
strncpy(dst+pos, root->name, dstSize-pos);
pos += strlen(root->name);
if(dst != NULL && pos + 1 < dstSize)
dst[pos] = '=';
pos++;
if(dst != NULL && dstSize > pos)
_gid_param_get_value_string(root, dst+pos, dstSize-pos);
pos += valueLen;
if(root->next != NULL)
root = root->next;
else
break;
}
if(pos + 1 < dstSize)
dst[pos] = '\0';
else if(dstSize > 0)
dst[dstSize - 1] = '\0';
return pos;
}
/* Frees memory that was allocated for a parameter.
* @param param - Pointer to the parameter's GIDParamBase. */
void _gid_param_free(GIDParamBase* param)
{
param->free_data(param->data);
free((char*)param->name);
free(param);
}
/* Frees all memory in a linked list of parameters.
* @param root - Pointer to the first GIDParamBase in the linked list of
* parameters to free. */
void _gid_free_params(GIDParamBase* root)
{
GIDParamBase* cur = root;
while(cur != NULL)
{
GIDParamBase* next = cur->next;
_gid_param_free(cur);
cur = next;
}
}
/* Defines the type of data stored in a GIDRowParam. */
typedef enum GIDRowParamType
{
/* A 64-bit signed integer is stored. */
GID_ROW_PARAM_TYPE_INT64,
/* A 64-bit unsigned integer is stored. */
GID_ROW_PARAM_TYPE_UINT64,
/* A pointer to a null-terminated string is stored. */
GID_ROW_PARAM_TYPE_STRING,
} GIDRowParamType;
/* Contains the value of one row in a GIDRowParam.
* A single value consists of multiple columns. */
typedef struct GIDRowParamValue
{
/* Pointer to the memory of the values in the columns. */
void* cols;
/* The number of stored columns. */
size_t col_count;
/* Pointer to the next linked GIDRowParamValue, or NULL if none. */
struct GIDRowParamValue* next_row;
} GIDRowParamValue;
/* Creates a GIDRowParamValue in memory.
* @param cols - Pointer to the values to store in the columns. This data
* will be copied.
* @param valueSize - The size, in bytes, of each column value.
* @param colCount - The number of columns.
* @param type - The GIDRowParamType that defines the type of data that is
* stored in each column.
* @returns - A pointer to the allocated GIDRowParamValue. */
GIDRowParamValue* _gid_create_row_param_value(
const void* cols,
size_t valueSize,
size_t colCount,
GIDRowParamType type)
{
GIDRowParamValue* ret = malloc(sizeof(GIDRowParamValue));
/* Add padding just in case a test accidentally goes out of bounds */
size_t paddingColCount = 32;
ret->cols = malloc((colCount + paddingColCount) * valueSize);
if(type != GID_ROW_PARAM_TYPE_STRING)
{
memcpy((void*)ret->cols, cols, colCount * valueSize);
}
else
{
/*Copy the string values, not just the pointers*/
for(size_t i = 0; i < colCount; i++)
((char**)ret->cols)[i] = (char*)_gid_strclone(((const char**)cols)[i]);
}
memset((uint8_t*)ret->cols+(colCount*valueSize), 0, paddingColCount * valueSize);
ret->col_count = colCount;
ret->next_row = NULL;
return ret;
}
/* Contains data for a 'row parameter', which is a parameter that stores
* multiple rows (defined by the test) each with multiple columns of values. */
typedef struct GIDRowParamData
{
/* Pointer to the first GIDRowParamValue that is defined for the test that
* contains this row parameter, or NULL if none. */
GIDRowParamValue* first_row;
/* Pointer to the last GIDRowParamValue that is defined for the test that
* contains this row parameter, or NULL if none. */
GIDRowParamValue* last_row;
/* Pointer to the GIDRowParamValue that contains the columns that are
* currently selected for this row parameter, or NULL if none. */
GIDRowParamValue* current_row;
/* The GIDRowParamType that defines the type of data stored in each column. */
GIDRowParamType type;
} GIDRowParamData;
/* Gets the number of values in a 'row parameter'.
* @param data - Pointer to the GIDRowParamData that was allocated for the
* row parameter.
* @returns - The number of rows (each value is one row) in the parameter. */
size_t _gid_row_param_value_count(const void* data)
{
const GIDRowParamData* rowData = data;
size_t count = 0;
GIDRowParamValue* value = rowData->first_row;
while(value != NULL)
{
count++;
value = value->next_row;
}
return count;
}
/* Gets the current row value of a 'row parameter'.
* @param data - Pointer to the GIDRowParamData.
* @returns - Pointer to the GIDRowParamValue, or NULL if the row parameter
* is empty. */
void* _gid_row_param_get_current_value(const void* data)
{
const GIDRowParamData* rowData = data;
return rowData->current_row;
}
/* Gets a string representation of a 'row parameter'.
* @param data - Pointer to the GIDRowParamData.
* @param dst - The destination buffer.
* @param dstSize - The size of the destination buffer.
* @returns - The actual size of the string, regardless of how much could
* fit in the destination buffer. */
size_t _gid_row_param_get_current_value_string(
const void* data,
char* dst,
size_t dstSize)
{
const GIDRowParamData* rowData = data;
size_t pos = 0;
if(pos + 1 < dstSize)
dst[pos] = '{';
pos++;
GIDRowParamValue* row = rowData->current_row;
if(row != NULL)
{
for(size_t i = 0; i < row->col_count; i++)
{
if(rowData->type == GID_ROW_PARAM_TYPE_STRING)
{
if(pos + 1 < dstSize)
dst[pos] = '\"';
pos++;
}
switch(rowData->type)
{
case GID_ROW_PARAM_TYPE_INT64:
pos += snprintf(dst != NULL ? dst+pos : NULL, dstSize > pos ? dstSize - pos : 0, "%"PRId64, ((int64_t*)row->cols)[i]);
break;
case GID_ROW_PARAM_TYPE_UINT64:
pos += snprintf(dst != NULL ? dst+pos : NULL, dstSize > pos ? dstSize - pos : 0, "%"PRIu64, ((uint64_t*)row->cols)[i]);
break;
case GID_ROW_PARAM_TYPE_STRING:
pos += snprintf(dst != NULL ? dst+pos : NULL, dstSize > pos ? dstSize - pos : 0, "%s", ((char**)row->cols)[i]);
break;
default:
if(pos + 1 < dstSize && dst != NULL)
dst[pos] = '?';
pos++;
break;
}
if(rowData->type == GID_ROW_PARAM_TYPE_STRING)
{
if(pos + 1 < dstSize)
dst[pos] = '\"';
pos++;
}
if(i + 1< row->col_count)
{
if(pos + 2 < dstSize)
{
dst[pos + 0] = ',';
dst[pos + 1] = ' ';
}
pos += 2;
}
}
}
if(pos + 1 < dstSize)
dst[pos] = '}';
pos++;
if(dstSize > 0)
{
size_t nullPos = pos;
if(nullPos >= dstSize)
nullPos = dstSize - 1;
dst[nullPos] = '\0';
}
return pos;
}
/* Moves the 'current value' of a 'row parameter' to the next row, if any.
* @param data - Pointer to the GIDRowParamData.
* @returns - True if there was a 'next row', otherwise false. */
int _gid_row_param_next_value(void* data)
{
GIDRowParamData* rowData = data;
if(rowData->current_row != NULL)
{
if(rowData->current_row->next_row != NULL)
{
rowData->current_row = rowData->current_row->next_row;
return 1;
}
else
{
//At last row
return 0;
}
}
else
{
//No values
return 0;
}
}
/* Resets the 'current value' of a 'row parameter' to the first
* stored row, if any.
* @param data - Pointer to the GIDRowParamData. */
void _gid_row_param_reset_value(void* data)
{
GIDRowParamData* rowData = data;
rowData->current_row = rowData->first_row;
}
/* Frees memory allocated for a row parameter.
* @param data - Pointer to the GIDRowParamData to free. */
void _gid_row_param_free_data(void* data)
{
GIDRowParamData* rowData = data;
GIDRowParamValue* value = rowData->first_row;
while(value != NULL)
{
GIDRowParamValue* next = value->next_row;
if(rowData->type == GID_ROW_PARAM_TYPE_STRING)
{
//Free all strings from memory
for(size_t i = 0; i < value->col_count; i++)
free(((char**)value->cols)[i]);
}
free((void*)value->cols);
free(value);
value = next;
}
free(data);
}
/* Creates a row parameter.
* @param name - The name of the parameter.
* @param type - The GIDRowParamType that defines the type of data stored in
* each column.
* @returns - Pointer to the GIDParamBase for the newly allocated row
* parameter. */
GIDParamBase* _gid_create_row_param(const char* name, GIDRowParamType type)
{
GIDRowParamData* data = malloc(sizeof(GIDRowParamData));
data->first_row = NULL;
data->last_row = NULL;
data->current_row = NULL;
data->type = type;
GIDParamBase* base = malloc(sizeof(GIDParamBase));
base->data = data;
base->name = _gid_strclone(name);
base->value_count = _gid_row_param_value_count;
base->current_value = _gid_row_param_get_current_value;
base->current_value_string = _gid_row_param_get_current_value_string;
base->next_value = _gid_row_param_next_value;
base->reset_value = _gid_row_param_reset_value;
base->free_data = _gid_row_param_free_data;
base->next = NULL;
return base;
}
/* Adds a row to a row parameter.
* @param param - Pointer to the GIDParamBase of the destination row
* parameter.
* @param value - Pointer to the GIDRowParamValue to add. */
void _gid_add_row_param_value(GIDParamBase* param, GIDRowParamValue* value)
{
GIDRowParamData* data = param->data;
GIDRowParamValue* prev = data->last_row;
data->last_row = value;
if(data->first_row == NULL)
{
data->first_row = value;
data->current_row = value;
}
if(prev != NULL)
prev->next_row = value;
}
/* Contains data for a 'range parameter', which is a parameter that contains
* integer values ranging from a test-defined minimum and maximum value.
* Note that the values are stored in signed 64-bit integers, but the parameter
* has the option to treat them as unsigned. */
typedef struct GIDRangeParamData
{
/* The minimum value. */
const int64_t min;
/* The maximum value. */
const int64_t max;
/* The current value. */
int64_t current;
/* Does the test want to treat the values in this parameter as though they
* are signed? Non-zero means yes, the values are interpreted as signed. Zero
* means that the values are interpreted as unsigned integers. */
int is_signed;
} GIDRangeParamData;
/* Gets the number of values in a range parameter.
* @param data - Pointer to the GIDRangeParamData.
* @returns - The number of values. */
size_t _gid_range_param_value_count(const void* data)
{
const GIDRangeParamData* rangeData = data;
if(rangeData->is_signed)
return (rangeData->max - rangeData->min) + 1;
else
return ((uint64_t)rangeData->max - (uint64_t)rangeData->min) + 1;
}
/* Gets the current value in a range parameter.
* @param data - Pointer to the GIDRangeParamData.
* @returns - Pointer to the value. */
void* _gid_range_param_get_current_value(const void* data)
{
const GIDRangeParamData* rangeData = data;
return (void*)&rangeData->current;
}
/* Gets a string representation of the current value in a range parameter.
* @param data - Pointer to the GIDRangeParamData.
* @param dst - The destination buffer.
* @param dstSize - The size of the destination buffer.
* @returns - The size of the string representation, regardless of how many
* characters were written to the destination buffer.*/
size_t _gid_range_param_get_current_value_string(
const void* data,
char* dst,
size_t dstSize)
{
const GIDRangeParamData* rangeData = data;
const char* format = rangeData->is_signed ? "%"PRId64 : ""PRIu64;
return snprintf(dst, dstSize, format, rangeData->current);
}
/* Increments the 'current value' of a range parameter, if it is below
* the maximum value.
* @param data - Pointer to the GIDRangeParamData.
* @returns - Non-zero if the value was incremented. Zero means that the
* maximum value is already selected, thus it cannot increment. */
int _gid_range_param_next_value(void* data)
{
GIDRangeParamData* rangeData = data;
if((rangeData->is_signed && rangeData->current < rangeData->max) || (!rangeData->is_signed && (uint64_t)rangeData->current < (uint64_t)rangeData->max))
{
rangeData->current++;
return 1;
}
else
{
return 0;
}
}
/* Resets the 'current value' of a range parameter to its minimum.
* @param data - Pointer to the GIDRangeParamData. */
void _gid_range_param_reset_value(void* data)
{
GIDRangeParamData* rangeData = data;
rangeData->current = rangeData->min;
}
/* Frees a GIDRangeParamData and all of its associated memory.
* @param data - Pointer to the GIDRangeParamData. */
void _gid_range_param_free_data(void* data)
{
free(data);
}
/* Creates a range parameter.
* @param name - The name of the variable.
* @param min - The minimum value of the parameter.
* @param max - The maximum value of the parameter.
* @param isSigned - Should the parameter's value be signed? If false (0), then
* the 'min' and 'max' arguments will be reinterpreted as unsigned.
* @returns - Pointer to the GIDParamBase of the range parameter. */
GIDParamBase* _gid_create_range_param(const char* name, int64_t min, int64_t max, int isSigned)
{
GIDRangeParamData* data = malloc(sizeof(GIDRangeParamData));
memcpy((int64_t*)&data->min, &min, sizeof(data->min));
memcpy((int64_t*)&data->current, &min, sizeof(data->current));
memcpy((int64_t*)&data->max, &max, sizeof(data->max));
data->is_signed = isSigned;
GIDParamBase* base = malloc(sizeof(GIDParamBase));
base->data = data;
base->name = _gid_strclone(name);
base->value_count = _gid_range_param_value_count;
base->current_value = _gid_range_param_get_current_value;
base->current_value_string = _gid_range_param_get_current_value_string;
base->next_value = _gid_range_param_next_value;
base->reset_value = _gid_range_param_reset_value;
base->free_data = _gid_range_param_free_data;
base->next = NULL;
return base;
}
/* Defines the type of values stored in an 'enum parameter.' */
typedef enum GIDEnumParamType
{
/* Signed 64-bit integer values. */
GID_ENUM_PARAM_TYPE_INT64,
/* Unsigned 64-bit integer values. */
GID_ENUM_PARAM_TYPE_UINT64,
/* Pointers to null-terminated strings. */
GID_ENUM_PARAM_TYPE_STRING,
} GIDEnumParamType;
/* Contains the data for an 'enum parameter', which is a parameter that has
* one value at a time from a set of values defined by the test. */
typedef struct GIDEnumParamData
{
/* Pointer to the values defined by the test. */
void* values;
/* Defines the type of value stored. */
GIDEnumParamType type;
/* The size of each value. */
size_t value_size;
/* The number of values defined by the test. */
size_t count;
/* The zero-based index of the current value. */
size_t index;
} GIDEnumParamData;
/* Gets the number of values in an 'enum parameter'.
* @param data - Pointer to the GIDEnumParamData.
* @returns - The number of values. */
size_t _gid_enum_param_value_count(const void* data)
{
const GIDEnumParamData* paramData = data;
return paramData->count;
}
/* Gets the current value of an 'enum parameter'.
* @param data - Pointer to the GIDEnumParamData.
* @returns - Pointer to the current value. */
void* _gid_enum_param_get_current_value(const void* data)
{
const GIDEnumParamData* paramData = data;
return ((uint8_t*)paramData->values)
+ (paramData->value_size * paramData->index);
}
/* Gets a string representation of the current value of an 'enum parameter.'
* @param data - Pointer to the GIDEnumParamData.
* @param dst - Destination buffer.
* @param dstSize - The size of the destination buffer.
* @returns - The total size of the string representation, regardless of how
* many characters were written to the destination buffer. */
size_t _gid_enum_param_get_current_value_string(
const void* data,
char* dst,
size_t dstSize)
{
const GIDEnumParamData* paramData = data;
const char* fallback = "[Unknown Type]";
switch(paramData->type)
{
case GID_ENUM_PARAM_TYPE_INT64:
return snprintf(
dst,
dstSize,
"%"PRIi64,
((int64_t*)paramData->values)[paramData->index]);
case GID_ENUM_PARAM_TYPE_UINT64:
return snprintf(
dst,
dstSize,
"%"PRIu64,
((uint64_t*)paramData->values)[paramData->index]);
case GID_ENUM_PARAM_TYPE_STRING:
return snprintf(
dst,
dstSize,
"\"%s\"",
((char**)paramData->values)[paramData->index]);
default:
strncpy(dst, fallback, dstSize);
return strlen(fallback);
}
}
/* Moves the value of an 'enum parameter' to the next value that was defined
* by the test, if any.
* @param data - Pointer to the GIDEnumParamData.
* @returns - Non-zero if a 'next value' was selected. Zero means that there
* are no more 'next' values. */
int _gid_enum_param_next_value(void* data)
{
GIDEnumParamData* paramData = data;
if(paramData->index + 1 < paramData->count)
{
paramData->index++;
return 1;
}
else
{
return 0;
}
}
/* Resets an 'enum parameter' to the first value defined by the test.
* @param data - Pointer to the GIDEnumParamData. */
void _gid_enum_param_reset_value(void* data)
{
GIDEnumParamData* paramData = data;
paramData->index = 0;
}
/* Frees memory allocated for an 'enum parameter'.
* @param data - Pointer to the GIDEnumParamData. */
void _gid_enum_param_free_data(void* data)
{
const GIDEnumParamData* paramData = data;
free(paramData->values);
free(data);
}
/* Creates an 'enum parameter'.
* @param name - The name of the parameter.
* @param type - The GIDEnumParamType that defines the type of values that
* will be stored in the parameter.
* @param values - Pointer to the array of values. These values will be copied.
* @param valueSize - The size of each value.
* @param valueCount - The number of values.
* @returns - Pointer to the GIDParamBase for the allocated enum parameter. */
GIDParamBase* _gid_create_enum_param(
const char* name,
GIDEnumParamType type,
void* values,
size_t valueSize,
size_t valueCount)
{
GIDEnumParamData* paramData = malloc(sizeof(GIDEnumParamData));
paramData->type = type;
paramData->values = malloc(valueSize * valueCount);
memcpy(paramData->values, values, valueSize * valueCount);
paramData->value_size = valueSize;
paramData->count = valueCount;
paramData->index = 0;
GIDParamBase* base = malloc(sizeof(GIDParamBase));
base->data = paramData;
base->name = _gid_strclone(name);
base->value_count = _gid_enum_param_value_count;
base->current_value = _gid_enum_param_get_current_value;
base->current_value_string = _gid_enum_param_get_current_value_string;
base->next_value = _gid_enum_param_next_value;
base->reset_value = _gid_enum_param_reset_value;
base->free_data = _gid_enum_param_free_data;
base->next = NULL;
return base;
}
/* Defines the status of a test in general (not for a specific
* configuration). */
typedef enum GIDTestStatus
{
/* The test has not yet started. */
GID_TEST_PENDING,
/* The test is currently running. This includes setup and
* teardown. */
GID_TEST_RUNNING,
/* The test has failed. Note that this value is assigned immediately
* when one test configuration fails, but other configurations may still
* be pending. */
GID_TEST_FAILED,
/* The test has passed with no failures. */
GID_TEST_PASSED,
} GIDTestStatus;
/* Defines the steps in a test run. */
typedef enum GIDTestStep
{
/* The setup for the test is being run. */
GID_STEP_SETUP = 0,
/* The test is being run. */
GID_STEP_RUN = 1,
/* The test is in the 'teardown' step. */
GID_STEP_TEARDOWN = 2,
} GIDTestStep;
/* Contains information about a test failure. */
typedef struct GIDTestFailure
{
/* The name of the test that failed. */
const char* test_name;
/* The configuration of the test that resulted in this failure.
* This is a string representation of all of the test's parameters. */
const char* configuration;
/* A message that explains why the test failed. */
const char* message;