cb479ffd74243daeff5bfedba5e1ca25e4cc3496
[com/asn1c.git] / libasn1parser / check_asn1p_integer.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <limits.h>
6 #include <assert.h>
7 #include <errno.h>
8
9 #include "asn1p_integer.h"
10
11 static void check(int lineno, asn1c_integer_t value, const char *expected);
12
13 struct {
14     uint8_t *buf; size_t size;
15 } ff = {
16     (uint8_t *)"sdf", 3
17 };
18
19 int main() {
20
21     check(__LINE__, 0, "{ (uint8_t *)\"\\x00\\0\", 1 }");
22     check(__LINE__, 1, "{ (uint8_t *)\"\\x01\\0\", 1 }");
23     check(__LINE__, 127, "{ (uint8_t *)\"\\x7f\\0\", 1 }");
24     check(__LINE__, 128, "{ (uint8_t *)\"\\x00\\x80\\0\", 2 }");
25     check(__LINE__, 129, "{ (uint8_t *)\"\\x00\\x81\\0\", 2 }");
26
27     check(__LINE__, -1, "{ (uint8_t *)\"\\xff\\0\", 1 }");
28     check(__LINE__, -2, "{ (uint8_t *)\"\\xfe\\0\", 1 }");
29     check(__LINE__, -127, "{ (uint8_t *)\"\\x81\\0\", 1 }");
30     check(__LINE__, -128, "{ (uint8_t *)\"\\x80\\0\", 1 }");
31     check(__LINE__, -129, "{ (uint8_t *)\"\\xff\\x7f\\0\", 2 }");
32     check(__LINE__, -254, "{ (uint8_t *)\"\\xff\\x02\\0\", 2 }");
33     check(__LINE__, -255, "{ (uint8_t *)\"\\xff\\x01\\0\", 2 }");
34     check(__LINE__, -256, "{ (uint8_t *)\"\\xff\\x00\\0\", 2 }");
35     check(__LINE__, -257, "{ (uint8_t *)\"\\xfe\\xff\\0\", 2 }");
36
37     check(__LINE__, ~(asn1c_integer_t)0, "{ (uint8_t *)\"\\xff\\0\", 1 }");
38
39     switch(sizeof(asn1c_integer_t)) {
40     case 4:
41         check(__LINE__,
42               ~(asn1c_integer_t)0
43                   & ~((asn1c_integer_t)1 << (8 * sizeof(asn1c_integer_t) - 1)),
44               "{ (uint8_t *)\""
45               "\\x7f\\xff\\xff\\xff"
46               "\\0\", 4 }");
47         break;
48     case 8:
49         check(__LINE__,
50               ~(asn1c_integer_t)0
51                   & ~((asn1c_integer_t)1 << (8 * sizeof(asn1c_integer_t) - 1)),
52               "{ (uint8_t *)\""
53               "\\x7f\\xff\\xff\\xff\\xff\\xff\\xff\\xff"
54               "\\0\", 8 }");
55         break;
56     case 16:
57         check(__LINE__,
58               ~(asn1c_integer_t)0
59                   & ~((asn1c_integer_t)1 << (8 * sizeof(asn1c_integer_t) - 1)),
60               "{ (uint8_t *)\""
61               "\\x7f\\xff\\xff\\xff\\xff\\xff\\xff\\xff"
62               "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff"
63               "\\0\", 16 }");
64
65         break;
66     default:
67         assert(!"Unreachable");
68     }
69
70     return 0;
71 }
72
73 static void check(int lineno, asn1c_integer_t value, const char *expected) {
74     abuf *ab;
75     ab = asn1p_integer_as_INTEGER(value);
76     assert(ab);
77     if(strcmp(ab->buffer, expected)) {
78         fprintf(stderr, "%02d: %s -> [%s], expected [%s]\n", lineno,
79                 asn1p_itoa(value), ab->buffer, expected);
80         assert(strcmp(ab->buffer, expected) == 0);
81     }
82     printf("%02d: %s -> %s\n", lineno, asn1p_itoa(value), ab->buffer);
83     abuf_free(ab);
84 }