Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame^] | 1 | #include <inttypes.h> |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame^] | 6 | #include <time.h> |
| 7 | #include <stdbool.h> |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 8 | |
| 9 | #include <osmocom/core/utils.h> |
| 10 | #include <osmocom/core/bits.h> |
| 11 | |
| 12 | static const uint8_t input[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; |
| 13 | static const uint8_t exp_out[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame^] | 14 | static char s[18]; |
| 15 | |
| 16 | enum END {LE, BE}; |
| 17 | |
| 18 | const char * end2str(enum END e) { |
| 19 | if (e == LE) return "LE"; |
| 20 | return "BE"; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | /* convenience wrappers */ |
| 25 | |
| 26 | inline uint64_t load64(enum END e, const uint8_t *buf, unsigned nbytes) { |
| 27 | return (e == BE) ? osmo_load64be_ext(buf, nbytes) : osmo_load64le_ext(buf, nbytes); |
| 28 | } |
| 29 | |
| 30 | inline uint32_t load32(enum END e, const uint8_t *buf, unsigned nbytes) { |
| 31 | return (e == BE) ? osmo_load32be_ext(buf, nbytes) : osmo_load32le_ext(buf, nbytes); |
| 32 | } |
| 33 | |
| 34 | inline uint16_t load16(enum END e, const uint8_t *buf) { |
| 35 | return (e == BE) ? osmo_load16be(buf) : osmo_load16le(buf); |
| 36 | } |
| 37 | |
| 38 | inline void store64(enum END e, uint64_t t, uint8_t *buf, unsigned nbytes) { |
| 39 | (e == BE) ? osmo_store64be_ext(t, buf, nbytes) : osmo_store64le_ext(t, buf, nbytes); |
| 40 | } |
| 41 | |
| 42 | inline void store32(enum END e, uint64_t t, uint8_t *buf, unsigned nbytes) { |
| 43 | (e == BE) ? osmo_store32be_ext(t, buf, nbytes) : osmo_store32le_ext(t, buf, nbytes); |
| 44 | } |
| 45 | |
| 46 | inline void store16(enum END e, uint64_t t, uint8_t *buf) { |
| 47 | (e == BE) ? osmo_store16be(t, buf) : osmo_store16le(t, buf); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /* helper functions */ |
| 52 | |
| 53 | inline bool printcheck(bool chk, unsigned nbytes, enum END e, bool b) |
| 54 | { |
| 55 | if (!chk) { |
| 56 | printf("%u %s FAILED", nbytes * 8, end2str(e)); |
| 57 | return true; |
| 58 | } |
| 59 | printf("%u %s OK", nbytes * 8, end2str(e)); |
| 60 | return b; |
| 61 | } |
| 62 | |
| 63 | inline bool dumpcheck(const char *dump, const char *s, unsigned nbytes, bool chk, enum END e, bool b) |
| 64 | { |
| 65 | bool x = printcheck(chk, nbytes, e, b); |
| 66 | if (!dump) return x; |
| 67 | |
| 68 | int m = memcmp(s, dump, nbytes); |
| 69 | if (0 == m) { |
| 70 | printf(", storage OK"); |
| 71 | return x; |
| 72 | } |
| 73 | printf(", [%d]", m); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* printcheckXX(): load/store 'test' and check against 'expected' value, compare to 'dump' buffer if given and print if necessary */ |
| 80 | |
| 81 | inline void printcheck64(enum END e, unsigned nbytes, uint64_t test, uint64_t expected, const char *dump, bool print) |
| 82 | { |
| 83 | uint8_t buf[nbytes]; |
| 84 | |
| 85 | store64(e, test, buf, nbytes); |
| 86 | |
| 87 | char *s = osmo_hexdump_nospc(buf, nbytes); |
| 88 | uint64_t result = load64(e, buf, nbytes); |
| 89 | |
| 90 | print = dumpcheck(dump, s, nbytes, result == expected, e, print); |
| 91 | |
| 92 | if (print) |
| 93 | printf(": buffer %s known buffer %s loaded %.16" PRIx64 " expected %.16" PRIx64, s, dump, result, expected); |
| 94 | printf("\n"); |
| 95 | } |
| 96 | |
| 97 | inline void printcheck32(enum END e, unsigned nbytes, uint32_t test, uint32_t expected, const char *dump, bool print) |
| 98 | { |
| 99 | uint8_t buf[nbytes]; |
| 100 | |
| 101 | store32(e, test, buf, nbytes); |
| 102 | |
| 103 | char *s = osmo_hexdump_nospc(buf, nbytes); |
| 104 | uint32_t result = load32(e, buf, nbytes); |
| 105 | |
| 106 | print = dumpcheck(dump, s, nbytes, result == expected, e, print); |
| 107 | |
| 108 | if (print) |
| 109 | printf(": buffer %s known buffer %s loaded %.8" PRIx32 " expected %.8" PRIx32, s, dump, result, expected); |
| 110 | printf("\n"); |
| 111 | } |
| 112 | |
| 113 | inline void printcheck16(enum END e, uint32_t test, uint32_t expected, const char *dump, bool print) |
| 114 | { |
| 115 | uint8_t buf[2]; |
| 116 | |
| 117 | store16(e, test, buf); |
| 118 | |
| 119 | char *s = osmo_hexdump_nospc(buf, 2); |
| 120 | uint16_t result = load16(e, buf); |
| 121 | |
| 122 | print = dumpcheck(dump, s, 2, result == expected, e, print); |
| 123 | |
| 124 | if (print) |
| 125 | printf(": buffer %s known buffer %s loaded %.4" PRIx16 " expected %.4" PRIx16, s, dump, result, expected); |
| 126 | printf("\n"); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* compute expected value - zero excessive bytes */ |
| 131 | |
| 132 | inline uint64_t exp64(enum END e, unsigned nbytes, uint64_t value) { |
| 133 | uint8_t adj = 64 - nbytes * 8; |
| 134 | uint64_t v = value << adj; |
| 135 | return (e == LE) ? v >> adj : v; |
| 136 | } |
| 137 | |
| 138 | inline uint32_t exp32(enum END e, unsigned nbytes, uint32_t value) { |
| 139 | uint8_t adj = 32 - nbytes * 8; |
| 140 | uint32_t v = value << adj; |
| 141 | return (e == LE) ? v >> adj : v; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /* run actual tests - if 'test' is 0 than generate random test value internally */ |
| 146 | |
| 147 | inline void check64(uint64_t test, uint64_t expected, unsigned nbytes, enum END e) |
| 148 | { |
| 149 | bool print = true; |
| 150 | if (0 == test && 0 == expected) { |
| 151 | test = ((uint64_t)rand() << 32) + rand(); |
| 152 | expected = exp64(e, nbytes, test); |
| 153 | print = false; |
| 154 | } |
| 155 | snprintf(s, 17, "%.16" PRIx64, expected); |
| 156 | printcheck64(e, nbytes, test, expected, (BE == e) ? s : NULL, print); |
| 157 | } |
| 158 | |
| 159 | inline void check32(uint32_t test, uint32_t expected, unsigned nbytes, enum END e) |
| 160 | { |
| 161 | bool print = true; |
| 162 | if (0 == test && 0 == expected) { |
| 163 | test = rand(); |
| 164 | expected = exp32(e, nbytes, test); |
| 165 | print = false; |
| 166 | } |
| 167 | snprintf(s, 17, "%.8" PRIx32, expected); |
| 168 | printcheck32(e, nbytes, test, expected, (BE == e) ? s : NULL, print); |
| 169 | } |
| 170 | |
| 171 | inline void check16(uint16_t test, enum END e) |
| 172 | { |
| 173 | bool print = true; |
| 174 | if (0 == test) { |
| 175 | test = (uint16_t)rand(); |
| 176 | print = false; |
| 177 | } |
| 178 | snprintf(s, 17, "%.4" PRIx16, test); |
| 179 | printcheck16(e, test, test, (BE == e) ? s : NULL, print); |
| 180 | } |
| 181 | |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 182 | |
| 183 | int main(int argc, char **argv) |
| 184 | { |
| 185 | uint8_t out[ARRAY_SIZE(input)]; |
| 186 | unsigned int offs; |
| 187 | |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame^] | 188 | srand(time(NULL)); |
| 189 | |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 190 | for (offs = 0; offs < sizeof(out); offs++) { |
| 191 | uint8_t *start = out + offs; |
| 192 | uint8_t len = sizeof(out) - offs; |
| 193 | |
| 194 | memcpy(out, input, sizeof(out)); |
| 195 | |
| 196 | printf("INORDER: %s\n", osmo_hexdump(start, len)); |
| 197 | osmo_revbytebits_buf(start, len); |
| 198 | printf("REVERSED: %s\n", osmo_hexdump(start, len)); |
| 199 | if (memcmp(start, exp_out + offs, len)) { |
| 200 | printf("EXPECTED: %s\n", osmo_hexdump(exp_out+offs, len)); |
| 201 | fprintf(stderr, "REVERSED != EXPECTED!\n"); |
| 202 | exit(1); |
| 203 | } |
| 204 | printf("\n"); |
| 205 | } |
| 206 | |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame^] | 207 | printf("checking byte packing...\n"); |
| 208 | |
| 209 | printf("running static tests...\n"); |
| 210 | |
| 211 | check64(0xDEADBEEFF00DCAFE, 0xDEADBEEFF00DCAFE, 8, BE); |
| 212 | check64(0xDEADBEEFF00DCAFE, 0xADBEEFF00DCAFE00, 7, BE); |
| 213 | check64(0xDEADBEEFF00DCAFE, 0xBEEFF00DCAFE0000, 6, BE); |
| 214 | check64(0xDEADBEEFF00DCAFE, 0xEFF00DCAFE000000, 5, BE); |
| 215 | |
| 216 | check64(0xDEADBEEFF00DCAFE, 0xDEADBEEFF00DCAFE, 8, LE); |
| 217 | check64(0xDEADBEEFF00DCAFE, 0x00ADBEEFF00DCAFE, 7, LE); |
| 218 | check64(0xDEADBEEFF00DCAFE, 0x0000BEEFF00DCAFE, 6, LE); |
| 219 | check64(0xDEADBEEFF00DCAFE, 0x000000EFF00DCAFE, 5, LE); |
| 220 | |
| 221 | check32(0xBABEFACE, 0xBABEFACE, 4, BE); |
| 222 | check32(0xBABEFACE, 0xBEFACE00, 3, BE); |
| 223 | |
| 224 | check32(0xBABEFACE, 0xBABEFACE, 4, LE); |
| 225 | check32(0xBABEFACE, 0x00BEFACE, 3, LE); |
| 226 | |
| 227 | check16(0xB00B, BE); |
| 228 | check16(0xB00B, LE); |
| 229 | |
| 230 | printf("running random tests...\n"); |
| 231 | |
| 232 | check64(0, 0, 8, BE); |
| 233 | check64(0, 0, 7, BE); |
| 234 | check64(0, 0, 6, BE); |
| 235 | check64(0, 0, 5, BE); |
| 236 | |
| 237 | check64(0, 0, 8, LE); |
| 238 | check64(0, 0, 7, LE); |
| 239 | check64(0, 0, 6, LE); |
| 240 | check64(0, 0, 5, LE); |
| 241 | |
| 242 | check32(0, 0, 4, BE); |
| 243 | check32(0, 0, 3, BE); |
| 244 | |
| 245 | check32(0, 0, 4, LE); |
| 246 | check32(0, 0, 3, LE); |
| 247 | |
| 248 | check16(0, BE); |
| 249 | check16(0, LE); |
| 250 | |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 251 | return 0; |
| 252 | } |