| 1 | #include "endianess.h" |
| 2 | #include <assert.h> |
| 3 | |
| 4 | void test_little_32(void) |
| 5 | { |
| 6 | uint32_t t; |
| 7 | uint8_t res[4]; |
| 8 | |
| 9 | t = 0x04030201U; |
| 10 | memset(res, 0xFF, 4); |
| 11 | u32to8_little(res, &t); |
| 12 | assert(0 == memcmp(res, "\x01\x02\x03\x04", 4)); |
| 13 | |
| 14 | t = ~0U; |
| 15 | u8to32_little(&t, res); |
| 16 | assert(t == 0x04030201U); |
| 17 | |
| 18 | t = ~0U; |
| 19 | t = load_u8to32_little(res); |
| 20 | assert(t == 0x04030201U); |
| 21 | |
| 22 | |
| 23 | t = ~0U; |
| 24 | t = LOAD_U32_LITTLE(res); |
| 25 | assert(t == 0x04030201U); |
| 26 | |
| 27 | t = 0x04030201U; |
| 28 | memset(res, 0xFF, 4); |
| 29 | STORE_U32_LITTLE(res, t); |
| 30 | assert(0 == memcmp(res, "\x01\x02\x03\x04", 4)); |
| 31 | } |
| 32 | |
| 33 | void test_big_32(void) |
| 34 | { |
| 35 | uint32_t t; |
| 36 | uint8_t res[4]; |
| 37 | |
| 38 | t = 0x04030201U; |
| 39 | memset(res, 0xFF, 4); |
| 40 | u32to8_big(res, &t); |
| 41 | assert(0 == memcmp(res, "\x04\x03\x02\x01", 4)); |
| 42 | |
| 43 | t = ~0U; |
| 44 | u8to32_big(&t, res); |
| 45 | assert(t == 0x04030201U); |
| 46 | |
| 47 | t = ~0U; |
| 48 | t = load_u8to32_big(res); |
| 49 | assert(t == 0x04030201U); |
| 50 | |
| 51 | |
| 52 | t = ~0U; |
| 53 | t = LOAD_U32_BIG(res); |
| 54 | assert(t == 0x04030201U); |
| 55 | |
| 56 | t = 0x04030201U; |
| 57 | memset(res, 0xFF, 4); |
| 58 | STORE_U32_BIG(res, t); |
| 59 | assert(0 == memcmp(res, "\x04\x03\x02\x01", 4)); |
| 60 | } |
| 61 | |
| 62 | void test_little_64(void) |
| 63 | { |
| 64 | uint64_t t; |
| 65 | uint8_t res[8]; |
| 66 | |
| 67 | t = 0x0807060504030201UL; |
| 68 | memset(res, 0xFF, 8); |
| 69 | u64to8_little(res, &t); |
| 70 | assert(0 == memcmp(res, "\x01\x02\x03\x04\x05\x06\x07\x08", 8)); |
| 71 | |
| 72 | t = ~0UL; |
| 73 | u8to64_little(&t, res); |
| 74 | assert(t == 0x0807060504030201UL); |
| 75 | |
| 76 | t = ~0UL; |
| 77 | t = load_u8to64_little(res); |
| 78 | assert(t == 0x0807060504030201UL); |
| 79 | |
| 80 | |
| 81 | t = ~0UL; |
| 82 | t = LOAD_U64_LITTLE(res); |
| 83 | assert(t == 0x0807060504030201UL); |
| 84 | |
| 85 | t = 0x0807060504030201UL; |
| 86 | memset(res, 0xFF, 8); |
| 87 | STORE_U64_LITTLE(res, t); |
| 88 | assert(0 == memcmp(res, "\x01\x02\x03\x04\x05\x06\x07\x08", 8)); |
| 89 | } |
| 90 | |
| 91 | void test_big_64(void) |
| 92 | { |
| 93 | uint64_t t; |
| 94 | uint8_t res[8]; |
| 95 | |
| 96 | t = 0x0807060504030201UL; |
| 97 | memset(res, 0xFF, 8); |
| 98 | u64to8_big(res, &t); |
| 99 | assert(0 == memcmp(res, "\x08\x07\x06\x05\x04\x03\x02\x01", 8)); |
| 100 | |
| 101 | t = ~0UL; |
| 102 | u8to64_big(&t, res); |
| 103 | assert(t == 0x0807060504030201UL); |
| 104 | |
| 105 | t = ~0UL; |
| 106 | t = load_u8to64_big(res); |
| 107 | assert(t == 0x0807060504030201UL); |
| 108 | |
| 109 | |
| 110 | t = ~0UL; |
| 111 | t = LOAD_U64_BIG(res); |
| 112 | assert(t == 0x0807060504030201UL); |
| 113 | |
| 114 | t = 0x0807060504030201UL; |
| 115 | memset(res, 0xFF, 8); |
| 116 | STORE_U64_BIG(res, t); |
| 117 | assert(0 == memcmp(res, "\x08\x07\x06\x05\x04\x03\x02\x01", 8)); |
| 118 | } |
| 119 | |
| 120 | void test_bytes_to_words(void) |
| 121 | { |
| 122 | int res; |
| 123 | uint8_t b2[2] = { 9, 3 }; |
| 124 | uint8_t b9[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 125 | uint64_t w2[2] = { 6, 4 }; |
| 126 | uint64_t w2_0[2]; |
| 127 | uint8_t b17[17]; |
| 128 | |
| 129 | res = bytes_to_words(NULL, 10, b2, 2); |
| 130 | assert(res == ERR_NULL); |
| 131 | res = bytes_to_words(w2, 10, NULL, 2); |
| 132 | assert(res == ERR_NULL); |
| 133 | res = bytes_to_words(w2, 2, b2, 0); |
| 134 | assert(res == ERR_NOT_ENOUGH_DATA); |
| 135 | res = bytes_to_words(w2, 0, b2, 2); |
| 136 | assert(res == ERR_NOT_ENOUGH_DATA); |
| 137 | |
| 138 | memset(b17, 0, 17); |
| 139 | b17[0] = 1; |
| 140 | res = bytes_to_words(w2, 2, b17, 17); |
| 141 | assert(res == ERR_MAX_DATA); |
| 142 | |
| 143 | b17[0] = 0; |
| 144 | res = bytes_to_words(w2, 2, b17, 17); |
| 145 | assert(res == 0); |
| 146 | assert(w2[0] == 0 && w2[1] == 0); |
| 147 | |
| 148 | res = bytes_to_words(w2_0, 2, b2, 2); |
| 149 | assert(w2_0[0] == 0x0903); |
| 150 | assert(w2_0[1] == 0); |
| 151 | assert(res == 0); |
| 152 | |
| 153 | res = bytes_to_words(w2_0, 2, b9, 9); |
| 154 | assert(w2_0[0] == 0x0203040506070809); |
| 155 | assert(w2_0[1] == 0x01); |
| 156 | assert(res == 0); |
| 157 | } |
| 158 | |
| 159 | void test_words_to_bytes(void) |
| 160 | { |
| 161 | int res; |
| 162 | uint8_t b2[2] = { 9, 3 }; |
| 163 | uint8_t b9[9]; |
| 164 | uint64_t w2[2] = { 6, 4 }; |
| 165 | uint8_t b17[17]; |
| 166 | uint64_t w3[3]; |
| 167 | unsigned i; |
| 168 | |
| 169 | memset(b17, 0xFF, 17); |
| 170 | |
| 171 | res = words_to_bytes(NULL, 16, w2, 2); |
| 172 | assert(res == ERR_NULL); |
| 173 | res = words_to_bytes(b2, 16, NULL, 2); |
| 174 | assert(res == ERR_NULL); |
| 175 | res = words_to_bytes(b2, 2, w2, 0); |
| 176 | assert(res == ERR_NOT_ENOUGH_DATA); |
| 177 | res = words_to_bytes(b2, 0, w2, 2); |
| 178 | assert(res == ERR_NOT_ENOUGH_DATA); |
| 179 | |
| 180 | res = words_to_bytes(b9, 8, w2, 2); |
| 181 | assert(res == ERR_MAX_DATA); |
| 182 | |
| 183 | res = words_to_bytes(b9, 9, w2, 2); |
| 184 | assert(res == 0); |
| 185 | assert(0 == memcmp(b9, "\x04\x00\x00\x00\x00\x00\x00\x00\x06", 9)); |
| 186 | |
| 187 | res = words_to_bytes(b9, 9, w2, 1); |
| 188 | assert(res == 0); |
| 189 | assert(0 == memcmp(b9, "\x00\x00\x00\x00\x00\x00\x00\x00\x06", 9)); |
| 190 | |
| 191 | res = words_to_bytes(b17, 17, w2, 2); |
| 192 | assert(b17[0] == 0); |
| 193 | assert(0 == memcmp(b17+1, "\x00\x00\x00\x00\x00\x00\x00\x04", 8)); |
| 194 | assert(0 == memcmp(b17+9, "\x00\x00\x00\x00\x00\x00\x00\x06", 8)); |
| 195 | assert(res == 0); |
| 196 | |
| 197 | memset(b17, 0xFF, sizeof b17); |
| 198 | memset(w3, 0, sizeof w3); |
| 199 | res = words_to_bytes(b17, 17, w3, 3); |
| 200 | assert(res == 0); |
| 201 | for (i=0; i<sizeof b17; i++) { |
| 202 | assert(b17[i] == 0); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | int main(void) |
| 207 | { |
| 208 | test_little_32(); |
| 209 | test_big_32(); |
| 210 | test_little_64(); |
| 211 | test_big_64(); |
| 212 | test_bytes_to_words(); |
| 213 | test_words_to_bytes(); |
| 214 | return 0; |
| 215 | } |