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/.
10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
13 #include <tools/time.hxx>
17 class TimeTest
: public CppUnit::TestFixture
21 void testClockValues();
23 CPPUNIT_TEST_SUITE(TimeTest
);
24 CPPUNIT_TEST(testTime
);
25 CPPUNIT_TEST(testClockValues
);
26 CPPUNIT_TEST_SUITE_END();
29 void TimeTest::testTime()
31 Time
aOrigTime(1, 56, 10);
32 auto nMS
= aOrigTime
.GetMSFromTime();
35 aNewTime
.MakeTimeFromMS(nMS
);
37 CPPUNIT_ASSERT(bool(aOrigTime
== aNewTime
));
40 void TimeTest::testClockValues()
42 double fTime
, fFractionOfSecond
;
43 sal_uInt16 nHour
, nMinute
, nSecond
;
46 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
47 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(0), nHour
);
48 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
49 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
50 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
53 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
54 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(0), nHour
);
55 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
56 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
57 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
60 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
61 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(0), nHour
);
62 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
63 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
64 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
67 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
68 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(12), nHour
);
69 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
70 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
71 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
74 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
75 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(12), nHour
);
76 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
77 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
78 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
81 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
82 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(18), nHour
);
83 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
84 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
85 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
87 fTime
= 0.0208333333333333;
88 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
89 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(0), nHour
);
90 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(30), nMinute
);
91 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
92 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
94 fTime
= 0.0000115740625;
95 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 3);
96 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(0), nHour
);
97 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
98 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
99 // Expect this to be a truncated 0.999999
100 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.999, fFractionOfSecond
, 0.0);
102 fTime
= 0.524268391203704;
103 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 3);
104 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(12), nHour
);
105 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(34), nMinute
);
106 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(56), nSecond
);
107 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.789, fFractionOfSecond
, 0.0);
110 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 13);
111 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(23), nHour
);
112 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(59), nMinute
);
113 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(59), nSecond
);
114 // Expect this to be exact within floating point accuracy.
115 // This is a hairy rounding condition, if it yields problems on any
116 // platform feel free to disable the test for that platform.
117 // At least when doing a 32-bit build on Linux x86 with GCC 8.2.1, when -Os from
118 // gb_COMPILEROPTFLAGS in solenv/gbuild/platform/LINUX_INTEL_GCC.mk is overridden by -O1 (or
119 // higher) passed into CXXFLAGS, the test fails with an actual value of 0.9136, for reasons not
120 // investigated further:
121 #if !(defined __GNUC__ && !defined __clang__ && defined X86 && defined __OPTIMIZE__ \
122 && !defined __OPTIMIZE_SIZE__)
123 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.9135999999999, fFractionOfSecond
,
128 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 4);
129 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(23), nHour
);
130 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(59), nMinute
);
131 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(59), nSecond
);
132 // Expect this to be rounded.
133 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.9136, fFractionOfSecond
, 0.0);
135 fTime
= -0.00000000001;
136 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 4);
137 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(23), nHour
);
138 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(59), nMinute
);
139 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(59), nSecond
);
140 // Expect this to be a truncated 0.999999
141 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.9999, fFractionOfSecond
, 0.0);
143 fTime
= -1e-24; // value insignificant for time
144 Time::GetClock(fTime
, nHour
, nMinute
, nSecond
, fFractionOfSecond
, 0);
145 CPPUNIT_ASSERT_EQUAL_MESSAGE("Hour value.", sal_uInt16(0), nHour
);
146 CPPUNIT_ASSERT_EQUAL_MESSAGE("Minute value.", sal_uInt16(0), nMinute
);
147 CPPUNIT_ASSERT_EQUAL_MESSAGE("Second value.", sal_uInt16(0), nSecond
);
148 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Fraction value.", 0.0, fFractionOfSecond
, 0.0);
151 CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest
);
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */