~alan-griffiths/miral/debug

« back to all changes in this revision

Viewing changes to miral-qt/tests/modules/General/timestamp_test.cpp

  • Committer: Gerry Boland
  • Date: 2016-06-01 22:06:51 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: gerry.boland@canonical.com-20160601220651-ge508tffql4e7u7c
Import QtMir code into miral-qt subdirectory. Disabled by default, use -DMIRAL_ENABLE_QT=1 to build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014-2015 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 */
 
16
 
 
17
#include "timestamp.h"
 
18
 
 
19
#include <gtest/gtest.h>
 
20
#include <gmock/gmock.h>
 
21
 
 
22
#include <QCoreApplication>
 
23
#include <QDebug>
 
24
 
 
25
using namespace qtmir;
 
26
 
 
27
class TimestampTest: public ::testing::Test
 
28
{
 
29
protected:
 
30
    virtual void SetUp() {
 
31
        resetStartTime(std::chrono::nanoseconds(0));
 
32
    }
 
33
};
 
34
 
 
35
TEST_F(TimestampTest, TestCompressAndUncompress)
 
36
{
 
37
    using namespace testing;
 
38
 
 
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();
 
42
 
 
43
    qtmir::Timestamp addToTimestamp(0);
 
44
 
 
45
    for (int i = 0; i < 100; i++) {
 
46
        auto timestamp = original_timestamp + addToTimestamp;
 
47
 
 
48
        qtmir::Timestamp compressedTimestamp = qtmir::compressTimestamp<qtmir::Timestamp>(timestamp);
 
49
 
 
50
        EXPECT_EQ(addToTimestamp, compressedTimestamp);
 
51
        EXPECT_EQ(qtmir::uncompressTimestamp<qtmir::Timestamp>(compressedTimestamp), timestamp);
 
52
 
 
53
        addToTimestamp += std::chrono::seconds(1);
 
54
    }
 
55
}
 
56
 
 
57
TEST_F(TimestampTest, TestOverflowWhenExceeding32bitCompression)
 
58
{
 
59
    using namespace testing;
 
60
 
 
61
    std::chrono::time_point<std::chrono::system_clock> now;
 
62
    now = std::chrono::system_clock::now();
 
63
    auto timestamp = now.time_since_epoch();
 
64
 
 
65
    typedef std::chrono::duration<quint32, std::milli> Timestamp32bit;
 
66
 
 
67
    // Do first compression. This will result in qield of 0 as seen in TestCompressUncompress
 
68
    auto compressedTimestamp = qtmir::compressTimestamp<Timestamp32bit>(timestamp);
 
69
 
 
70
    // Add the quint32 limit +1 to get an overflow when we compress the timestamp
 
71
    timestamp += Timestamp32bit::max() + std::chrono::nanoseconds(1);
 
72
    
 
73
    compressedTimestamp = qtmir::compressTimestamp<Timestamp32bit>(timestamp);
 
74
 
 
75
    EXPECT_EQ(0, compressedTimestamp.count());
 
76
    // ensure the uncompression will yields the original timestamp
 
77
    EXPECT_EQ(qtmir::uncompressTimestamp<Timestamp32bit>(compressedTimestamp), timestamp);
 
78
}
 
 
b'\\ No newline at end of file'