File indexing completed on 2024-05-12 16:35:54

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; only
0007    version 2 of the License.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "TestDatabaseFunctions.h"
0021 
0022 #include "CellStorage.h"
0023 #include "Map.h"
0024 #include "NamedAreaManager.h"
0025 #include "Sheet.h"
0026 
0027 #include "TestKspreadCommon.h"
0028 
0029 #define CHECK_EVAL(x,y) { Value z(RoundNumber(y)); QCOMPARE(evaluate(x,z), (z)); }
0030 #define ROUND(x) (roundf(1e10 * x) / 1e10)
0031 
0032 // round to get at most 10-digits number
0033 static Value RoundNumber(const Value& v)
0034 {
0035     if (v.isNumber()) {
0036         double d = numToDouble(v.asFloat());
0037         if (fabs(d) < DBL_EPSILON)
0038             d = 0.0;
0039         return Value(ROUND(d));
0040     } else
0041         return v;
0042 }
0043 
0044 Value TestDatabaseFunctions::evaluate(const QString& formula, Value& ex)
0045 {
0046     Formula f(m_map->sheet(0));
0047     QString expr = formula;
0048     if (expr[0] != '=')
0049         expr.prepend('=');
0050     f.setExpression(expr);
0051     Value result = f.eval();
0052 
0053     if (result.isFloat() && ex.isInteger())
0054         ex = Value(ex.asFloat());
0055     if (result.isInteger() && ex.isFloat())
0056         result = Value(result.asFloat());
0057 
0058     return RoundNumber(result);
0059 }
0060 
0061 void TestDatabaseFunctions::initTestCase()
0062 {
0063     FunctionModuleRegistry::instance()->loadFunctionModules();
0064     m_map = new Map(0 /* no Doc */);
0065     m_map->addNewSheet();
0066     Sheet* sheet = m_map->sheet(0);
0067     CellStorage* storage = sheet->cellStorage();
0068 
0069     // TESTDB = A18:I31
0070     m_map->namedAreaManager()->insert(Region(QRect(QPoint(1, 18), QPoint(9, 31)), sheet), "TESTDB");
0071     // A18:A31
0072     storage->setValue(1, 18, Value("TestID"));
0073     for (int row = 19; row <= 31; ++row)
0074         storage->setValue(1, row, Value((double)::pow(2.0, row - 19)));
0075     // B18:B31
0076     storage->setValue(2, 18, Value("Constellation"));
0077     QList<QString> constellations = QList<QString>() << "Cancer" << "Canis Major" << "Canis Minor"
0078                                     << "Carina" << "Draco" << "Eridanus" << "Gemini" << "Hercules" << "Orion" << "Phoenix"
0079                                     << "Scorpio" << "Ursa Major" << "Ursa Minor";
0080     for (int i = 0; i < constellations.count(); ++i)
0081         storage->setValue(2, 19 + i, Value(constellations[i]));
0082     // C18:C31
0083     storage->setValue(3, 18, Value("Bright Stars"));
0084     QList<int> stars = QList<int>() << 0 << 5 << 2 << 5 << 3 << 4 << 4 << 0 << 8 << 1 << 9 << 6 << 2;
0085     for (int i = 0; i < stars.count(); ++i)
0086         storage->setValue(3, 19 + i, Value(stars[i]));
0087     // B36:B37
0088     storage->setValue(2, 36, Value("Bright Stars"));
0089     storage->setValue(2, 37, Value(4));
0090     // D36:D37
0091     storage->setValue(4, 36, Value("Constellation"));
0092     storage->setValue(4, 37, Value("Ursa Major"));
0093 }
0094 
0095 void TestDatabaseFunctions::testDAVERAGE()
0096 {
0097     CHECK_EVAL("=DAVERAGE(TESTDB; \"TestID\"; B36:B37)", Value(48));
0098 }
0099 
0100 void TestDatabaseFunctions::testDCOUNT()
0101 {
0102     CHECK_EVAL("=DCOUNT(TESTDB; \"Bright Stars\"; B36:B37)", Value(2));
0103 }
0104 
0105 void TestDatabaseFunctions::testDCOUNTA()
0106 {
0107     CHECK_EVAL("=DCOUNTA(TESTDB; \"Bright Stars\"; B36:B37)", Value(2));
0108 }
0109 
0110 void TestDatabaseFunctions::testDGET()
0111 {
0112     CHECK_EVAL("=DGET(TESTDB; \"TestID\"; D36:D37)", Value(2048));
0113     CHECK_EVAL("=DGET(TESTDB; \"TestID\"; B36:B37)", Value::errorVALUE());
0114 }
0115 
0116 void TestDatabaseFunctions::testDMAX()
0117 {
0118     CHECK_EVAL("=DMAX(TESTDB; \"TestID\"; B36:B37)", Value(64));
0119 }
0120 
0121 void TestDatabaseFunctions::testDMIN()
0122 {
0123     CHECK_EVAL("=DMIN(TESTDB; \"TestID\"; B36:B37)", Value(32));
0124 }
0125 
0126 void TestDatabaseFunctions::testDPRODUCT()
0127 {
0128     CHECK_EVAL("=DPRODUCT(TESTDB; \"TestID\"; B36:B37)", Value(2048));
0129 }
0130 
0131 void TestDatabaseFunctions::testDSTDEV()
0132 {
0133     CHECK_EVAL("=DSTDEV(TESTDB; \"TestID\"; B36:B37)", Value(22.6274169979695));
0134 }
0135 
0136 void TestDatabaseFunctions::testDSTDEVP()
0137 {
0138     CHECK_EVAL("=DSTDEVP(TESTDB; \"TestID\"; B36:B37)", Value(16));
0139 }
0140 
0141 void TestDatabaseFunctions::testDSUM()
0142 {
0143     CHECK_EVAL("=DSUM(TESTDB; \"TestID\"; B36:B37)", Value(96));
0144 }
0145 
0146 void TestDatabaseFunctions::testDVAR()
0147 {
0148     CHECK_EVAL("=DVAR(TESTDB; \"TestID\"; B36:B37)", Value(512));
0149 }
0150 
0151 void TestDatabaseFunctions::testDVARP()
0152 {
0153     CHECK_EVAL("=DVARP(TESTDB; \"TestID\"; B36:B37)", Value(256));
0154 }
0155 
0156 void TestDatabaseFunctions::cleanupTestCase()
0157 {
0158     delete m_map;
0159 }
0160 
0161 QTEST_MAIN(TestDatabaseFunctions)