chiark / gitweb /
Synchronize with DisOrder 4.1
[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 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 static 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   for(n = 0; n <= UCHAR_MAX; ++n) {
29     if(!isxdigit(n))
30       insist(unhexdigitq(n) == -1);
31   }
32   insist(unhexdigitq('0') == 0);
33   insist(unhexdigitq('1') == 1);
34   insist(unhexdigitq('2') == 2);
35   insist(unhexdigitq('3') == 3);
36   insist(unhexdigitq('4') == 4);
37   insist(unhexdigitq('5') == 5);
38   insist(unhexdigitq('6') == 6);
39   insist(unhexdigitq('7') == 7);
40   insist(unhexdigitq('8') == 8);
41   insist(unhexdigitq('9') == 9);
42   insist(unhexdigitq('a') == 10);
43   insist(unhexdigitq('b') == 11);
44   insist(unhexdigitq('c') == 12);
45   insist(unhexdigitq('d') == 13);
46   insist(unhexdigitq('e') == 14);
47   insist(unhexdigitq('f') == 15);
48   insist(unhexdigitq('A') == 10);
49   insist(unhexdigitq('B') == 11);
50   insist(unhexdigitq('C') == 12);
51   insist(unhexdigitq('D') == 13);
52   insist(unhexdigitq('E') == 14);
53   insist(unhexdigitq('F') == 15);
54   check_string(hex(h, sizeof h), "00ff807f");
55   check_string(hex(0, 0), "");
56   u = unhex("00ff807f", &ul);
57   insist(ul == 4);
58   insist(memcmp(u, h, 4) == 0);
59   u = unhex("00FF807F", &ul);
60   insist(ul == 4);
61   insist(memcmp(u, h, 4) == 0);
62   u = unhex("", &ul);
63   insist(ul == 0);
64   fprintf(stderr, "2 ERROR reports expected {\n");
65   insist(unhex("F", 0) == 0);
66   insist(unhex("az", 0) == 0);
67   fprintf(stderr, "}\n");
68 }
69
70 TEST(hex);
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 */