-
Notifications
You must be signed in to change notification settings - Fork 2
/
printer_test.c
157 lines (132 loc) · 5.51 KB
/
printer_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
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
#include "printer.c"
#include <assert.h>
#include <stdio.h>
void test_print_amount() {
char printed[24];
print_amount(0, printed, sizeof(printed));
assert_string_equal(printed, "0 SOL");
print_amount(1, printed, sizeof(printed));
assert_string_equal(printed, "0.000000001 SOL");
print_amount(1000000000, printed, sizeof(printed));
assert_string_equal(printed, "1 SOL");
print_amount(10000000000000001, printed, sizeof(printed));
assert_string_equal(printed, "10000000.000000001 SOL");
print_amount(10000000001, printed, sizeof(printed));
assert_string_equal(printed, "10.000000001 SOL");
print_amount(10000000100000000, printed, sizeof(printed));
assert_string_equal(printed, "10000000.1 SOL");
}
void test_print_token_amount() {
char printed[26];
print_token_amount(0, "TST", 0, printed, sizeof(printed));
assert_string_equal(printed, "0 TST");
print_token_amount(0, "TST", 10, printed, sizeof(printed));
assert_string_equal(printed, "0 TST");
print_token_amount(0, "TST", 19, printed, sizeof(printed));
assert_string_equal(printed, "0 TST");
print_token_amount(1, "TST", 0, printed, sizeof(printed));
assert_string_equal(printed, "1 TST");
print_token_amount(1, "TST", 10, printed, sizeof(printed));
assert_string_equal(printed, "0.0000000001 TST");
print_token_amount(1, "TST", 19, printed, sizeof(printed));
assert_string_equal(printed, "0.0000000000000000001 TST");
print_token_amount(10000000000000000000ULL, "TST", 19, printed, sizeof(printed));
assert_string_equal(printed, "1 TST");
print_token_amount(UINT64_MAX, "TST", 19, printed, sizeof(printed));
assert_string_equal(printed, "1.8446744073709551615 TST");
print_token_amount(UINT64_MAX, "TST", 10, printed, sizeof(printed));
assert_string_equal(printed, "1844674407.3709551615 TST");
print_token_amount(UINT64_MAX, "TST", 1, printed, sizeof(printed));
assert_string_equal(printed, "1844674407370955161.5 TST");
print_token_amount(UINT64_MAX, "TST", 0, printed, sizeof(printed));
assert_string_equal(printed, "18446744073709551615 TST");
}
void test_print_sized_string() {
char buf[5];
const char test[] = {0x74, 0x65, 0x73, 0x74};
SizedString string = {sizeof(test), test};
assert(print_sized_string(&string, buf, sizeof(buf)) == 0);
assert_string_equal(buf, "test");
assert(print_sized_string(&string, buf, 4) == 1);
assert_string_equal(buf, "te~");
assert(print_sized_string(&string, buf, 2) == 1);
assert_string_equal(buf, "~");
assert(print_sized_string(&string, buf, 1) == 1);
assert_string_equal(buf, "");
}
void test_print_string() {
char buf[5];
assert(print_string("fits", buf, sizeof(buf)) == 0);
assert_string_equal("fits", buf);
assert(print_string("too long", buf, sizeof(buf)) == 1);
assert_string_equal("too~", buf);
assert(print_string("too_long", buf, 2) == 1);
assert_string_equal("~", buf);
assert(print_string("too_long", buf, 1) == 1);
assert_string_equal("", buf);
}
void test_print_summary() {
char summary[27];
assert(print_summary("GADFVW3UXVKDOU626XUPYDJU2BFCGFJHQ6SREYOZ6IJV4XSHOALEQN2I",
summary,
sizeof(summary),
12,
12) == 0);
assert_string_equal(summary, "GADFVW3UXVKD..4XSHOALEQN2I");
assert(print_summary("GADFVW3UXVKDOU626XUPYDJU2BFCGFJHQ6SREYOZ6IJV4XSHOALEQN2I",
summary,
sizeof(summary),
6,
6) == 0);
assert_string_equal(summary, "GADFVW..LEQN2I");
const char* test_fits = "short enough";
assert(print_summary(test_fits, summary, sizeof(summary), 12, 12) == 0);
assert_string_equal(summary, test_fits);
assert(print_summary("buffer too small", NULL, 0, 12, 12) == 1);
}
void test_print_i64() {
char buf[21];
assert(print_i64(INT64_MIN, buf, sizeof(buf)) == 0);
assert_string_equal(buf, "-9223372036854775808");
assert(print_i64(INT64_MAX, buf, sizeof(buf)) == 0);
assert_string_equal(buf, "9223372036854775807");
assert(print_i64(0, buf, sizeof(buf)) == 0);
assert_string_equal(buf, "0");
assert(print_i64(-1, buf, sizeof(buf)) == 0);
assert_string_equal(buf, "-1");
}
void test_print_u64() {
#define U64_MAX_STR (20 + 1) // strlen("18446744073709551615") + NUL
char out[U64_MAX_STR];
assert(print_u64(0, out, sizeof(out)) == 0);
assert_string_equal(out, "0");
assert(print_u64(UINT64_MAX, out, sizeof(out)) == 0);
assert_string_equal(out, "18446744073709551615");
assert(print_u64(UINT64_MAX, out, sizeof(out) - 1) == 1);
assert(print_u64(0, NULL, 0) == 1);
}
void test_print_timestamp() {
#define RFC3339_MAX (4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1)
char out[RFC3339_MAX];
int64_t unix_epoch = 0;
const char* expect_unix_epoch = "1970-01-01 00:00:00";
assert(print_timestamp(unix_epoch, out, sizeof(out)) == 0);
assert_string_equal(out, expect_unix_epoch);
int64_t now = 1588374349;
const char* expect_now = "2020-05-01 23:05:49";
assert(print_timestamp(now, out, sizeof(out)) == 0);
assert_string_equal(out, expect_now);
assert(print_timestamp(0, out, sizeof(out) - 1) == 1);
}
int main() {
test_print_amount();
test_print_token_amount();
test_print_sized_string();
test_print_string();
test_print_summary();
test_print_i64();
test_print_u64();
test_print_timestamp();
printf("passed\n");
return 0;
}