chiark / gitweb /
Leave a bit of headroom above test port number, since we go at least
[disorder] / libtests / t-hex.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
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  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "test.h"
19
20 static void test_hex(void) {
21   unsigned n;
22   static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
23   uint8_t *u;
24   size_t ul;
25
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
68 TEST(hex);
69
70 /*
71 Local Variables:
72 c-basic-offset:2
73 comment-column:40
74 fill-column:79
75 indent-tabs-mode:nil
76 End:
77 */