chiark / gitweb /
Merge from 3.0 fixes branch
[disorder] / lib / 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 2 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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 #include "test.h"
21
22 void test_hex(void) {
23   unsigned n;
24   static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
25   uint8_t *u;
26   size_t ul;
27
28   fprintf(stderr, "test_hex\n");
29
30   for(n = 0; n <= UCHAR_MAX; ++n) {
31     if(!isxdigit(n))
32       insist(unhexdigitq(n) == -1);
33   }
34   insist(unhexdigitq('0') == 0);
35   insist(unhexdigitq('1') == 1);
36   insist(unhexdigitq('2') == 2);
37   insist(unhexdigitq('3') == 3);
38   insist(unhexdigitq('4') == 4);
39   insist(unhexdigitq('5') == 5);
40   insist(unhexdigitq('6') == 6);
41   insist(unhexdigitq('7') == 7);
42   insist(unhexdigitq('8') == 8);
43   insist(unhexdigitq('9') == 9);
44   insist(unhexdigitq('a') == 10);
45   insist(unhexdigitq('b') == 11);
46   insist(unhexdigitq('c') == 12);
47   insist(unhexdigitq('d') == 13);
48   insist(unhexdigitq('e') == 14);
49   insist(unhexdigitq('f') == 15);
50   insist(unhexdigitq('A') == 10);
51   insist(unhexdigitq('B') == 11);
52   insist(unhexdigitq('C') == 12);
53   insist(unhexdigitq('D') == 13);
54   insist(unhexdigitq('E') == 14);
55   insist(unhexdigitq('F') == 15);
56   check_string(hex(h, sizeof h), "00ff807f");
57   check_string(hex(0, 0), "");
58   u = unhex("00ff807f", &ul);
59   insist(ul == 4);
60   insist(memcmp(u, h, 4) == 0);
61   u = unhex("00FF807F", &ul);
62   insist(ul == 4);
63   insist(memcmp(u, h, 4) == 0);
64   u = unhex("", &ul);
65   insist(ul == 0);
66   fprintf(stderr, "2 ERROR reports expected {\n");
67   insist(unhex("F", 0) == 0);
68   insist(unhex("az", 0) == 0);
69   fprintf(stderr, "}\n");
70 }
71
72 /*
73 Local Variables:
74 c-basic-offset:2
75 comment-column:40
76 fill-column:79
77 indent-tabs-mode:nil
78 End:
79 */