2009-04-06 19:08:13 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
|
2009-04-06 19:08:13 +02:00
|
|
|
*
|
|
|
|
* 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-06 19:08:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-25 12:49:06 +01:00
|
|
|
#include "mathlib.h"
|
2009-04-06 19:08:13 +02:00
|
|
|
#include "testsuite.h"
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class TestMathLib : public TestFixture {
|
2009-04-06 19:08:13 +02:00
|
|
|
public:
|
2013-08-07 16:27:37 +02:00
|
|
|
TestMathLib() : TestFixture("TestMathLib") {
|
|
|
|
}
|
2009-04-06 19:08:13 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void run() {
|
2009-04-06 19:08:13 +02:00
|
|
|
TEST_CASE(calculate);
|
2012-04-12 14:02:09 +02:00
|
|
|
TEST_CASE(calculate1);
|
2009-05-02 11:12:49 +02:00
|
|
|
TEST_CASE(convert);
|
2009-09-01 21:06:46 +02:00
|
|
|
TEST_CASE(isint);
|
2010-04-02 02:19:38 +02:00
|
|
|
TEST_CASE(isnegative);
|
|
|
|
TEST_CASE(isfloat);
|
2011-07-19 09:10:34 +02:00
|
|
|
TEST_CASE(isGreater)
|
|
|
|
TEST_CASE(isGreaterEqual)
|
|
|
|
TEST_CASE(isEqual)
|
|
|
|
TEST_CASE(isNotEqual)
|
|
|
|
TEST_CASE(isLess)
|
|
|
|
TEST_CASE(isLessEqual)
|
2013-07-19 09:27:33 +02:00
|
|
|
TEST_CASE(naninf)
|
2011-07-19 09:10:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isGreater() const {
|
2011-07-19 09:10:34 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isGreater("1.0", "0.001"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isGreater("-1.0", "0.001"));
|
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isGreaterEqual() const {
|
2011-07-19 09:10:34 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isGreaterEqual("1.00", "1.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isGreaterEqual("1.001", "1.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isGreaterEqual("1.0", "0.001"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isGreaterEqual("-1.0", "0.001"));
|
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isEqual() const {
|
2011-07-19 09:10:34 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isEqual("1.0", "1.0"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isEqual("1.", "1.01"));
|
2011-07-29 23:37:40 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isEqual("0.1","1.0E-1"));
|
2011-07-19 09:10:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isNotEqual() const {
|
2011-07-19 09:10:34 +02:00
|
|
|
ASSERT_EQUALS(false , MathLib::isNotEqual("1.0", "1.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isNotEqual("1.", "1.01"));
|
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isLess() const {
|
2011-07-19 09:10:34 +02:00
|
|
|
ASSERT_EQUALS(false , MathLib::isLess("1.0", "0.001"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isLess("-1.0", "0.001"));
|
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isLessEqual() const {
|
2011-07-19 09:10:34 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isLessEqual("1.00", "1.0"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isLessEqual("1.001", "1.0"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isLessEqual("1.0", "0.001"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isLessEqual("-1.0", "0.001"));
|
2009-04-06 19:08:13 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void calculate() const {
|
2009-12-27 17:46:24 +01:00
|
|
|
// addition
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("256", MathLib::add("0xff", "1"));
|
2009-12-27 14:30:23 +01:00
|
|
|
ASSERT_EQUALS("249", MathLib::add("250", "-1"));
|
|
|
|
ASSERT_EQUALS("251", MathLib::add("250", "1"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("-2.0" , MathLib::add("-1.", "-1"));
|
2009-12-27 14:30:23 +01:00
|
|
|
ASSERT_EQUALS("-1" , MathLib::add("0", "-1"));
|
|
|
|
ASSERT_EQUALS("1" , MathLib::add("1", "0"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("0.0" , MathLib::add("0", "0."));
|
|
|
|
ASSERT_EQUALS("1.00000001" , MathLib::add("1", "0.00000001")); // #4016
|
2012-09-05 13:57:57 +02:00
|
|
|
ASSERT_EQUALS("30666.22" , MathLib::add("30666.22", "0.0")); // #4068
|
2009-12-27 14:30:23 +01:00
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
// subtraction
|
2009-12-27 14:30:23 +01:00
|
|
|
ASSERT_EQUALS("254", MathLib::subtract("0xff", "1"));
|
|
|
|
ASSERT_EQUALS("251", MathLib::subtract("250", "-1"));
|
|
|
|
ASSERT_EQUALS("249", MathLib::subtract("250", "1"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("0.0" , MathLib::subtract("-1.", "-1"));
|
2009-12-27 14:30:23 +01:00
|
|
|
ASSERT_EQUALS("1" , MathLib::subtract("0", "-1"));
|
|
|
|
ASSERT_EQUALS("1" , MathLib::subtract("1", "0"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("0.0" , MathLib::subtract("0", "0."));
|
2012-09-05 13:57:57 +02:00
|
|
|
ASSERT_EQUALS("0.99999999" , MathLib::subtract("1", "0.00000001")); // #4016
|
2012-08-22 20:50:39 +02:00
|
|
|
ASSERT_EQUALS("30666.22" , MathLib::subtract("30666.22", "0.0")); // #4068
|
2012-09-06 20:15:32 +02:00
|
|
|
ASSERT_EQUALS("0.0" , MathLib::subtract("0.0", "0.0"));
|
2009-12-27 14:30:23 +01:00
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
// multiply
|
2010-11-15 21:03:49 +01:00
|
|
|
ASSERT_EQUALS("-0.003" , MathLib::multiply("-1e-3", "3"));
|
|
|
|
ASSERT_EQUALS("-11.96" , MathLib::multiply("-2.3", "5.2"));
|
|
|
|
ASSERT_EQUALS("3000" , MathLib::multiply("1E3", "3"));
|
|
|
|
ASSERT_EQUALS("3000" , MathLib::multiply("1E+3", "3"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("3000.0" , MathLib::multiply("1.0E3", "3"));
|
|
|
|
ASSERT_EQUALS("-3000.0" , MathLib::multiply("-1.0E3", "3"));
|
|
|
|
ASSERT_EQUALS("-3000.0" , MathLib::multiply("-1.0E+3", "3"));
|
|
|
|
ASSERT_EQUALS("0.0" , MathLib::multiply("+1.0E+3", "0"));
|
2011-02-05 12:53:28 +01:00
|
|
|
ASSERT_EQUALS("2147483648" , MathLib::multiply("2","1073741824"));
|
|
|
|
ASSERT_EQUALS("536870912" , MathLib::multiply("512","1048576"));
|
2009-12-27 14:30:23 +01:00
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
// divide
|
2011-02-05 12:53:28 +01:00
|
|
|
ASSERT_EQUALS("1" , MathLib::divide("1", "1"));
|
|
|
|
ASSERT_EQUALS("0" , MathLib::divide("0", "1"));
|
|
|
|
ASSERT_EQUALS("5" , MathLib::divide("-10", "-2"));
|
2009-12-27 14:30:23 +01:00
|
|
|
ASSERT_EQUALS("-2.5", MathLib::divide("-10.", "4"));
|
2009-12-27 17:46:24 +01:00
|
|
|
ASSERT_EQUALS("2.5" , MathLib::divide("-10.", "-4"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("5.0" , MathLib::divide("25.5", "5.1"));
|
|
|
|
ASSERT_EQUALS("7.0" , MathLib::divide("21.", "3"));
|
2011-02-05 12:53:28 +01:00
|
|
|
ASSERT_EQUALS("1" , MathLib::divide("3", "2"));
|
2012-09-06 20:15:32 +02:00
|
|
|
ASSERT_THROW(MathLib::divide("123", "0"), InternalError); // throw
|
2013-05-09 18:50:24 +02:00
|
|
|
ASSERT_THROW(MathLib::divide("-9223372036854775808", "-1"), InternalError); // #4520 - out of range => throw
|
2012-09-06 20:15:32 +02:00
|
|
|
MathLib::divide("123", "0.0"); // don't throw
|
2009-12-27 14:30:23 +01:00
|
|
|
|
2012-01-08 21:19:44 +01:00
|
|
|
// Unknown action should throw exception
|
|
|
|
ASSERT_THROW(MathLib::calculate("1","2",'j'),InternalError);
|
2009-04-06 19:08:13 +02:00
|
|
|
}
|
2009-05-02 11:12:49 +02:00
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void calculate1() const { // mod
|
2012-09-06 20:15:32 +02:00
|
|
|
ASSERT_EQUALS("0" , MathLib::calculate("2" , "1" , '%'));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("0.0" , MathLib::calculate("2.0" , "1.0" , '%'));
|
2012-04-12 14:02:09 +02:00
|
|
|
ASSERT_EQUALS("2" , MathLib::calculate("12" , "5" , '%'));
|
|
|
|
ASSERT_EQUALS("1" , MathLib::calculate("100" , "3" , '%'));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS("12.0" , MathLib::calculate("12.0" , "13.0" , '%'));
|
2012-04-12 14:02:09 +02:00
|
|
|
ASSERT_EQUALS("1.3" , MathLib::calculate("5.3" , "2.0" , '%'));
|
|
|
|
ASSERT_EQUALS("1.7" , MathLib::calculate("18.5" , "4.2" , '%'));
|
2012-09-06 20:15:32 +02:00
|
|
|
ASSERT_THROW(MathLib::calculate("123", "0", '%'), InternalError); // throw
|
|
|
|
MathLib::calculate("123", "0.0", '%'); // don't throw
|
2012-04-12 14:02:09 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void convert() const {
|
2009-12-27 17:46:24 +01:00
|
|
|
// ------------------
|
|
|
|
// tolong conversion:
|
|
|
|
// ------------------
|
2009-12-27 15:18:18 +01:00
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
// from hex
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS(10 , MathLib::toLongNumber("0xa"));
|
|
|
|
ASSERT_EQUALS(10995 , MathLib::toLongNumber("0x2AF3"));
|
|
|
|
ASSERT_EQUALS(-10 , MathLib::toLongNumber("-0xa"));
|
2009-12-27 15:18:18 +01:00
|
|
|
ASSERT_EQUALS(-10995, MathLib::toLongNumber("-0x2AF3"));
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS(10 , MathLib::toLongNumber("+0xa"));
|
2009-12-27 15:18:18 +01:00
|
|
|
ASSERT_EQUALS(10995 , MathLib::toLongNumber("+0x2AF3"));
|
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
// from octal
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS(8 , MathLib::toLongNumber("010"));
|
|
|
|
ASSERT_EQUALS(8 , MathLib::toLongNumber("+010"));
|
|
|
|
ASSERT_EQUALS(-8 , MathLib::toLongNumber("-010"));
|
|
|
|
ASSERT_EQUALS(125 , MathLib::toLongNumber("0175"));
|
|
|
|
ASSERT_EQUALS(125 , MathLib::toLongNumber("+0175"));
|
|
|
|
ASSERT_EQUALS(-125 , MathLib::toLongNumber("-0175"));
|
2009-12-27 15:18:18 +01:00
|
|
|
|
2012-09-10 21:13:32 +02:00
|
|
|
// from binary
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS(0 , MathLib::toLongNumber("0b0"));
|
|
|
|
ASSERT_EQUALS(1 , MathLib::toLongNumber("0b1"));
|
|
|
|
ASSERT_EQUALS(1 , MathLib::toLongNumber("+0b1"));
|
|
|
|
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-0b1"));
|
|
|
|
ASSERT_EQUALS(215 , MathLib::toLongNumber("0b11010111"));
|
|
|
|
ASSERT_EQUALS(-215 , MathLib::toLongNumber("-0b11010111"));
|
|
|
|
ASSERT_EQUALS(215 , MathLib::toLongNumber("0B11010111"));
|
2012-09-10 21:13:32 +02:00
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
// from base 10
|
2012-09-11 16:50:42 +02:00
|
|
|
ASSERT_EQUALS(10 , MathLib::toLongNumber("10"));
|
|
|
|
ASSERT_EQUALS(10 , MathLib::toLongNumber("10."));
|
|
|
|
ASSERT_EQUALS(10 , MathLib::toLongNumber("10.0"));
|
|
|
|
ASSERT_EQUALS(100 , MathLib::toLongNumber("10E+1"));
|
|
|
|
ASSERT_EQUALS(1 , MathLib::toLongNumber("10E-1"));
|
|
|
|
ASSERT_EQUALS(100 , MathLib::toLongNumber("+10E+1"));
|
|
|
|
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-10E-1"));
|
|
|
|
ASSERT_EQUALS(100 , MathLib::toLongNumber("+10.E+1"));
|
|
|
|
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-10.E-1"));
|
|
|
|
ASSERT_EQUALS(100 , MathLib::toLongNumber("+10.0E+1"));
|
|
|
|
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-10.0E-1"));
|
2010-04-02 20:23:37 +02:00
|
|
|
|
2012-11-27 17:37:49 +01:00
|
|
|
// from long long
|
|
|
|
ASSERT_EQUALS(0xFF00000000000000LL, MathLib::toLongNumber("0xFF00000000000000LL"));
|
|
|
|
ASSERT_EQUALS(0x0A00000000000000LL, MathLib::toLongNumber("0x0A00000000000000LL"));
|
|
|
|
|
2010-04-02 20:42:06 +02:00
|
|
|
// -----------------
|
|
|
|
// to double number:
|
|
|
|
// -----------------
|
2012-09-17 13:18:19 +02:00
|
|
|
ASSERT_EQUALS_DOUBLE(10.0 , MathLib::toDoubleNumber("10"));
|
2010-12-30 22:56:53 +01:00
|
|
|
ASSERT_EQUALS_DOUBLE(1000.0, MathLib::toDoubleNumber("10E+2"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(100.0 , MathLib::toDoubleNumber("1.0E+2"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(-100.0, MathLib::toDoubleNumber("-1.0E+2"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(-1e+10, MathLib::toDoubleNumber("-1.0E+10"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(100.0 , MathLib::toDoubleNumber("+1.0E+2"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(1e+10 , MathLib::toDoubleNumber("+1.0E+10"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(100.0 , MathLib::toDoubleNumber("1.0E+2"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(1e+10 , MathLib::toDoubleNumber("1.0E+10"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0E+0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0E-0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0E+00"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0E-00"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("-0E+00"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("+0E-00"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0."));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("0.0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("-0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("+0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("-0."));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("+0."));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("-0.0"));
|
|
|
|
ASSERT_EQUALS_DOUBLE(0.0 , MathLib::toDoubleNumber("+0.0"));
|
2010-04-02 20:42:06 +02:00
|
|
|
|
2009-05-02 11:12:49 +02:00
|
|
|
}
|
2009-09-01 21:06:46 +02:00
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isint() const {
|
2009-12-27 17:46:24 +01:00
|
|
|
// zero tests
|
2009-12-27 14:46:47 +01:00
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("0."));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("0.0"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-0."));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+0."));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-0.0"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+0.0"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+0.0E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+0.0E-1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-0.0E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-0.0E-1"));
|
|
|
|
|
2009-12-27 17:46:24 +01:00
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("-1"));
|
2009-12-27 14:46:47 +01:00
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("+1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("+1E+1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("+1E+10000"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("-1E+1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("-1E+10000"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-1E-1"));
|
2009-12-27 17:46:24 +01:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-1E-10000"));
|
|
|
|
|
2012-09-17 13:18:19 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0xff"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0xa"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0l"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0L"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0ul"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0ull"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0llu"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("333L"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("330L"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("330llu"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("07"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isInt("0123"));
|
2009-09-01 21:06:46 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("0.4"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("2352.3f"));
|
2009-12-27 14:46:47 +01:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("0.00004"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("2352.00001f"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(".4"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("1.0E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("1.0E-1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-1.0E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+1.0E-1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-1.E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+1.E-1"));
|
2010-04-02 22:18:50 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" 1.0E+1"));
|
2010-12-15 18:45:53 +01:00
|
|
|
// with whitespace in front
|
2010-04-02 22:18:50 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" 1.0E-1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" -1.0E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" +1.0E-1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" -1.E+1"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" +1.E-1"));
|
2010-12-15 18:45:53 +01:00
|
|
|
// with whitespace in front and end
|
2010-04-02 22:18:50 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" 1.0E-1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" -1.0E+1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" +1.0E-1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" -1.E+1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt(" +1.E-1 "));
|
2010-12-15 18:45:53 +01:00
|
|
|
// with whitespace in front and end
|
2010-04-02 22:18:50 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("1.0E-1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-1.0E+1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+1.0E-1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("-1.E+1 "));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("+1.E-1 "));
|
2010-05-12 23:49:09 +02:00
|
|
|
// test some garbage
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("u"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("l"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("ul"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("ll"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("U"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("L"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("uL"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isInt("LL"));
|
2009-09-01 21:06:46 +02:00
|
|
|
}
|
2010-04-02 02:19:38 +02:00
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isnegative() const {
|
2012-09-17 13:18:19 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isNegative("-1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isNegative("-1."));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isNegative("-1.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isNegative("-1.0E+2"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isNegative("-1.0E-2"));
|
2010-04-02 02:19:38 +02:00
|
|
|
|
2010-04-02 07:32:03 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isNegative("+1"));
|
2010-04-02 02:19:38 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isNegative("+1."));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isNegative("+1.0"));
|
|
|
|
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E+2"));
|
2010-04-02 07:32:03 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isNegative("+1.0E-2"));
|
|
|
|
}
|
2010-04-02 02:19:38 +02:00
|
|
|
|
2012-08-02 11:13:09 +02:00
|
|
|
void isfloat() const {
|
2010-04-02 02:19:38 +02:00
|
|
|
ASSERT_EQUALS(false, MathLib::isFloat("0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("0."));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("0.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-0."));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("+0."));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-0.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("+0.0"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("+0.0E+1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("+0.0E-1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-0.0E+1"));
|
2010-04-02 07:32:03 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-0.0E-1"));
|
2010-04-02 02:19:38 +02:00
|
|
|
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("1"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("-1"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("+1"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("+1E+1"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("+1E+10000"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("-1E+1"));
|
|
|
|
ASSERT_EQUALS(false , MathLib::isFloat("-1E+10000"));
|
2011-01-07 12:27:12 +01:00
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat(".1250E+04"));
|
2010-04-02 02:19:38 +02:00
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-1E-1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-1E-10000"));
|
|
|
|
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("0.4"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("2352.3f"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("0.00004"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("2352.00001f"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat(".4"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("1.0E+1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("1.0E-1"));
|
|
|
|
ASSERT_EQUALS(true , MathLib::isFloat("-1.0E+1"));
|
2010-04-02 07:32:03 +02:00
|
|
|
}
|
2013-07-19 09:27:33 +02:00
|
|
|
|
|
|
|
void naninf() {
|
|
|
|
ASSERT_EQUALS("inf.0", MathLib::divide("0.0", "0.0")); // nan
|
|
|
|
ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.0")); // inf
|
|
|
|
ASSERT_EQUALS("inf.0", MathLib::divide("-3.0", "0.0")); // -inf
|
|
|
|
}
|
2009-04-06 19:08:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestMathLib)
|