Branch data Line data Source code
1 : : // Copyright 2020-2021 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #undef NDEBUG
5 : :
6 : : #include <zix/attributes.h>
7 : : #include <zix/digest.h>
8 : :
9 : : #include <assert.h>
10 : : #include <stddef.h>
11 : : #include <stdint.h>
12 : :
13 : : // Just basic smoke tests to ensure the hash functions are reacting to data
14 : :
15 : : static void
16 : 1 : test_digest(void)
17 : : {
18 : : static const uint8_t data[] = {
19 : : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
20 : :
21 : 1 : size_t last = 0U;
22 : :
23 [ + + ]: 8 : for (size_t offset = 0; offset < 7; ++offset) {
24 : 7 : const size_t len = 8U - offset;
25 [ + + ]: 42 : for (size_t i = offset; i < 8; ++i) {
26 : 35 : const size_t h = zix_digest(0U, &data[i], len);
27 [ - + ]: 35 : assert(h != last);
28 : 35 : last = h;
29 : : }
30 : : }
31 : 1 : }
32 : :
33 : : static void
34 : 1 : test_digest32(void)
35 : : {
36 : : static const uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
37 : :
38 : 1 : uint32_t last = 0U;
39 : :
40 [ + + ]: 4 : for (size_t offset = 0; offset < 3; ++offset) {
41 [ + + ]: 12 : for (size_t i = offset; i < 4; ++i) {
42 : 9 : const uint32_t h = zix_digest32(0U, &data[i], 4);
43 [ - + ]: 9 : assert(h != last);
44 : 9 : last = h;
45 : : }
46 : : }
47 : 1 : }
48 : :
49 : : static void
50 : 1 : test_digest64(void)
51 : : {
52 : : static const uint8_t data[] = {
53 : : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
54 : :
55 : 1 : uint64_t last = 0U;
56 : :
57 [ + + ]: 8 : for (size_t offset = 0; offset < 7; ++offset) {
58 [ + + ]: 42 : for (size_t i = offset; i < 8; ++i) {
59 : 35 : const uint64_t h = zix_digest64(0U, &data[i], 8);
60 [ - + ]: 35 : assert(h != last);
61 : 35 : last = h;
62 : : }
63 : : }
64 : 1 : }
65 : :
66 : : static void
67 : 1 : test_digest32_aligned(void)
68 : : {
69 : : static const uint32_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
70 : :
71 : 1 : uint32_t last = 0U;
72 : :
73 [ + + ]: 4 : for (size_t offset = 0; offset < 3; ++offset) {
74 : 3 : const size_t len = 4U - offset;
75 [ + + ]: 12 : for (size_t i = offset; i < 4; ++i) {
76 : : const uint32_t h =
77 : 9 : zix_digest32_aligned(0U, &data[i], len * sizeof(uint32_t));
78 [ - + ]: 9 : assert(h != last);
79 : 9 : last = h;
80 : : }
81 : : }
82 : 1 : }
83 : :
84 : : static void
85 : 1 : test_digest64_aligned(void)
86 : : {
87 : : static const uint64_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
88 : :
89 : 1 : uint64_t last = 0U;
90 : :
91 [ + + ]: 4 : for (size_t offset = 0; offset < 3; ++offset) {
92 : 3 : const size_t len = 4U - offset;
93 [ + + ]: 12 : for (size_t i = offset; i < 4; ++i) {
94 : : const uint64_t h =
95 : 9 : zix_digest64_aligned(0U, &data[i], len * sizeof(uint64_t));
96 [ - + ]: 9 : assert(h != last);
97 : 9 : last = h;
98 : : }
99 : : }
100 : 1 : }
101 : :
102 : : static void
103 : 1 : test_digest_aligned(void)
104 : : {
105 : : static const size_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
106 : :
107 : 1 : size_t last = 0U;
108 : :
109 [ + + ]: 4 : for (size_t offset = 0; offset < 3; ++offset) {
110 : 3 : const size_t len = 4U - offset;
111 [ + + ]: 12 : for (size_t i = offset; i < 4; ++i) {
112 : 9 : const size_t h = zix_digest_aligned(0U, &data[i], len * sizeof(size_t));
113 [ - + ]: 9 : assert(h != last);
114 : 9 : last = h;
115 : : }
116 : : }
117 : 1 : }
118 : :
119 : : ZIX_PURE_FUNC int
120 : 1 : main(void)
121 : : {
122 : 1 : test_digest32();
123 : 1 : test_digest64();
124 : 1 : test_digest();
125 : :
126 : 1 : test_digest32_aligned();
127 : 1 : test_digest64_aligned();
128 : 1 : test_digest_aligned();
129 : :
130 : 1 : return 0;
131 : : }
|