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 9 : class FractionTest : public CppUnit::TestFixture
32 : {
33 : public:
34 :
35 1 : void testFraction()
36 : {
37 1 : const Fraction aFract(1082130431,1073741824);
38 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #1 not approximately equal to 1.007812499068677",
39 1 : rtl::math::approxEqual((double)aFract,1.007812499068677) );
40 :
41 2 : Fraction aFract2( aFract );
42 1 : aFract2.ReduceInaccurate(8);
43 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #2 not 1",
44 : aFract2.GetNumerator() == 1 &&
45 1 : aFract2.GetDenominator() == 1 );
46 :
47 2 : Fraction aFract3( 0x7AAAAAAA, 0x35555555 );
48 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 cancellation wrong",
49 : aFract3.GetNumerator() == 0x7AAAAAAA &&
50 1 : aFract3.GetDenominator() == 0x35555555 );
51 1 : aFract3.ReduceInaccurate(30);
52 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 ReduceInaccurate errorneously cut precision",
53 : aFract3.GetNumerator() == 0x7AAAAAAA &&
54 1 : aFract3.GetDenominator() == 0x35555555 );
55 :
56 1 : aFract3.ReduceInaccurate(29);
57 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 29 bits failed",
58 : aFract3.GetNumerator() == 0x3D555555 &&
59 1 : aFract3.GetDenominator() == 0x1AAAAAAA );
60 :
61 1 : aFract3.ReduceInaccurate(9);
62 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 9 bits failed",
63 : aFract3.GetNumerator() == 0x0147 &&
64 1 : aFract3.GetDenominator() == 0x008E );
65 :
66 1 : aFract3.ReduceInaccurate(1);
67 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 1 bit failed",
68 : aFract3.GetNumerator() == 2 &&
69 1 : aFract3.GetDenominator() == 1 );
70 :
71 1 : aFract3.ReduceInaccurate(0);
72 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 0 bits failed",
73 : aFract3.GetNumerator() == 2 &&
74 1 : aFract3.GetDenominator() == 1 );
75 :
76 : #if SAL_TYPES_SIZEOFLONG == 8
77 2 : Fraction aFract4(0x7AAAAAAAAAAAAAAA, 0x3555555555555555);
78 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #4 cancellation wrong",
79 : aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
80 1 : aFract4.GetDenominator() == 0x3555555555555555 );
81 1 : aFract4.ReduceInaccurate(62);
82 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate errorneously cut precision",
83 : aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
84 1 : aFract4.GetDenominator() == 0x3555555555555555 );
85 :
86 1 : aFract4.ReduceInaccurate(61);
87 2 : CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate reduce to 61 bit failed",
88 : aFract4.GetNumerator() == 0x3D55555555555555 &&
89 2 : aFract4.GetDenominator() == 0x1AAAAAAAAAAAAAAA );
90 : #endif
91 1 : }
92 :
93 1 : void testMinLongDouble() {
94 1 : Fraction f(double(SAL_MIN_INT32));
95 1 : CPPUNIT_ASSERT_EQUAL(long(SAL_MIN_INT32), f.GetNumerator());
96 1 : CPPUNIT_ASSERT_EQUAL(1L, f.GetDenominator());
97 1 : }
98 :
99 1 : void testCreateFromDoubleIn32BitsPlatform() {
100 : // This pass in 64 bits but fail in 32 bits
101 1 : Fraction f(0.960945);
102 1 : CPPUNIT_ASSERT_EQUAL(true, f.IsValid());
103 1 : }
104 :
105 2 : CPPUNIT_TEST_SUITE(FractionTest);
106 1 : CPPUNIT_TEST(testFraction);
107 1 : CPPUNIT_TEST(testMinLongDouble);
108 1 : CPPUNIT_TEST(testCreateFromDoubleIn32BitsPlatform);
109 5 : CPPUNIT_TEST_SUITE_END();
110 : };
111 :
112 :
113 1 : CPPUNIT_TEST_SUITE_REGISTRATION(FractionTest);
114 3 : } // namespace tools
115 :
116 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|