| 1 | /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | |
| 16 | #include "my_config.h" |
| 17 | #include "config.h" |
| 18 | #include <tap.h> |
| 19 | #include <my_global.h> |
| 20 | #include <my_sys.h> |
| 21 | #include <m_string.h> |
| 22 | #include <sql_string.h> |
| 23 | #include <my_decimal.h> |
| 24 | |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | Test my_decimal constuctor and assignement operators |
| 29 | */ |
| 30 | static int |
| 31 | test_copy_and_compare() |
| 32 | { |
| 33 | my_decimal d1,d2; |
| 34 | |
| 35 | ulonglong val= 42; |
| 36 | |
| 37 | ok(ulonglong2decimal(val,&d1) == 0, "Pass" ); |
| 38 | d2= d1; |
| 39 | my_decimal d3(d1); |
| 40 | |
| 41 | ok(my_decimal_cmp(&d1, &d2) == 0, "Pass" ); |
| 42 | ok(my_decimal_cmp(&d2, &d3) == 0, "Pass" ); |
| 43 | ok(my_decimal_cmp(&d3, &d1) == 0,"Pass" ); |
| 44 | |
| 45 | ulonglong val1, val2, val3; |
| 46 | ok(decimal2ulonglong(&d1, &val1) == 0, "Pass" ); |
| 47 | ok(decimal2ulonglong(&d2, &val2) == 0,"Pass" ); |
| 48 | ok(decimal2ulonglong(&d3, &val3) == 0,"Pass" ); |
| 49 | |
| 50 | ok(val == val1,"Pass" ); |
| 51 | ok(val == val2,"Pass" ); |
| 52 | ok(val == val3,"Pass" ); |
| 53 | |
| 54 | // The CTOR/operator=() generated by the compiler would fail here: |
| 55 | val= 45; |
| 56 | ok(ulonglong2decimal(val, &d1) == 0,"Pass" ); |
| 57 | ok(my_decimal_cmp(&d1, &d2) == 1,"Pass" ); |
| 58 | ok(my_decimal_cmp(&d1, &d3) == 1,"Pass" ); |
| 59 | |
| 60 | return 0; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | static int |
| 65 | test_decimal2string() |
| 66 | { |
| 67 | decimal_t d1; |
| 68 | decimal_digit_t buffer[DECIMAL_BUFF_LENGTH+2]; |
| 69 | char *str_end; |
| 70 | const char strnum[]= "0.1234567890123456789012345678901234567890123467" ; |
| 71 | char strbuff[50]; |
| 72 | int len= 40; |
| 73 | int i; |
| 74 | |
| 75 | bzero(strbuff, sizeof(strbuff)); |
| 76 | str_end= (char *)(strnum + (sizeof(strnum) - 1)); |
| 77 | |
| 78 | d1.len= DECIMAL_BUFF_LENGTH + 2; |
| 79 | d1.buf= buffer; |
| 80 | |
| 81 | string2decimal(strnum, &d1, &str_end); |
| 82 | decimal2string(&d1, strbuff, &len, 0, 0, 'X'); |
| 83 | |
| 84 | /* last digit is not checked due to possible rounding */ |
| 85 | for (i= 0; i < 38 && strbuff[i] == strnum[i]; i++); |
| 86 | ok(i == 38, "Number" ); |
| 87 | for (i= 39; i < 50 && strbuff[i] == 0; i++); |
| 88 | ok(i == 50, "No overrun" ); |
| 89 | |
| 90 | return 0; |
| 91 | |
| 92 | } |
| 93 | int main() |
| 94 | { |
| 95 | plan(15); |
| 96 | diag("Testing my_decimal constructor and assignment operators" ); |
| 97 | |
| 98 | test_copy_and_compare(); |
| 99 | test_decimal2string(); |
| 100 | |
| 101 | return exit_status(); |
| 102 | } |
| 103 | |