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

0001 /* This file is part of the KDE project
0002    Copyright 2007 Brad Hards <bradh@frogmouth.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 #include "TestBitopsFunctions.h"
0020 
0021 #include "TestKspreadCommon.h"
0022 
0023 #include <QTest>
0024 
0025 void TestBitopsFunctions::initTestCase()
0026 {
0027     FunctionModuleRegistry::instance()->loadFunctionModules();
0028 }
0029 
0030 // because we may need to promote expected value from integer to float
0031 #define CHECK_EVAL(x,y) { Value z(y); QCOMPARE(evaluate(x,z),(z)); }
0032 
0033 Value TestBitopsFunctions::evaluate(const QString& formula, Value& ex)
0034 {
0035     Formula f;
0036     QString expr = formula;
0037     if (expr[0] != '=')
0038         expr.prepend('=');
0039     f.setExpression(expr);
0040     Value result = f.eval();
0041 
0042     if (result.isFloat() && ex.isInteger())
0043         ex = Value(ex.asFloat());
0044     if (result.isInteger() && ex.isFloat())
0045         result = Value(result.asFloat());
0046 
0047     return result;
0048 }
0049 
0050 void TestBitopsFunctions::testBITAND()
0051 {
0052     // basic check of all four bit combinations
0053     CHECK_EVAL("BITAND(12;10)", Value(8));
0054     // test using an all-zero combo
0055     CHECK_EVAL("BITAND(7;0)", Value(0));
0056     // test of 31-bit value
0057     CHECK_EVAL("BITAND(2147483641; 2147483637)", Value(2147483633));
0058     // test of 32-bit value
0059     CHECK_EVAL("BITAND(4294967289.0; 4294967285.0)", Value(4294967281LL));
0060     // test of 32-bit value
0061     CHECK_EVAL("BITAND(4294967289; 4294967285)", Value(4294967281LL));
0062     // test of 48 bit value
0063     CHECK_EVAL("BITAND(281474976710649 ; 281474976710645)",  Value(281474976710641LL));
0064     // test of 48 bit value
0065     CHECK_EVAL("BITAND(281474976710655; 281474976710655)",  Value(281474976710655LL));
0066     // test of 48 bit value
0067     CHECK_EVAL("BITAND(281474976710655; 281474976710655)<>281474976710656", Value(true));
0068 }
0069 
0070 
0071 void TestBitopsFunctions::testBITOR()
0072 {
0073     // basic check of all four bit combinations
0074     CHECK_EVAL("BITOR(12;10)", Value(14));
0075     // test using an all-zero combo
0076     CHECK_EVAL("BITOR(7;0)", Value(7));
0077     // test of 31-bit value
0078     CHECK_EVAL("BITOR(2147483641; 2147483637)", Value(2147483645));
0079     // test of 32-bit value
0080     CHECK_EVAL("BITOR(4294967289.0; 4294967285.0)", Value(4294967293LL));
0081     // test of 32-bit value
0082     CHECK_EVAL("BITOR(4294967289; 4294967285)", Value(4294967293LL));
0083     // test of 48 bit value
0084     CHECK_EVAL("BITOR(281474976710649; 281474976710645)",  Value(281474976710653LL));
0085     // test of 48 bit value
0086     CHECK_EVAL("BITOR(281474976710655; 281474976710655)",  Value(281474976710655LL));
0087     // test of 48 bit value
0088     CHECK_EVAL("BITOR(281474976710655; 281474976710655)<>281474976710656", Value(true));
0089 }
0090 
0091 void TestBitopsFunctions::testBITXOR()
0092 {
0093     // basic check of all four bit combinations
0094     CHECK_EVAL("BITXOR(12;10)", Value(6));
0095     // test using an all-zero combo
0096     CHECK_EVAL("BITXOR(7;0)", Value(7));
0097     // test of 31-bit value
0098     CHECK_EVAL("BITXOR(2147483641; 2)", Value(2147483643));
0099     // test of 32-bit value
0100     CHECK_EVAL("BITXOR(4294967289.0; 2.0)", Value(4294967291LL));
0101     // test of 32-bit value
0102     CHECK_EVAL("BITXOR(4294967289; 2)", Value(4294967291LL));
0103     // test of 48 bit value
0104     CHECK_EVAL("BITXOR(281474976710649 ; 2)",  Value(281474976710651LL));
0105     // test of 48 bit value
0106     CHECK_EVAL("BITXOR(281474976710655; 0)",  Value(281474976710655LL));
0107     // test of 48 bit value
0108     CHECK_EVAL("BITXOR(281474976710655; 0)<>281474976710656", Value(true));
0109 }
0110 
0111 void TestBitopsFunctions::testBITLSHIFT()
0112 {
0113     CHECK_EVAL("BITLSHIFT(63;2)", Value(252));
0114     CHECK_EVAL("BITLSHIFT(63;0)", Value(63));
0115     CHECK_EVAL("BITLSHIFT(63;-2)", Value(15));
0116     CHECK_EVAL("BITLSHIFT(1;47)", Value(140737488355328LL));
0117     // test for 31 bits
0118     CHECK_EVAL("BITLSHIFT(2147483641; 0)", Value(2147483641LL));
0119     // test for 32 bits
0120     CHECK_EVAL("BITLSHIFT(4294967289; 0)", Value(4294967289LL));
0121     // test for 48 bits
0122     CHECK_EVAL("BITLSHIFT(281474976710649; 0)", Value(281474976710649LL));
0123 }
0124 
0125 void TestBitopsFunctions::testBITRSHIFT()
0126 {
0127     CHECK_EVAL("BITRSHIFT(63;2)", Value(15));
0128     CHECK_EVAL("BITRSHIFT(63;0)", Value(63));
0129     CHECK_EVAL("BITRSHIFT(63;-2)", Value(252));
0130     CHECK_EVAL("BITRSHIFT(63;48)", Value(0));
0131     // test for 31 bits
0132     CHECK_EVAL("BITRSHIFT(2147483641; 0)", Value(2147483641LL));
0133     // test for 32 bits
0134     CHECK_EVAL("BITRSHIFT(4294967289; 0)", Value(4294967289LL));
0135     // test for 48 bits
0136     CHECK_EVAL("BITRSHIFT(281474976710649 ; 0)", Value(281474976710649LL));
0137 }
0138 
0139 QTEST_MAIN(TestBitopsFunctions)