| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "plugin/x/client/mysqlxclient/xdecimal.h" |
| 28 | |
| 29 | namespace xpl { |
| 30 | namespace test { |
| 31 | |
| 32 | TEST(xdecimal, str) { |
| 33 | EXPECT_EQ(std::string("\x00\x1C", 2), xcl::Decimal::from_str("1").to_bytes()); |
| 34 | EXPECT_EQ(std::string("\x00\x12\x7c", 3), |
| 35 | xcl::Decimal::from_str("127").to_bytes()); |
| 36 | EXPECT_EQ(std::string("\x05\x12\x34\x51\x23\x45\xc0", 7), |
| 37 | xcl::Decimal::from_str("12345.12345").to_bytes()); |
| 38 | |
| 39 | EXPECT_EQ("12345.12345", xcl::Decimal::from_str("12345.12345").to_string()); |
| 40 | EXPECT_EQ("1234.12345", xcl::Decimal::from_str("1234.12345").to_string()); |
| 41 | EXPECT_EQ("1234.1234", xcl::Decimal::from_str("1234.1234").to_string()); |
| 42 | EXPECT_EQ("1.1", xcl::Decimal::from_str("1.1").to_string()); |
| 43 | EXPECT_EQ("12.34", xcl::Decimal::from_str("12.34").to_string()); |
| 44 | EXPECT_EQ("-1.1", xcl::Decimal::from_str("-1.1").to_string()); |
| 45 | EXPECT_EQ("-12.34", xcl::Decimal::from_str("-12.34").to_string()); |
| 46 | EXPECT_EQ("1.1", xcl::Decimal::from_str("+1.1").to_string()); |
| 47 | EXPECT_EQ("12.34", xcl::Decimal::from_str("+12.34").to_string()); |
| 48 | EXPECT_EQ("1", xcl::Decimal::from_str("1").to_string()); |
| 49 | EXPECT_EQ("12", xcl::Decimal::from_str("12").to_string()); |
| 50 | EXPECT_EQ("-1", xcl::Decimal::from_str("-1").to_string()); |
| 51 | EXPECT_EQ("-12", xcl::Decimal::from_str("-12").to_string()); |
| 52 | EXPECT_EQ("1", xcl::Decimal::from_str("+1").to_string()); |
| 53 | EXPECT_EQ("12", xcl::Decimal::from_str("+12").to_string()); |
| 54 | } |
| 55 | |
| 56 | TEST(xdecimal, bytes) { |
| 57 | EXPECT_EQ("-1234567", |
| 58 | xcl::Decimal::from_bytes( |
| 59 | xcl::Decimal::from_bytes(std::string("\x00\x12\x34\x56\x7d", 5)) |
| 60 | .to_bytes()) |
| 61 | .to_string()); |
| 62 | EXPECT_EQ("-123456", |
| 63 | xcl::Decimal::from_bytes( |
| 64 | xcl::Decimal::from_bytes(std::string("\x00\x12\x34\x56\xd0", 5)) |
| 65 | .to_bytes()) |
| 66 | .to_string()); |
| 67 | EXPECT_EQ("1234567", |
| 68 | xcl::Decimal::from_bytes( |
| 69 | xcl::Decimal::from_bytes(std::string("\x00\x12\x34\x56\x7c", 5)) |
| 70 | .to_bytes()) |
| 71 | .to_string()); |
| 72 | EXPECT_EQ("123456", |
| 73 | xcl::Decimal::from_bytes( |
| 74 | xcl::Decimal::from_bytes(std::string("\x00\x12\x34\x56\xc0", 5)) |
| 75 | .to_bytes()) |
| 76 | .to_string()); |
| 77 | EXPECT_EQ( |
| 78 | "-1234567.00", |
| 79 | xcl::Decimal::from_bytes( |
| 80 | xcl::Decimal::from_bytes(std::string("\x02\x12\x34\x56\x70\x0d", 6)) |
| 81 | .to_bytes()) |
| 82 | .to_string()); |
| 83 | EXPECT_EQ( |
| 84 | "-123456.11", |
| 85 | xcl::Decimal::from_bytes( |
| 86 | xcl::Decimal::from_bytes(std::string("\x02\x12\x34\x56\x11\xd0", 6)) |
| 87 | .to_bytes()) |
| 88 | .to_string()); |
| 89 | EXPECT_EQ( |
| 90 | "1234567.20", |
| 91 | xcl::Decimal::from_bytes( |
| 92 | xcl::Decimal::from_bytes(std::string("\x02\x12\x34\x56\x72\x0c", 6)) |
| 93 | .to_bytes()) |
| 94 | .to_string()); |
| 95 | EXPECT_EQ( |
| 96 | "123456.34", |
| 97 | xcl::Decimal::from_bytes( |
| 98 | xcl::Decimal::from_bytes(std::string("\x02\x12\x34\x56\x34\xc0", 6)) |
| 99 | .to_bytes()) |
| 100 | .to_string()); |
| 101 | } |
| 102 | |
| 103 | TEST(xdecimal, invalid) { |
| 104 | ASSERT_FALSE(xcl::Decimal::from_str("bla").is_valid()); |
| 105 | ASSERT_FALSE(xcl::Decimal::from_str("042423x").is_valid()); |
| 106 | ASSERT_FALSE(xcl::Decimal::from_str("--042423").is_valid()); |
| 107 | ASSERT_FALSE(xcl::Decimal::from_str("-").is_valid()); |
| 108 | ASSERT_FALSE(xcl::Decimal::from_str("+").is_valid()); |
| 109 | ASSERT_FALSE(xcl::Decimal::from_str("+-").is_valid()); |
| 110 | ASSERT_TRUE(xcl::Decimal::from_str("-.0").is_valid()); |
| 111 | ASSERT_FALSE(xcl::Decimal::from_str("0.rewq").is_valid()); |
| 112 | ASSERT_FALSE(xcl::Decimal::from_str("0.0.0").is_valid()); |
| 113 | ASSERT_FALSE(xcl::Decimal::from_str("0.0.").is_valid()); |
| 114 | ASSERT_FALSE(xcl::Decimal::from_str("0f").is_valid()); |
| 115 | } |
| 116 | |
| 117 | } // namespace test |
| 118 | } // namespace xpl |