vlm | 46ffdf6 | 2007-07-23 06:48:26 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <assert.h> |
| 3 | |
| 4 | #include <INTEGER.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 5 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 6 | static char *shared_scratch_start; |
| 7 | |
| 8 | static int _print2buf(const void *buf, size_t size, void *key) { |
| 9 | (void)key; |
| 10 | memcpy(shared_scratch_start, buf, size); |
| 11 | shared_scratch_start += size; |
| 12 | *shared_scratch_start = '\0'; /* 0-termination */ |
| 13 | return 0; |
| 14 | } |
| 15 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | static void |
| 17 | check(uint8_t *buf, int size, long check_long, int check_ret) { |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 18 | char scratch[128]; |
| 19 | char verify[32]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | INTEGER_t val; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 21 | uint8_t *buf_end = buf + size; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | int ret; |
| 23 | long rlong = 123; |
| 24 | |
| 25 | assert(buf); |
| 26 | assert(size >= 0); |
| 27 | |
| 28 | val.buf = buf; |
| 29 | val.size = size; |
| 30 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 31 | printf("Testing: ["); |
| 32 | for(; buf < buf_end; buf++) { |
| 33 | if(buf != val.buf) printf(":"); |
| 34 | printf("%02x", *buf); |
| 35 | } |
| 36 | printf("]: "); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 38 | ret = asn_INTEGER2long(&val, &rlong); |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 39 | printf(" (%ld, %d) vs (%ld, %d)\n", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | rlong, ret, check_long, check_ret); |
| 41 | assert(ret == check_ret); |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 42 | printf("%ld %ld\n", rlong, check_long); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | assert(rlong == check_long); |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 44 | |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 45 | if(check_ret == 0) { |
| 46 | INTEGER_t val2; |
| 47 | long rlong2; |
| 48 | val2.buf = 0; |
| 49 | val2.size = 0; |
| 50 | ret = asn_long2INTEGER(&val2, rlong); |
| 51 | assert(ret == 0); |
| 52 | assert(val2.buf); |
| 53 | assert(val2.size <= val.size); /* At least as compact */ |
| 54 | ret = asn_INTEGER2long(&val, &rlong2); |
| 55 | assert(ret == 0); |
| 56 | assert(rlong == rlong2); |
| 57 | } |
| 58 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 59 | shared_scratch_start = scratch; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 60 | ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch); |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 61 | assert(shared_scratch_start < scratch + sizeof(scratch)); |
| 62 | assert(ret == 0); |
| 63 | ret = snprintf(verify, sizeof(verify), "%ld", check_long); |
| 64 | assert(ret < sizeof(verify)); |
| 65 | ret = strcmp(scratch, verify); |
| 66 | printf(" [%s] vs [%s]: %d%s\n", |
| 67 | scratch, verify, ret, |
| 68 | (check_ret == -1)?" (expected to fail)":"" |
| 69 | ); |
| 70 | if(check_ret == -1) { |
| 71 | assert(strcmp(scratch, verify)); |
| 72 | } else { |
| 73 | assert(strcmp(scratch, verify) == 0); |
| 74 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 75 | } |
| 76 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 77 | static void |
| 78 | check_xer(int tofail, char *xmldata, long orig_value) { |
| 79 | INTEGER_t *st = 0; |
| 80 | asn_dec_rval_t rc; |
| 81 | long value; |
| 82 | int ret; |
| 83 | |
vlm | cdc58f4 | 2004-10-21 12:23:41 +0000 | [diff] [blame] | 84 | printf("[%s] vs %ld: ", xmldata, orig_value); |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 85 | |
| 86 | rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st, |
| 87 | xmldata, strlen(xmldata)); |
| 88 | if(rc.code != RC_OK) { |
| 89 | assert(tofail); |
| 90 | printf("\tfailed, as expected\n"); |
| 91 | return; |
| 92 | } |
| 93 | assert(!tofail); |
| 94 | |
| 95 | ret = asn_INTEGER2long(st, &value); |
| 96 | assert(ret == 0); |
| 97 | |
| 98 | printf("\t%ld\n", value); |
| 99 | |
| 100 | assert(value == orig_value); |
| 101 | |
| 102 | asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0); |
| 103 | } |
| 104 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | int |
| 106 | main(int ac, char **av) { |
| 107 | uint8_t buf1[] = { 1 }; |
| 108 | uint8_t buf2[] = { 0xff }; |
| 109 | uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 110 | uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 }; |
| 111 | uint8_t buf5[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; |
| 112 | uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 113 | uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 114 | uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c }; |
| 115 | uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c }; |
| 116 | uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c }; |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 117 | uint8_t buf11[] = { 0x80, 0, 0, 0 }; |
| 118 | uint8_t buf12[] = { 0x80, 0 }; |
| 119 | uint8_t buf13[] = { 0x80 }; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | |
| 121 | #define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret) |
| 122 | |
| 123 | CHECK(buf1, 1, 0); |
| 124 | CHECK(buf2, -1, 0); |
| 125 | CHECK(buf3, -1, 0); |
| 126 | CHECK(buf4, -16, 0); |
| 127 | CHECK(buf5, 257, 0); |
| 128 | CHECK(buf6, 123, -1); |
| 129 | CHECK(buf7, 123, -1); |
| 130 | CHECK(buf8, 0x7F7E7D7C, 0); |
| 131 | CHECK(buf9, 0x7F7E7D7C, 0); |
| 132 | CHECK(buf10, 0x7F7E7D7C, 0); |
vlm | e4c682a | 2005-08-15 01:02:47 +0000 | [diff] [blame] | 133 | CHECK(buf11, -2147483647-1, 0); /* 0x80000000 */ |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 134 | CHECK(buf12, -32768, 0); |
| 135 | CHECK(buf13, -128, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 136 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 137 | check_xer(-1, "", 0); |
| 138 | check_xer(-1, "<INTEGER></INTEGER>", 0); |
vlm | cdc58f4 | 2004-10-21 12:23:41 +0000 | [diff] [blame] | 139 | check_xer(-1, "<INTEGER> </INTEGER>", 0); |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 140 | check_xer(-1, "<INTEGER>-</INTEGER>", 0); |
| 141 | check_xer(-1, "<INTEGER>+</INTEGER>", 0); |
| 142 | check_xer(-1, "<INTEGER>+-</INTEGER>", 0); |
| 143 | check_xer(0, "<INTEGER>+0</INTEGER>", 0); |
| 144 | check_xer(0, "<INTEGER>-0</INTEGER>", 0); |
| 145 | check_xer(0, "<INTEGER>+1</INTEGER>", 1); |
| 146 | check_xer(0, "<INTEGER>-1</INTEGER>", -1); |
| 147 | check_xer(0, "<INTEGER>1</INTEGER>", 1); |
| 148 | check_xer(0, "<INTEGER>-15</INTEGER>", -15); |
| 149 | check_xer(0, "<INTEGER>+15</INTEGER>", 15); |
| 150 | check_xer(0, "<INTEGER>15</INTEGER>", 15); |
| 151 | check_xer(0, "<INTEGER> 15</INTEGER>", 15); |
| 152 | check_xer(0, "<INTEGER> 15 </INTEGER>", 15); |
| 153 | check_xer(0, "<INTEGER>15 </INTEGER>", 15); |
| 154 | check_xer(0, "<INTEGER> +15 </INTEGER>", 15); |
| 155 | check_xer(-1, "<INTEGER> +15 -</INTEGER>", 0); |
| 156 | check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0); |
| 157 | check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0); |
| 158 | check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0); |
| 159 | check_xer(0, "<INTEGER>1234</INTEGER>", 1234); |
| 160 | check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0); |
| 161 | check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647); |
vlm | e4c682a | 2005-08-15 01:02:47 +0000 | [diff] [blame] | 162 | check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483647-1); |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 163 | check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647); |
| 164 | check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647); |
vlm | 1069dac | 2004-10-23 10:17:02 +0000 | [diff] [blame] | 165 | if(sizeof(long) == 4) { |
| 166 | check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0); |
| 167 | check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0); |
| 168 | check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0); |
| 169 | check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0); |
| 170 | check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* special */ |
| 171 | check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0); |
| 172 | check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0); |
| 173 | } |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 174 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | } |