2 * printf format routines - test program
3 * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
20 static const struct test_data tests[] = {
21 { (u8 *) "abcde", 5, "abcde" },
22 { (u8 *) "a\0b\nc\ed\re\tf", 11, "a\\0b\\nc\\ed\\re\\tf" },
23 { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
24 { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
25 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
26 "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
27 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
28 "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
29 { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
30 "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
35 static void print_hex(const u8 *data, size_t len)
38 for (i = 0; i < len; i++)
39 printf(" %02x", data[i]);
43 int main(int argc, char *argv[])
51 for (i = 0; tests[i].data; i++) {
52 const struct test_data *test = &tests[i];
54 print_hex(test->data, test->len);
55 printf_encode(buf, sizeof(buf), test->data, test->len);
56 printf(" -> \"%s\"\n", buf);
58 binlen = printf_decode(bin, sizeof(bin), buf);
59 if (binlen != test->len ||
60 os_memcmp(bin, test->data, binlen) != 0) {
61 printf("Error in decoding#1:");
62 print_hex(bin, binlen);
67 binlen = printf_decode(bin, sizeof(bin), test->encoded);
68 if (binlen != test->len ||
69 os_memcmp(bin, test->data, binlen) != 0) {
70 printf("Error in decoding#2:");
71 print_hex(bin, binlen);
78 printf("%d test(s) failed\n", errors);