~mterry/qtmir/warn-on-xapp

« back to all changes in this revision

Viewing changes to tests/modules/General/timestamp_test.cpp

  • Committer: CI Train Bot
  • Author(s): Nick Dedekind
  • Date: 2015-10-21 11:46:54 UTC
  • mfrom: (369.4.13 qtmir)
  • Revision ID: ci-train-bot@canonical.com-20151021114654-zxs13246bxcdci9l
Added touch performance tracing and test.
Approved by: Gerry Boland

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
TEST(TimestampTest, TestCompressAndUncompress)
 
28
{
 
29
    using namespace testing;
 
30
 
 
31
    int argc = 0;
 
32
    QCoreApplication app(argc, NULL);
 
33
 
 
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();
 
37
 
 
38
    std::chrono::nanoseconds addToTimestamp(0);
 
39
    for (int i = 0; i < 100; i++) {
 
40
        auto timestamp = original_timestamp + addToTimestamp;
 
41
 
 
42
        ulong compressedTimestamp = qtmir::compressTimestamp<ulong>(timestamp);
 
43
 
 
44
        EXPECT_EQ(addToTimestamp.count(), compressedTimestamp);
 
45
        EXPECT_EQ(qtmir::uncompressTimestamp<ulong>(compressedTimestamp), timestamp);
 
46
 
 
47
        addToTimestamp += std::chrono::milliseconds(1);
 
48
    }
 
49
}
 
50
 
 
51
TEST(TimestampTest, TestOverflowWhenExceeding32bitCompression)
 
52
{
 
53
    using namespace testing;
 
54
 
 
55
    int argc = 0;
 
56
    QCoreApplication app(argc, NULL);
 
57
 
 
58
    std::chrono::time_point<std::chrono::system_clock> now;
 
59
    now = std::chrono::system_clock::now();
 
60
    auto timestamp = now.time_since_epoch();
 
61
 
 
62
    // Do first compression. This will result in qield of 0 as seen in TestCompressUncompress
 
63
    quint32 compressedTimestamp = qtmir::compressTimestamp<quint32>(timestamp);
 
64
 
 
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);
 
68
 
 
69
    EXPECT_EQ(0, compressedTimestamp);
 
70
    // ensure the uncompression will yields the original timestamp
 
71
    EXPECT_EQ(qtmir::uncompressTimestamp<quint32>(compressedTimestamp), timestamp);
 
72
}
 
 
b'\\ No newline at end of file'