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