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
TEST(TimestampTest, TestCompressAndUncompress)
29
using namespace testing;
32
QCoreApplication app(argc, NULL);
34
std::chrono::time_point<std::chrono::system_clock> now;
35
now = std::chrono::system_clock::now();
36
auto original_timestamp = now.time_since_epoch();
38
std::chrono::nanoseconds addToTimestamp(0);
39
for (int i = 0; i < 100; i++) {
40
auto timestamp = original_timestamp + addToTimestamp;
42
ulong compressedTimestamp = qtmir::compressTimestamp<ulong>(timestamp);
44
EXPECT_EQ(addToTimestamp.count(), compressedTimestamp);
45
EXPECT_EQ(qtmir::uncompressTimestamp<ulong>(compressedTimestamp), timestamp);
47
addToTimestamp += std::chrono::milliseconds(1);
51
TEST(TimestampTest, TestOverflowWhenExceeding32bitCompression)
53
using namespace testing;
56
QCoreApplication app(argc, NULL);
58
std::chrono::time_point<std::chrono::system_clock> now;
59
now = std::chrono::system_clock::now();
60
auto timestamp = now.time_since_epoch();
62
// Do first compression. This will result in qield of 0 as seen in TestCompressUncompress
63
quint32 compressedTimestamp = qtmir::compressTimestamp<quint32>(timestamp);
65
// Add the quint32 limit +1 to get an overflow when we compress the timestamp
66
timestamp += std::chrono::nanoseconds(std::numeric_limits<quint32>::max()) + std::chrono::nanoseconds(1);
67
compressedTimestamp = qtmir::compressTimestamp<quint32>(timestamp);
69
EXPECT_EQ(0, compressedTimestamp);
70
// ensure the uncompression will yields the original timestamp
71
EXPECT_EQ(qtmir::uncompressTimestamp<quint32>(compressedTimestamp), timestamp);
b'\\ No newline at end of file'