Branch libreoffice-5-0-4
[LibreOffice.git] / tools / qa / cppunit / test_fract.cxx
blob6352588ef6b21960d9b08e0f32a0a6b77963386d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <cppunit/plugin/TestPlugIn.h>
25 #include <rtl/math.hxx>
26 #include <tools/fract.hxx>
28 namespace tools
31 class FractionTest : public CppUnit::TestFixture
33 public:
35 void testFraction()
37 const Fraction aFract(1082130431,1073741824);
38 CPPUNIT_ASSERT_MESSAGE( "Fraction #1 not approximately equal to 1.007812499068677",
39 rtl::math::approxEqual((double)aFract,1.007812499068677) );
41 Fraction aFract2( aFract );
42 aFract2.ReduceInaccurate(8);
43 CPPUNIT_ASSERT_MESSAGE( "Fraction #2 not 1",
44 aFract2.GetNumerator() == 1 &&
45 aFract2.GetDenominator() == 1 );
47 Fraction aFract3( 0x7AAAAAAA, 0x35555555 );
48 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 cancellation wrong",
49 aFract3.GetNumerator() == 0x7AAAAAAA &&
50 aFract3.GetDenominator() == 0x35555555 );
51 aFract3.ReduceInaccurate(30);
52 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 ReduceInaccurate errorneously cut precision",
53 aFract3.GetNumerator() == 0x7AAAAAAA &&
54 aFract3.GetDenominator() == 0x35555555 );
56 aFract3.ReduceInaccurate(29);
57 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 29 bits failed",
58 aFract3.GetNumerator() == 0x3D555555 &&
59 aFract3.GetDenominator() == 0x1AAAAAAA );
61 aFract3.ReduceInaccurate(9);
62 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 9 bits failed",
63 aFract3.GetNumerator() == 0x0147 &&
64 aFract3.GetDenominator() == 0x008E );
66 aFract3.ReduceInaccurate(1);
67 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 1 bit failed",
68 aFract3.GetNumerator() == 2 &&
69 aFract3.GetDenominator() == 1 );
71 aFract3.ReduceInaccurate(0);
72 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 0 bits failed",
73 aFract3.GetNumerator() == 2 &&
74 aFract3.GetDenominator() == 1 );
76 #if SAL_TYPES_SIZEOFLONG == 8
77 Fraction aFract4(0x7AAAAAAAAAAAAAAA, 0x3555555555555555);
78 CPPUNIT_ASSERT_MESSAGE( "Fraction #4 cancellation wrong",
79 aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
80 aFract4.GetDenominator() == 0x3555555555555555 );
81 aFract4.ReduceInaccurate(62);
82 CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate errorneously cut precision",
83 aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
84 aFract4.GetDenominator() == 0x3555555555555555 );
86 aFract4.ReduceInaccurate(61);
87 CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate reduce to 61 bit failed",
88 aFract4.GetNumerator() == 0x3D55555555555555 &&
89 aFract4.GetDenominator() == 0x1AAAAAAAAAAAAAAA );
90 #endif
93 void testMinLongDouble() {
94 Fraction f(double(SAL_MIN_INT32));
95 CPPUNIT_ASSERT_EQUAL(long(SAL_MIN_INT32), f.GetNumerator());
96 CPPUNIT_ASSERT_EQUAL(1L, f.GetDenominator());
99 void testCreateFromDoubleIn32BitsPlatform() {
100 // This pass in 64 bits but fail in 32 bits
101 Fraction f(0.960945);
102 CPPUNIT_ASSERT_EQUAL(true, f.IsValid());
105 CPPUNIT_TEST_SUITE(FractionTest);
106 CPPUNIT_TEST(testFraction);
107 CPPUNIT_TEST(testMinLongDouble);
108 CPPUNIT_TEST(testCreateFromDoubleIn32BitsPlatform);
109 CPPUNIT_TEST_SUITE_END();
113 CPPUNIT_TEST_SUITE_REGISTRATION(FractionTest);
114 } // namespace tools
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */