1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23 // MARKER(update_precomp.py): autogen include statement, do not remove
24 #include "precompiled_tools.hxx"
25 // autogenerated file with codegen.pl
26
27 #include "gtest/gtest.h"
28 #include <rtl/math.hxx>
29 #include <tools/fract.hxx>
30
31 #include <stdio.h>
32
33 namespace tools
34 {
35
36 class FractionTest : public ::testing::Test
37 {
38 public:
SetUp()39 void SetUp()
40 {
41 }
42
TearDown()43 void TearDown()
44 {
45 }
46 };
47
TEST_F(FractionTest,testFraction)48 TEST_F(FractionTest, testFraction)
49 {
50 const Fraction aFract(1082130431,1073741824);
51 ASSERT_TRUE(rtl::math::approxEqual((double)aFract,1.007812499068677)) << "Fraction #1 not approximately equal to 1.007812499068677";
52
53 Fraction aFract2( aFract );
54 aFract2.ReduceInaccurate(8);
55 ASSERT_TRUE(aFract2.GetNumerator() == 1 &&
56 aFract2.GetDenominator() == 1) << "Fraction #2 not 1";
57
58 Fraction aFract3( 0x7AAAAAAA, 0x35555555 );
59 ASSERT_TRUE(aFract3.GetNumerator() == 0x7AAAAAAA &&
60 aFract3.GetDenominator() == 0x35555555) << "Fraction #3 cancellation wrong";
61 aFract3.ReduceInaccurate(30);
62 ASSERT_TRUE(aFract3.GetNumerator() == 0x7AAAAAAA &&
63 aFract3.GetDenominator() == 0x35555555) << "Fraction #3 ReduceInaccurate errorneously cut precision";
64
65 aFract3.ReduceInaccurate(29);
66 ASSERT_TRUE(aFract3.GetNumerator() == 0x3D555555 &&
67 aFract3.GetDenominator() == 0x1AAAAAAA) << "Fraction #3 reduce to 29 bits failed";
68
69 aFract3.ReduceInaccurate(9);
70 ASSERT_TRUE(aFract3.GetNumerator() == 0x0147 &&
71 aFract3.GetDenominator() == 0x008E) << "Fraction #3 reduce to 9 bits failed";
72
73 aFract3.ReduceInaccurate(1);
74 ASSERT_TRUE(aFract3.GetNumerator() == 2 &&
75 aFract3.GetDenominator() == 1) << "Fraction #3 reduce to 1 bit failed";
76
77 aFract3.ReduceInaccurate(0);
78 ASSERT_TRUE(aFract3.GetNumerator() == 2 &&
79 aFract3.GetDenominator() == 1) << "Fraction #3 reduce to 0 bits failed";
80
81 #if SAL_TYPES_SIZEOFLONG == 8
82 Fraction aFract4(0x7AAAAAAAAAAAAAAA, 0x3555555555555555);
83 ASSERT_TRUE(aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
84 aFract4.GetDenominator() == 0x3555555555555555) << "Fraction #4 cancellation wrong";
85 aFract4.ReduceInaccurate(62);
86 ASSERT_TRUE(aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
87 aFract4.GetDenominator() == 0x3555555555555555) << "Fraction #4 ReduceInaccurate errorneously cut precision";
88
89 aFract4.ReduceInaccurate(61);
90 ASSERT_TRUE(aFract4.GetNumerator() == 0x3D55555555555555 &&
91 aFract4.GetDenominator() == 0x1AAAAAAAAAAAAAAA) << "Fraction #4 ReduceInaccurate reduce to 61 bit failed";
92 #endif
93 }
94
95 } // namespace tools
96
97
main(int argc,char ** argv)98 int main(int argc, char **argv)
99 {
100 ::testing::InitGoogleTest(&argc, argv);
101 return RUN_ALL_TESTS();
102 }
103