-
Notifications
You must be signed in to change notification settings - Fork 74
/
test_format.c
105 lines (79 loc) · 3.31 KB
/
test_format.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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <cmocka.h>
#include "common/format.h"
static void test_format_i64(void **state) {
(void) state;
char temp[22] = {0};
int64_t value = 0;
assert_true(format_i64(temp, sizeof(temp), value));
assert_string_equal(temp, "0");
value = (int64_t) 9223372036854775807ull; // MAX_INT64
memset(temp, 0, sizeof(temp));
assert_true(format_i64(temp, sizeof(temp), value));
assert_string_equal(temp, "9223372036854775807");
// buffer too small
assert_false(format_i64(temp, sizeof(temp) - 5, value));
value = (int64_t) -9223372036854775808ull; // MIN_INT64
memset(temp, 0, sizeof(temp));
assert_true(format_i64(temp, sizeof(temp), value));
assert_string_equal(temp, "-9223372036854775808");
}
static void test_format_u64(void **state) {
(void) state;
char temp[21] = {0};
uint64_t value = 0;
assert_true(format_u64(temp, sizeof(temp), value));
assert_string_equal(temp, "0");
value = (uint64_t) 18446744073709551615ull; // MAX_UNT64
memset(temp, 0, sizeof(temp));
assert_true(format_u64(temp, sizeof(temp), value));
assert_string_equal(temp, "18446744073709551615");
// buffer too small
assert_false(format_u64(temp, sizeof(temp) - 5, value));
}
static void test_format_fpu64(void **state) {
(void) state;
char temp[22] = {0};
uint64_t amount = 100000000ull; // satoshi
memset(temp, 0, sizeof(temp));
assert_true(format_fpu64(temp, sizeof(temp), amount, 8));
assert_string_equal(temp, "1.00000000"); // BTC
amount = 24964823ull; // satoshi
memset(temp, 0, sizeof(temp));
assert_true(format_fpu64(temp, sizeof(temp), amount, 8));
assert_string_equal(temp, "0.24964823"); // BTC
amount = 100ull; // satoshi
memset(temp, 0, sizeof(temp));
assert_true(format_fpu64(temp, sizeof(temp), amount, 8));
assert_string_equal(temp, "0.00000100"); // BTC
// buffer too small
assert_false(format_fpu64(temp, sizeof(temp) - 16, amount, 8));
char temp2[50] = {0};
amount = 1000000000000000000ull; // wei
assert_true(format_fpu64(temp2, sizeof(temp2), amount, 18));
assert_string_equal(temp2, "1.000000000000000000"); // ETH
// buffer too small
assert_false(format_fpu64(temp2, sizeof(temp2) - 20, amount, 18));
}
static void test_format_hex(void **state) {
(void) state;
uint8_t address[] = {0xde, 0xb, 0x29, 0x56, 0x69, 0xa9, 0xfd, 0x93, 0xd5, 0xf2,
0x8d, 0x9e, 0xc8, 0x5e, 0x40, 0xf4, 0xcb, 0x69, 0x7b, 0xae};
char output[2 * sizeof(address) + 1] = {0};
assert_int_equal(2 * sizeof(address) + 1,
format_hex(address, sizeof(address), output, sizeof(output)));
assert_string_equal(output, "de0b295669a9fd93d5f28d9ec85e40f4cb697bae");
assert_int_equal(-1, format_hex(address, sizeof(address), output, sizeof(address)));
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_format_i64),
cmocka_unit_test(test_format_u64),
cmocka_unit_test(test_format_fpu64),
cmocka_unit_test(test_format_hex)};
return cmocka_run_group_tests(tests, NULL, NULL);
}