File indexing completed on 2024-05-12 05:40:47
0001 /*************************************************************************** 0002 * Copyright (C) 2011 by Renaud Guezennec * 0003 * http://renaudguezennec.homelinux.org/accueil,3.html * 0004 * * 0005 * Rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include <QtCore/QCoreApplication> 0022 #include <QtCore/QString> 0023 #include <QtTest/QtTest> 0024 0025 #include "formula/formulamanager.h" 0026 0027 class TestFormula : public QObject 0028 { 0029 Q_OBJECT 0030 0031 public: 0032 TestFormula(); 0033 0034 private slots: 0035 void initTestCase(); 0036 void getAndSetTest(); 0037 void commandsTest(); 0038 void wrongCommandsTest(); 0039 void cleanupTestCase(); 0040 0041 private: 0042 Formula::FormulaManager* m_formulaMan; 0043 QHash<QString, QString>* m_variable; 0044 }; 0045 0046 TestFormula::TestFormula() {} 0047 0048 void TestFormula::initTestCase() 0049 { 0050 m_formulaMan= new Formula::FormulaManager(); 0051 m_variable= new QHash<QString, QString>(); 0052 0053 m_variable->insert("Name", "John Doe"); 0054 m_variable->insert("weight", "85kg"); 0055 m_variable->insert("age", "18"); 0056 m_variable->insert("size", "1.69"); 0057 m_variable->insert("strenght", "4"); 0058 m_variable->insert("intelligence", "5"); 0059 m_variable->insert("speed", "59"); 0060 m_variable->insert("ecole", "Akodo"); 0061 m_variable->insert("arme", "Katana"); 0062 m_variable->insert("XP", "0"); 0063 m_variable->insert("max health", "82"); 0064 m_variable->insert("mana", "10000"); 0065 m_variable->insert("level", "-1"); 0066 m_variable->insert("agility", "-2.5"); 0067 m_variable->insert("manipulation", "10"); 0068 m_variable->insert("mental health", "90%"); 0069 0070 m_formulaMan->setConstantHash(m_variable); 0071 } 0072 0073 void TestFormula::getAndSetTest() {} 0074 0075 void TestFormula::commandsTest() 0076 { 0077 QStringList commands; 0078 0079 commands << "=4*4" 0080 << "=10+8" 0081 << "=10-7" 0082 << "=50/5" 0083 << "=25-52" 0084 << "=${speed}+10" 0085 << "=${size}*10" 0086 << "=${level}-100" 0087 << "=2+4/4" 0088 << "=${manipulation}/100" 0089 << "=${size}*${speed}" 0090 << "=${agility}-100" 0091 << "=${agility}*100" 0092 << "=min(${intelligence},${strenght})" 0093 << "=max(${intelligence},${strenght})" 0094 << "=abs(${size})" 0095 << "=18*1.69-10/-1" 0096 << "=avg(${age},20})" 0097 << "=(10+2)*3" 0098 << "=(10-4)*3" 0099 << "=(10-4)+6)*2" 0100 << "=(10+2)*(10-8)"; 0101 0102 QStringList results; 0103 results << "16" 0104 << "18" 0105 << "3" 0106 << "10" 0107 << "-27" 0108 << "69" 0109 << "16.9" 0110 << "-101" 0111 << "3" 0112 << "0.1" 0113 << "99.71" 0114 << "-102.5" 0115 << "-250" 0116 << "4" 0117 << "5" 0118 << "1" 0119 << "40.42" 0120 << "19" 0121 << "36" 0122 << "18" 0123 << "24" 0124 << "24"; 0125 0126 for(int i= 0; i < commands.size(); ++i) 0127 { 0128 QString cmd= commands.at(i); 0129 QString result= results.at(i); 0130 0131 QVariant a= m_formulaMan->getValue(cmd); 0132 qDebug() << a << result << cmd; 0133 QVERIFY2(a.toDouble() == result.toDouble(), cmd.toStdString().c_str()); 0134 } 0135 } 0136 void TestFormula::wrongCommandsTest() 0137 { 0138 QStringList commands; 0139 0140 foreach(QString cmd, commands) 0141 { 0142 QVariant a= m_formulaMan->getValue(cmd); 0143 0144 // QVERIFY2(a==,cmd.toStdString().c_str()); 0145 } 0146 } 0147 void TestFormula::cleanupTestCase() 0148 { 0149 delete m_formulaMan; 0150 delete m_variable; 0151 } 0152 0153 QTEST_MAIN(TestFormula); 0154 0155 #include "tst_formula.moc"