]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/fletcher16_test.c
2 * BIRD Library -- Fletcher-16 Tests
4 * (c) 2015 CZ.NIC z.s.p.o.
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #include "test/birdtest.h"
10 #include "lib/fletcher16.h"
13 straightforward_fletcher16_compute(const char *data
)
15 int count
= strlen(data
);
21 for (index
= 0; index
< count
; ++index
)
23 sum1
= (sum1
+ data
[index
]) % 255;
24 sum2
= (sum2
+ sum1
) % 255;
27 return (sum2
<< 8) | sum1
;
31 straightforward_fletcher16_checksum(const char *data
)
36 csum
= straightforward_fletcher16_compute(data
);
38 f1
= (csum
>> 8) & 0xff;
39 c0
= 0xff - ((f0
+ f1
) % 0xff);
40 c1
= 0xff - ((f0
+ c0
) % 0xff);
42 return (c1
<< 8) | c0
;
46 test_fletcher16(void *out_
, const void *in_
, const void *expected_out_
)
50 const u16
*expected_out
= expected_out_
;
52 struct fletcher16_context ctxt
;
54 fletcher16_init(&ctxt
);
55 fletcher16_update(&ctxt
, in
, strlen(in
));
56 put_u16(out
, fletcher16_compute(&ctxt
));
58 return (*out
== *expected_out
) ? BT_SUCCESS
: BT_FAILURE
;
62 test_fletcher16_checksum(void *out_
, const void *in_
, const void *expected_out_
)
66 const u16
*expected_out
= expected_out_
;
68 struct fletcher16_context ctxt
;
71 fletcher16_init(&ctxt
);
72 fletcher16_update(&ctxt
, in
, len
);
73 put_u16(out
, fletcher16_final(&ctxt
, len
, len
));
75 return (*out
== *expected_out
) ? BT_SUCCESS
: BT_FAILURE
;
79 t_fletcher16_compute(void)
81 struct bt_pair test_vectors
[] = {
84 .out
= & (const u16
) { 0x0403 },
88 .out
= & ((const u16
) { straightforward_fletcher16_compute("") }),
92 .out
= & ((const u16
) { straightforward_fletcher16_compute("a") }),
96 .out
= & ((const u16
) { straightforward_fletcher16_compute("abcd") }),
99 .in
= "message digest",
100 .out
= & ((const u16
) { straightforward_fletcher16_compute("message digest") }),
103 .in
= "abcdefghijklmnopqrstuvwxyz",
104 .out
= & ((const u16
) { straightforward_fletcher16_compute("abcdefghijklmnopqrstuvwxyz") }),
107 .in
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
108 .out
= & ((const u16
) { straightforward_fletcher16_compute("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
111 .in
= "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
112 .out
= & ((const u16
) { straightforward_fletcher16_compute("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
116 return bt_assert_batch(test_vectors
, test_fletcher16
, bt_fmt_str
, bt_fmt_unsigned
);
120 t_fletcher16_checksum(void)
122 struct bt_pair test_vectors
[] = {
125 .out
= & ((const u16
) { straightforward_fletcher16_checksum("\001\002") }),
129 .out
= & ((const u16
) { straightforward_fletcher16_checksum("") }),
133 .out
= & ((const u16
) { straightforward_fletcher16_checksum("a") }),
137 .out
= & ((const u16
) { straightforward_fletcher16_checksum("abcd") }),
140 .in
= "message digest",
141 .out
= & ((const u16
) { straightforward_fletcher16_checksum("message digest") }),
144 .in
= "abcdefghijklmnopqrstuvwxyz",
145 .out
= & ((const u16
) { straightforward_fletcher16_checksum("abcdefghijklmnopqrstuvwxyz") }),
148 .in
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
149 .out
= & ((const u16
) { straightforward_fletcher16_checksum("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
152 .in
= "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
153 .out
= & ((const u16
) { straightforward_fletcher16_checksum("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
157 return bt_assert_batch(test_vectors
, test_fletcher16_checksum
, bt_fmt_str
, bt_fmt_unsigned
);
161 main(int argc
, char *argv
[])
165 bt_test_suite(t_fletcher16_compute
, "Fletcher-16 Compute Tests");
166 bt_test_suite(t_fletcher16_checksum
, "Fletcher-16 Checksum Tests");
168 return bt_exit_value();