chiark / gitweb /
Merge playlist branch against trunk to date.
[disorder] / libtests / t-hex.c
CommitLineData
b90f122b
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
b90f122b 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
b90f122b 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
b90f122b 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b90f122b
RK
17 */
18#include "test.h"
19
c68d8eba 20static void test_hex(void) {
b90f122b
RK
21 unsigned n;
22 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
23 uint8_t *u;
24 size_t ul;
25
b90f122b
RK
26 for(n = 0; n <= UCHAR_MAX; ++n) {
27 if(!isxdigit(n))
28 insist(unhexdigitq(n) == -1);
29 }
30 insist(unhexdigitq('0') == 0);
31 insist(unhexdigitq('1') == 1);
32 insist(unhexdigitq('2') == 2);
33 insist(unhexdigitq('3') == 3);
34 insist(unhexdigitq('4') == 4);
35 insist(unhexdigitq('5') == 5);
36 insist(unhexdigitq('6') == 6);
37 insist(unhexdigitq('7') == 7);
38 insist(unhexdigitq('8') == 8);
39 insist(unhexdigitq('9') == 9);
40 insist(unhexdigitq('a') == 10);
41 insist(unhexdigitq('b') == 11);
42 insist(unhexdigitq('c') == 12);
43 insist(unhexdigitq('d') == 13);
44 insist(unhexdigitq('e') == 14);
45 insist(unhexdigitq('f') == 15);
46 insist(unhexdigitq('A') == 10);
47 insist(unhexdigitq('B') == 11);
48 insist(unhexdigitq('C') == 12);
49 insist(unhexdigitq('D') == 13);
50 insist(unhexdigitq('E') == 14);
51 insist(unhexdigitq('F') == 15);
52 check_string(hex(h, sizeof h), "00ff807f");
53 check_string(hex(0, 0), "");
54 u = unhex("00ff807f", &ul);
55 insist(ul == 4);
56 insist(memcmp(u, h, 4) == 0);
57 u = unhex("00FF807F", &ul);
58 insist(ul == 4);
59 insist(memcmp(u, h, 4) == 0);
60 u = unhex("", &ul);
61 insist(ul == 0);
62 fprintf(stderr, "2 ERROR reports expected {\n");
63 insist(unhex("F", 0) == 0);
64 insist(unhex("az", 0) == 0);
65 fprintf(stderr, "}\n");
66}
67
c68d8eba
RK
68TEST(hex);
69
b90f122b
RK
70/*
71Local Variables:
72c-basic-offset:2
73comment-column:40
74fill-column:79
75indent-tabs-mode:nil
76End:
77*/