librfn
An ad-hoc utility library
hextest.c
Go to the documentation of this file.
1 /*
2  * enumtest.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #undef NDEBUG
15 //#define VERBOSE
16 
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <librfn.h>
23 
24 static unsigned long parse32(const char *s)
25 {
26  unsigned long acc = 0;
27 
28  for (int i=0; i<4; i++) {
29  int res = hex_get_byte(s, &s);
30  if (res < 0)
31  return acc;
32  acc = (acc << 8) + res;
33  }
34 
35  return acc;
36 }
37 
38 void test_get_byte(void)
39 {
40 #define V(x) verify(x == parse32(#x))
41  V(00);
42  V(0x00);
43  V(0x12345678);
44  V(0x80000000);
45  V(0xCCccCcCc);
46  V(0xdeadbeef);
47  V(0xfeedface);
48  V(0xffffffff);
49 #undef V
50 
51  verify(0 == parse32("00000000"));
52  verify(0x1234abcd == parse32("1234abcd"));
53 
54  /* single characters are not (correctly) parseable. hex_get_byte() is
55  * designed to decode a hexdump... it does *not* use C lexing rules.
56  */
57 #define X(x) verify(x != parse32(#x))
58  X(1);
59  X(123);
60 #undef X
61 
62 }
63 
64 int main()
65 {
66  test_get_byte();
67  return 0;
68 }
void test_get_byte(void)
Definition: hextest.c:38
#define X(x)
#define verify(x)
Definition: util.h:55
#define V(x)
int hex_get_byte(const char *s, const char **p)
Definition: hex.c:58
int main()
Definition: hextest.c:64