Commit | Line | Data |
---|---|---|
b90f122b RK |
1 | /* |
2 | * This file is part of DisOrder. | |
8a886602 | 3 | * Copyright (C) 2005, 2007, 2008, 2010 Richard Kettlewell |
b90f122b | 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 | 20 | static 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); | |
dbad4ee4 | 55 | insist(u != 0); |
b90f122b RK |
56 | insist(ul == 4); |
57 | insist(memcmp(u, h, 4) == 0); | |
58 | u = unhex("00FF807F", &ul); | |
dbad4ee4 | 59 | insist(u != 0); |
b90f122b RK |
60 | insist(ul == 4); |
61 | insist(memcmp(u, h, 4) == 0); | |
62 | u = unhex("", &ul); | |
dbad4ee4 | 63 | insist(u != 0); |
b90f122b RK |
64 | insist(ul == 0); |
65 | fprintf(stderr, "2 ERROR reports expected {\n"); | |
66 | insist(unhex("F", 0) == 0); | |
67 | insist(unhex("az", 0) == 0); | |
68 | fprintf(stderr, "}\n"); | |
69 | } | |
70 | ||
c68d8eba RK |
71 | TEST(hex); |
72 | ||
b90f122b RK |
73 | /* |
74 | Local Variables: | |
75 | c-basic-offset:2 | |
76 | comment-column:40 | |
77 | fill-column:79 | |
78 | indent-tabs-mode:nil | |
79 | End: | |
80 | */ |