xref: /aoo42x/main/tools/test/tests.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
30*cdf0e10cSrcweir #include "precompiled_tools.hxx"
31*cdf0e10cSrcweir // autogenerated file with codegen.pl
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
34*cdf0e10cSrcweir #include <rtl/math.hxx>
35*cdf0e10cSrcweir #include <tools/fract.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <stdio.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir namespace tools
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir class FractionTest : public CppUnit::TestFixture
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir public:
45*cdf0e10cSrcweir     void setUp()
46*cdf0e10cSrcweir     {
47*cdf0e10cSrcweir     }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     void tearDown()
50*cdf0e10cSrcweir     {
51*cdf0e10cSrcweir     }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     void testFraction()
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         const Fraction aFract(1082130431,1073741824);
56*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #1 not approximately equal to 1.007812499068677",
57*cdf0e10cSrcweir                                 rtl::math::approxEqual((double)aFract,1.007812499068677) );
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir         Fraction aFract2( aFract );
60*cdf0e10cSrcweir         aFract2.ReduceInaccurate(8);
61*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #2 not 1",
62*cdf0e10cSrcweir                                 aFract2.GetNumerator() == 1 &&
63*cdf0e10cSrcweir                                 aFract2.GetDenominator() == 1 );
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         Fraction aFract3( 0x7AAAAAAA, 0x35555555 );
66*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #3 cancellation wrong",
67*cdf0e10cSrcweir                                 aFract3.GetNumerator() == 0x7AAAAAAA &&
68*cdf0e10cSrcweir                                 aFract3.GetDenominator() == 0x35555555 );
69*cdf0e10cSrcweir         aFract3.ReduceInaccurate(30);
70*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #3 ReduceInaccurate errorneously cut precision",
71*cdf0e10cSrcweir                                 aFract3.GetNumerator() == 0x7AAAAAAA &&
72*cdf0e10cSrcweir                                 aFract3.GetDenominator() == 0x35555555 );
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         aFract3.ReduceInaccurate(29);
75*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 29 bits failed",
76*cdf0e10cSrcweir                                 aFract3.GetNumerator() == 0x3D555555 &&
77*cdf0e10cSrcweir                                 aFract3.GetDenominator() == 0x1AAAAAAA );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         aFract3.ReduceInaccurate(9);
80*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 9 bits failed",
81*cdf0e10cSrcweir                                 aFract3.GetNumerator() == 0x0147 &&
82*cdf0e10cSrcweir                                 aFract3.GetDenominator() == 0x008E );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         aFract3.ReduceInaccurate(1);
85*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 1 bit failed",
86*cdf0e10cSrcweir                                 aFract3.GetNumerator() == 2 &&
87*cdf0e10cSrcweir                                 aFract3.GetDenominator() == 1 );
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         aFract3.ReduceInaccurate(0);
90*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 0 bits failed",
91*cdf0e10cSrcweir                                 aFract3.GetNumerator() == 2 &&
92*cdf0e10cSrcweir                                 aFract3.GetDenominator() == 1 );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir #if SAL_TYPES_SIZEOFLONG == 8
95*cdf0e10cSrcweir 		Fraction aFract4(0x7AAAAAAAAAAAAAAA, 0x3555555555555555);
96*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Fraction #4 cancellation wrong",
97*cdf0e10cSrcweir 						        aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
98*cdf0e10cSrcweir 								aFract4.GetDenominator() == 0x3555555555555555 );
99*cdf0e10cSrcweir 		aFract4.ReduceInaccurate(62);
100*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate errorneously cut precision",
101*cdf0e10cSrcweir 								aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
102*cdf0e10cSrcweir 								aFract4.GetDenominator() == 0x3555555555555555 );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 		aFract4.ReduceInaccurate(61);
105*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate reduce to 61 bit failed",
106*cdf0e10cSrcweir 								aFract4.GetNumerator() == 0x3D55555555555555 &&
107*cdf0e10cSrcweir 								aFract4.GetDenominator() == 0x1AAAAAAAAAAAAAAA );
108*cdf0e10cSrcweir #endif
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(FractionTest);
112*cdf0e10cSrcweir     CPPUNIT_TEST(testFraction);
113*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
114*cdf0e10cSrcweir };
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir // -----------------------------------------------------------------------------
117*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(tools::FractionTest, "FractionTest");
118*cdf0e10cSrcweir } // namespace tools
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir // -----------------------------------------------------------------------------
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
124*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
125*cdf0e10cSrcweir NOADDITIONAL;
126*cdf0e10cSrcweir 
127