summaryrefslogtreecommitdiff
path: root/tests/unit/tci/test_tci_protocol.cpp
blob: 95b4e01da217a62041c0072fbc432dc7f69f59ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <limits>

#include <QtTest>

#include "Transceiver/TCIProtocol.hpp"

class TestTciProtocol final
  : public QObject
{
  Q_OBJECT

private slots:
  void parsesFrequency_data ()
  {
    QTest::addColumn<QString> ("text");
    QTest::addColumn<quint64> ("expected");

    QTest::newRow ("ordinary HF") << QString {"14074000"} << quint64 {14074000u};
    QTest::newRow ("below signed 32-bit boundary") << QString {"2147483647"} << quint64 {2147483647u};
    QTest::newRow ("at signed 32-bit boundary") << QString {"2147483648"} << quint64 {2147483648u};
    QTest::newRow ("above signed 32-bit boundary") << QString {"2147483649"} << quint64 {2147483649u};
    QTest::newRow ("10.368 GHz") << QString {"10368000000"} << quint64 {10368000000u};
    QTest::newRow ("upper supported TCI range") << QString {"250000000000"} << quint64 {250000000000u};
    QTest::newRow ("leading zeros") << QString {"00014074000"} << quint64 {14074000u};
    QTest::newRow ("zero") << QString {"0"} << quint64 {0u};
    QTest::newRow ("quint64 maximum")
      << QString {"18446744073709551615"} << (std::numeric_limits<quint64>::max) ();
  }

  void parsesFrequency ()
  {
    QFETCH (QString, text);
    QFETCH (quint64, expected);

    quint64 frequency {99u};
    QVERIFY (TciProtocol::parse_frequency (text, &frequency));
    QCOMPARE (frequency, expected);
  }

  void rejectsMalformedFrequency_data ()
  {
    QTest::addColumn<QString> ("text");

    QTest::newRow ("empty") << QString {};
    QTest::newRow ("negative") << QString {"-14074000"};
    QTest::newRow ("positive sign") << QString {"+14074000"};
    QTest::newRow ("leading whitespace") << QString {" 14074000"};
    QTest::newRow ("trailing whitespace") << QString {"14074000 "};
    QTest::newRow ("group separator") << QString {"14,074,000"};
    QTest::newRow ("decimal") << QString {"14074.000"};
    QTest::newRow ("exponent") << QString {"14074e3"};
    QTest::newRow ("trailing text") << QString {"14074000Hz"};
    QTest::newRow ("unicode digit") << QString::fromUtf8 ("١٤٠٧٤٠٠٠");
    QTest::newRow ("replacement character")
      << QString {"14"} + QChar {QChar::ReplacementCharacter} + QString {"074000"};
    QTest::newRow ("overflow") << QString {"18446744073709551616"};
  }

  void rejectsMalformedFrequency ()
  {
    QFETCH (QString, text);

    quint64 frequency {99u};
    QVERIFY (!TciProtocol::parse_frequency (text, &frequency));
    QCOMPARE (frequency, quint64 {99u});
  }

  void rejectsMissingOutput ()
  {
    QVERIFY (!TciProtocol::parse_frequency (QString {"14074000"}, nullptr));
  }

  void comparesFrequencyDifference_data ()
  {
    QTest::addColumn<quint64> ("lhs");
    QTest::addColumn<quint64> ("rhs");
    QTest::addColumn<bool> ("expected");

    QTest::newRow ("ordinary HF below threshold")
      << quint64 {14074000u} << quint64 {15073999u} << false;
    QTest::newRow ("below threshold")
      << quint64 {10368000000u} << quint64 {10368999999u} << false;
    QTest::newRow ("exact threshold")
      << quint64 {10368000000u} << quint64 {10369000000u} << false;
    QTest::newRow ("above threshold")
      << quint64 {10368000000u} << quint64 {10369000001u} << true;
    QTest::newRow ("above threshold reversed")
      << quint64 {10369000001u} << quint64 {10368000000u} << true;
    QTest::newRow ("signed boundary adjacent")
      << quint64 {2147483647u} << quint64 {2147483648u} << false;
    QTest::newRow ("upper supported range")
      << quint64 {250000000000u} << quint64 {249998999999u} << true;
    QTest::newRow ("quint64 upper edge")
      << (std::numeric_limits<quint64>::max) ()
      << (std::numeric_limits<quint64>::max) () - quint64 {1000001u} << true;
  }

  void comparesFrequencyDifference ()
  {
    QFETCH (quint64, lhs);
    QFETCH (quint64, rhs);
    QFETCH (bool, expected);

    QCOMPARE (TciProtocol::frequency_difference_exceeds (lhs, rhs, quint64 {1000000u}), expected);
  }
};

QTEST_MAIN (TestTciProtocol)

#include "test_tci_protocol.moc"