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_EQUAL_MESSAGE( "Fraction #2 not 1",
43 sal_Int32(1), aFract2
.GetNumerator() );
44 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #2 not 1",
45 sal_Int32(1), aFract2
.GetDenominator() );
47 Fraction
aFract3( 0x7AAAAAAA, 0x35555555 );
48 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 cancellation wrong",
49 sal_Int32(0x7AAAAAAA), aFract3
.GetNumerator() );
50 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 cancellation wrong",
51 sal_Int32(0x35555555), aFract3
.GetDenominator() );
52 aFract3
.ReduceInaccurate(30);
53 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 ReduceInaccurate erroneously cut precision",
54 sal_Int32(0x7AAAAAAA), aFract3
.GetNumerator() );
55 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 ReduceInaccurate erroneously cut precision",
56 sal_Int32(0x35555555), aFract3
.GetDenominator() );
58 aFract3
.ReduceInaccurate(29);
59 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 29 bits failed",
60 sal_Int32(0x3D555555), aFract3
.GetNumerator() );
61 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 29 bits failed",
62 sal_Int32(0x1AAAAAAA), aFract3
.GetDenominator() );
64 aFract3
.ReduceInaccurate(9);
65 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 9 bits failed",
66 sal_Int32(0x0147), aFract3
.GetNumerator() );
67 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 9 bits failed",
68 sal_Int32(0x008E), aFract3
.GetDenominator() );
70 aFract3
.ReduceInaccurate(1);
71 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 1 bit failed",
72 sal_Int32(2), aFract3
.GetNumerator() );
73 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 1 bit failed",
74 sal_Int32(1), aFract3
.GetDenominator() );
76 aFract3
.ReduceInaccurate(0);
77 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 0 bits failed",
78 sal_Int32(2), aFract3
.GetNumerator() );
79 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fraction #3 reduce to 0 bits failed",
80 sal_Int32(1), aFract3
.GetDenominator() );
84 void testMinLongDouble() {
85 Fraction
f(double(SAL_MIN_INT32
));
86 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32
, f
.GetNumerator());
87 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), f
.GetDenominator());
90 void testCreateFromDoubleIn32BitsPlatform() {
91 // This pass in 64 bits but fail in 32 bits
93 CPPUNIT_ASSERT_EQUAL(true, f
.IsValid());
96 CPPUNIT_TEST_SUITE(FractionTest
);
97 CPPUNIT_TEST(testFraction
);
98 CPPUNIT_TEST(testMinLongDouble
);
99 CPPUNIT_TEST(testCreateFromDoubleIn32BitsPlatform
);
100 CPPUNIT_TEST_SUITE_END();
104 CPPUNIT_TEST_SUITE_REGISTRATION(FractionTest
);
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */