Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / tools / test / tests.cxx
blobf8e14ca29c0937e8d26c5835e7202fe68939c187
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:
34 void setUp()
38 void tearDown()
42 void testFraction()
44 const Fraction aFract(1082130431,1073741824);
45 CPPUNIT_ASSERT_MESSAGE( "Fraction #1 not approximately equal to 1.007812499068677",
46 rtl::math::approxEqual((double)aFract,1.007812499068677) );
48 Fraction aFract2( aFract );
49 aFract2.ReduceInaccurate(8);
50 CPPUNIT_ASSERT_MESSAGE( "Fraction #2 not 1",
51 aFract2.GetNumerator() == 1 &&
52 aFract2.GetDenominator() == 1 );
54 Fraction aFract3( 0x7AAAAAAA, 0x35555555 );
55 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 cancellation wrong",
56 aFract3.GetNumerator() == 0x7AAAAAAA &&
57 aFract3.GetDenominator() == 0x35555555 );
58 aFract3.ReduceInaccurate(30);
59 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 ReduceInaccurate errorneously cut precision",
60 aFract3.GetNumerator() == 0x7AAAAAAA &&
61 aFract3.GetDenominator() == 0x35555555 );
63 aFract3.ReduceInaccurate(29);
64 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 29 bits failed",
65 aFract3.GetNumerator() == 0x3D555555 &&
66 aFract3.GetDenominator() == 0x1AAAAAAA );
68 aFract3.ReduceInaccurate(9);
69 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 9 bits failed",
70 aFract3.GetNumerator() == 0x0147 &&
71 aFract3.GetDenominator() == 0x008E );
73 aFract3.ReduceInaccurate(1);
74 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 1 bit failed",
75 aFract3.GetNumerator() == 2 &&
76 aFract3.GetDenominator() == 1 );
78 aFract3.ReduceInaccurate(0);
79 CPPUNIT_ASSERT_MESSAGE( "Fraction #3 reduce to 0 bits failed",
80 aFract3.GetNumerator() == 2 &&
81 aFract3.GetDenominator() == 1 );
83 #if SAL_TYPES_SIZEOFLONG == 8
84 Fraction aFract4(0x7AAAAAAAAAAAAAAA, 0x3555555555555555);
85 CPPUNIT_ASSERT_MESSAGE( "Fraction #4 cancellation wrong",
86 aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
87 aFract4.GetDenominator() == 0x3555555555555555 );
88 aFract4.ReduceInaccurate(62);
89 CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate errorneously cut precision",
90 aFract4.GetNumerator() == 0x7AAAAAAAAAAAAAAA &&
91 aFract4.GetDenominator() == 0x3555555555555555 );
93 aFract4.ReduceInaccurate(61);
94 CPPUNIT_ASSERT_MESSAGE( "Fraction #4 ReduceInaccurate reduce to 61 bit failed",
95 aFract4.GetNumerator() == 0x3D55555555555555 &&
96 aFract4.GetDenominator() == 0x1AAAAAAAAAAAAAAA );
97 #endif
100 CPPUNIT_TEST_SUITE(FractionTest);
101 CPPUNIT_TEST(testFraction);
102 CPPUNIT_TEST_SUITE_END();
105 // -----------------------------------------------------------------------------
106 CPPUNIT_TEST_SUITE_REGISTRATION(FractionTest);
107 } // namespace tools
109 CPPUNIT_PLUGIN_IMPLEMENT();
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */