2
* Copyright (C) 2014-2015 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 3, as published by
6
* the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "timestamp.h"
19
#include <gtest/gtest.h>
20
#include <gmock/gmock.h>
22
#include <QCoreApplication>
25
using namespace qtmir;
27
class TimestampTest: public ::testing::Test
30
virtual void SetUp() {
31
resetStartTime(std::chrono::nanoseconds(0));
35
TEST_F(TimestampTest, TestCompressAndUncompress)
37
using namespace testing;
39
std::chrono::time_point<std::chrono::system_clock> now;
40
now = std::chrono::system_clock::now();
41
auto original_timestamp = now.time_since_epoch();
43
qtmir::Timestamp addToTimestamp(0);
45
for (int i = 0; i < 100; i++) {
46
auto timestamp = original_timestamp + addToTimestamp;
48
qtmir::Timestamp compressedTimestamp = qtmir::compressTimestamp<qtmir::Timestamp>(timestamp);
50
EXPECT_EQ(addToTimestamp, compressedTimestamp);
51
EXPECT_EQ(qtmir::uncompressTimestamp<qtmir::Timestamp>(compressedTimestamp), timestamp);
53
addToTimestamp += std::chrono::seconds(1);
57
TEST_F(TimestampTest, TestOverflowWhenExceeding32bitCompression)
59
using namespace testing;
61
std::chrono::time_point<std::chrono::system_clock> now;
62
now = std::chrono::system_clock::now();
63
auto timestamp = now.time_since_epoch();
65
typedef std::chrono::duration<quint32, std::milli> Timestamp32bit;
67
// Do first compression. This will result in qield of 0 as seen in TestCompressUncompress
68
auto compressedTimestamp = qtmir::compressTimestamp<Timestamp32bit>(timestamp);
70
// Add the quint32 limit +1 to get an overflow when we compress the timestamp
71
timestamp += Timestamp32bit::max() + std::chrono::nanoseconds(1);
73
compressedTimestamp = qtmir::compressTimestamp<Timestamp32bit>(timestamp);
75
EXPECT_EQ(0, compressedTimestamp.count());
76
// ensure the uncompression will yields the original timestamp
77
EXPECT_EQ(qtmir::uncompressTimestamp<Timestamp32bit>(compressedTimestamp), timestamp);
b'\\ No newline at end of file'