1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include <rtl/math.hxx>
25 #include <tools/fract.hxx>
30 class FractionTest
: public CppUnit::TestFixture
36 const Fraction
aFract(1082130431,1073741824);
37 CPPUNIT_ASSERT_MESSAGE( "Fraction #1 not approximately equal to 1.007812499068677",
38 rtl::math::approxEqual(static_cast<double>(aFract
),1.007812499068677) );
40 Fraction
aFract2( aFract
);
41 aFract2
.ReduceInaccurate(8);
42 CPPUNIT_ASSERT_MESSAGE( "Fraction #2 not 1",
43 aFract2
.GetNumerator() == 1 &&
44 aFract2
.GetDenominator() == 1 );
46 Fraction
aFract3( 0x7AAAAAAA, 0x35555555 );
47 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 cancellation wrong",
48 aFract3
.GetNumerator() == 0x7AAAAAAA &&
49 aFract3
.GetDenominator() == 0x35555555 );
50 aFract3
.ReduceInaccurate(30);
51 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 ReduceInaccurate erroneously cut precision",
52 aFract3
.GetNumerator() == 0x7AAAAAAA &&
53 aFract3
.GetDenominator() == 0x35555555 );
55 aFract3
.ReduceInaccurate(29);
56 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 29 bits failed",
57 aFract3
.GetNumerator() == 0x3D555555 &&
58 aFract3
.GetDenominator() == 0x1AAAAAAA );
60 aFract3
.ReduceInaccurate(9);
61 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 9 bits failed",
62 aFract3
.GetNumerator() == 0x0147 &&
63 aFract3
.GetDenominator() == 0x008E );
65 aFract3
.ReduceInaccurate(1);
66 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 1 bit failed",
67 aFract3
.GetNumerator() == 2 &&
68 aFract3
.GetDenominator() == 1 );
70 aFract3
.ReduceInaccurate(0);
71 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 0 bits failed",
72 aFract3
.GetNumerator() == 2 &&
73 aFract3
.GetDenominator() == 1 );
77 void testMinLongDouble() {
78 Fraction
f(double(SAL_MIN_INT32
));
79 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32
, f
.GetNumerator());
80 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), f
.GetDenominator());
83 void testCreateFromDoubleIn32BitsPlatform() {
84 // This pass in 64 bits but fail in 32 bits
86 CPPUNIT_ASSERT_EQUAL(true, f
.IsValid());
89 CPPUNIT_TEST_SUITE(FractionTest
);
90 CPPUNIT_TEST(testFraction
);
91 CPPUNIT_TEST(testMinLongDouble
);
92 CPPUNIT_TEST(testCreateFromDoubleIn32BitsPlatform
);
93 CPPUNIT_TEST_SUITE_END();
97 CPPUNIT_TEST_SUITE_REGISTRATION(FractionTest
);
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */