26 #include "decimal_utils.hpp"
27 #include "CharsetMap.hpp"
29 #include "my_global.h"
33 void test_decimal(
const char *s,
int prec,
int scale,
int expected_rv)
35 char bin_buff[128], str_buff[128];
40 r1 = decimal_str2bin(s, strlen(s), prec, scale, bin_buff, 128);
41 if(r1 <= E_DEC_OVERFLOW)
42 r2 = decimal_bin2str(bin_buff, 128, prec, scale, str_buff, 128);
44 printf(
"[%-2d,%-2d] %-29s => res=%d,%d %s\n",
45 prec, scale, s, r1, r2, str_buff);
47 if(r1 != expected_rv) {
48 printf(
"decimal_str2bin returned %d when %d was expected.\n",
57 printf(
"==== init MySQL lib ====\n");
61 printf(
"==== decimal_str2bin() / decimal_bin2str() ====\n");
63 test_decimal(
"100", 3, -1, E_DEC_BAD_SCALE);
64 test_decimal(
"3.3", 2, 1, E_DEC_OK);
65 test_decimal(
"124.000", 20, 4, E_DEC_OK);
66 test_decimal(
"-11", 14, 1, E_DEC_OK);
67 test_decimal(
"1.123456000000000", 20, 16, E_DEC_OK);
68 test_decimal(
"-20.333", 4, 2, E_DEC_TRUNCATED);
69 test_decimal(
"0", 20, 10, E_DEC_OK);
70 test_decimal(
"1 ", 20, 10, E_DEC_OK);
71 test_decimal(
"1,35", 20, 10, E_DEC_OK);
72 test_decimal(
"text", 20, 10, E_DEC_BAD_NUM);
75 printf(
"\n==== CharsetMap ==== \n");
84 printf(
"UTF-8 charset num: %d UTF-16 or UCS-2 charset num: %d\n",
86 if((utf8_num == 0) || (utf16_num == 0)) exit(1);
90 const char *utf8 = csmap.
getName(utf8_num);
91 if(strcmp(utf8,
"UTF-8")) exit(1);
97 printf(
"Using mysql's %s for UTF-16.\n", utf16);
107 const char my_word_latin1[6] = { 0xFC,
'l',
'k',
'e',
'r', 0};
108 const char my_word_utf8[7] = { 0xC3, 0xBC,
'l',
'k',
'e',
'r', 0};
109 const char my_word_truncated[5] = { 0xC3, 0xBC,
'l',
'k', 0};
110 const unsigned char my_bad_utf8[5] = {
'l' , 0xBC,
'a',
'd', 0};
111 char result_buff_1[32];
112 char result_buff_2[32];
113 char result_buff_too_small[4];
118 printf(
"latin1 charset number: %d standard name: \"%s\" \n",
119 latin1_num, csmap.
getName(latin1_num));
120 assert(latin1_num != 0);
121 assert(! strcmp(csmap.
getName(latin1_num) ,
"windows-1252"));
123 printf(
"Latin1: \"%s\" UTF8: \"%s\" \n",
124 my_word_latin1, my_word_utf8);
130 my_word_utf8, result_buff_1);
131 printf(
"Recode Test 1 - UTF-8 to Latin-1: %d %ld %ld \"%s\" => \"%s\" \n",
132 rr1, lengths[0], lengths[1], my_word_utf8, result_buff_1);
133 assert(rr1 == CharsetMap::RECODE_OK);
134 assert(lengths[0] == 7);
135 assert(lengths[1] == 6);
136 assert(!strcmp(result_buff_1, my_word_latin1));
142 my_word_latin1, result_buff_2);
143 printf(
"Recode Test 2 - Latin-1 to UTF-8: %d %ld %ld \"%s\" => \"%s\" \n",
144 rr2, lengths[0], lengths[1], my_word_latin1, result_buff_2);
145 assert(rr2 == CharsetMap::RECODE_OK);
146 assert(lengths[0] == 6);
147 assert(lengths[1] == 7);
148 assert(!(strcmp(result_buff_2, my_word_utf8)));
154 my_word_latin1, result_buff_too_small);
155 printf(
"Recode Test 3 - too-small buffer: %d %ld %ld \"%s\" => \"%s\" \n",
156 rr3, lengths[0], lengths[1], my_word_latin1, result_buff_too_small);
157 assert(rr3 == CharsetMap::RECODE_BUFF_TOO_SMALL);
158 assert(lengths[0] == 3);
159 assert(lengths[1] == 4);
161 assert(!(strncmp(result_buff_too_small, my_word_truncated, 4)));
165 printf(
"Recode Test 4 - invalid charset: %d \n", rr4);
166 assert(rr4 == CharsetMap::RECODE_BAD_CHARSET);
171 int rr5 = csmap.
recode(lengths, utf8_num, latin1_num,
172 my_bad_utf8, result_buff_2);
173 printf(
"Recode Test 5 - ill-formed source string: %d \n", rr5);
174 assert(rr5 == CharsetMap::RECODE_BAD_SRC);
177 printf(
"isMultibyte TEST: ");
178 const bool * result1, * result2, * result3;
182 printf(
"latin 1: %s UTF16: %s UTF8: %s\n",
183 *result1 ?
"Yes" :
"No" ,
184 *result2 ?
"Yes" :
"No" ,
185 *result3 ?
"Yes" :
"No");
190 int nNull = 0, nSingle = 0, nMulti = 0;
191 for(
int i = 0 ;
i < 256 ;
i++) {
199 printf(
"Charset stats: %d unused, %d single-byte, %d multi-byte\n",
200 nNull, nSingle, nMulti);
202 assert(nNull && nSingle && nMulti);