-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_tests.c
86 lines (67 loc) · 2.47 KB
/
generic_tests.c
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
/*
*/
/*
NOTES:
all signed values start with 's'
this was done with intent b/c of how the macros work...
typedef signed int i;
i is a common iterator and would cause conflict later down the road, im sure.
*/
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include "fits.h"
int main() {
/*
Out of lazyness, and readability, I used the limit values to simulate overflow issues.
Realistically you would pass a variable or value to this.
*/
size_t example = strlen("hello");
printf("Example:%d\n\n", si_fits(example));
printf("Obvious Passes:\n");
printf("ULL\t%d\n", ull_fits(ULLONG_MAX));
printf("UL\t%d\n", ul_fits(ULONG_MAX));
printf("SLL\t%d\n", sll_fits(LLONG_MAX));
printf("SL\t%d\n", sl_fits(LONG_MAX));
printf("UI\t%d\n", ui_fits(UINT_MAX));
printf("SI\t%d\n", si_fits(INT_MAX));
printf("US\t%d\n", us_fits(USHRT_MAX));
printf("SS\t%d\n", ss_fits(SHRT_MAX));
printf("UC\t%d\n", uc_fits(UCHAR_MAX));
printf("SC\t%d\n", sc_fits(SCHAR_MAX));
printf("SLL\t%d\n", sll_fits(LLONG_MAX));
printf("SL\t%d\n", sl_fits(LONG_MAX));
printf("SI\t%d\n", si_fits(INT_MAX));
printf("SS\t%d\n", ss_fits(SHRT_MAX));
printf("SC\t%d\n", sc_fits(SCHAR_MAX));
printf("\nObvious Fails:\n");
printf("SLL\t%d\n", sll_fits(ULONG_MAX));
printf("SL\t%d\n", sl_fits(ULONG_MAX));
printf("SI\t%d\n", si_fits(UINT_MAX));
printf("US\t%d\n", us_fits(USHRT_MAX+1));
printf("SS\t%d\n", ss_fits(USHRT_MAX));
printf("UC\t%d\n", uc_fits(UCHAR_MAX + 1));
printf("SC\t%d\n", sc_fits(ULONG_MAX));
printf("ULL\t%d\n", ull_fits(INT_MIN));
printf("UL\t%d\n", ul_fits(INT_MIN));
printf("UI\t%d\n", ui_fits(INT_MIN));
printf("US\t%d\n", us_fits(INT_MIN));
printf("UC\t%d\n", uc_fits(INT_MIN));
// ???
// printf("\nUnresolved:\n");
/*
According to limits.h, UINT_MAX is defined as 0xffffffffU
Meaning: after it's processed the value is an unsigned int
Thus without explicit casting, it overflows before the check.
This can be seen via:
printf("UI\t%llu\n", (UINT_MAX+1));
printf("UI\t%llu\n", ((ull)UINT_MAX+1));
However, this is an impractical case since variables were never used:
ull oddity = UINT_MAX;
printf("UI\t%llu\n", oddity);
printf("UI\t%llu\n", oddity+1);
printf("UI\t%d\n", ui_fits(oddity+1));
*/
printf("UI\t%d\n", ui_fits((ull)UINT_MAX+1));
return 0;
}