|
| 1 | +/* Copyright (c) 2019, MariaDB |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | + |
| 16 | +/** |
| 17 | + @file |
| 18 | +
|
| 19 | + Unit tests for serialization and deserialization functions |
| 20 | +*/ |
| 21 | + |
| 22 | +#include "tap.h" |
| 23 | + |
| 24 | +#include "my_byteorder.h" |
| 25 | +#include "myisampack.h" |
| 26 | +#include "m_string.h" |
| 27 | + |
| 28 | +void test_byte_order() |
| 29 | +{ |
| 30 | + MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE) |
| 31 | + uchar buf[CPU_LEVEL1_DCACHE_LINESIZE * 2]; |
| 32 | + |
| 33 | + uchar *aligned= buf; |
| 34 | + uchar *not_aligned= buf + CPU_LEVEL1_DCACHE_LINESIZE - 1; |
| 35 | + |
| 36 | +#define TEST(STORE_NAME, LOAD_NAME, TYPE, VALUE, BYTES) \ |
| 37 | + { \ |
| 38 | + TYPE value= VALUE; \ |
| 39 | + uchar bytes[]= BYTES; \ |
| 40 | + STORE_NAME(aligned, value); \ |
| 41 | + ok(!memcmp(aligned, bytes, sizeof(bytes)), "aligned\t\t" #STORE_NAME); \ |
| 42 | + ok(LOAD_NAME(aligned) == value, "aligned\t\t" #LOAD_NAME); \ |
| 43 | + STORE_NAME(not_aligned, value); \ |
| 44 | + ok(!memcmp(not_aligned, bytes, sizeof(bytes)), \ |
| 45 | + "not aligned\t" #STORE_NAME); \ |
| 46 | + ok(LOAD_NAME(not_aligned) == value, "not aligned\t" #LOAD_NAME); \ |
| 47 | + } |
| 48 | + |
| 49 | +#define ARRAY_2(A, B) {A, B} |
| 50 | +#define ARRAY_3(A, B, C) {A, B, C} |
| 51 | +#define ARRAY_4(A, B, C, D) {A, B, C, D} |
| 52 | +#define ARRAY_5(A, B, C, D, E) {A, B, C, D, E} |
| 53 | +#define ARRAY_6(A, B, C, D, E, F) {A, B, C, D, E, F} |
| 54 | +#define ARRAY_7(A, B, C, D, E, F, G) {A, B, C, D, E, F, G} |
| 55 | +#define ARRAY_8(A, B, C, D, E, F, G, H) {A, B, C, D, E, F, G, H} |
| 56 | + |
| 57 | + TEST(int2store, sint2korr, int16, 0x0201, ARRAY_2(1, 2)); |
| 58 | + TEST(int3store, sint3korr, int32, 0xffffffff, ARRAY_3(0xff, 0xff, 0xff)); |
| 59 | + TEST(int3store, sint3korr, int32, 0x030201, ARRAY_3(1, 2, 3)); |
| 60 | + TEST(int4store, sint4korr, int32, 0xffffffff, |
| 61 | + ARRAY_4(0xff, 0xff, 0xff, 0xff)); |
| 62 | + TEST(int4store, sint4korr, int32, 0x04030201, ARRAY_4(1, 2, 3, 4)); |
| 63 | + TEST(int8store, sint8korr, longlong, 0x0807060504030201, |
| 64 | + ARRAY_8(1, 2, 3, 4, 5, 6, 7, 8)); |
| 65 | + TEST(int8store, sint8korr, longlong, 0xffffffffffffffff, |
| 66 | + ARRAY_8(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff)); |
| 67 | + |
| 68 | + TEST(int2store, uint2korr, uint16, 0x0201, ARRAY_2(1, 2)); |
| 69 | + TEST(int3store, uint3korr, uint32, 0x030201, ARRAY_3(1, 2, 3)); |
| 70 | + TEST(int4store, uint4korr, uint32, 0x04030201, ARRAY_4(1, 2, 3, 4)); |
| 71 | + TEST(int5store, uint5korr, ulonglong, 0x0504030201, ARRAY_5(1, 2, 3, 4, 5)); |
| 72 | + TEST(int6store, uint6korr, ulonglong, 0x060504030201, |
| 73 | + ARRAY_6(1, 2, 3, 4, 5, 6)); |
| 74 | + TEST(int8store, uint8korr, ulonglong, 0x0807060504030201, |
| 75 | + ARRAY_8(1, 2, 3, 4, 5, 6, 7, 8)); |
| 76 | + |
| 77 | + TEST(mi_int5store, mi_uint5korr, ulonglong, 0x0504030201, |
| 78 | + ARRAY_5(5, 4, 3, 2, 1)); |
| 79 | + TEST(mi_int6store, mi_uint6korr, ulonglong, 0x060504030201, |
| 80 | + ARRAY_6(6, 5, 4, 3, 2, 1)); |
| 81 | + TEST(mi_int7store, mi_uint7korr, ulonglong, 0x07060504030201, |
| 82 | + ARRAY_7(7, 6, 5, 4, 3, 2, 1)); |
| 83 | + TEST(mi_int8store, mi_uint8korr, ulonglong, 0x0807060504030201, |
| 84 | + ARRAY_8(8, 7, 6, 5, 4, 3, 2, 1)); |
| 85 | + |
| 86 | +#undef ARRAY_8 |
| 87 | +#undef ARRAY_7 |
| 88 | +#undef ARRAY_6 |
| 89 | +#undef ARRAY_5 |
| 90 | +#undef ARRAY_4 |
| 91 | +#undef ARRAY_3 |
| 92 | +#undef ARRAY_2 |
| 93 | + |
| 94 | +#undef TEST |
| 95 | +} |
| 96 | + |
| 97 | +int main(int argc, char **argv) |
| 98 | +{ |
| 99 | + plan(68); |
| 100 | + test_byte_order(); |
| 101 | + return exit_status(); |
| 102 | +} |
0 commit comments