-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.c
132 lines (97 loc) · 2.77 KB
/
math_test.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
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
#include "sc_math.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
void test1()
{
uint64_t x;
char *t;
char buf[32];
assert(sc_math_min(199, 299) == 199);
assert(sc_math_max(199, 299) == 299);
assert(sc_math_is_pow2(0) == false);
x = sc_math_to_pow2(0);
assert(x == 1);
assert(sc_math_is_pow2(x) == true);
x = sc_math_to_pow2(1);
assert(x == 1);
assert(sc_math_is_pow2(x) == true);
x = sc_math_to_pow2(1023);
assert(x == 1024);
assert(sc_math_is_pow2(x) == true);
x = sc_math_size_to_bytes("1");
assert(x == 1);
x = sc_math_size_to_bytes("1b");
assert(x == 1);
x = sc_math_size_to_bytes("1k");
assert(x == 1 * 1024);
x = sc_math_size_to_bytes("1kb");
assert(x == 1 * 1024);
x = sc_math_size_to_bytes("1m");
assert(x == 1 * 1024 * 1024);
x = sc_math_size_to_bytes("1mb");
assert(x == 1 * 1024 * 1024);
x = sc_math_size_to_bytes("1g");
assert(x == 1 * 1024 * 1024 * 1024);
x = sc_math_size_to_bytes("1gb");
assert(x == 1 * 1024 * 1024 * 1024);
x = sc_math_size_to_bytes("1gx");
assert(x == -1);
x = sc_math_size_to_bytes("gx");
assert(x == -1);
x = sc_math_size_to_bytes("1xgx");
assert(x == -1);
x = sc_math_size_to_bytes("1xb");
assert(x == -1);
x = sc_math_size_to_bytes("1p");
assert(x == UINT64_C(1 * 1024 * 1024 * 1024 * 1024));
x = sc_math_size_to_bytes("1pb");
assert(x == UINT64_C(1 * 1024 * 1024 * 1024 * 1024));
t = sc_math_bytes_to_size(buf, sizeof(buf), 1024);
assert(strcmp(t, "1024.00 B") == 0);
t = sc_math_bytes_to_size(buf, sizeof(buf), 2 * 1024);
assert(strcmp(t, "2.00 KB") == 0);
t = sc_math_bytes_to_size(buf, sizeof(buf), 2 * 1024 * 1024);
assert(strcmp(t, "2.00 MB") == 0);
t = sc_math_bytes_to_size(buf, sizeof(buf), (uint64_t)2 * 1024 * 1024 * 1024);
assert(strcmp(t, "2.00 GB") == 0);
t = sc_math_bytes_to_size(buf, sizeof(buf), (uint64_t)2 * 1024 * 1024 * 1024 * 1024);
assert(strcmp(t, "2.00 TB") == 0);
t = sc_math_bytes_to_size(buf, sizeof(buf), (uint64_t)2 * 1024 * 1024 * 1024 * 1024 * 1024);
assert(strcmp(t, "2.00 PB") == 0);
}
#ifdef SC_HAVE_WRAP
bool fail_snprintf;
int __wrap_snprintf(char *str, size_t size, const char *format, ...)
{
int rc;
va_list va;
if (!fail_snprintf) {
va_start(va, format);
rc = vsnprintf(str, size, format, va);
va_end(va);
return rc;
}
return -1;
}
void fail_test()
{
char* t;
char buf[32];
fail_snprintf = true;
t = sc_math_bytes_to_size(buf, sizeof(buf), 2 * 1024);
assert(t == NULL);
fail_snprintf = false;
}
#else
void fail_test()
{
}
#endif
int main()
{
test1();
fail_test();
return 0;
}