diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 18:07:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 18:07:14 +0000 |
commit | a175314c3e5827eb193872241446f2f8f5c9d33c (patch) | |
tree | cd3d60ca99ae00829c52a6ca79150a5b6e62528b /unittest/mysys | |
parent | Initial commit. (diff) | |
download | mariadb-10.5-a175314c3e5827eb193872241446f2f8f5c9d33c.tar.xz mariadb-10.5-a175314c3e5827eb193872241446f2f8f5c9d33c.zip |
Adding upstream version 1:10.5.12.upstream/1%10.5.12upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'unittest/mysys')
-rw-r--r-- | unittest/mysys/CMakeLists.txt | 28 | ||||
-rw-r--r-- | unittest/mysys/aes-t.c | 99 | ||||
-rw-r--r-- | unittest/mysys/base64-t.c | 99 | ||||
-rw-r--r-- | unittest/mysys/bitmap-t.c | 541 | ||||
-rw-r--r-- | unittest/mysys/byte_order-t.c | 102 | ||||
-rw-r--r-- | unittest/mysys/crc32-t.c | 69 | ||||
-rw-r--r-- | unittest/mysys/dynstring-t.c | 74 | ||||
-rw-r--r-- | unittest/mysys/lf-t.c | 199 | ||||
-rw-r--r-- | unittest/mysys/ma_dyncol-t.c | 881 | ||||
-rw-r--r-- | unittest/mysys/my_atomic-t.c | 131 | ||||
-rw-r--r-- | unittest/mysys/my_delete-t.c | 57 | ||||
-rw-r--r-- | unittest/mysys/my_getopt-t.c | 394 | ||||
-rw-r--r-- | unittest/mysys/my_malloc-t.c | 44 | ||||
-rw-r--r-- | unittest/mysys/my_rdtsc-t.c | 230 | ||||
-rw-r--r-- | unittest/mysys/my_vsnprintf-t.c | 211 | ||||
-rw-r--r-- | unittest/mysys/queues-t.c | 139 | ||||
-rw-r--r-- | unittest/mysys/stacktrace-t.c | 69 | ||||
-rw-r--r-- | unittest/mysys/thr_template.c | 73 | ||||
-rw-r--r-- | unittest/mysys/waiting_threads-t.c | 286 |
19 files changed, 3726 insertions, 0 deletions
diff --git a/unittest/mysys/CMakeLists.txt b/unittest/mysys/CMakeLists.txt new file mode 100644 index 00000000..4b947ab7 --- /dev/null +++ b/unittest/mysys/CMakeLists.txt @@ -0,0 +1,28 @@ +# Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA + +MY_ADD_TESTS(bitmap base64 my_atomic my_rdtsc lf my_malloc my_getopt dynstring + byte_order + queues stacktrace crc32 LINK_LIBRARIES mysys) +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}) +MY_ADD_TESTS(ma_dyncol LINK_LIBRARIES mysys) + +IF(WIN32) + MY_ADD_TESTS(my_delete LINK_LIBRARIES mysys) +ENDIF() + diff --git a/unittest/mysys/aes-t.c b/unittest/mysys/aes-t.c new file mode 100644 index 00000000..34704e06 --- /dev/null +++ b/unittest/mysys/aes-t.c @@ -0,0 +1,99 @@ +/* Copyright (c) 2003, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc. + Use is subject to license terms. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include <my_crypt.h> +#include <tap.h> +#include <string.h> +#include <ctype.h> + +#define DO_TEST(mode, nopad, slen, fill, dlen, hash) \ + SKIP_BLOCK_IF(mode == 0xDEADBEAF, nopad ? 4 : 5, #mode " not supported") \ + { \ + memset(src, fill, src_len= slen); \ + ok(my_aes_crypt(mode, nopad | ENCRYPTION_FLAG_ENCRYPT, \ + src, src_len, dst, &dst_len, \ + key, sizeof(key), iv, sizeof(iv)) == MY_AES_OK, \ + "encrypt " #mode " %u %s", src_len, nopad ? "nopad" : "pad"); \ + if (!nopad) \ + ok (dst_len == my_aes_get_size(mode, src_len), "my_aes_get_size");\ + my_md5(md5, (char*)dst, dst_len); \ + ok(dst_len == dlen && memcmp(md5, hash, sizeof(md5)) == 0, "md5"); \ + ok(my_aes_crypt(mode, nopad | ENCRYPTION_FLAG_DECRYPT, \ + dst, dst_len, ddst, &ddst_len, \ + key, sizeof(key), iv, sizeof(iv)) == MY_AES_OK, \ + "decrypt " #mode " %u", dst_len); \ + ok(ddst_len == src_len && memcmp(src, ddst, src_len) == 0, "memcmp"); \ + } + +#define DO_TEST_P(M,S,F,D,H) DO_TEST(M,0,S,F,D,H) +#define DO_TEST_N(M,S,F,D,H) DO_TEST(M,ENCRYPTION_FLAG_NOPAD,S,F,D,H) + +/* useful macro for debugging */ +#define PRINT_MD5() \ + do { \ + uint i; \ + printf("\""); \ + for (i=0; i < sizeof(md5); i++) \ + printf("\\x%02x", md5[i]); \ + printf("\"\n"); \ + } while(0); + +#ifndef HAVE_EncryptAes128Ctr +const uint MY_AES_CTR=0xDEADBEAF; +#endif +#ifndef HAVE_EncryptAes128Gcm +const uint MY_AES_GCM=0xDEADBEAF; +#endif + +int +main(int argc __attribute__((unused)),char *argv[]) +{ + uchar key[16]= {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6}; + uchar iv[16]= {2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7}; + uchar src[1000], dst[1100], ddst[1000]; + uchar md5[MY_MD5_HASH_SIZE]; + uint src_len, dst_len, ddst_len; + + MY_INIT(argv[0]); + + plan(87); + DO_TEST_P(MY_AES_ECB, 200, '.', 208, "\xd8\x73\x8e\x3a\xbc\x66\x99\x13\x7f\x90\x23\x52\xee\x97\x6f\x9a"); + DO_TEST_P(MY_AES_ECB, 128, '?', 144, "\x19\x58\x33\x85\x4c\xaa\x7f\x06\xd1\xb2\xec\xd7\xb7\x6a\xa9\x5b"); + DO_TEST_P(MY_AES_CBC, 159, '%', 160, "\x4b\x03\x18\x3d\xf1\xa7\xcd\xa1\x46\xb3\xc6\x8a\x92\xc0\x0f\xc9"); + DO_TEST_P(MY_AES_CBC, 192, '@', 208, "\x54\xc4\x75\x1d\xff\xe0\xf6\x80\xf0\x85\xbb\x8b\xda\x07\x21\x17"); + DO_TEST_N(MY_AES_ECB, 200, '.', 200, "\xbf\xec\x43\xd1\x66\x8d\x01\xad\x3a\x25\xee\xa6\x3d\xc6\xc4\x68"); + DO_TEST_N(MY_AES_ECB, 128, '?', 128, "\x5b\x44\x20\xf3\xd9\xb4\x9d\x74\x5e\xb7\x5a\x0a\xe7\x32\x35\xc3"); + DO_TEST_N(MY_AES_CBC, 159, '%', 159, "\xf3\x6e\x40\x00\x3c\x08\xa0\xb1\x2d\x1f\xcf\xce\x54\xc9\x73\x83"); + DO_TEST_N(MY_AES_CBC, 192, '@', 192, "\x30\xe5\x28\x8c\x4a\x3b\x02\xd7\x56\x40\x59\x25\xac\x58\x09\x22"); + DO_TEST_P(MY_AES_CTR, 200, '.', 200, "\x5a\x77\x19\xea\x67\x50\xe3\xab\x7f\x39\x6f\xc4\xa8\x09\xc5\x88"); + DO_TEST_P(MY_AES_GCM, 128, '?', 144, "\x54\x6a\x7c\xa2\x04\xdc\x6e\x80\x1c\xcd\x5f\x7a\x7b\x08\x9e\x9d"); + + /* test short inputs (less that one block) */ + DO_TEST_P(MY_AES_ECB, 1, '.', 16, "\x6c\xd7\x66\x5b\x1b\x1e\x3a\x04\xfd\xb1\x91\x8d\x0e\xfd\xf1\x86"); + DO_TEST_P(MY_AES_ECB, 2, '?', 16, "\xdb\x84\x9e\xaf\x5f\xcc\xdb\x6b\xf2\x1c\xeb\x53\x75\xa3\x53\x5e"); + DO_TEST_P(MY_AES_CBC, 3, '%', 16, "\x60\x8e\x45\x9a\x07\x39\x63\xce\x02\x19\xdd\x52\xe3\x09\x2a\x66"); + DO_TEST_P(MY_AES_CBC, 4, '@', 16, "\x90\xc2\x6b\xf8\x84\x79\x83\xbd\xc1\x60\x71\x04\x55\x6a\xce\x9e"); + DO_TEST_N(MY_AES_ECB, 5, '.', 5, "\x6b\x60\xdc\xa4\x24\x9b\x02\xbb\x24\x41\x9b\xb0\xd1\x01\xcd\xba"); + DO_TEST_N(MY_AES_ECB, 6, '?', 6, "\x35\x8f\xb7\x9d\xd9\x61\x21\xcf\x25\x66\xd5\x9e\x91\xc1\x42\x7e"); + DO_TEST_N(MY_AES_CBC, 7, '%', 7, "\x94\x5e\x80\x71\x41\x7a\x64\x5d\x6f\x2e\x5b\x66\x9b\x5a\x3d\xda"); + DO_TEST_N(MY_AES_CBC, 8, '@', 8, "\xb8\x53\x97\xb9\x40\xa6\x98\xaf\x0c\x7b\x9a\xac\xad\x7e\x3c\xe0"); + DO_TEST_P(MY_AES_GCM, 9, '?', 25, "\x5e\x05\xfd\xb2\x8e\x17\x04\x1e\xff\x6d\x71\x81\xcd\x85\x8d\xb5"); + + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/base64-t.c b/unittest/mysys/base64-t.c new file mode 100644 index 00000000..590e285d --- /dev/null +++ b/unittest/mysys/base64-t.c @@ -0,0 +1,99 @@ +/* Copyright (c) 2003, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc. + Use is subject to license terms. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include <tap.h> +#include <string.h> + +#define BASE64_LOOP_COUNT 500 +#define BASE64_ROWS 4 /* Number of ok(..) */ + +int +main(int argc __attribute__((unused)),char *argv[]) +{ + int i, cmp; + size_t j, k, l, dst_len, needed_length; + MY_INIT(argv[0]); + + plan(BASE64_LOOP_COUNT * BASE64_ROWS); + + for (i= 0; i < BASE64_LOOP_COUNT; i++) + { + /* Create source data */ + const size_t src_len= rand() % 1000 + 1; + + char * src= (char *) malloc(src_len); + char * s= src; + char * str; + char * dst; + + for (j= 0; j<src_len; j++) + { + char c= rand(); + *s++= c; + } + + /* Encode */ + needed_length= my_base64_needed_encoded_length((int)src_len); + str= (char *) malloc(needed_length); + for (k= 0; k < needed_length; k++) + str[k]= 0xff; /* Fill memory to check correct NUL termination */ + ok(my_base64_encode(src, src_len, str) == 0, + "my_base64_encode: size %d", i); + ok(needed_length == strlen(str) + 1, + "my_base64_needed_encoded_length: size %d", i); + + /* Decode */ + dst= (char *) malloc(my_base64_needed_decoded_length((int)strlen(str))); + dst_len= my_base64_decode(str, strlen(str), dst, NULL, 0); + ok(dst_len == src_len, "Comparing lengths"); + + cmp= memcmp(src, dst, src_len); + ok(cmp == 0, "Comparing encode-decode result"); + if (cmp != 0) + { + /* FIXME: This only prints last value of the compared strings */ + char buf[80]; + diag(" --------- src --------- --------- dst ---------"); + for (k= 0; k<src_len; k+=8) + { + sprintf(buf, "%.4x ", (uint) k); + for (l=0; l<8 && k+l<src_len; l++) + { + unsigned char c= src[k+l]; + sprintf(buf, "%.2x ", (unsigned)c); + } + + sprintf(buf, " "); + + for (l=0; l<8 && k+l<dst_len; l++) + { + unsigned char c= dst[k+l]; + sprintf(buf, "%.2x ", (unsigned)c); + } + diag("%s", buf); + } + diag("src length: %.8x, dst length: %.8x\n", + (uint) src_len, (uint) dst_len); + } + free(dst); + free(str); + free(src); + } + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/bitmap-t.c b/unittest/mysys/bitmap-t.c new file mode 100644 index 00000000..e8f41b32 --- /dev/null +++ b/unittest/mysys/bitmap-t.c @@ -0,0 +1,541 @@ +/* + Copyright (c) 2006, 2012, Oracle and/or its affiliates. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA + + This test was copied from the unit test inside the + mysys/my_bitmap.c file and adapted by Mats Kindahl to use the mytap + library. +*/ + +#include <my_global.h> +#include <my_sys.h> +#include <my_bitmap.h> +#include <tap.h> +#include <m_string.h> + +#define MAX_TESTED_BITMAP_SIZE 1024 + +uint get_rand_bit(uint bitsize) +{ + return (rand() % bitsize); +} + +my_bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize) +{ + uint i, test_bit; + uint no_loops= bitsize > 128 ? 128 : bitsize; + for (i=0; i < no_loops; i++) + { + test_bit= get_rand_bit(bitsize); + bitmap_set_bit(map, test_bit); + if (!bitmap_is_set(map, test_bit)) + goto error1; + bitmap_clear_bit(map, test_bit); + if (bitmap_is_set(map, test_bit)) + goto error2; + } + return FALSE; +error1: + printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize); + return TRUE; +error2: + printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize); + return TRUE; +} + +my_bool test_flip_bit(MY_BITMAP *map, uint bitsize) +{ + uint i, test_bit; + uint no_loops= bitsize > 128 ? 128 : bitsize; + for (i=0; i < no_loops; i++) + { + test_bit= get_rand_bit(bitsize); + bitmap_flip_bit(map, test_bit); + if (!bitmap_is_set(map, test_bit)) + goto error1; + bitmap_flip_bit(map, test_bit); + if (bitmap_is_set(map, test_bit)) + goto error2; + } + return FALSE; +error1: + printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize); + return TRUE; +error2: + printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize); + return TRUE; +} + + +my_bool test_get_all_bits(MY_BITMAP *map, uint bitsize) +{ + uint i; + bitmap_set_all(map); + if (!bitmap_is_set_all(map)) + goto error1; + if (!bitmap_is_prefix(map, bitsize)) + goto error5; + bitmap_clear_all(map); + if (!bitmap_is_clear_all(map)) + goto error2; + if (!bitmap_is_prefix(map, 0)) + goto error6; + for (i=0; i<bitsize;i++) + bitmap_set_bit(map, i); + if (!bitmap_is_set_all(map)) + goto error3; + for (i=0; i<bitsize;i++) + bitmap_clear_bit(map, i); + if (!bitmap_is_clear_all(map)) + goto error4; + return FALSE; +error1: + diag("Error in set_all, bitsize = %u", bitsize); + return TRUE; +error2: + diag("Error in clear_all, bitsize = %u", bitsize); + return TRUE; +error3: + diag("Error in bitmap_is_set_all, bitsize = %u", bitsize); + return TRUE; +error4: + diag("Error in bitmap_is_clear_all, bitsize = %u", bitsize); + return TRUE; +error5: + diag("Error in set_all through set_prefix, bitsize = %u", bitsize); + return TRUE; +error6: + diag("Error in clear_all through set_prefix, bitsize = %u", bitsize); + return TRUE; +} + +my_bool test_compare_operators(MY_BITMAP *map, uint bitsize) +{ + uint i, j, test_bit1, test_bit2, test_bit3,test_bit4; + uint no_loops= bitsize > 128 ? 128 : bitsize; + MY_BITMAP map2_obj, map3_obj; + MY_BITMAP *map2= &map2_obj, *map3= &map3_obj; + my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE]; + my_bitmap_map map3buf[MAX_TESTED_BITMAP_SIZE]; + my_bitmap_init(&map2_obj, map2buf, bitsize, FALSE); + my_bitmap_init(&map3_obj, map3buf, bitsize, FALSE); + bitmap_clear_all(map2); + bitmap_clear_all(map3); + for (i=0; i < no_loops; i++) + { + test_bit1=get_rand_bit(bitsize); + bitmap_set_prefix(map, test_bit1); + test_bit2=get_rand_bit(bitsize); + bitmap_set_prefix(map2, test_bit2); + bitmap_intersect(map, map2); + test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1; + bitmap_set_prefix(map3, test_bit3); + if (!bitmap_cmp(map, map3)) + goto error1; + bitmap_clear_all(map); + bitmap_clear_all(map2); + bitmap_clear_all(map3); + test_bit1=get_rand_bit(bitsize); + test_bit2=get_rand_bit(bitsize); + test_bit3=get_rand_bit(bitsize); + bitmap_set_prefix(map, test_bit1); + bitmap_set_prefix(map2, test_bit2); + test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1; + bitmap_set_prefix(map3, test_bit3); + bitmap_union(map, map2); + if (!bitmap_cmp(map, map3)) + goto error2; + bitmap_clear_all(map); + bitmap_clear_all(map2); + bitmap_clear_all(map3); + test_bit1=get_rand_bit(bitsize); + test_bit2=get_rand_bit(bitsize); + test_bit3=get_rand_bit(bitsize); + bitmap_set_prefix(map, test_bit1); + bitmap_set_prefix(map2, test_bit2); + bitmap_xor(map, map2); + test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1; + test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1; + bitmap_set_prefix(map3, test_bit3); + for (j=0; j < test_bit4; j++) + bitmap_clear_bit(map3, j); + if (!bitmap_cmp(map, map3)) + goto error3; + bitmap_clear_all(map); + bitmap_clear_all(map2); + bitmap_clear_all(map3); + test_bit1=get_rand_bit(bitsize); + test_bit2=get_rand_bit(bitsize); + test_bit3=get_rand_bit(bitsize); + bitmap_set_prefix(map, test_bit1); + bitmap_set_prefix(map2, test_bit2); + bitmap_subtract(map, map2); + if (test_bit2 < test_bit1) + { + bitmap_set_prefix(map3, test_bit1); + for (j=0; j < test_bit2; j++) + bitmap_clear_bit(map3, j); + } + if (!bitmap_cmp(map, map3)) + goto error4; + bitmap_clear_all(map); + bitmap_clear_all(map2); + bitmap_clear_all(map3); + test_bit1=get_rand_bit(bitsize); + bitmap_set_prefix(map, test_bit1); + bitmap_invert(map); + bitmap_set_all(map3); + for (j=0; j < test_bit1; j++) + bitmap_clear_bit(map3, j); + if (!bitmap_cmp(map, map3)) + goto error5; + bitmap_clear_all(map); + bitmap_clear_all(map3); + } + return FALSE; +error1: + diag("intersect error bitsize=%u,size1=%u,size2=%u", bitsize, + test_bit1,test_bit2); + return TRUE; +error2: + diag("union error bitsize=%u,size1=%u,size2=%u", bitsize, + test_bit1,test_bit2); + return TRUE; +error3: + diag("xor error bitsize=%u,size1=%u,size2=%u", bitsize, + test_bit1,test_bit2); + return TRUE; +error4: + diag("subtract error bitsize=%u,size1=%u,size2=%u", bitsize, + test_bit1,test_bit2); + return TRUE; +error5: + diag("invert error bitsize=%u,size=%u", bitsize, + test_bit1); + return TRUE; +} + +my_bool test_count_bits_set(MY_BITMAP *map, uint bitsize) +{ + uint i, bit_count=0, test_bit; + uint no_loops= bitsize > 128 ? 128 : bitsize; + for (i=0; i < no_loops; i++) + { + test_bit=get_rand_bit(bitsize); + if (!bitmap_is_set(map, test_bit)) + { + bitmap_set_bit(map, test_bit); + bit_count++; + } + } + if (bit_count==0 && bitsize > 0) + goto error1; + if (bitmap_bits_set(map) != bit_count) + goto error2; + return FALSE; +error1: + diag("No bits set bitsize = %u", bitsize); + return TRUE; +error2: + diag("Wrong count of bits set, bitsize = %u", bitsize); + return TRUE; +} + +my_bool test_get_first_bit(MY_BITMAP *map, uint bitsize) +{ + uint i, test_bit= 0; + uint no_loops= bitsize > 128 ? 128 : bitsize; + + bitmap_set_all(map); + for (i=0; i < bitsize; i++) + bitmap_clear_bit(map, i); + if (bitmap_get_first_set(map) != MY_BIT_NONE) + goto error1; + bitmap_clear_all(map); + for (i=0; i < bitsize; i++) + bitmap_set_bit(map, i); + if (bitmap_get_first(map) != MY_BIT_NONE) + goto error2; + bitmap_clear_all(map); + + for (i=0; i < no_loops; i++) + { + test_bit=get_rand_bit(bitsize); + bitmap_set_bit(map, test_bit); + if (bitmap_get_first_set(map) != test_bit) + goto error1; + bitmap_set_all(map); + bitmap_clear_bit(map, test_bit); + if (bitmap_get_first(map) != test_bit) + goto error2; + bitmap_clear_all(map); + } + return FALSE; +error1: + diag("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit); + return TRUE; +error2: + diag("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit); + return TRUE; +} + +my_bool test_get_next_bit(MY_BITMAP *map, uint bitsize) +{ + uint i, j, test_bit; + uint no_loops= bitsize > 128 ? 128 : bitsize; + for (i=0; i < no_loops; i++) + { + 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; + bitmap_clear_all(map); + } + return FALSE; +error1: + diag("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit); + return TRUE; +} + +my_bool test_prefix(MY_BITMAP *map, uint bitsize) +{ + uint i, j, test_bit; + uint no_loops= bitsize > 128 ? 128 : bitsize; + for (i=0; i < no_loops; i++) + { + test_bit=get_rand_bit(bitsize); + bitmap_set_prefix(map, test_bit); + if (!bitmap_is_prefix(map, test_bit)) + goto error1; + bitmap_clear_all(map); + for (j=0; j < test_bit; j++) + bitmap_set_bit(map, j); + if (!bitmap_is_prefix(map, test_bit)) + goto error2; + bitmap_set_all(map); + for (j=bitsize - 1; ~(j-test_bit); j--) + bitmap_clear_bit(map, j); + if (!bitmap_is_prefix(map, test_bit)) + goto error3; + bitmap_clear_all(map); + } + for (i=0; i < bitsize; i++) + { + if (bitmap_is_prefix(map, i + 1)) + goto error4; + bitmap_set_bit(map, i); + if (!bitmap_is_prefix(map, i + 1)) + goto error5; + test_bit=get_rand_bit(bitsize); + bitmap_set_bit(map, test_bit); + if (test_bit <= i && !bitmap_is_prefix(map, i + 1)) + goto error5; + else if (test_bit > i) + { + if (bitmap_is_prefix(map, i + 1)) + goto error4; + bitmap_clear_bit(map, test_bit); + } + } + return FALSE; +error1: + diag("prefix1 error bitsize = %u, prefix_size = %u", bitsize,test_bit); + return TRUE; +error2: + diag("prefix2 error bitsize = %u, prefix_size = %u", bitsize,test_bit); + return TRUE; +error3: + diag("prefix3 error bitsize = %u, prefix_size = %u", bitsize,test_bit); + return TRUE; +error4: + diag("prefix4 error bitsize = %u, i = %u", bitsize,i); + return TRUE; +error5: + diag("prefix5 error bitsize = %u, i = %u", bitsize,i); + return TRUE; +} + +my_bool test_compare(MY_BITMAP *map, uint bitsize) +{ + MY_BITMAP map2; + uint32 map2buf[MAX_TESTED_BITMAP_SIZE]; + uint i, test_bit; + uint no_loops= bitsize > 128 ? 128 : bitsize; + if (my_bitmap_init(&map2, map2buf, bitsize, FALSE)) + { + diag("init error for bitsize %d", bitsize); + return TRUE; + } + /* Test all 4 possible combinations of set/unset bits. */ + for (i=0; i < no_loops; i++) + { + test_bit=get_rand_bit(bitsize); + bitmap_clear_bit(map, test_bit); + bitmap_clear_bit(&map2, test_bit); + if (!bitmap_is_subset(map, &map2)) + goto error_is_subset; + bitmap_set_bit(map, test_bit); + if (bitmap_is_subset(map, &map2)) + goto error_is_subset; + bitmap_set_bit(&map2, test_bit); + if (!bitmap_is_subset(map, &map2)) + goto error_is_subset; + bitmap_clear_bit(map, test_bit); + if (!bitmap_is_subset(map, &map2)) + goto error_is_subset; + /* Note that test_bit is not cleared i map2. */ + } + bitmap_clear_all(map); + bitmap_clear_all(&map2); + /* Test all 4 possible combinations of set/unset bits. */ + for (i=0; i < no_loops; i++) + { + test_bit=get_rand_bit(bitsize); + if (bitmap_is_overlapping(map, &map2)) + goto error_is_overlapping; + bitmap_set_bit(map, test_bit); + if (bitmap_is_overlapping(map, &map2)) + goto error_is_overlapping; + bitmap_set_bit(&map2, test_bit); + if (!bitmap_is_overlapping(map, &map2)) + goto error_is_overlapping; + bitmap_clear_bit(map, test_bit); + if (bitmap_is_overlapping(map, &map2)) + goto error_is_overlapping; + bitmap_clear_bit(&map2, test_bit); + /* Note that test_bit is not cleared i map2. */ + } + return FALSE; +error_is_subset: + diag("is_subset error bitsize = %u", bitsize); + return TRUE; +error_is_overlapping: + diag("is_overlapping error bitsize = %u", bitsize); + return TRUE; +} + +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]; + uint i, test_bit1, test_bit2, test_bit3; + if (my_bitmap_init(&map2, map2buf, bitsize2, FALSE)) + { + diag("init error for bitsize %d", bitsize2); + return TRUE; + } + test_bit1= get_rand_bit(bitsize); + test_bit2= get_rand_bit(bitsize); + bitmap_set_bit(map, test_bit1); + bitmap_set_bit(map, test_bit2); + test_bit3= get_rand_bit(bitsize2); + bitmap_set_bit(&map2, test_bit3); + if (test_bit2 < bitsize2) + bitmap_set_bit(&map2, test_bit2); + + bitmap_intersect(map, &map2); + if (test_bit2 < bitsize2) + { + if (!bitmap_is_set(map, test_bit2)) + goto error; + bitmap_clear_bit(map, test_bit2); + } + if (test_bit1 == test_bit3) + { + if (!bitmap_is_set(map, test_bit1)) + goto error; + bitmap_clear_bit(map, test_bit1); + } + if (!bitmap_is_clear_all(map)) + goto error; + + bitmap_set_all(map); + bitmap_set_all(&map2); + for (i=0; i < bitsize2; i++) + bitmap_clear_bit(&map2, i); + bitmap_intersect(map, &map2); + if (!bitmap_is_clear_all(map)) + goto error; + return FALSE; +error: + diag("intersect error bitsize = %u, bit1 = %u, bit2 = %u, bit3 = %u", + bitsize, test_bit1, test_bit2, test_bit3); + return TRUE; +} + +my_bool do_test(uint bitsize) +{ + MY_BITMAP map; + my_bitmap_map buf[MAX_TESTED_BITMAP_SIZE]; + if (my_bitmap_init(&map, buf, bitsize, FALSE)) + { + diag("init error for bitsize %d", bitsize); + goto error; + } + if (test_set_get_clear_bit(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_flip_bit(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_get_all_bits(&map, bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_compare_operators(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_count_bits_set(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_get_first_bit(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_get_next_bit(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_prefix(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_compare(&map,bitsize)) + goto error; + bitmap_clear_all(&map); + if (test_intersect(&map,bitsize)) + goto error; + return FALSE; +error: + return TRUE; +} + +int main(int argc __attribute__((unused)),char *argv[]) +{ + int i; + int const min_size = 1; + int const max_size = MAX_TESTED_BITMAP_SIZE; + MY_INIT(argv[0]); + + plan((max_size - min_size)/7+1); + + /* + It's ok to do steps in 7, as i module 64 will go trough all values 1..63. + Any errors in the code should manifest as we are working with integers + of size 16, 32, or 64 bits... + */ + for (i= min_size; i < max_size; i+=7) + ok(do_test(i) == 0, "bitmap size %d", i); + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/byte_order-t.c b/unittest/mysys/byte_order-t.c new file mode 100644 index 00000000..d4481a41 --- /dev/null +++ b/unittest/mysys/byte_order-t.c @@ -0,0 +1,102 @@ +/* Copyright (c) 2019, MariaDB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ + +/** + @file + + Unit tests for serialization and deserialization functions +*/ + +#include "tap.h" + +#include "my_byteorder.h" +#include "myisampack.h" +#include "m_string.h" + +void test_byte_order() +{ + MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE) + uchar buf[CPU_LEVEL1_DCACHE_LINESIZE * 2]; + + uchar *aligned= buf; + uchar *not_aligned= buf + CPU_LEVEL1_DCACHE_LINESIZE - 1; + +#define TEST(STORE_NAME, LOAD_NAME, TYPE, VALUE, BYTES) \ + { \ + TYPE value= VALUE; \ + uchar bytes[]= BYTES; \ + STORE_NAME(aligned, value); \ + ok(!memcmp(aligned, bytes, sizeof(bytes)), "aligned\t\t" #STORE_NAME); \ + ok(LOAD_NAME(aligned) == value, "aligned\t\t" #LOAD_NAME); \ + STORE_NAME(not_aligned, value); \ + ok(!memcmp(not_aligned, bytes, sizeof(bytes)), \ + "not aligned\t" #STORE_NAME); \ + ok(LOAD_NAME(not_aligned) == value, "not aligned\t" #LOAD_NAME); \ + } + +#define ARRAY_2(A, B) {A, B} +#define ARRAY_3(A, B, C) {A, B, C} +#define ARRAY_4(A, B, C, D) {A, B, C, D} +#define ARRAY_5(A, B, C, D, E) {A, B, C, D, E} +#define ARRAY_6(A, B, C, D, E, F) {A, B, C, D, E, F} +#define ARRAY_7(A, B, C, D, E, F, G) {A, B, C, D, E, F, G} +#define ARRAY_8(A, B, C, D, E, F, G, H) {A, B, C, D, E, F, G, H} + + TEST(int2store, sint2korr, int16, 0x0201, ARRAY_2(1, 2)); + TEST(int3store, sint3korr, int32, 0xffffffff, ARRAY_3(0xff, 0xff, 0xff)); + TEST(int3store, sint3korr, int32, 0x030201, ARRAY_3(1, 2, 3)); + TEST(int4store, sint4korr, int32, 0xffffffff, + ARRAY_4(0xff, 0xff, 0xff, 0xff)); + TEST(int4store, sint4korr, int32, 0x04030201, ARRAY_4(1, 2, 3, 4)); + TEST(int8store, sint8korr, longlong, 0x0807060504030201, + ARRAY_8(1, 2, 3, 4, 5, 6, 7, 8)); + TEST(int8store, sint8korr, longlong, 0xffffffffffffffff, + ARRAY_8(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff)); + + TEST(int2store, uint2korr, uint16, 0x0201, ARRAY_2(1, 2)); + TEST(int3store, uint3korr, uint32, 0x030201, ARRAY_3(1, 2, 3)); + TEST(int4store, uint4korr, uint32, 0x04030201, ARRAY_4(1, 2, 3, 4)); + TEST(int5store, uint5korr, ulonglong, 0x0504030201, ARRAY_5(1, 2, 3, 4, 5)); + TEST(int6store, uint6korr, ulonglong, 0x060504030201, + ARRAY_6(1, 2, 3, 4, 5, 6)); + TEST(int8store, uint8korr, ulonglong, 0x0807060504030201, + ARRAY_8(1, 2, 3, 4, 5, 6, 7, 8)); + + TEST(mi_int5store, mi_uint5korr, ulonglong, 0x0504030201, + ARRAY_5(5, 4, 3, 2, 1)); + TEST(mi_int6store, mi_uint6korr, ulonglong, 0x060504030201, + ARRAY_6(6, 5, 4, 3, 2, 1)); + TEST(mi_int7store, mi_uint7korr, ulonglong, 0x07060504030201, + ARRAY_7(7, 6, 5, 4, 3, 2, 1)); + TEST(mi_int8store, mi_uint8korr, ulonglong, 0x0807060504030201, + ARRAY_8(8, 7, 6, 5, 4, 3, 2, 1)); + +#undef ARRAY_8 +#undef ARRAY_7 +#undef ARRAY_6 +#undef ARRAY_5 +#undef ARRAY_4 +#undef ARRAY_3 +#undef ARRAY_2 + +#undef TEST +} + +int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) +{ + plan(68); + test_byte_order(); + return exit_status(); +} diff --git a/unittest/mysys/crc32-t.c b/unittest/mysys/crc32-t.c new file mode 100644 index 00000000..9834d217 --- /dev/null +++ b/unittest/mysys/crc32-t.c @@ -0,0 +1,69 @@ +/* Copyright (c) MariaDB 2020 + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include <my_crypt.h> +#include <tap.h> +#include <string.h> +#include <ctype.h> +#include <zlib.h> + +/* + Check that optimized crc32 (ieee, or ethernet polynomical) returns the same + result as zlib (not so well optimized, yet, but trustworthy) +*/ +#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) + + +#define LONG_STR "1234567890234568900212345678901231213123321212123123123123123"\ + "............................................................................." \ + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \ + "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" \ + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" + +int main(int argc __attribute__((unused)),char *argv[]) +{ + MY_INIT(argv[0]); + plan(14); + 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); + 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); + ok(0 == my_crc32c(0, NULL, 0), "crc32c data = NULL, length = 0"); + + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/dynstring-t.c b/unittest/mysys/dynstring-t.c new file mode 100644 index 00000000..194d435d --- /dev/null +++ b/unittest/mysys/dynstring-t.c @@ -0,0 +1,74 @@ +/* Copyright (c) 2016, MariaDB + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <m_string.h> +#include <my_sys.h> +#include <tap.h> + +DYNAMIC_STRING str1; + +static void check(const char *res) +{ + ok(strcmp(str1.str, res) == 0, "strcmp: %s", str1.str); + str1.length= 0; +} + +int main(void) +{ + plan(23); + + IF_WIN(skip_all("Test of POSIX shell escaping rules, not for CMD.EXE\n"), ); + + ok(init_dynamic_string(&str1, NULL, 0, 32) == 0, "init"); + + ok(dynstr_append_os_quoted(&str1, "test1", NULL) == 0, "append"); + check("'test1'"); + + ok(dynstr_append_os_quoted(&str1, "con", "cat", NULL) == 0, "append"); + check("'concat'"); + + ok(dynstr_append_os_quoted(&str1, "", NULL) == 0, "append"); + check("''"); + + ok(dynstr_append_os_quoted(&str1, "space inside", NULL) == 0, "append"); + check("'space inside'"); + + ok(dynstr_append_os_quoted(&str1, "single'quote", NULL) == 0, "append"); + check("'single'\"'\"'quote'"); + + ok(dynstr_append_os_quoted(&str1, "many'single'quotes", NULL) == 0, "append"); + check("'many'\"'\"'single'\"'\"'quotes'"); + + ok(dynstr_append_os_quoted(&str1, "'single quoted'", NULL) == 0, "append"); + check("''\"'\"'single quoted'\"'\"''"); + + ok(dynstr_append_os_quoted(&str1, "double\"quote", NULL) == 0, "append"); + check("'double\"quote'"); + + ok(dynstr_append_os_quoted(&str1, "mixed\"single'and\"double'quotes", NULL) == 0, "append"); + check("'mixed\"single'\"'\"'and\"double'\"'\"'quotes'"); + + ok(dynstr_append_os_quoted(&str1, "back\\space", NULL) == 0, "append"); + check("'back\\space'"); + + ok(dynstr_append_os_quoted(&str1, "backspace\\'and\\\"quote", NULL) == 0, "append"); + check("'backspace\\'\"'\"'and\\\"quote'"); + + dynstr_free(&str1); + + return exit_status(); +} + diff --git a/unittest/mysys/lf-t.c b/unittest/mysys/lf-t.c new file mode 100644 index 00000000..088ecadd --- /dev/null +++ b/unittest/mysys/lf-t.c @@ -0,0 +1,199 @@ +/* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +/** + @file + + Unit tests for lock-free algorithms of mysys +*/ + +#include "thr_template.c" + +#include <lf.h> + +int32 inserts= 0, N; +LF_ALLOCATOR lf_allocator; +LF_HASH lf_hash; + +int with_my_thread_init=0; + +/* + pin allocator - alloc and release an element in a loop +*/ +pthread_handler_t test_lf_pinbox(void *arg) +{ + int m= *(int *)arg; + LF_PINS *pins; + + if (with_my_thread_init) + my_thread_init(); + + pins= lf_pinbox_get_pins(&lf_allocator.pinbox); + + for (; m ; m--) + { + lf_pinbox_put_pins(pins); + pins= lf_pinbox_get_pins(&lf_allocator.pinbox); + } + lf_pinbox_put_pins(pins); + + if (with_my_thread_init) + my_thread_end(); + + return 0; +} + +/* + thread local data area, allocated using lf_alloc. + union is required to enforce the minimum required element size (sizeof(ptr)) +*/ +typedef union { + int32 data; + void *not_used; +} TLA; + +pthread_handler_t test_lf_alloc(void *arg) +{ + int m= (*(int *)arg)/2; + int32 x,y= 0; + LF_PINS *pins; + + if (with_my_thread_init) + my_thread_init(); + + pins= lf_alloc_get_pins(&lf_allocator); + + for (x= ((int)(intptr)(&m)); m ; m--) + { + TLA *node1, *node2; + x= (x*m+0x87654321) & INT_MAX32; + node1= (TLA *)lf_alloc_new(pins); + node1->data= x; + y+= node1->data; + node1->data= 0; + node2= (TLA *)lf_alloc_new(pins); + node2->data= x; + y-= node2->data; + node2->data= 0; + lf_alloc_free(pins, node1); + lf_alloc_free(pins, node2); + } + lf_alloc_put_pins(pins); + pthread_mutex_lock(&mutex); + bad+= y; + + if (--N == 0) + { + diag("%d mallocs, %d pins in stack", + lf_allocator.mallocs, lf_allocator.pinbox.pins_in_array); +#ifdef MY_LF_EXTRA_DEBUG + bad|= lf_allocator.mallocs - lf_alloc_pool_count(&lf_allocator); +#endif + } + pthread_mutex_unlock(&mutex); + + if (with_my_thread_init) + my_thread_end(); + return 0; +} + +my_bool do_sum(void *num, void *acc) +{ + *(int *)acc += *(int *)num; + return 0; +} + + +#define N_TLH 1000 +pthread_handler_t test_lf_hash(void *arg) +{ + int m= (*(int *)arg)/(2*N_TLH); + int32 x,y,z,sum= 0, ins= 0, scans= 0; + LF_PINS *pins; + + if (with_my_thread_init) + my_thread_init(); + + pins= lf_hash_get_pins(&lf_hash); + + for (x= ((int)(intptr)(&m)); m ; m--) + { + int i; + y= x; + for (i= 0; i < N_TLH; i++) + { + x= (x*(m+i)+0x87654321) & INT_MAX32; + z= (x<0) ? -x : x; + if (lf_hash_insert(&lf_hash, pins, &z)) + { + sum+= z; + ins++; + } + else + { + int unused= 0; + lf_hash_iterate(&lf_hash, pins, do_sum, &unused); + scans++; + } + } + for (i= 0; i < N_TLH; i++) + { + y= (y*(m+i)+0x87654321) & INT_MAX32; + z= (y<0) ? -y : y; + if (lf_hash_delete(&lf_hash, pins, (uchar *)&z, sizeof(z))) + sum-= z; + } + } + lf_hash_put_pins(pins); + pthread_mutex_lock(&mutex); + bad+= sum; + inserts+= ins; + + if (--N == 0) + { + diag("%d mallocs, %d pins in stack, %d hash size, %d inserts, %d scans", + lf_hash.alloc.mallocs, lf_hash.alloc.pinbox.pins_in_array, + lf_hash.size, inserts, scans); + bad|= lf_hash.count; + } + pthread_mutex_unlock(&mutex); + if (with_my_thread_init) + my_thread_end(); + return 0; +} + + +void do_tests() +{ + plan(6); + + lf_alloc_init(&lf_allocator, sizeof(TLA), offsetof(TLA, not_used)); + lf_hash_init(&lf_hash, sizeof(int), LF_HASH_UNIQUE, 0, sizeof(int), 0, + &my_charset_bin); + + with_my_thread_init= 1; + test_concurrently("lf_pinbox (with my_thread_init)", test_lf_pinbox, N= THREADS, CYCLES); + test_concurrently("lf_alloc (with my_thread_init)", test_lf_alloc, N= THREADS, CYCLES); + test_concurrently("lf_hash (with my_thread_init)", test_lf_hash, N= THREADS, CYCLES); + + with_my_thread_init= 0; + test_concurrently("lf_pinbox (without my_thread_init)", test_lf_pinbox, N= THREADS, CYCLES); + test_concurrently("lf_alloc (without my_thread_init)", test_lf_alloc, N= THREADS, CYCLES); + test_concurrently("lf_hash (without my_thread_init)", test_lf_hash, N= THREADS, CYCLES); + + lf_hash_destroy(&lf_hash); + lf_alloc_destroy(&lf_allocator); +} + diff --git a/unittest/mysys/ma_dyncol-t.c b/unittest/mysys/ma_dyncol-t.c new file mode 100644 index 00000000..d76f1b49 --- /dev/null +++ b/unittest/mysys/ma_dyncol-t.c @@ -0,0 +1,881 @@ +/* Copyright (c) 2011, Monty Program Ab + Copyright (c) 2011, Oleksandr Byelkin + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +#include <my_global.h> +#include <my_sys.h> +#include <m_string.h> +#include <ma_dyncol.h> +#include <tap.h> + +void test_value_single_null() +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_NULL; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= (res.type == DYN_COL_NULL); +err: + ok(rc, "%s", "NULL"); + /* cleanup */ + mariadb_dyncol_free(&str); +} + +void test_value_single_uint(ulonglong num, const char *name) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_UINT; + val.x.ulong_value= num; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= (res.type == DYN_COL_UINT) && (res.x.ulong_value == num); + num= res.x.ulong_value; +err: + ok(rc, "%s - %llu", name, num); + /* cleanup */ + mariadb_dyncol_free(&str); +} + +void test_value_single_sint(longlong num, const char *name) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_INT; + val.x.long_value= num; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= (res.type == DYN_COL_INT) && (res.x.long_value == num); + num= res.x.ulong_value; +err: + ok(rc, "%s - %lld", name, num); + /* cleanup */ + mariadb_dyncol_free(&str); +} + + +void test_value_single_double(double num, const char *name) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_DOUBLE; + val.x.double_value= num; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= (res.type == DYN_COL_DOUBLE) && (res.x.double_value == num); + num= res.x.double_value; +err: + ok(rc, "%s - %lf", name, num); + /* cleanup */ + mariadb_dyncol_free(&str); +} + +void test_value_single_decimal(const char *num) +{ + char *end= (((char*)num) + strlen(num)); + char buff[80]; + int rc= FALSE; + int length= 80; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + + /* init values */ + mariadb_dyncol_prepare_decimal(&val); // special procedure for decimal!!! + if (string2decimal(num, &val.x.decimal.value, &end) != E_DEC_OK) + goto err; + mariadb_dyncol_value_init(&res); + + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= ((res.type == DYN_COL_DECIMAL) && + (decimal_cmp(&res.x.decimal.value, &val.x.decimal.value) == 0)); + decimal2string(&res.x.decimal.value, buff, &length, 0, 0, ' '); +err: + ok(rc, "%s - %s", num, buff); + /* cleanup */ + mariadb_dyncol_free(&str); +} + +static CHARSET_INFO *charset_list[]= +{ +#ifdef HAVE_CHARSET_big5 + &my_charset_big5_chinese_ci, + &my_charset_big5_bin, +#endif +#ifdef HAVE_CHARSET_euckr + &my_charset_euckr_korean_ci, + &my_charset_euckr_bin, +#endif +#ifdef HAVE_CHARSET_gb2312 + &my_charset_gb2312_chinese_ci, + &my_charset_gb2312_bin, +#endif +#ifdef HAVE_CHARSET_gbk + &my_charset_gbk_chinese_ci, + &my_charset_gbk_bin, +#endif +#ifdef HAVE_CHARSET_latin1 + &my_charset_latin1, + &my_charset_latin1_bin, +#endif +#ifdef HAVE_CHARSET_sjis + &my_charset_sjis_japanese_ci, + &my_charset_sjis_bin, +#endif +#ifdef HAVE_CHARSET_tis620 + &my_charset_tis620_thai_ci, + &my_charset_tis620_bin, +#endif +#ifdef HAVE_CHARSET_ujis + &my_charset_ujis_japanese_ci, + &my_charset_ujis_bin, +#endif +#ifdef HAVE_CHARSET_utf8mb3 + &my_charset_utf8mb3_general_ci, +#ifdef HAVE_UCA_COLLATIONS + &my_charset_utf8mb3_unicode_ci, +#endif + &my_charset_utf8mb3_bin, +#endif +}; + + +void test_value_single_string(const char *string, size_t len, + CHARSET_INFO *cs) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + + /* init values */ + val.type= DYN_COL_STRING; + val.x.string.value.str= (char*)string; + val.x.string.value.length= len; + val.x.string.charset= cs; + mariadb_dyncol_value_init(&res); + + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= ((res.type == DYN_COL_STRING) && + (res.x.string.value.length == len) && + (memcmp(res.x.string.value.str, string, len) == 0) && + (res.x.string.charset->number == cs->number)); +err: + ok(rc, "'%s' - '%s' %u %u-%s", string, + res.x.string.value.str, (uint)res.x.string.value.length, + (uint)res.x.string.charset->number, res.x.string.charset->name); + /* cleanup */ + val.x.string.value.str= NULL; // we did not allocated it + mariadb_dyncol_free(&str); +} + +void test_value_single_date(uint year, uint month, uint day, const char *name) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_DATE; + val.x.time_value.time_type= MYSQL_TIMESTAMP_DATE; + val.x.time_value.year= year; + val.x.time_value.month= month; + val.x.time_value.day= day; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= ((res.type == DYN_COL_DATE) && + (res.x.time_value.time_type == MYSQL_TIMESTAMP_DATE) && + (res.x.time_value.year == year) && + (res.x.time_value.month == month) && + (res.x.time_value.day == day)); +err: + ok(rc, "%s - %04u-%02u-%02u", name, year, month, day); + /* cleanup */ + mariadb_dyncol_free(&str); +} + +void test_value_single_time(uint neg, uint hour, uint minute, uint second, + uint mic, const char *name) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_TIME; + val.x.time_value.time_type= MYSQL_TIMESTAMP_TIME; + val.x.time_value.neg= neg; + val.x.time_value.hour= hour; + val.x.time_value.minute= minute; + val.x.time_value.second= second; + val.x.time_value.second_part= mic; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= ((res.type == DYN_COL_TIME) && + (res.x.time_value.time_type == MYSQL_TIMESTAMP_TIME) && + (res.x.time_value.neg == (int)neg) && + (res.x.time_value.hour == hour) && + (res.x.time_value.minute == minute) && + (res.x.time_value.second == second) && + (res.x.time_value.second_part == mic)); +err: + ok(rc, "%s - %c%02u:%02u:%02u.%06u", name, (neg ? '-' : '+'), + hour, minute, second, mic); + /* cleanup */ + mariadb_dyncol_free(&str); +} + + +void test_value_single_datetime(uint neg, uint year, uint month, uint day, + uint hour, uint minute, uint second, + uint mic, const char *name) +{ + int rc= FALSE; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + /* init values */ + val.type= DYN_COL_DATETIME; + val.x.time_value.time_type= MYSQL_TIMESTAMP_DATETIME; + val.x.time_value.neg= neg; + val.x.time_value.year= year; + val.x.time_value.month= month; + val.x.time_value.day= day; + val.x.time_value.hour= hour; + val.x.time_value.minute= minute; + val.x.time_value.second= second; + val.x.time_value.second_part= mic; + mariadb_dyncol_value_init(&res); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 1, ids, &val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + if (mariadb_dyncol_get_num(&str, 1, &res)) + goto err; + rc= ((res.type == DYN_COL_DATETIME) && + (res.x.time_value.time_type == MYSQL_TIMESTAMP_DATETIME) && + (res.x.time_value.neg == (int)neg) && + (res.x.time_value.year == year) && + (res.x.time_value.month == month) && + (res.x.time_value.day == day) && + (res.x.time_value.hour == hour) && + (res.x.time_value.minute == minute) && + (res.x.time_value.second == second) && + (res.x.time_value.second_part == mic)); +err: + ok(rc, "%s - %c %04u-%02u-%02u %02u:%02u:%02u.%06u", name, (neg ? '-' : '+'), + year, month, day, hour, minute, second, mic); + /* cleanup */ + mariadb_dyncol_free(&str); +} + + +void test_value_multi(ulonglong num0, + longlong num1, + double num2, + const char *num3, + const char *string4, size_t len4, CHARSET_INFO *cs4, + uint year5, uint month5, uint day5, + uint neg6, uint hour6, uint minute6, + uint second6, uint mic6, + uint neg7, uint year7, uint month7, uint day7, + uint hour7, uint minute7, uint second7, + uint mic7, + uint *column_numbers, + const char *name) +{ + char *end3= (((char*)num3) + strlen(num3)); + int rc= FALSE; + uint i; + DYNAMIC_COLUMN_VALUE val[9], res[9]; + DYNAMIC_COLUMN str; + /* init values */ + val[0].type= DYN_COL_UINT; + val[0].x.ulong_value= num0; + val[1].type= DYN_COL_INT; + val[1].x.long_value= num1; + val[2].type= DYN_COL_DOUBLE; + val[2].x.double_value= num2; + mariadb_dyncol_prepare_decimal(val + 3); // special procedure for decimal!!! + if (string2decimal(num3, &val[3].x.decimal.value, &end3) != E_DEC_OK) + goto err; + val[4].type= DYN_COL_STRING; + val[4].x.string.value.str= (char*)string4; + val[4].x.string.value.length= len4; + val[4].x.string.charset= cs4; + val[5].type= DYN_COL_DATE; + val[5].x.time_value.time_type= MYSQL_TIMESTAMP_DATE; + val[5].x.time_value.year= year5; + val[5].x.time_value.month= month5; + val[5].x.time_value.day= day5; + val[6].type= DYN_COL_TIME; + val[6].x.time_value.time_type= MYSQL_TIMESTAMP_TIME; + val[6].x.time_value.neg= neg6; + val[6].x.time_value.hour= hour6; + val[6].x.time_value.minute= minute6; + val[6].x.time_value.second= second6; + val[6].x.time_value.second_part= mic6; + val[7].type= DYN_COL_DATETIME; + val[7].x.time_value.time_type= MYSQL_TIMESTAMP_DATETIME; + val[7].x.time_value.neg= neg7; + val[7].x.time_value.year= year7; + val[7].x.time_value.month= month7; + val[7].x.time_value.day= day7; + val[7].x.time_value.hour= hour7; + val[7].x.time_value.minute= minute7; + val[7].x.time_value.second= second7; + val[7].x.time_value.second_part= mic7; + val[8].type= DYN_COL_NULL; + for (i= 0; i < 9; i++) + mariadb_dyncol_value_init(res + i); + /* create column */ + if (mariadb_dyncol_create_many_num(&str, 9, column_numbers, val, 1)) + goto err; + dynstr_append(&str, "\1"); str.length--; //check for overflow + /* read column */ + for (i= 0; i < 9; i++) + if (mariadb_dyncol_get_num(&str, column_numbers[i], res + i)) + goto err; + rc= ((res[0].type == DYN_COL_UINT) && + (res[0].x.ulong_value == num0) && + (res[1].type == DYN_COL_INT) && + (res[1].x.long_value == num1) && + (res[2].type == DYN_COL_DOUBLE) && + (res[2].x.double_value == num2) && + (res[3].type == DYN_COL_DECIMAL) && + (decimal_cmp(&res[3].x.decimal.value, &val[3].x.decimal.value) == 0) && + (res[4].type == DYN_COL_STRING) && + (res[4].x.string.value.length == len4) && + (memcmp(res[4].x.string.value.str, string4, len4) == 0) && + (res[4].x.string.charset->number == cs4->number) && + (res[5].type == DYN_COL_DATE) && + (res[5].x.time_value.time_type == MYSQL_TIMESTAMP_DATE) && + (res[5].x.time_value.year == year5) && + (res[5].x.time_value.month == month5) && + (res[5].x.time_value.day == day5) && + (res[6].type == DYN_COL_TIME) && + (res[6].x.time_value.time_type == MYSQL_TIMESTAMP_TIME) && + (res[6].x.time_value.neg == (int)neg6) && + (res[6].x.time_value.hour == hour6) && + (res[6].x.time_value.minute == minute6) && + (res[6].x.time_value.second == second6) && + (res[6].x.time_value.second_part == mic6) && + (res[7].type == DYN_COL_DATETIME) && + (res[7].x.time_value.time_type == MYSQL_TIMESTAMP_DATETIME) && + (res[7].x.time_value.neg == (int)neg7) && + (res[7].x.time_value.year == year7) && + (res[7].x.time_value.month == month7) && + (res[7].x.time_value.day == day7) && + (res[7].x.time_value.hour == hour7) && + (res[7].x.time_value.minute == minute7) && + (res[7].x.time_value.second == second7) && + (res[7].x.time_value.second_part == mic7) && + (res[8].type == DYN_COL_NULL)); +err: + ok(rc, "%s", name); + /* cleanup */ + val[4].x.string.value.str= NULL; // we did not allocated it + mariadb_dyncol_free(&str); +} + + +void test_value_multi_same_num() +{ + int rc= FALSE; + uint i; + DYNAMIC_COLUMN_VALUE val[5]; + uint column_numbers[]= {3,4,5,3,6}; // same column numbers + DYNAMIC_COLUMN str; + /* init values */ + for (i= 0; i < 5; i++) + val[i].type= DYN_COL_NULL; + /* create column */ + if (!mariadb_dyncol_create_many_num(&str, 5, column_numbers, val, 1)) + goto err; + rc= TRUE; +err: + ok(rc, "%s", "same column numbers check"); + /* cleanup */ + mariadb_dyncol_free(&str); +} + + +void test_update_multi(uint *column_numbers, uint *column_values, + my_bool *null_values, int only_add, int all) +{ + int rc= FALSE; + int i, j; + DYNAMIC_COLUMN str; + DYNAMIC_COLUMN_VALUE val; + + val.type= DYN_COL_UINT; + val.x.ulong_value= column_values[0]; + if (mariadb_dyncol_create_many_num(&str, 1, column_numbers, &val, 1)) + goto err; + for (i= 1; i < all; i++) + { + val.type= (null_values[i] ? DYN_COL_NULL : DYN_COL_UINT); + val.x.ulong_value= column_values[i]; + if (mariadb_dyncol_update_many_num(&str, 1, column_numbers +i, &val)) + goto err; + + /* check value(s) */ + for (j= i; j >= (i < only_add ? 0 : i); j--) + { + if (mariadb_dyncol_get_num(&str, column_numbers[j], &val)) + goto err; + if (null_values[j]) + { + if (val.type != DYN_COL_NULL || + mariadb_dyncol_exists_num(&str, column_numbers[j]) == ER_DYNCOL_YES) + goto err; + } + else + { + if (val.type != DYN_COL_UINT || + val.x.ulong_value != column_values[j] || + mariadb_dyncol_exists_num(&str, column_numbers[j]) == ER_DYNCOL_NO) + goto err; + } + } + if (i < only_add) + { + uint elements, *num; + if (mariadb_dyncol_list_num(&str, &elements, &num)) + { + my_free(num); + goto err; + } + /* cross check arrays */ + if ((int)elements != i + 1) + { + my_free(num); + goto err; + } + for(j= 0; j < i + 1; j++) + { + int k; + for(k= 0; + k < i + 1 && column_numbers[j] != num[k]; + k++); + if (k >= i + 1) + { + my_free(num); + goto err; + } + for(k= 0; + k < i + 1 && column_numbers[k] != num[j]; + k++); + if (k >= i + 1) + { + my_free(num); + goto err; + } + } + my_free(num); + } + } + + rc= TRUE; +err: + ok(rc, "%s", "add/delete/update"); + /* cleanup */ + mariadb_dyncol_free(&str); +} + +void test_empty_string() +{ + DYNAMIC_COLUMN_VALUE val, res; + DYNAMIC_COLUMN str; + uint *array_of_uint; + uint number_of_uint; + int rc; + uint ids[1]= {1}; + DYNAMIC_COLUMN_VALUE vals[1]; + /* empty string */ + bzero(&str, sizeof(str)); + + rc= mariadb_dyncol_get_num(&str, 1, &res); + ok( (rc == ER_DYNCOL_OK) && (res.type == DYN_COL_NULL), "%s", "empty get"); + + vals[0].type= DYN_COL_NULL; + rc= mariadb_dyncol_update_many_num(&str, 1, ids, vals); + ok( (rc == ER_DYNCOL_OK) && (str.str == 0), "%s", "empty delete"); + + rc= mariadb_dyncol_exists_num(&str, 1); + ok( (rc == ER_DYNCOL_NO), "%s", "empty exists"); + + rc= mariadb_dyncol_list_num(&str, &number_of_uint, &array_of_uint); + ok( (rc == ER_DYNCOL_OK) && (number_of_uint == 0) && (str.str == 0), + "%s", "empty list"); + + val.type= DYN_COL_UINT; + val.x.ulong_value= 1212; + rc= mariadb_dyncol_update_many_num(&str, 1, ids, &val); + if (rc == ER_DYNCOL_OK) + rc= mariadb_dyncol_get_num(&str, 1, &res); + ok( (rc == ER_DYNCOL_OK) && (str.str != 0) && + (res.type == DYN_COL_UINT) && (res.x.ulong_value == val.x.ulong_value), + "%s", "empty update"); + mariadb_dyncol_free(&str); +} + +static void test_mdev_4994() +{ + DYNAMIC_COLUMN dyncol; + LEX_STRING key= {0,0}; + DYNAMIC_COLUMN_VALUE val; + int rc; + + val.type= DYN_COL_NULL; + + mariadb_dyncol_init(&dyncol); + rc= mariadb_dyncol_create_many_named(&dyncol, 1, &key, &val, 0); /* crash */ + ok( (rc == ER_DYNCOL_OK), "%s", "test_mdev_4994"); + mariadb_dyncol_free(&dyncol); +} + +static void test_mdev_4995() +{ + DYNAMIC_COLUMN dyncol; + uint column_count= 5; + int rc; + + mariadb_dyncol_init(&dyncol); + rc= mariadb_dyncol_column_count(&dyncol,&column_count); + + ok( (rc == ER_DYNCOL_OK), "%s", "test_mdev_4995"); +} + +void test_update_many(uint *column_numbers, uint *column_values, + uint column_count, + uint *update_numbers, uint *update_values, + my_bool *update_nulls, uint update_count, + uint *result_numbers, uint *result_values, + uint result_count) +{ + int rc= FALSE; + uint i; + DYNAMIC_COLUMN str1; + DYNAMIC_COLUMN str2; + DYNAMIC_COLUMN_VALUE *val, *upd, *res; + + val= (DYNAMIC_COLUMN_VALUE *)malloc(sizeof(DYNAMIC_COLUMN_VALUE) * + column_count); + upd= (DYNAMIC_COLUMN_VALUE *)malloc(sizeof(DYNAMIC_COLUMN_VALUE) * + update_count); + res= (DYNAMIC_COLUMN_VALUE *)malloc(sizeof(DYNAMIC_COLUMN_VALUE) * + result_count); + + + for (i= 0; i < column_count; i++) + { + val[i].type= DYN_COL_UINT; + val[i].x.ulong_value= column_values[i]; + } + for (i= 0; i < update_count; i++) + { + if (update_nulls[i]) + upd[i].type= DYN_COL_NULL; + else + { + upd[i].type= DYN_COL_UINT; + upd[i].x.ulong_value= update_values[i]; + } + } + for (i= 0; i < result_count; i++) + { + res[i].type= DYN_COL_UINT; + res[i].x.ulong_value= result_values[i]; + } + if (mariadb_dyncol_create_many_num(&str1, column_count, column_numbers, val, 1)) + goto err; + if (mariadb_dyncol_update_many_num(&str1, update_count, update_numbers, upd)) + goto err; + if (mariadb_dyncol_create_many_num(&str2, result_count, result_numbers, res, 1)) + goto err; + if (str1.length == str2.length && + memcmp(str1.str, str2.str, str1.length) ==0) + rc= TRUE; + +err: + ok(rc, "%s", "update_many"); + /* cleanup */ + free(val); + free(upd); + free(res); + mariadb_dyncol_free(&str1); + mariadb_dyncol_free(&str2); +} + +static void test_mdev_9773() +{ + int rc; + uint i; + uint num_keys[5]= {1,2,3,4,5}; + char const *strval[]= {"Val1", "Val2", "Val3", "Val4", "Val5"}; + DYNAMIC_COLUMN_VALUE vals[5]; + DYNAMIC_COLUMN dynstr; + uint unpack_columns= 0; + MYSQL_LEX_STRING *unpack_keys= 0; + DYNAMIC_COLUMN_VALUE *unpack_vals= 0; + + for (i = 0; i < 5; i++) + { + vals[i].type= DYN_COL_STRING; + vals[i].x.string.value.str= (char *)strval[i]; + vals[i].x.string.value.length= strlen(strval[i]); + vals[i].x.string.charset= &my_charset_latin1; + } + + mariadb_dyncol_init(&dynstr); + + /* create numeric */ + rc= mariadb_dyncol_create_many_num(&dynstr, 5, num_keys, vals, 1); + + if (rc == ER_DYNCOL_OK) + rc= mariadb_dyncol_unpack(&dynstr, &unpack_columns, &unpack_keys, + &unpack_vals); + ok (rc == ER_DYNCOL_OK && unpack_columns == 5, "5 fields unpacked"); + for (i = 0; i < unpack_columns; i++) + { + ok(memcmp(unpack_vals[i].x.string.value.str, + vals[i].x.string.value.str, vals[i].x.string.value.length) == 0, + "unpack %u", i); + } + + my_free(unpack_keys); + my_free(unpack_vals); + mariadb_dyncol_free(&dynstr); +} + +int main(int argc __attribute__((unused)), char **argv) +{ + uint i; + char *big_string= (char *)malloc(1024*1024); + + MY_INIT(argv[0]); + plan(68); + + if (!big_string) + exit(1); + for (i= 0; i < 1024*1024; i++) + big_string[i]= ('0' + (i % 10)); + test_value_single_null(); + test_value_single_uint(0, "0"); + test_value_single_uint(0xffffffffffffffffULL, "0xffffffffffffffff"); + test_value_single_uint(0xaaaaaaaaaaaaaaaaULL, "0xaaaaaaaaaaaaaaaa"); + test_value_single_uint(0x5555555555555555ULL, "0x5555555555555555"); + test_value_single_uint(27652, "27652"); + test_value_single_sint(0, "0"); + test_value_single_sint(1, "1"); + test_value_single_sint(-1, "-1"); + test_value_single_sint(0x7fffffffffffffffLL, "0x7fffffffffffffff"); + test_value_single_sint(0xaaaaaaaaaaaaaaaaLL, "0xaaaaaaaaaaaaaaaa"); + test_value_single_sint(0x5555555555555555LL, "0x5555555555555555"); + test_value_single_sint(0x8000000000000000LL, "0x8000000000000000"); + test_value_single_double(0.0, "0.0"); + test_value_single_double(1.0, "1.0"); + test_value_single_double(-1.0, "-1.0"); + test_value_single_double(1.0e100, "1.0e100"); + test_value_single_double(1.0e-100, "1.0e-100"); + test_value_single_double(9999999999999999999999999999999999999.0, + "9999999999999999999999999999999999999.0"); + test_value_single_double(-9999999999999999999999999999999999999.0, + "-9999999999999999999999999999999999999.0"); + test_value_single_decimal("0"); + test_value_single_decimal("1"); + test_value_single_decimal("-1"); + test_value_single_decimal("9999999999999999999999999999999"); + test_value_single_decimal("-9999999999999999999999999999999"); + test_value_single_decimal("0.9999999999999999999999999999999"); + test_value_single_decimal("-0.9999999999999999999999999999999"); + test_value_single_string("", 0, charset_list[0]); + test_value_single_string("", 1, charset_list[0]); + test_value_single_string("1234567890", 11, charset_list[0]); + test_value_single_string("nulls\0\0\0\0\0", 10, charset_list[0]); + sprintf(big_string, "%x", 0x7a); + test_value_single_string(big_string, 0x7a, charset_list[0]); + sprintf(big_string, "%x", 0x80); + test_value_single_string(big_string, 0x80, charset_list[0]); + sprintf(big_string, "%x", 0x7ffa); + test_value_single_string(big_string, 0x7ffa, charset_list[0]); + sprintf(big_string, "%x", 0x8000); + test_value_single_string(big_string, 0x8000, charset_list[0]); + sprintf(big_string, "%x", 1024*1024); + test_value_single_string(big_string, 1024*1024, charset_list[0]); + test_value_single_date(0, 0, 0, "zero date"); + test_value_single_date(9999, 12, 31, "max date"); + test_value_single_date(2011, 3, 26, "some date"); + test_value_single_time(0, 0, 0, 0, 0, "zero time"); + test_value_single_time(1, 23, 59, 59, 999999, "min time"); + test_value_single_time(0, 23, 59, 59, 999999, "max time"); + test_value_single_time(0, 21, 36, 20, 28, "some time"); + test_value_single_datetime(0, 0, 0, 0, 0, 0, 0, 0, "zero datetime"); + test_value_single_datetime(1, 9999, 12, 31, 23, 59, 59, 999999, + "min datetime"); + test_value_single_datetime(0, 9999, 12, 31, 23, 59, 59, 999999, + "max datetime"); + test_value_single_datetime(0, 2011, 3, 26, 21, 53, 12, 3445, + "some datetime"); + { + uint column_numbers[]= {100,1,2,3,4,5,6,7,8}; + test_value_multi(0, 0, 0.0, "0", + "", 0, charset_list[0], + 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + column_numbers, + "zero data"); + } + { + uint column_numbers[]= {10,1,12,37,4,57,6,76,87}; + test_value_multi(0xffffffffffffffffULL, 0x7fffffffffffffffLL, + 99999999.999e120, "9999999999999999999999999999999", + big_string, 1024*1024, charset_list[0], + 9999, 12, 31, + 0, 23, 59, 59, 999999, + 0, 9999, 12, 31, 23, 59, 59, 999999, + column_numbers, + "much data"); + } + free(big_string); + { + uint column_numbers[]= {101,12,122,37,24,572,16,726,77}; + test_value_multi(37878, -3344, + 2873.3874, "92743.238984789898", + "string", 6, charset_list[0], + 2011, 3, 26, + 1, 23, 23, 20, 333, + 0, 2011, 3, 26, 23, 23, 53, 334, + column_numbers, + "zero data"); + } + test_value_multi_same_num(); + { + uint column_numbers[]= {1,2,3,4,5,6,7,2, 3, 4}; + uint column_values[]= {1,2,3,4,5,6,7,0,30,40}; + my_bool null_values[]= {0,0,0,0,0,0,0,1, 0, 0}; + + test_update_multi(column_numbers, column_values, null_values, 7, 10); + } + { + uint column_numbers[]= {4,3,2,1, 1,2,3,4}; + uint column_values[]= {4,3,2,1, 0,0,0,0}; + my_bool null_values[]= {0,0,0,0, 1,1,1,1}; + + test_update_multi(column_numbers, column_values, null_values, 4, 8); + } + { + uint column_numbers[]= {4,3,2,1, 4,3,2,1}; + uint column_values[]= {4,3,2,1, 0,0,0,0}; + my_bool null_values[]= {0,0,0,0, 1,1,1,1}; + + test_update_multi(column_numbers, column_values, null_values, 4, 8); + } + test_empty_string(); + { + uint column_numbers[]= {1, 2, 3}; + uint column_values[]= {1, 2, 3}; + uint update_numbers[]= {4, 3, 2, 1}; + uint update_values[]= {40,30, 0,10}; + my_bool update_nulls[]={0, 0, 1, 0}; + uint result_numbers[]= {1, 3, 4}; + uint result_values[]= {10,30,40}; + test_update_many(column_numbers, column_values, 3, + update_numbers, update_values, update_nulls, 4, + result_numbers, result_values, 3); + } + test_mdev_4994(); + test_mdev_4995(); + test_mdev_9773(); + + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/my_atomic-t.c b/unittest/mysys/my_atomic-t.c new file mode 100644 index 00000000..1995a8e8 --- /dev/null +++ b/unittest/mysys/my_atomic-t.c @@ -0,0 +1,131 @@ +/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include "thr_template.c" +#include "my_atomic.h" + +volatile uint32 b32; +volatile int32 c32; + +/* add and sub a random number in a loop. Must get 0 at the end */ +pthread_handler_t test_atomic_add(void *arg) +{ + int m= (*(int *)arg)/2; + int32 x; + for (x= ((int)(intptr)(&m)); m ; m--) + { + x= (x*m+0x87654321) & INT_MAX32; + my_atomic_add32(&bad, x); + my_atomic_add32(&bad, -x); + } + return 0; +} + +volatile int64 a64; +/* add and sub a random number in a loop. Must get 0 at the end */ +pthread_handler_t test_atomic_add64(void *arg) +{ + int m= (*(int *)arg)/2; + int64 x; + for (x= ((int64)(intptr)(&m)); m ; m--) + { + x= (x*m+0xfdecba987654321LL) & INT_MAX64; + my_atomic_add64(&a64, x); + my_atomic_add64(&a64, -x); + } + return 0; +} + + +/* + 1. generate thread number 0..N-1 from b32 + 2. add it to bad + 3. swap thread numbers in c32 + 4. (optionally) one more swap to avoid 0 as a result + 5. subtract result from bad + must get 0 in bad at the end +*/ +pthread_handler_t test_atomic_fas(void *arg) +{ + int m= *(int *)arg; + int32 x; + + x= my_atomic_add32(&b32, 1); + + my_atomic_add32(&bad, x); + + for (; m ; m--) + x= my_atomic_fas32(&c32, x); + + if (!x) + x= my_atomic_fas32(&c32, x); + + my_atomic_add32(&bad, -x); + + return 0; +} + +/* + same as test_atomic_add, but my_atomic_add32 is emulated with + my_atomic_cas32 - notice that the slowdown is proportional to the + number of CPUs +*/ +pthread_handler_t test_atomic_cas(void *arg) +{ + int m= (*(int *)arg)/2, ok= 0; + int32 x, y; + for (x= ((int)(intptr)(&m)); m ; m--) + { + y= my_atomic_load32(&bad); + x= (x*m+0x87654321) & INT_MAX32; + do { + ok= my_atomic_cas32((int32*) &bad, &y, y+x); + } while (!ok) ; + do { + ok= my_atomic_cas32((int32*) &bad, &y, y-x); + } while (!ok) ; + } + return 0; +} + + +void do_tests() +{ + plan(5); + + b32= c32= 0; + test_concurrently("my_atomic_add32", test_atomic_add, THREADS, CYCLES); + b32= c32= 0; + test_concurrently("my_atomic_fas32", test_atomic_fas, THREADS, CYCLES); + b32= c32= 0; + test_concurrently("my_atomic_cas32", test_atomic_cas, THREADS, CYCLES); + + { + /* + If b is not volatile, the wrong assembly code is generated on OSX Lion + as the variable is optimized away as a constant. + See Bug#62533 / Bug#13030056. + Another workaround is to specify architecture explicitly using e.g. + CFLAGS/CXXFLAGS= "-m64". + */ + volatile int64 b=0x1000200030004000LL; + a64=0; + my_atomic_add64(&a64, b); + ok(a64==b, "add64"); + } + a64=0; + test_concurrently("my_atomic_add64", test_atomic_add64, THREADS, CYCLES); + bad= (a64 != 0); +} diff --git a/unittest/mysys/my_delete-t.c b/unittest/mysys/my_delete-t.c new file mode 100644 index 00000000..7bc6cf89 --- /dev/null +++ b/unittest/mysys/my_delete-t.c @@ -0,0 +1,57 @@ +/* Copyright (c) 2011, Monty Program Ab + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include "tap.h" + + +int main(int argc __attribute__((unused)),char *argv[]) +{ + char tmp_dir[MAX_PATH]; + char tmp_filename[MAX_PATH]; + HANDLE h, h2; + + MY_INIT(argv[0]); + + plan(6); + + GetTempPathA(MAX_PATH, tmp_dir); + ok(GetTempFileNameA(tmp_dir, "foo", 0, tmp_filename) != 0, "create temp file"); + ok(my_delete(tmp_filename,MYF(0)) == 0, "Delete closed file"); + + + /* Delete an open file */ + ok(GetTempFileNameA(tmp_dir, "foo", 0, tmp_filename) != 0, "create temp file 2"); + h = CreateFileA(tmp_filename, GENERIC_READ|GENERIC_WRITE, + FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL); + ok (h != INVALID_HANDLE_VALUE || h != 0, "open temp file"); + ok(my_delete(tmp_filename, MYF(0)) == 0, "Delete open file"); + + + /* + Check if it is possible to reuse file name after delete (not all handles + to it are closed. + */ + h2 = CreateFileA(tmp_filename, GENERIC_READ|GENERIC_WRITE, + FILE_SHARE_DELETE, NULL, CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, NULL); + ok(h2 != 0 && h2 != INVALID_HANDLE_VALUE, "Reuse file name"); + CloseHandle(h); + CloseHandle(h2); + + my_end(0); + return exit_status(); +} + diff --git a/unittest/mysys/my_getopt-t.c b/unittest/mysys/my_getopt-t.c new file mode 100644 index 00000000..4b50468a --- /dev/null +++ b/unittest/mysys/my_getopt-t.c @@ -0,0 +1,394 @@ +/* Copyright (c) 2015, MariaDB Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +#include <my_global.h> +#include <my_getopt.h> +#include <mysys_err.h> +#include <stdarg.h> +#include <tap.h> + +ulonglong opt_ull; +ulong opt_ul; +int arg_c, res; +char **arg_v, *arg_s[100]; + +ulong mopts_num; +char *mopts_str; +my_bool mopts_bool; +static struct my_option mopts_options[]= +{ + {"str", 0, + "Something numeric.", + &mopts_str, &mopts_str, 0, GET_STR, + REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, + {"bool", 0, + "Something true or false", + &mopts_bool, &mopts_bool, 0, GET_BOOL, + OPT_ARG, FALSE, 0, 0, 0, 0, 0}, + {"num", 0, + "Something numeric.", + &mopts_num, &mopts_num, 0, GET_ULONG, + REQUIRED_ARG, 1000000L, 1, ULONG_MAX, 0, 2, 0}, + {"ull", 0, "ull", &opt_ull, &opt_ull, + 0, GET_ULL, REQUIRED_ARG, 1, 0, ~0ULL, 0, 0, 0}, + {"ul", 0, "ul", &opt_ul, &opt_ul, + 0, GET_ULONG, REQUIRED_ARG, 1, 0, 0xFFFFFFFF, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} +}; + +my_bool dummy_get_one_option(const struct my_option *opt __attribute__((unused)), + const char *argument __attribute__((unused)), + const char *filename __attribute__((unused))) +{ + return FALSE; +} + +void run(const char *arg, ...) +{ + va_list ap; + va_start(ap, arg); + arg_v= arg_s; + *arg_v++= (char*)"<skipped>"; + while (arg) + { + *arg_v++= (char*)arg; + arg= va_arg(ap, char*); + } + va_end(ap); + arg_c= (int)(arg_v - arg_s); + arg_v= arg_s; + res= handle_options(&arg_c, &arg_v, mopts_options, &dummy_get_one_option); +} + +int mopts1_argc= 4; +const char *mopts1_argv[]= {"mopts1", "--num=123", "--str=str", "--bool"}; +void test_mopts1() +{ + int rc; + char **av= (char **)mopts1_argv; + + rc= handle_options(&mopts1_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts1 call"); + ok( (mopts_num == 122), "%s", "test_mopts1 num"); + ok( (strncmp(mopts_str, "str", 4) == 0), "%s", "test_mopts1 str"); + ok( (mopts_bool == 1), "%s", "test_mopts1 bool"); +} + +int mopts2_argc= 4; +const char *mopts2_argv[]= {"mopts2", "--num=123", "--num=124", "--bool=0"}; +void test_mopts2() +{ + int rc; + char **av= (char **)mopts2_argv; + + rc= handle_options(&mopts2_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts2 call"); + ok( (mopts_num == 124), "%s", "test_mopts2 num"); + ok( (strncmp(mopts_str, "ddd", 4) == 0), "%s", "test_mopts2 str"); + ok( (mopts_bool == 0), "%s", "test_mopts2 bool"); +} + +int mopts3_argc= 4; +const char *mopts3_argv[]= {"mopts3", "--loose-foo", "--loose-loose-foo", "--enable-bool"}; +void test_mopts3() +{ + int rc; + char **av= (char **)mopts3_argv; + + rc= handle_options(&mopts3_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts3 call"); + ok( (mopts_num == 1000000L), "%s", "test_mopts3 num"); + ok( (strncmp(mopts_str, "ddd", 4) == 0), "%s", "test_mopts3 str"); + ok( (mopts_bool == 1), "%s", "test_mopts3 bool"); +} + +int mopts4_argc= 3; +const char *mopts4_argv[]= {"mopts4", "--loose-str=aa", "--skip-bool"}; +void test_mopts4() +{ + int rc; + char **av= (char **)mopts4_argv; + + rc= handle_options(&mopts4_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts4 call"); + ok( (mopts_num == 1000000L), "%s", "test_mopts4 num"); + ok( (strncmp(mopts_str, "aa", 3) == 0), "%s", "test_mopts4 str"); + ok( (mopts_bool == 0), "%s", "test_mopts4 bool"); +} + +int mopts5_argc= 2; +const char *mopts5_argv[]= {"mopts5", "--loose-skip-bool"}; +void test_mopts5() +{ + int rc; + char **av= (char **)mopts5_argv; + + rc= handle_options(&mopts5_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts5 call"); + ok( (mopts_num == 1000000L), "%s", "test_mopts5 num"); + ok( (strncmp(mopts_str, "ddd", 4) == 0), "%s", "test_mopts5 str"); + ok( (mopts_bool == 0), "%s", "test_mopts5 bool"); +} + +int mopts6_argc= 2; +const char *mopts6_argv[]= {"mopts6", "--loose-skip-skip-bool"}; +void test_mopts6() +{ + int rc; + char **av= (char **)mopts6_argv; + + rc= handle_options(&mopts6_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts6 call"); + ok( (mopts_num == 1000000L), "%s", "test_mopts6 num"); + ok( (strncmp(mopts_str, "ddd", 4) == 0), "%s", "test_mopts6 str"); + ok( (mopts_bool == 0), "%s", "test_mopts6 bool"); +} + +int mopts7_argc= 2; +const char *mopts7_argv[]= {"mopts7", "--loose-disable-skip-bool"}; +void test_mopts7() +{ + int rc; + char **av= (char **)mopts7_argv; + + rc= handle_options(&mopts7_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts7 call"); + ok( (mopts_num == 1000000L), "%s", "test_mopts7 num"); + ok( (strncmp(mopts_str, "ddd", 4) == 0), "%s", "test_mopts7 str"); + ok( (mopts_bool == 0), "%s", "test_mopts7 bool"); +} + +int mopts8_argc= 2; +const char *mopts8_argv[]= {"mopts8", "--loose-disable-enable-bool"}; +void test_mopts8() +{ + int rc; + char **av= (char **)mopts8_argv; + + rc= handle_options(&mopts8_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_mopts8 call"); + ok( (mopts_num == 1000000L), "%s", "test_mopts7 num"); + ok( (strncmp(mopts_str, "ddd", 4) == 0), "%s", "test_mopts7 str"); + ok( (mopts_bool == 1), "%s", "test_mopts7 bool"); +} + +int mopts9_argc= 2; +const char *mopts9_argv[]= {"mopts9", "--foo"}; +void test_mopts9() +{ + int rc; + char **av= (char **)mopts9_argv; + + rc= handle_options(&mopts9_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc != 0), "%s", "test_mopts9 call"); +} + +int mopts10_argc= 2; +const char *mopts10_argv[]= {"mopts10", "--skip-foo"}; +void test_mopts10() +{ + int rc; + char **av= (char **)mopts10_argv; + + rc= handle_options(&mopts10_argc, &av, mopts_options, &dummy_get_one_option); + ok( (rc != 0), "%s", "test_mopts10 call"); +} + +ulong auto_num; +static struct my_option auto_options[]= +{ + {"anum", 0, + "Something numeric.", + &auto_num, &auto_num, 0, GET_ULONG | GET_AUTO, + REQUIRED_ARG, 1000000L, 1, ULONG_MAX, 0, 1, 0}, + {"num", 0, + "Something numeric.", + &mopts_num, &mopts_num, 0, GET_ULONG, + REQUIRED_ARG, 1000000L, 1, ULONG_MAX, 0, 1, 0}, + { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} +}; + + + +my_bool auto_get_one_option(const struct my_option *opt, + const char *argument, + const char *filename __attribute__((unused))) +{ + if (argument == autoset_my_option) + { + *((ulong*)opt->value)= 111; + } + return FALSE; +} + +int auto2_argc= 3; +const char *auto2_argv[]= {"auto2", "--num=123", "--autoset-num"}; +void test_auto2() +{ + int rc; + char **av= (char **)auto2_argv; + + rc= handle_options(&auto2_argc, &av, auto_options, &auto_get_one_option); + ok( (rc == EXIT_ARGUMENT_INVALID), "%s", "test_auto2 call"); +} + +int auto3_argc= 3; +const char *auto3_argv[]= {"auto3", "--anum=123", "--autoset-anum"}; +void test_auto3() +{ + int rc; + char **av= (char **)auto3_argv; + + rc= handle_options(&auto3_argc, &av, auto_options, &auto_get_one_option); + ok( (rc == 0), "%s", "test_auto3 call"); + ok( (mopts_num == 1000000L), "%s", "test_auto3 num"); + ok( (auto_num == 111), "%s", "test_auto3 anum"); +} + +int auto4_argc= 3; +const char *auto4_argv[]= {"auto4", "--loose-autoset-num", "--loose-autoset-anum"}; +void test_auto4() +{ + int rc; + char **av= (char **)auto4_argv; + + rc= handle_options(&auto4_argc, &av, auto_options, &auto_get_one_option); + ok( (rc == 0), "%s", "test_auto4 call"); + ok( (mopts_num == 1000000L), "%s", "test_auto4 num"); + ok( (auto_num == 111), "%s", "test_auto4 anum"); +} + +int auto5_argc= 3; +const char *auto5_argv[]= {"auto5", "--autoset-loose-num", "--autoset-loose-anum"}; +void test_auto5() +{ + int rc; + char **av= (char **)auto5_argv; + + rc= handle_options(&auto5_argc, &av, auto_options, &auto_get_one_option); + ok( (rc == 0), "%s", "test_auto5 call"); + ok( (mopts_num == 1000000L), "%s", "test_auto5 num"); + ok( (auto_num == 111), "%s", "test_auto5 anum"); +} + +int auto6_argc= 3; +const char *auto6_argv[]= {"auto6", "--autoset-anum", "--anum=123"}; +void test_auto6() +{ + int rc; + char **av= (char **)auto6_argv; + + rc= handle_options(&auto6_argc, &av, auto_options, &auto_get_one_option); + ok( (rc == 0), "%s", "test_auto6 call"); + ok( (mopts_num == 1000000L), "%s", "test_auto6 num"); + ok( (auto_num == 123), "%s", "test_auto6 anum"); +} + + +ulong max_num= ULONG_MAX; +static struct my_option max_options[]= +{ + {"num", 0, + "Something numeric.", + &mopts_num, &max_num, 0, GET_ULONG, + REQUIRED_ARG, 1000000L, 1, 1000001L, 0, 1, 0}, + { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} +}; + +int max1_argc= 3; +const char *max1_argv[]= {"max1", "--num=100", "--num=200"}; +void test_max1() +{ + int rc; + char **av= (char **)max1_argv; + + rc= handle_options(&max1_argc, &av, max_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_max1 call"); + ok( (mopts_num == 200), "%s", "test_max1 num"); + ok( (max_num == 1000001L), "%s", "test_max1 max_num"); +} +int max2_argc= 3; +const char *max2_argv[]= {"max2", "--maximum-num=100", "--num=200"}; +void test_max2() +{ + int rc; + char **av= (char **)max2_argv; + + rc= handle_options(&max2_argc, &av, max_options, &dummy_get_one_option); + ok( (rc == 0), "%s", "test_max2 call"); + ok( (mopts_num == 200), "%s", "test_max2 num"); + ok( (max_num == 100), "%s", "test_max2 max_num"); +} + +int main(int argc __attribute__((unused)), char **argv) +{ + MY_INIT(argv[0]); + plan(4*8 + 1*4 + 3*4 + 3*2 + 2); + + /* gcc 4.1.2 doesn't want it in the initializer, we have to do it run-time */ + mopts_options[0].def_value= (intptr)"ddd"; + + test_mopts1(); + test_mopts2(); + test_mopts3(); + test_mopts4(); + test_mopts5(); + test_mopts6(); + test_mopts7(); + test_mopts8(); + + test_mopts9(); + test_mopts10(); + test_auto2(); + + test_auto3(); + test_auto4(); + test_auto5(); + test_auto6(); + + test_max1(); + test_max2(); + + run("--ull=100", NULL); + ok(res==0 && arg_c==0 && opt_ull==100, + "res:%d, argc:%d, opt_ull:%llu", res, arg_c, opt_ull); + + /* + negative numbers are wrapped. this is kinda questionable, + we might want to fix it eventually. but it'd be a change in behavior, + users might've got used to "-1" meaning "max possible value" + */ + run("--ull=-100", NULL); + ok(res==0 && arg_c==0 && opt_ull==18446744073709551516ULL, + "res:%d, argc:%d, opt_ull:%llu", res, arg_c, opt_ull); + run("--ul=-100", NULL); + ok(res==0 && arg_c==0 && opt_ul==4294967295UL, + "res:%d, argc:%d, opt_ul:%lu", res, arg_c, opt_ul); + + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/my_malloc-t.c b/unittest/mysys/my_malloc-t.c new file mode 100644 index 00000000..0ee6b2b9 --- /dev/null +++ b/unittest/mysys/my_malloc-t.c @@ -0,0 +1,44 @@ +/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include "tap.h" + +int main(int argc __attribute__((unused)),char *argv[]) +{ + void *p; + MY_INIT(argv[0]); + + plan(4); + + p= my_malloc(PSI_NOT_INSTRUMENTED, 0, MYF(0)); + ok(p != NULL, "Zero-sized block allocation."); + + p= my_realloc(PSI_NOT_INSTRUMENTED, p, 32, MYF(0)); + ok(p != NULL, "Reallocated zero-sized block."); + + p= my_realloc(PSI_NOT_INSTRUMENTED, p, 16, MYF(0)); + ok(p != NULL, "Trimmed block."); + + my_free(p); + p= NULL; + + ok((my_free(p), 1), "Free NULL pointer."); + + my_end(0); + return exit_status(); +} + diff --git a/unittest/mysys/my_rdtsc-t.c b/unittest/mysys/my_rdtsc-t.c new file mode 100644 index 00000000..81005e9e --- /dev/null +++ b/unittest/mysys/my_rdtsc-t.c @@ -0,0 +1,230 @@ +/* Copyright (c) 2008 MySQL AB, 2009 Sun Microsystems, Inc. + Use is subject to license terms. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +/* + rdtsc3 -- multi-platform timer code + pgulutzan@mysql.com, 2005-08-29 + modified 2008-11-02 + + When you run rdtsc3, it will print the contents of + "my_timer_info". The display indicates + what timer routine is best for a given platform. + + For example, this is the display on production.mysql.com, + a 2.8GHz Xeon with Linux 2.6.17, gcc 3.3.3: + + cycles nanoseconds microseconds milliseconds ticks +------------- ------------- ------------- ------------- ------------- + 1 11 13 18 17 + 2815019607 1000000000 1000000 1049 102 + 1 1000 1 1 1 + 88 4116 3888 4092 2044 + + The first line shows routines, e.g. 1 = MY_TIMER_ROUTINE_ASM_X86. + The second line shows frequencies, e.g. 2815019607 is nearly 2.8GHz. + The third line shows resolutions, e.g. 1000 = very poor resolution. + The fourth line shows overheads, e.g. ticks takes 2044 cycles. +*/ + +#include "my_global.h" +#include "my_rdtsc.h" +#include "tap.h" + +#define LOOP_COUNT 100 + +MY_TIMER_INFO myt; + +void test_init() +{ + my_timer_init(&myt); + + diag("----- Routine ---------------"); + diag("myt.cycles.routine : %13llu", myt.cycles.routine); + diag("myt.nanoseconds.routine : %13llu", myt.nanoseconds.routine); + diag("myt.microseconds.routine : %13llu", myt.microseconds.routine); + diag("myt.milliseconds.routine : %13llu", myt.milliseconds.routine); + diag("myt.ticks.routine : %13llu", myt.ticks.routine); + + diag("----- Frequency -------------"); + diag("myt.cycles.frequency : %13llu", myt.cycles.frequency); + diag("myt.nanoseconds.frequency : %13llu", myt.nanoseconds.frequency); + diag("myt.microseconds.frequency : %13llu", myt.microseconds.frequency); + diag("myt.milliseconds.frequency : %13llu", myt.milliseconds.frequency); + diag("myt.ticks.frequency : %13llu", myt.ticks.frequency); + + diag("----- Resolution ------------"); + diag("myt.cycles.resolution : %13llu", myt.cycles.resolution); + diag("myt.nanoseconds.resolution : %13llu", myt.nanoseconds.resolution); + diag("myt.microseconds.resolution : %13llu", myt.microseconds.resolution); + diag("myt.milliseconds.resolution : %13llu", myt.milliseconds.resolution); + diag("myt.ticks.resolution : %13llu", myt.ticks.resolution); + + diag("----- Overhead --------------"); + diag("myt.cycles.overhead : %13llu", myt.cycles.overhead); + diag("myt.nanoseconds.overhead : %13llu", myt.nanoseconds.overhead); + diag("myt.microseconds.overhead : %13llu", myt.microseconds.overhead); + diag("myt.milliseconds.overhead : %13llu", myt.milliseconds.overhead); + diag("myt.ticks.overhead : %13llu", myt.ticks.overhead); + + ok(1, "my_timer_init() did not crash"); +} + +void test_cycle() +{ + ulonglong t1= my_timer_cycles(); + ulonglong t2; + int i; + int backward= 0; + int nonzero= 0; + + for (i=0 ; i < LOOP_COUNT ; i++) + { + t2= my_timer_cycles(); + if (t1 >= t2) + backward++; + if (t2 != 0) + nonzero++; + t1= t2; + } + + /* Expect at most 1 backward, the cycle value can overflow */ + ok((backward <= 1), "The cycle timer is strictly increasing"); + + if (myt.cycles.routine != 0) + ok((nonzero != 0), "The cycle timer is implemented"); + else + ok((nonzero == 0), "The cycle timer is not implemented and returns 0"); +} + +void test_nanosecond() +{ + ulonglong t1= my_timer_nanoseconds(); + ulonglong t2; + int i; + int backward= 0; + int nonzero= 0; + + for (i=0 ; i < LOOP_COUNT ; i++) + { + t2= my_timer_nanoseconds(); + if (t1 > t2) + backward++; + if (t2 != 0) + nonzero++; + t1= t2; + } + + ok((backward == 0), "The nanosecond timer is increasing"); + + if (myt.nanoseconds.routine != 0) + ok((nonzero != 0), "The nanosecond timer is implemented"); + else + ok((nonzero == 0), "The nanosecond timer is not implemented and returns 0"); +} + +void test_microsecond() +{ + ulonglong t1= my_timer_microseconds(); + ulonglong t2; + int i; + int backward= 0; + int nonzero= 0; + + for (i=0 ; i < LOOP_COUNT ; i++) + { + t2= my_timer_microseconds(); + if (t1 > t2) + backward++; + if (t2 != 0) + nonzero++; + t1= t2; + } + + ok((backward == 0), "The microsecond timer is increasing"); + + if (myt.microseconds.routine != 0) + ok((nonzero != 0), "The microsecond timer is implemented"); + else + ok((nonzero == 0), "The microsecond timer is not implemented and returns 0"); +} + +void test_millisecond() +{ + ulonglong t1= my_timer_milliseconds(); + ulonglong t2; + int i; + int backward= 0; + int nonzero= 0; + + for (i=0 ; i < LOOP_COUNT ; i++) + { + t2= my_timer_milliseconds(); + if (t1 > t2) + backward++; + if (t2 != 0) + nonzero++; + t1= t2; + } + + ok((backward == 0), "The millisecond timer is increasing"); + + if (myt.milliseconds.routine != 0) + ok((nonzero != 0), "The millisecond timer is implemented"); + else + ok((nonzero == 0), "The millisecond timer is not implemented and returns 0"); +} + +void test_tick() +{ + ulonglong t1= my_timer_ticks(); + ulonglong t2; + int i; + int backward= 0; + int nonzero= 0; + + for (i=0 ; i < LOOP_COUNT ; i++) + { + t2= my_timer_ticks(); + if (t1 > t2) + backward++; + if (t2 != 0) + nonzero++; + t1= t2; + } + + ok((backward == 0), "The tick timer is increasing"); + + if (myt.ticks.routine != 0) + ok((nonzero != 0), "The tick timer is implemented"); + else + ok((nonzero == 0), "The tick timer is not implemented and returns 0"); +} + +int main(int argc __attribute__((unused)), + char ** argv __attribute__((unused))) +{ + plan(11); + + test_init(); + test_cycle(); + test_nanosecond(); + test_microsecond(); + test_millisecond(); + test_tick(); + + return 0; +} + diff --git a/unittest/mysys/my_vsnprintf-t.c b/unittest/mysys/my_vsnprintf-t.c new file mode 100644 index 00000000..1a0b4080 --- /dev/null +++ b/unittest/mysys/my_vsnprintf-t.c @@ -0,0 +1,211 @@ +/* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <m_string.h> +#include <tap.h> + +char buf[1024]; /* let's hope that's enough */ + +static void test_w_len(const char *res, size_t buflen, const char *fmt, ...) +{ + va_list args; + size_t len; + va_start(args,fmt); + len= my_vsnprintf(buf, buflen, fmt, args); + va_end(args); + ok(strlen(res) == len && strcmp(buf, res) == 0, "\"%s\"", buf); +} + +static void test1(const char *res, const char *fmt, ...) +{ + va_list args; + size_t len; + va_start(args,fmt); + len= my_vsnprintf(buf, sizeof(buf)-1, fmt, args); + va_end(args); + ok(strlen(res) == len && strcmp(buf, res) == 0, "\"%s\"", buf); +} + +static void test_many(const char **res, const char *fmt, ...) +{ + va_list args; + size_t len; + va_start(args,fmt); + len= my_vsnprintf(buf, sizeof(buf)-1, fmt, args); + va_end(args); + + for (; *res ; res++) + { + if (strlen(*res) == len && strcmp(buf, *res) == 0) + { + ok(1, "\"%s\"", buf); + return; + } + } + ok(0, "\"%s\"", buf); +} + + +int main(void) +{ + plan(47); + + test1("Constant string", + "Constant string"); + + test1("Format specifier s works", + "Format specifier s %s", "works"); + test1("Format specifier b works (mysql extension)", + "Format specifier b %.5b (mysql extension)", "works!!!"); + test1("Format specifier c !", + "Format specifier c %c", '!'); + test1("Format specifier d 1", + "Format specifier d %d", 1); + test1("Format specifier i 1", + "Format specifier i %i", 1); + test1("Format specifier u 2", + "Format specifier u %u", 2); + test1("Format specifier o 375", + "Format specifier o %o", 0375); + test1("Format specifier x a", + "Format specifier x %x", 10); + test1("Format specifier X B", + "Format specifier X %X", 11); + test1("Format specifier p 0x5", + "Format specifier p %p", 5); + test1("Format specifier f 3.141593", + "Format specifier f %f", 3.1415926); + test1("Format specifier g 3.1416", + "Format specifier g %g", 3.1415926); + + test1("Flag '-' is ignored < 1>", + "Flag '-' is ignored <%-4d>", 1); + test1("Flag '0' works <0006>", + "Flag '0' works <%04d>", 6); + + test1("Width is ignored for strings <x> <y>", + "Width is ignored for strings <%04s> <%5s>", "x", "y"); + + test1("Precision works for strings <abcde>", + "Precision works for strings <%.5s>", "abcdef!"); + test1("Precision works for strings <ab...>", + "Precision works for strings <%.5T>", "abcdef!"); + + test1("Flag '`' (backtick) works: `abcd` `op``q` (mysql extension)", + "Flag '`' (backtick) works: %`s %`.4s (mysql extension)", + "abcd", "op`qrst"); + + test1("Flag '`' (backtick) works: `abcd` `op``q...` (mysql extension)", + "Flag '`' (backtick) works: %`T %`.7T (mysql extension)", + "abcd", "op`qrstuuuuuuuuu"); + + test1("Flag '`' (backtick) works: `abcd` `.` (mysql extension)", + "Flag '`' (backtick) works: %`T %`.1T (mysql extension)", + "abcd", "op`qrstuuuuuuuuu"); + + test1("Flag '`' (backtick) works: `abcd` `...` (mysql extension)", + "Flag '`' (backtick) works: %`T %`.3T (mysql extension)", + "abcd", "op`qrstuuuuuuuuu"); + + test1("Flag '`' (backtick) works: `abcd` `op...` (mysql extension)", + "Flag '`' (backtick) works: %`T %`.5T (mysql extension)", + "abcd", "op`qrstuuuuuuuuu"); + + test1("Flag '`' (backtick) works: `abcd` `op``...` (mysql extension)", + "Flag '`' (backtick) works: %`T %`.6T (mysql extension)", + "abcd", "op`qrstuuuuuuuuu"); + + test1("Length modifiers work: 1 * -1 * 2 * 3", + "Length modifiers work: %d * %ld * %lld * %zd", 1, -1L, 2LL, (size_t)3); + + test1("Length modifiers work: 1 * -1 * 2 * 3", + "Length modifiers work: %i * %li * %lli * %zd", 1, -1L, 2LL, (size_t)3); + + test1("long long X: 123456789abcdef0", + "long long X: %llx", 0x123456789abcdef0LL); + + test1("(null) pointer is fine", + "%s pointer is fine", NULL); + + test1("Positional arguments work: on the dark side they are", + "Positional arguments work: %3$s %1$s %2$s", + "they", "are", "on the dark side"); + + test1("Asterisk '*' as a width works: < 4>", + "Asterisk '*' as a width works: <%*d>", 5, 4); + + test1("Asterisk '*' as a precision works: <qwerty>", + "Asterisk '*' as a precision works: <%.*s>", 6, "qwertyuiop"); + + test1("Asterisk '*' as a precision works: <qwe...>", + "Asterisk '*' as a precision works: <%.*T>", 6, "qwertyuiop"); + + test1("Positional arguments for a width: < 4>", + "Positional arguments for a width: <%1$*2$d>", 4, 5); + + test1("Positional arguments for a precision: <qwerty>", + "Positional arguments for a precision: <%1$.*2$s>", "qwertyuiop", 6); + + test1("Positional arguments for a precision: <qwe...>", + "Positional arguments for a precision: <%1$.*2$T>", "qwertyuiop", 6); + + test1("Positional arguments and a width: <0000ab>", + "Positional arguments and a width: <%1$06x>", 0xab); + + test1("Positional arguments octal: <7777>", + "Positional arguments octal: <%1$o>", 07777); + + /* Can't use int arguments, as they may be different size from pointers */ + + test1("Padding and %p <0x12> <0x034> <0x0000ab> < 0xcd>", + "Padding and %%p <%04p> <%05p> <%08p> <%8p>", + (void*) 0x12, (void*) 0x34, (void*) 0xab, (void*) 0xcd); + + test1("F with a width (ignored) and precision: <12.34568>", + "F with a width (ignored) and precision: <%10.5f>", 12.3456789); + test1("G with a width (ignored) and precision: <12.35>", + "G with a width (ignored) and precision: <%10.5g>", 12.3456789); + + { + /* Test that %M works */ + const char *results[]= + { + "Error 1 \"Operation not permitted\"", /* Linux */ + "Error 1 \"Not owner\"", /* Solaris */ + NullS + }; + test_many(results, "Error %M", 1); + } + + test1("M with 0 error code: 0 \"Internal error/check (Not system error)\"", + "M with 0 error code: %M", 0); + + test1("M with positional: 0 \"Internal error/check (Not system error)\"", + "M with positional: %1$M", 0); + + test1("M with width: 0 \"Internal error...", + "M with width: %.20M", 0); + test1("M with width positional: 0 \"Internal error...", + "M with width positional: %2$.*1$M", 20, 0); + + test_w_len("M small buf: 0 \"..", + 19, "M small buf: %M", 0); + test_w_len("M small buf positional: 0 \"..", + 30, "M small buf positional: %1$M", 0); + + return exit_status(); +} + diff --git a/unittest/mysys/queues-t.c b/unittest/mysys/queues-t.c new file mode 100644 index 00000000..23cb0da1 --- /dev/null +++ b/unittest/mysys/queues-t.c @@ -0,0 +1,139 @@ +/* Copyright (c) 2020, MariaDB Corporation + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include <queues.h> +#include <my_rnd.h> +#include "tap.h" + +int cmp(void *arg __attribute__((unused)), uchar *a, uchar *b) +{ + return *a < *b ? -1 : *a > *b; +} + +#define rnd(R) ((uint)(my_rnd(R) * INT_MAX32)) + +#define el(Q,I) ((uint)*queue_element(Q, I)) + +my_bool verbose; + +my_bool check_queue(QUEUE *queue) +{ + char b[1024]={0}, *s, *e=b+sizeof(b)-2; + my_bool ok=1; + uint i; + + s= b + my_snprintf(b, e-b, "%x", el(queue, 1)); + for (i=2; i <= queue->elements; i++) + { + s+= my_snprintf(s, e-s, ", %x", el(queue, i)); + ok &= el(queue, i) <= el(queue, i>>1); + } + if (!ok || verbose) + diag("%s", b); + return ok; +} + +int main(int argc __attribute__((unused)), char *argv[]) +{ + QUEUE q, *queue=&q; + MY_INIT(argv[0]); + plan(19); + + verbose=1; + + init_queue(queue, 256, 0, 1, cmp, NULL, 0, 0); + queue_insert(queue, (uchar*)"\x99"); + queue_insert(queue, (uchar*)"\x19"); + queue_insert(queue, (uchar*)"\x36"); + queue_insert(queue, (uchar*)"\x17"); + queue_insert(queue, (uchar*)"\x12"); + queue_insert(queue, (uchar*)"\x05"); + queue_insert(queue, (uchar*)"\x25"); + queue_insert(queue, (uchar*)"\x09"); + queue_insert(queue, (uchar*)"\x15"); + queue_insert(queue, (uchar*)"\x06"); + queue_insert(queue, (uchar*)"\x11"); + queue_insert(queue, (uchar*)"\x01"); + queue_insert(queue, (uchar*)"\x04"); + queue_insert(queue, (uchar*)"\x13"); + queue_insert(queue, (uchar*)"\x24"); + ok(check_queue(queue), "after insert"); + queue_remove(queue, 5); + ok(check_queue(queue), "after remove 5th"); + + queue_element(queue, 1) = (uchar*)"\x01"; + queue_element(queue, 2) = (uchar*)"\x10"; + queue_element(queue, 3) = (uchar*)"\x04"; + queue_element(queue, 4) = (uchar*)"\x09"; + queue_element(queue, 5) = (uchar*)"\x13"; + queue_element(queue, 6) = (uchar*)"\x03"; + queue_element(queue, 7) = (uchar*)"\x08"; + queue_element(queue, 8) = (uchar*)"\x07"; + queue_element(queue, 9) = (uchar*)"\x06"; + queue_element(queue,10) = (uchar*)"\x12"; + queue_element(queue,11) = (uchar*)"\x05"; + queue_element(queue,12) = (uchar*)"\x02"; + queue_element(queue,13) = (uchar*)"\x11"; + queue->elements= 13; + ok(!check_queue(queue), "manually filled (queue property violated)"); + + queue_fix(queue); + ok(check_queue(queue), "fixed"); + + ok(*queue_remove_top(queue) == 0x13, "remove top 13"); + ok(*queue_remove_top(queue) == 0x12, "remove top 12"); + ok(*queue_remove_top(queue) == 0x11, "remove top 11"); + ok(*queue_remove_top(queue) == 0x10, "remove top 10"); + ok(*queue_remove_top(queue) == 0x09, "remove top 9"); + ok(*queue_remove_top(queue) == 0x08, "remove top 8"); + ok(*queue_remove_top(queue) == 0x07, "remove top 7"); + ok(*queue_remove_top(queue) == 0x06, "remove top 6"); + ok(*queue_remove_top(queue) == 0x05, "remove top 5"); + ok(*queue_remove_top(queue) == 0x04, "remove top 4"); + ok(*queue_remove_top(queue) == 0x03, "remove top 3"); + ok(*queue_remove_top(queue) == 0x02, "remove top 2"); + ok(*queue_remove_top(queue) == 0x01, "remove top 1"); + + /* random test */ + { + int i, res; + struct my_rnd_struct rand; + my_rnd_init(&rand, (ulong)(intptr)&i, (ulong)(intptr)argv); + verbose=0; + + for (res= i=1; i <= 250; i++) + { + uchar *s=alloca(2); + *s= rnd(&rand) % 251; + queue_insert(queue, s); + res &= check_queue(queue); + } + ok(res, "inserted 250"); + + while (queue->elements) + { + queue_remove(queue, (rnd(&rand) % queue->elements) + 1); + res &= check_queue(queue); + } + ok(res, "removed 250"); + } + + delete_queue(queue); + my_end(0); + return exit_status(); +} + diff --git a/unittest/mysys/stacktrace-t.c b/unittest/mysys/stacktrace-t.c new file mode 100644 index 00000000..d8408f80 --- /dev/null +++ b/unittest/mysys/stacktrace-t.c @@ -0,0 +1,69 @@ + +/* Copyright (c) 2020, MariaDB Corporation. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include <stdio.h> +#include <my_stacktrace.h> +#include <tap.h> + +char b_bss[10]; + +void test_my_safe_print_str() +{ + char b_stack[10]; + char *b_heap= strdup("LEGAL"); + memcpy(b_stack, "LEGAL", 6); + memcpy(b_bss, "LEGAL", 6); + +#ifdef HAVE_STACKTRACE +#ifndef __SANITIZE_ADDRESS__ + fprintf(stderr, "\n===== stack =====\n"); + my_safe_print_str(b_stack, 65535); + fprintf(stderr, "\n===== heap =====\n"); + my_safe_print_str(b_heap, 65535); + fprintf(stderr, "\n===== BSS =====\n"); + my_safe_print_str(b_bss, 65535); + fprintf(stderr, "\n===== data =====\n"); + my_safe_print_str("LEGAL", 65535); + fprintf(stderr, "\n===== Above is a junk, but it is expected. =====\n"); +#endif /*__SANITIZE_ADDRESS__*/ + fprintf(stderr, "\n===== Nornal length test =====\n"); + my_safe_print_str("LEGAL", 5); + fprintf(stderr, "\n===== NULL =====\n"); + my_safe_print_str(0, 5); +#ifndef __SANITIZE_ADDRESS__ + fprintf(stderr, "\n===== (const char*) 1 =====\n"); + my_safe_print_str((const char*)1, 5); +#endif /*__SANITIZE_ADDRESS__*/ +#endif /*HAVE_STACKTRACE*/ + + free(b_heap); + + ok(1, "test_my_safe_print_str"); +} + + +int main(int argc __attribute__((unused)), char **argv) +{ + MY_INIT(argv[0]); + plan(1); + + test_my_safe_print_str(); + + my_end(0); + return exit_status(); +} diff --git a/unittest/mysys/thr_template.c b/unittest/mysys/thr_template.c new file mode 100644 index 00000000..42e035d9 --- /dev/null +++ b/unittest/mysys/thr_template.c @@ -0,0 +1,73 @@ +/* Copyright (c) 2006-2008 MySQL AB, 2009 Sun Microsystems, Inc. + Use is subject to license terms. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include <my_global.h> +#include <my_sys.h> +#include <tap.h> + +volatile uint32 bad; +pthread_mutex_t mutex; + +void do_tests(); + +void test_concurrently(const char *test, pthread_handler handler, int n, int m) +{ + pthread_t *threads= malloc(n * sizeof(pthread_t)); + int i; + ulonglong now= my_interval_timer(); + + assert(threads); + bad= 0; + + diag("Testing %s with %d threads, %d iterations... ", test, n, m); + for (i= 0; i < n; i++) + { + if (pthread_create(&threads[i], 0, handler, &m) != 0) + { + diag("Could not create thread"); + abort(); + } + } + + for (i= 0; i < n; i++) + pthread_join(threads[i], 0); + + now= my_interval_timer() - now; + free(threads); + ok(!bad, "tested %s in %g secs (%d)", test, ((double)now)/1e9, bad); +} + +int main(int argc __attribute__((unused)), char **argv) +{ + MY_INIT(argv[0]); + + if (argv[1] && *argv[1]) + DBUG_SET_INITIAL(argv[1]); + + pthread_mutex_init(&mutex, 0); + +#define CYCLES 3000 +#define THREADS 30 + + diag("N CPUs: %d", my_getncpus()); + + do_tests(); + + pthread_mutex_destroy(&mutex); + my_end(0); + return exit_status(); +} + diff --git a/unittest/mysys/waiting_threads-t.c b/unittest/mysys/waiting_threads-t.c new file mode 100644 index 00000000..82623157 --- /dev/null +++ b/unittest/mysys/waiting_threads-t.c @@ -0,0 +1,286 @@ +/* Copyright (C) 2008 MySQL AB, 2008-2009 Sun Microsystems, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */ + +#include "thr_template.c" +#include <waiting_threads.h> +#include <m_string.h> + +struct test_wt_thd { + WT_THD thd; + pthread_mutex_t lock; +} thds[THREADS]; + +uint i, cnt; +pthread_mutex_t lock; +pthread_cond_t thread_sync; + +ulong wt_timeout_short=100, wt_deadlock_search_depth_short=4; +ulong wt_timeout_long=10000, wt_deadlock_search_depth_long=15; + +#define reset(ARRAY) bzero(ARRAY, sizeof(ARRAY)) + +/* see explanation of the kill strategies in waiting_threads.h */ +enum { LATEST, RANDOM, YOUNGEST, LOCKS } kill_strategy; + +WT_RESOURCE_TYPE restype={ wt_resource_id_memcmp, 0}; + +#define rnd() ((uint)(my_rnd(&rand) * INT_MAX32)) + +/* + stress test: wait on a random number of random threads. + it always succeeds (unless crashes or hangs). +*/ +pthread_handler_t test_wt(void *arg) +{ + int m, n, i, id, res; + struct my_rnd_struct rand; + + my_thread_init(); + + pthread_mutex_lock(&mutex); + id= cnt++; + wt_thd_lazy_init(& thds[id].thd, + & wt_deadlock_search_depth_short, & wt_timeout_short, + & wt_deadlock_search_depth_long, & wt_timeout_long); + + /* now, wait for everybody to be ready to run */ + if (cnt >= THREADS) + pthread_cond_broadcast(&thread_sync); + else + while (cnt < THREADS) + pthread_cond_wait(&thread_sync, &mutex); + pthread_mutex_unlock(&mutex); + + my_rnd_init(&rand, (ulong)(intptr)&m, id); + if (kill_strategy == YOUNGEST) + thds[id].thd.weight= (ulong) ~ my_interval_timer(); + if (kill_strategy == LOCKS) + thds[id].thd.weight= 0; + + for (m= *(int *)arg; m ; m--) + { + WT_RESOURCE_ID resid; + int blockers[THREADS/10], j, k; + + resid.value= id; + resid.type= &restype; + + res= 0; + + /* prepare for waiting for a random number of random threads */ + for (j= n= (rnd() % THREADS)/10; !res && j >= 0; j--) + { +retry: + i= rnd() % (THREADS-1); /* pick a random thread */ + if (i >= id) i++; /* with a number from 0 to THREADS-1 excluding ours */ + + for (k=n; k >=j; k--) /* the one we didn't pick before */ + if (blockers[k] == i) + goto retry; + blockers[j]= i; + + if (kill_strategy == RANDOM) + thds[id].thd.weight= rnd(); + + pthread_mutex_lock(& thds[i].lock); + res= wt_thd_will_wait_for(& thds[id].thd, & thds[i].thd, &resid); + pthread_mutex_unlock(& thds[i].lock); + } + + if (!res) + { + pthread_mutex_lock(&lock); + res= wt_thd_cond_timedwait(& thds[id].thd, &lock); + pthread_mutex_unlock(&lock); + } + + if (res) + { + pthread_mutex_lock(& thds[id].lock); + pthread_mutex_lock(&lock); + wt_thd_release_all(& thds[id].thd); + pthread_mutex_unlock(&lock); + pthread_mutex_unlock(& thds[id].lock); + if (kill_strategy == LOCKS) + thds[id].thd.weight= 0; + if (kill_strategy == YOUNGEST) + thds[id].thd.weight= (ulong)~ my_interval_timer(); + } + else if (kill_strategy == LOCKS) + thds[id].thd.weight++; + } + + pthread_mutex_lock(&mutex); + /* wait for everybody to finish */ + if (!--cnt) + pthread_cond_broadcast(&thread_sync); + else + while (cnt) + pthread_cond_wait(&thread_sync, &mutex); + + pthread_mutex_lock(& thds[id].lock); + pthread_mutex_lock(&lock); + wt_thd_release_all(& thds[id].thd); + pthread_mutex_unlock(&lock); + pthread_mutex_unlock(& thds[id].lock); + wt_thd_destroy(& thds[id].thd); + pthread_mutex_unlock(&mutex); + + DBUG_PRINT("wt", ("exiting")); + my_thread_end(); + return 0; +} + +void do_one_test() +{ + double sum, sum0; + DBUG_ENTER("do_one_test"); + + reset(wt_cycle_stats); + reset(wt_wait_stats); + wt_success_stats=0; + cnt=0; + test_concurrently("waiting_threads", test_wt, THREADS, CYCLES); + + sum=sum0=0; + for (cnt=0; cnt < WT_CYCLE_STATS; cnt++) + sum+= wt_cycle_stats[0][cnt] + wt_cycle_stats[1][cnt]; + for (cnt=0; cnt < WT_CYCLE_STATS; cnt++) + if (wt_cycle_stats[0][cnt] + wt_cycle_stats[1][cnt] > 0) + { + sum0+=wt_cycle_stats[0][cnt] + wt_cycle_stats[1][cnt]; + diag("deadlock cycles of length %2u: %4u %4u %8.2f %%", cnt, + wt_cycle_stats[0][cnt], wt_cycle_stats[1][cnt], 1e2*sum0/sum); + } + diag("depth exceeded: %u %u", + wt_cycle_stats[0][cnt], wt_cycle_stats[1][cnt]); + for (cnt=0; cnt < WT_WAIT_STATS; cnt++) + if (wt_wait_stats[cnt]>0) + diag("deadlock waits up to %7llu us: %5u", + wt_wait_table[cnt], wt_wait_stats[cnt]); + diag("timed out: %u", wt_wait_stats[cnt]); + diag("successes: %u", wt_success_stats); + + DBUG_VOID_RETURN; +} + +void do_tests() +{ + DBUG_ENTER("do_tests"); + if (skip_big_tests) + { + skip(1, "Big test skipped"); + return; + } + plan(13); + compile_time_assert(THREADS >= 4); + + DBUG_PRINT("wt", ("================= initialization ===================")); + + pthread_cond_init(&thread_sync, 0); + pthread_mutex_init(&lock, 0); + wt_init(); + for (cnt=0; cnt < THREADS; cnt++) + pthread_mutex_init(& thds[cnt].lock, 0); + { + WT_RESOURCE_ID resid[4]; + for (i=0; i < array_elements(resid); i++) + { + wt_thd_lazy_init(& thds[i].thd, + & wt_deadlock_search_depth_short, & wt_timeout_short, + & wt_deadlock_search_depth_long, & wt_timeout_long); + resid[i].value= i+1; + resid[i].type= &restype; + } + + DBUG_PRINT("wt", ("================= manual test ===================")); + +#define ok_wait(X,Y, R) \ + ok(wt_thd_will_wait_for(& thds[X].thd, & thds[Y].thd, &resid[R]) == 0, \ + "thd[" #X "] will wait for thd[" #Y "]") +#define ok_deadlock(X,Y,R) \ + ok(wt_thd_will_wait_for(& thds[X].thd, & thds[Y].thd, &resid[R]) == WT_DEADLOCK, \ + "thd[" #X "] will wait for thd[" #Y "] - deadlock") + + ok_wait(0,1,0); + ok_wait(0,2,0); + ok_wait(0,3,0); + + pthread_mutex_lock(&lock); + bad= wt_thd_cond_timedwait(& thds[0].thd, &lock); + pthread_mutex_unlock(&lock); + ok(bad == WT_TIMEOUT, "timeout test returned %d", bad); + + ok_wait(0,1,0); + ok_wait(1,2,1); + ok_deadlock(2,0,2); + + pthread_mutex_lock(&lock); + ok(wt_thd_cond_timedwait(& thds[0].thd, &lock) == WT_TIMEOUT, "as always"); + ok(wt_thd_cond_timedwait(& thds[1].thd, &lock) == WT_TIMEOUT, "as always"); + wt_thd_release_all(& thds[0].thd); + wt_thd_release_all(& thds[1].thd); + wt_thd_release_all(& thds[2].thd); + wt_thd_release_all(& thds[3].thd); + + for (i=0; i < array_elements(resid); i++) + { + wt_thd_release_all(& thds[i].thd); + wt_thd_destroy(& thds[i].thd); + } + pthread_mutex_unlock(&lock); + } + + wt_deadlock_search_depth_short=6; + wt_timeout_short=1000; + wt_timeout_long= 100; + wt_deadlock_search_depth_long=16; + DBUG_PRINT("wt", ("================= stress test ===================")); + + diag("timeout_short=%lu us, deadlock_search_depth_short=%lu", + wt_timeout_short, wt_deadlock_search_depth_short); + diag("timeout_long=%lu us, deadlock_search_depth_long=%lu", + wt_timeout_long, wt_deadlock_search_depth_long); + +#ifndef _WIN32 +#define test_kill_strategy(X) \ + diag("kill strategy: " #X); \ + DBUG_EXECUTE("reset_file", \ + { rewind(DBUG_FILE); my_chsize(fileno(DBUG_FILE), 0, 0, MYF(MY_WME)); }); \ + DBUG_PRINT("info", ("kill strategy: " #X)); \ + kill_strategy=X; \ + do_one_test(); +#else +#define test_kill_strategy(X) \ + diag("kill strategy: " #X); \ + DBUG_PRINT("info", ("kill strategy: " #X)); \ + kill_strategy=X; \ + do_one_test(); +#endif + + test_kill_strategy(LATEST); + test_kill_strategy(RANDOM); + test_kill_strategy(YOUNGEST); + test_kill_strategy(LOCKS); + + DBUG_PRINT("wt", ("================= cleanup ===================")); + for (cnt=0; cnt < THREADS; cnt++) + pthread_mutex_destroy(& thds[cnt].lock); + wt_end(); + pthread_mutex_destroy(&lock); + pthread_cond_destroy(&thread_sync); + DBUG_VOID_RETURN; +} + |