1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtSerialBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include <QtTest/QtTest>
38#include <QtSerialBus/QModbusDataUnit>
39
40class tst_QModbusDataUnit : public QObject
41{
42 Q_OBJECT
43public:
44 explicit tst_QModbusDataUnit();
45
46private slots:
47 void constructors();
48 void setters();
49 void testAPI();
50};
51
52tst_QModbusDataUnit::tst_QModbusDataUnit()
53{
54}
55
56void tst_QModbusDataUnit::constructors()
57{
58 QModbusDataUnit unit1(QModbusDataUnit::Coils);
59
60 QCOMPARE(unit1.registerType(), QModbusDataUnit::Coils);
61 QCOMPARE(unit1.startAddress(), 0);
62 QCOMPARE(unit1.valueCount(), 0u);
63 QVERIFY(unit1.values().isEmpty());
64
65 QModbusDataUnit coils(QModbusDataUnit::Coils, 15, 20);
66 QCOMPARE(coils.registerType(), QModbusDataUnit::Coils);
67 QCOMPARE(coils.startAddress(), 15);
68 const auto values = coils.values();
69 QCOMPARE(values.size(), 20);
70 QCOMPARE(values, QVector<quint16>(20));
71 for (auto val : values) {
72 QCOMPARE(val, quint16(0));
73 }
74 QCOMPARE(coils.valueCount(), 20u);
75
76 QModbusDataUnit unit2(QModbusDataUnit::HoldingRegisters, 3, QVector<quint16>() << 9);
77 QCOMPARE(unit2.registerType(), QModbusDataUnit::HoldingRegisters);
78 QCOMPARE(unit2.startAddress(), 3);
79 QCOMPARE(unit2.values().size(), 1);
80 QCOMPARE(unit2.values().at(0), (quint16) 9);
81 QCOMPARE(unit2.valueCount(), 1u);
82
83 QVector<quint16> data;
84 data.append(t: 5);
85 data.append(t: 6);
86 data.append(t: 7);
87
88 QModbusDataUnit unit3(QModbusDataUnit::InputRegisters, 2, data);
89 QCOMPARE(unit3.registerType(), QModbusDataUnit::InputRegisters);
90 QCOMPARE(unit3.startAddress(), 2);
91 QCOMPARE(unit3.values().size(), 3);
92 QCOMPARE(unit3.values().at(0), (quint16) 5);
93 QCOMPARE(unit3.values().at(1), (quint16) 6);
94 QCOMPARE(unit3.values().at(2), (quint16) 7);
95 QCOMPARE(unit3.valueCount(), 3u);
96}
97
98void tst_QModbusDataUnit::setters()
99{
100 QModbusDataUnit unit(QModbusDataUnit::HoldingRegisters, 3, QVector<quint16>() << 9);
101 QCOMPARE(unit.valueCount(), 1u);
102
103 unit.setRegisterType(QModbusDataUnit::InputRegisters);
104 unit.setStartAddress(2);
105 QVector<quint16> data;
106 data.append(t: 9u);
107 data.append(t: 5u);
108 unit.setValues(data);
109
110 QCOMPARE(unit.registerType(), QModbusDataUnit::InputRegisters);
111 QCOMPARE(unit.startAddress(), 2);
112 QCOMPARE(unit.valueCount(), 2u);
113 QCOMPARE(unit.values().size(), 2);
114 QCOMPARE(unit.values().at(0), (quint16) 9);
115 QCOMPARE(unit.values().at(1), (quint16) 5);
116
117 //valueCount can be adjusted but values() stays the same
118 unit.setValueCount(1);
119 QCOMPARE(unit.valueCount(), 1u);
120 QCOMPARE(unit.values().size(), 2);
121 QCOMPARE(unit.values().at(0), (quint16) 9);
122 QCOMPARE(unit.values().at(1), (quint16) 5);
123}
124
125void tst_QModbusDataUnit::testAPI()
126{
127 {
128 QModbusDataUnit unit;
129 QCOMPARE(unit.isValid(), false);
130 unit.setStartAddress(15);
131 QCOMPARE(unit.isValid(), false);
132 unit.setRegisterType(QModbusDataUnit::Coils);
133 QCOMPARE(unit.isValid(), true);
134 }
135
136 {
137 QModbusDataUnit unit;
138 QCOMPARE(unit.isValid(), false);
139 unit.setRegisterType(QModbusDataUnit::Coils);
140 QCOMPARE(unit.isValid(), false);
141 unit.setStartAddress(15);
142 QCOMPARE(unit.isValid(), true);
143 }
144
145 QModbusDataUnit unit;
146 QCOMPARE(unit.isValid(), false);
147 QCOMPARE(unit.registerType(), QModbusDataUnit::Invalid);
148 QCOMPARE(unit.startAddress(), -1);
149 QCOMPARE(unit.valueCount(), 0u);
150 QCOMPARE(unit.values(), QVector<quint16>());
151
152 unit.setStartAddress(15);
153 unit.setRegisterType(QModbusDataUnit::Coils);
154 QCOMPARE(unit.isValid(), true);
155
156 QCOMPARE(unit.values(), QVector<quint16>());
157 unit.setValues(QVector<quint16> {1, 2, 3, 4});
158 QCOMPARE(unit.values(), QVector<quint16>({1, 2, 3, 4}));
159
160 QCOMPARE(unit.valueCount(), 4u);
161 unit.setValueCount(25);
162 QCOMPARE(unit.valueCount(), 25u);
163
164 QCOMPARE(unit.value(0), quint16(1));
165 unit.setValue(index: 0, newValue: 25);
166 QCOMPARE(unit.value(0), quint16(25));
167}
168
169QTEST_MAIN(tst_QModbusDataUnit)
170
171#include "tst_qmodbusdataunit.moc"
172

source code of qtserialbus/tests/auto/qmodbusdataunit/tst_qmodbusdataunit.cpp