From 347c164c35eddab388009470e6848cb361ac93f8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 15:22:53 +0200 Subject: Merging upstream version 1:10.11.8. Signed-off-by: Daniel Baumann --- unittest/mysys/CMakeLists.txt | 2 +- unittest/mysys/bitmap-t.c | 152 ++++++++++++++++++++++++++++++++++++++++-- unittest/mysys/crc32-t.c | 138 +++++++++++++++++++++++++++++--------- unittest/mysys/my_getopt-t.c | 4 +- 4 files changed, 258 insertions(+), 38 deletions(-) (limited to 'unittest/mysys') diff --git a/unittest/mysys/CMakeLists.txt b/unittest/mysys/CMakeLists.txt index 4b947ab7..9dd5e5db 100644 --- a/unittest/mysys/CMakeLists.txt +++ b/unittest/mysys/CMakeLists.txt @@ -19,7 +19,7 @@ MY_ADD_TESTS(bitmap base64 my_atomic my_rdtsc lf my_malloc my_getopt dynstring MY_ADD_TESTS(my_vsnprintf LINK_LIBRARIES strings mysys) MY_ADD_TESTS(aes LINK_LIBRARIES mysys mysys_ssl) ADD_DEFINITIONS(${SSL_DEFINES}) -INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR}) +INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS}) MY_ADD_TESTS(ma_dyncol LINK_LIBRARIES mysys) IF(WIN32) diff --git a/unittest/mysys/bitmap-t.c b/unittest/mysys/bitmap-t.c index 22466355..8e12b2b8 100644 --- a/unittest/mysys/bitmap-t.c +++ b/unittest/mysys/bitmap-t.c @@ -29,6 +29,8 @@ uint get_rand_bit(uint bitsize) { + if (bitsize == 0) + return 0; return (rand() % bitsize); } @@ -121,6 +123,8 @@ error6: return TRUE; } +PRAGMA_DISABLE_CHECK_STACK_FRAME + my_bool test_compare_operators(MY_BITMAP *map, uint bitsize) { uint i, j, test_bit1, test_bit2, test_bit3,test_bit4; @@ -226,6 +230,8 @@ error5: test_bit1); return TRUE; } +PRAGMA_REENABLE_CHECK_STACK_FRAME + my_bool test_count_bits_set(MY_BITMAP *map, uint bitsize) { @@ -266,7 +272,7 @@ my_bool test_get_first_bit(MY_BITMAP *map, uint bitsize) bitmap_clear_all(map); for (i=0; i < bitsize; i++) bitmap_set_bit(map, i); - if (bitmap_get_first(map) != MY_BIT_NONE) + if (bitmap_get_first_clear(map) != MY_BIT_NONE) goto error2; bitmap_clear_all(map); @@ -278,7 +284,7 @@ my_bool test_get_first_bit(MY_BITMAP *map, uint bitsize) goto error1; bitmap_set_all(map); bitmap_clear_bit(map, test_bit); - if (bitmap_get_first(map) != test_bit) + if (bitmap_get_first_clear(map) != test_bit) goto error2; bitmap_clear_all(map); } @@ -297,14 +303,45 @@ my_bool test_get_next_bit(MY_BITMAP *map, uint bitsize) uint no_loops= bitsize > 128 ? 128 : bitsize; for (i=0; i < no_loops; i++) { + uint count= 0, bits_set= 0; + bitmap_clear_all(map); test_bit=get_rand_bit(bitsize); for (j=0; j < test_bit; j++) bitmap_set_next(map); if (!bitmap_is_prefix(map, test_bit)) goto error1; + j= bitmap_get_first_set(map); + if (j == MY_BIT_NONE) + { + if (test_bit != 0) + goto error1; + continue; + } + count= 1; + while ((j= bitmap_get_next_set(map,j)) != MY_BIT_NONE) + count++; + if (count != test_bit) + goto error1; + + if (test_bit < 3) + continue; bitmap_clear_all(map); + for (j=1; j < test_bit; j+=2) + { + bits_set++; + bitmap_set_bit(map, j); + } + if ((j= bitmap_get_first_set(map)) == MY_BIT_NONE) + goto error1; + count= 1; + while ((j= bitmap_get_next_set(map,j)) != MY_BIT_NONE) + count++; + if (count != bits_set) + goto error1; } + return FALSE; + error1: diag("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit); return TRUE; @@ -371,7 +408,7 @@ error5: my_bool test_compare(MY_BITMAP *map, uint bitsize) { MY_BITMAP map2; - uint32 map2buf[MAX_TESTED_BITMAP_SIZE]; + my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE]; uint i, test_bit; uint no_loops= bitsize > 128 ? 128 : bitsize; if (my_bitmap_init(&map2, map2buf, bitsize)) @@ -431,7 +468,7 @@ my_bool test_intersect(MY_BITMAP *map, uint bitsize) { uint bitsize2 = 1 + get_rand_bit(MAX_TESTED_BITMAP_SIZE - 1); MY_BITMAP map2; - uint32 map2buf[MAX_TESTED_BITMAP_SIZE]; + my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE]; uint i, test_bit1, test_bit2, test_bit3; if (my_bitmap_init(&map2, map2buf, bitsize2)) { @@ -477,6 +514,107 @@ error: return TRUE; } +my_bool test_copy(MY_BITMAP *map, uint bitsize) +{ + my_bitmap_map buff[16], buff2[16], buff3[16]; + MY_BITMAP map2, map3; + uint rnd_bit; + + my_bitmap_init(&map2, buff, sizeof(buff)*8); + my_bitmap_init(&map3, buff2, sizeof(buff)*8); + bitmap_set_all(&map2); + bitmap_set_all(&map3); + + bitsize= MY_MIN(bitsize, map2.n_bits); + bitmap_copy(map, &map2); + if (bitmap_bits_set(map) != bitsize) + { + diag("bitmap_copy failed on bitsize %d", bitsize); + return 1; + } + bitmap_set_prefix(&map2, rnd_bit= get_rand_bit(bitsize)+1); + bitmap_export((uchar*) buff3, &map2); + bitmap_import(&map3, (uchar*) buff3); + if (!bitmap_cmp(&map2, &map3)) + { + diag("bitmap_export/bitmap_import failed on bitsize %d rnd_bit: %d", + bitsize, rnd_bit); + return 1; + } + return 0; +} + +static my_bool exec_bitmap_exists_intersection(MY_BITMAP **maps, uint bitsize, + uint start, uint end, uint bit) +{ + bitmap_clear_all(maps[0]); + bitmap_clear_all(maps[1]); + bitmap_set_bit(maps[0], bit); + bitmap_set_bit(maps[1], bit); + return bitmap_exists_intersection(maps, 2, start, end); +} + +my_bool test_bitmap_exists_intersection(MY_BITMAP *map, uint bitsize) +{ + MY_BITMAP map2; + uint start_bit, end_bit, rnd_bit; + MY_BITMAP *maps[2]; + maps[0]= map; + maps[1]= &map2; + + my_bitmap_init(&map2, 0, bitsize); + bitmap_clear_all(map); + bitmap_clear_all(&map2); + + start_bit= get_rand_bit(bitsize); + end_bit= get_rand_bit(bitsize); + if (start_bit > end_bit) + swap_variables(uint, start_bit, end_bit); + rnd_bit= start_bit+get_rand_bit(end_bit-start_bit); + + if (!exec_bitmap_exists_intersection(maps, bitsize, start_bit, end_bit, + rnd_bit)) + goto err; + + start_bit= end_bit= rnd_bit= 0; + if (!exec_bitmap_exists_intersection(maps, bitsize, start_bit, end_bit, + rnd_bit)) + goto err; + + start_bit= rnd_bit= 0 ; end_bit= bitsize-1; + if (!exec_bitmap_exists_intersection(maps, bitsize, start_bit, end_bit, + rnd_bit)) + goto err; + + start_bit= rnd_bit= end_bit= bitsize-1; + if (!exec_bitmap_exists_intersection(maps, bitsize, start_bit, end_bit, + rnd_bit)) + goto err; + + if (bitsize > 1) + { + start_bit= end_bit= 1 ; rnd_bit= 0; + if (exec_bitmap_exists_intersection(maps, bitsize, start_bit, end_bit, + rnd_bit)) + goto err; + + start_bit= end_bit= bitsize-1 ; rnd_bit= bitsize-2; + if (exec_bitmap_exists_intersection(maps, bitsize, start_bit, end_bit, + rnd_bit)) + goto err; + } + + my_bitmap_free(&map2); + return 0; +err: + diag("bitmap_exist_intersection failed on bitsize: %d start_bit: %d " + "end_bit: %d rnd_bit: %d", + bitsize, start_bit, end_bit, rnd_bit); + my_bitmap_free(&map2); + return 1; +} + + my_bool do_test(uint bitsize) { MY_BITMAP map; @@ -515,6 +653,12 @@ my_bool do_test(uint bitsize) bitmap_clear_all(&map); if (test_intersect(&map,bitsize)) goto error; + bitmap_clear_all(&map); + if (test_copy(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_bitmap_exists_intersection(&map, bitsize)) + goto error; return FALSE; error: return TRUE; diff --git a/unittest/mysys/crc32-t.c b/unittest/mysys/crc32-t.c index 9834d217..7079aeb6 100644 --- a/unittest/mysys/crc32-t.c +++ b/unittest/mysys/crc32-t.c @@ -1,4 +1,4 @@ -/* Copyright (c) MariaDB 2020 +/* Copyright (c) MariaDB 2020, 2024 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -19,51 +19,127 @@ #include #include #include -#include /* - Check that optimized crc32 (ieee, or ethernet polynomical) returns the same - result as zlib (not so well optimized, yet, but trustworthy) + The following lookup table oriented computation of CRC-32 + is based on the Public Domain / Creative Commons CC0 Perl code from + http://billauer.co.il/blog/2011/05/perl-crc32-crc-xs-module/ */ -#define DO_TEST_CRC32(crc,str) \ - ok(crc32(crc,(const Bytef *)str,(uint)(sizeof(str)-1)) == my_checksum(crc, str, sizeof(str)-1), "crc32 '%s'",str) -/* Check that CRC32-C calculation returns correct result*/ -#define DO_TEST_CRC32C(crc,str,expected) \ - do { \ - unsigned int v = my_crc32c(crc, str, sizeof(str)-1); \ - printf("crc32(%u,'%s',%zu)=%u\n",crc,str,sizeof(str)-1,v); \ - ok(expected == my_crc32c(crc, str, sizeof(str)-1),"crc32c '%s'",str); \ - }while(0) +/** Lookup tables */ +static uint32 tab_3309[256], tab_castagnoli[256]; +/** Initialize a lookup table for a CRC-32 polynomial */ +static void init_lookup(uint32 *tab, uint32 polynomial) +{ + unsigned i; + for (i= 0; i < 256; i++) + { + uint32 x= i; + unsigned j; + for (j= 0; j < 8; j++) + if (x & 1) + x= (x >> 1) ^ polynomial; + else + x>>= 1; + tab[i]= x; + } +} + +/** Compute a CRC-32 one octet at a time based on a lookup table */ +static uint crc_(uint32 crc, const void *buf, size_t len, const uint32 *tab) +{ + const unsigned char *b= buf; + const unsigned char *const end = b + len; + crc^= 0xffffffff; + while (b != end) + crc= ((crc >> 8) & 0xffffff) ^ tab[(crc ^ *b++) & 0xff]; + crc^= 0xffffffff; + return crc; +} + +static uint crc32(uint32 crc, const void *buf, size_t len) +{ return crc_(crc, buf, len, tab_3309); } +static uint crc32c(uint32 crc, const void *buf, size_t len) +{ return crc_(crc, buf, len, tab_castagnoli); } -#define LONG_STR "1234567890234568900212345678901231213123321212123123123123123"\ - "............................................................................." \ - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \ - "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" \ - "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +static char buf[16384]; + +typedef uint (*check)(uint32, const void*, size_t); + +static size_t test_buf(check c1, check c2) +{ + size_t s; + for (s= sizeof buf; s; s--) + if (c1(0, buf, s) != c2(0, buf, s)) + break; + return s; +} + +#define DO_TEST_CRC32(crc,str,len) \ + ok(crc32(crc,str,len) == my_checksum(crc, str, len), \ + "crc32(%u,'%.*s')", crc, (int) len, str) + +/* Check that CRC-32C calculation returns correct result*/ +#define DO_TEST_CRC32C(crc,str,len) \ + ok(crc32c(crc,str,len) == my_crc32c(crc, str, len), \ + "crc32c(%u,'%.*s')", crc, (int) len, str) + +static const char STR[]= + "123456789012345678900212345678901231213123321212123123123123123" + "..........................................................................." + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; int main(int argc __attribute__((unused)),char *argv[]) { MY_INIT(argv[0]); - plan(14); + init_lookup(tab_3309, 0xedb88320); + init_lookup(tab_castagnoli, 0x82f63b78); + + plan(36); printf("%s\n",my_crc32c_implementation()); - DO_TEST_CRC32(0,""); - DO_TEST_CRC32(1,""); - DO_TEST_CRC32(0,"12345"); - DO_TEST_CRC32(1,"12345"); - DO_TEST_CRC32(0,"1234567890123456789"); - DO_TEST_CRC32(0, LONG_STR); + DO_TEST_CRC32(0,STR,0); + DO_TEST_CRC32(1,STR,0); + DO_TEST_CRC32(0,STR,3); + DO_TEST_CRC32(0,STR,5); + DO_TEST_CRC32(1,STR,5); + DO_TEST_CRC32(0,STR,15); + DO_TEST_CRC32(0,STR,16); + DO_TEST_CRC32(0,STR,19); + DO_TEST_CRC32(0,STR,32); + DO_TEST_CRC32(0,STR,63); + DO_TEST_CRC32(0,STR,64); + DO_TEST_CRC32(0,STR,65); + DO_TEST_CRC32(0,STR,255); + DO_TEST_CRC32(0,STR,256); + DO_TEST_CRC32(0,STR,257); + DO_TEST_CRC32(0,STR,(sizeof(STR)-1)); ok(0 == my_checksum(0, NULL, 0) , "crc32 data = NULL, length = 0"); - DO_TEST_CRC32C(0,"", 0); - DO_TEST_CRC32C(1,"", 1); - DO_TEST_CRC32C(0, "12345", 416359221); - DO_TEST_CRC32C(1, "12345", 549473433); - DO_TEST_CRC32C(0, "1234567890123456789", 2366987449U); - DO_TEST_CRC32C(0, LONG_STR, 3009234172U); + DO_TEST_CRC32C(0,STR,0); + DO_TEST_CRC32C(1,STR,0); + DO_TEST_CRC32C(0,STR,3); + DO_TEST_CRC32C(0,STR,5); + DO_TEST_CRC32C(1,STR,5); + DO_TEST_CRC32C(0,STR,15); + DO_TEST_CRC32C(0,STR,16); + DO_TEST_CRC32C(0,STR,19); + DO_TEST_CRC32C(0,STR,32); + DO_TEST_CRC32C(0,STR,63); + DO_TEST_CRC32C(0,STR,64); + DO_TEST_CRC32C(0,STR,65); + DO_TEST_CRC32C(0,STR,255); + DO_TEST_CRC32C(0,STR,256); + DO_TEST_CRC32C(0,STR,257); + DO_TEST_CRC32C(0,STR,(sizeof(STR)-1)); ok(0 == my_crc32c(0, NULL, 0), "crc32c data = NULL, length = 0"); + memset(buf, 0x5a, sizeof buf); + ok(0 == test_buf(my_checksum, crc32), "crc32 with various lengths"); + ok(0 == test_buf(my_crc32c, crc32c), "crc32c with various lengths"); + my_end(0); return exit_status(); } diff --git a/unittest/mysys/my_getopt-t.c b/unittest/mysys/my_getopt-t.c index 6ffbfb20..a71c4676 100644 --- a/unittest/mysys/my_getopt-t.c +++ b/unittest/mysys/my_getopt-t.c @@ -378,10 +378,10 @@ int main(int argc __attribute__((unused)), char **argv) "res:%d, argc:%d, opt_ull:%llu", res, arg_c, opt_ull); run("--ull=-100", NULL); - ok(res==9 && arg_c==1 && opt_ull==0ULL, + ok(res==13 && arg_c==0 && opt_ull==0ULL, "res:%d, argc:%d, opt_ull:%llu", res, arg_c, opt_ull); run("--ul=-100", NULL); - ok(res==9 && arg_c==1 && opt_ul==0UL, + ok(res==13 && arg_c==0 && opt_ul==0UL, "res:%d, argc:%d, opt_ul:%lu", res, arg_c, opt_ul); my_end(0); -- cgit v1.2.3