added testing for mathlib

This commit is contained in:
Daniel Marjamäki 2009-04-06 19:08:13 +02:00
parent f0d870c52d
commit 97f5380a2c
5 changed files with 104 additions and 50 deletions

View File

@ -38,6 +38,7 @@ TESTOBJ = test/testautovariables.o \
test/testfilelister.o \
test/testfunctionusage.o \
test/testincompletestatement.o \
test/testmathlib.o \
test/testmemleak.o \
test/testmemleakmp.o \
test/testother.o \
@ -150,7 +151,7 @@ src/filelister.o: src/filelister.cpp src/filelister.h
src/main.o: src/main.cpp src/cppcheckexecutor.h src/errorlogger.h src/settings.h
$(CXX) $(CXXFLAGS) -c -o src/main.o src/main.cpp
src/mathlib.o: src/mathlib.cpp
src/mathlib.o: src/mathlib.cpp src/mathlib.h src/token.h
$(CXX) $(CXXFLAGS) -c -o src/mathlib.o src/mathlib.cpp
src/preprocessor.o: src/preprocessor.cpp src/preprocessor.h src/errorlogger.h src/settings.h src/tokenize.h src/token.h
@ -201,6 +202,9 @@ test/testfunctionusage.o: test/testfunctionusage.cpp src/tokenize.h src/settings
test/testincompletestatement.o: test/testincompletestatement.cpp test/testsuite.h src/errorlogger.h src/settings.h src/tokenize.h src/token.h src/checkother.h src/check.h
$(CXX) $(CXXFLAGS) -c -o test/testincompletestatement.o test/testincompletestatement.cpp
test/testmathlib.o: test/testmathlib.cpp src/mathlib.h src/token.h test/testsuite.h src/errorlogger.h src/settings.h
$(CXX) $(CXXFLAGS) -c -o test/testmathlib.o test/testmathlib.cpp
test/testmemleak.o: test/testmemleak.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkmemoryleak.h src/check.h test/testsuite.h
$(CXX) $(CXXFLAGS) -c -o test/testmemleak.o test/testmemleak.cpp

View File

@ -7,8 +7,4 @@ description=
run_cmd=
[files]
current_page=3
FILE_NAME_0=1943;0;0;16;0;1;0;/home/daniel/cppcheck/src/check.h;
FILE_NAME_1=2387;0;0;16;0;1;0;/home/daniel/cppcheck/src/checkstl.h;
FILE_NAME_2=3967;1;0;16;0;1;0;/home/daniel/cppcheck/src/checkstl.cpp;
FILE_NAME_3=2852;1;0;16;0;1;0;/home/daniel/cppcheck/test/teststl.cpp;
current_page=0

View File

@ -1,4 +1,4 @@
#include "mathLib.h"
#include "mathlib.h"
#include <fstream>
@ -9,82 +9,82 @@
#include <cstdlib>
#include <cmath>
double MathLib::ToNumber(const std::string &str)
double MathLib::toNumber(const std::string &str)
{
return atof(str.c_str());
return atof(str.c_str());
}
std::string MathLib::ToString(double d)
std::string MathLib::toString(double d)
{
std::ostringstream result;
result<<d;
return result.str();
std::ostringstream result;
result << d;
return result.str();
}
std::string MathLib::add(const std::string & first, const std::string & second)
{
return ToString(ToNumber(first) + ToNumber(second));
return toString(toNumber(first) + toNumber(second));
}
std::string MathLib::substract(const std::string &first, const std::string &second)
std::string MathLib::subtract(const std::string &first, const std::string &second)
{
return ToString(ToNumber(first) - ToNumber(second));
return toString(toNumber(first) - toNumber(second));
}
std::string MathLib::divide(const std::string &first, const std::string &second)
{
return ToString(ToNumber(first) / ToNumber(second));
return toString(toNumber(first) / toNumber(second));
}
std::string MathLib::multiply(const std::string &first, const std::string &second)
{
return ToString(ToNumber(first) * ToNumber(second));
return toString(toNumber(first) * toNumber(second));
}
std::string MathLib::sin(const std::string &tok)
{
return ToString(::sin(ToNumber(tok)));
return toString(::sin(toNumber(tok)));
}
std::string MathLib::cos(const std::string &tok)
{
return ToString(::cos(ToNumber(tok)));
return toString(::cos(toNumber(tok)));
}
std::string MathLib::tan(const std::string &tok)
{
return ToString(::tan(ToNumber(tok)));
return toString(::tan(toNumber(tok)));
}
std::string MathLib::abs(const std::string &tok)
{
return ToString(::abs(ToNumber(tok)));
return toString(::abs(toNumber(tok)));
}
bool MathLib::IsGreater(const std::string &first, const std::string &second)
bool MathLib::isGreater(const std::string &first, const std::string &second)
{
return ToNumber(first) > ToNumber(second);
return toNumber(first) > toNumber(second);
}
bool MathLib::IsEqual(const std::string &first, const std::string &second)
bool MathLib::isEqual(const std::string &first, const std::string &second)
{
return ToNumber(first) == ToNumber(second);
return toNumber(first) == toNumber(second);
}
bool MathLib::IsLess(const std::string &first, const std::string &second)
bool MathLib::isLess(const std::string &first, const std::string &second)
{
return ToNumber(first) < ToNumber(second);
return toNumber(first) < toNumber(second);
}
int MathLib::compare(const std::string &first, const std::string &second)
{
if(ToNumber(first) < ToNumber(second))
return -1;
if(ToNumber(first) == ToNumber(second))
return 0;
return 1;
if (toNumber(first) < toNumber(second))
return -1;
if (toNumber(first) == toNumber(second))
return 0;
return 1;
}

View File

@ -1,20 +1,26 @@
#include "token.h"
class MathLib
#ifndef mathlibH
#define mathlibH
#include "token.h"
class MathLib
{
private:
static double ToNumber(const std::string & str);
static std::string ToString(double d);
static double toNumber(const std::string & str);
static std::string toString(double d);
public:
static std::string add(const std::string & first, const std::string & second);
static std::string substract(const std::string & first, const std::string & second);
static std::string multiply(const std::string & first, const std::string & second);
static std::string divide(const std::string & first, const std::string & second);
static std::string sin(const std::string & tok);
static std::string cos(const std::string & tok);
static std::string tan(const std::string & tok);
static std::string abs(const std::string & tok);
static bool IsGreater (const std::string & first, const std::string & second);
static bool IsEqual (const std::string & first, const std::string & second);
static bool IsLess (const std::string & first, const std::string & second);
static int compare(const std::string & first, const std::string & second);
};
static std::string add(const std::string & first, const std::string & second);
static std::string subtract(const std::string & first, const std::string & second);
static std::string multiply(const std::string & first, const std::string & second);
static std::string divide(const std::string & first, const std::string & second);
static std::string sin(const std::string & tok);
static std::string cos(const std::string & tok);
static std::string tan(const std::string & tok);
static std::string abs(const std::string & tok);
static bool isGreater(const std::string & first, const std::string & second);
static bool isEqual(const std::string & first, const std::string & second);
static bool isLess(const std::string & first, const std::string & second);
static int compare(const std::string & first, const std::string & second);
};
#endif

48
test/testmathlib.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
* Leandro Penz, Kimmo Varis, Vesa Pikki
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "../src/mathlib.h"
#include "testsuite.h"
class TestMathLib : public TestFixture
{
public:
TestMathLib() : TestFixture("TestMathLib")
{ }
private:
void run()
{
TEST_CASE(calculate);
}
void calculate()
{
ASSERT_EQUALS(std::string("256"), MathLib::add("0xff", "1"));
ASSERT_EQUALS(std::string("0.003"), MathLib::multiply("1e-3", "3"));
ASSERT_EQUALS(std::string("5"), MathLib::divide("25.5", "5.1"));
}
};
REGISTER_TEST(TestMathLib)