Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include <cppunit/TestFixture.h>
22 : #include <cppunit/extensions/HelperMacros.h>
23 : #include <cppunit/plugin/TestPlugIn.h>
24 :
25 : #include <rtl/math.hxx>
26 : #include <tools/fract.hxx>
27 :
28 : namespace tools
29 : {
30 :
31 18 : class FractionTest : public CppUnit::TestFixture
32 : {
33 : public:
34 :
35 2 : void testFraction()
36 : {
37 2 : const Fraction aFract(1082130431,1073741824);
38 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #1 not approximately equal to 1.007812499068677",
39 2 : rtl::math::approxEqual((double)aFract,1.007812499068677) );
40 :
41 2 : Fraction aFract2( aFract );
42 2 : aFract2.ReduceInaccurate(8);
43 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #2 not 1",
44 : aFract2.GetNumerator() == 1 &&
45 2 : aFract2.GetDenominator() == 1 );
46 :
47 2 : Fraction aFract3( 0x7AAAAAAA, 0x35555555 );
48 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 cancellation wrong",
49 : aFract3.GetNumerator() == 0x7AAAAAAA &&
50 2 : aFract3.GetDenominator() == 0x35555555 );
51 2 : aFract3.ReduceInaccurate(30);
52 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 ReduceInaccurate errorneously cut precision",
53 : aFract3.GetNumerator() == 0x7AAAAAAA &&
54 2 : aFract3.GetDenominator() == 0x35555555 );
55 :
56 2 : aFract3.ReduceInaccurate(29);
57 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 29 bits failed",
58 : aFract3.GetNumerator() == 0x3D555555 &&
59 2 : aFract3.GetDenominator() == 0x1AAAAAAA );
60 :
61 2 : aFract3.ReduceInaccurate(9);
62 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 9 bits failed",
63 : aFract3.GetNumerator() == 0x0147 &&
64 2 : aFract3.GetDenominator() == 0x008E );
65 :
66 2 : aFract3.ReduceInaccurate(1);
67 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 1 bit failed",
68 : aFract3.GetNumerator() == 2 &&
69 2 : aFract3.GetDenominator() == 1 );
70 :
71 2 : aFract3.ReduceInaccurate(0);
72 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 0 bits failed",
73 : aFract3.GetNumerator() == 2 &&
74 2 : aFract3.GetDenominator() == 1 );
75 :
76 : #if SAL_TYPES_SIZEOFLONG == 8
77 2 : Fraction aFract4(0x7AAAAAAAAAAAAAAA, 0x3555555555555555);
78 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #4 cancellation wrong",
79 : aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
80 2 : aFract4.GetDenominator() == 0x3555555555555555 );
81 2 : aFract4.ReduceInaccurate(62);
82 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate errorneously cut precision",
83 : aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
84 2 : aFract4.GetDenominator() == 0x3555555555555555 );
85 :
86 2 : aFract4.ReduceInaccurate(61);
87 4 : CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate reduce to 61 bit failed",
88 : aFract4.GetNumerator() == 0x3D55555555555555 &&
89 2 : aFract4.GetDenominator() == 0x1AAAAAAAAAAAAAAA );
90 : #endif
91 2 : }
92 :
93 2 : void testMinLongDouble() {
94 2 : Fraction f(double(SAL_MIN_INT32));
95 2 : CPPUNIT_ASSERT_EQUAL(long(SAL_MIN_INT32), f.GetNumerator());
96 2 : CPPUNIT_ASSERT_EQUAL(1L, f.GetDenominator());
97 2 : }
98 :
99 2 : void testCreateFromDoubleIn32BitsPlatform() {
100 : // This pass in 64 bits but fail in 32 bits
101 2 : Fraction f(0.960945);
102 2 : CPPUNIT_ASSERT_EQUAL(true, f.IsValid());
103 2 : }
104 :
105 4 : CPPUNIT_TEST_SUITE(FractionTest);
106 2 : CPPUNIT_TEST(testFraction);
107 2 : CPPUNIT_TEST(testMinLongDouble);
108 2 : CPPUNIT_TEST(testCreateFromDoubleIn32BitsPlatform);
109 4 : CPPUNIT_TEST_SUITE_END();
110 : };
111 :
112 :
113 2 : CPPUNIT_TEST_SUITE_REGISTRATION(FractionTest);
114 6 : } // namespace tools
115 :
116 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|