File: ocd_t.cpp

package info (click to toggle)
openorienteering-mapper 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 67,132 kB
  • sloc: cpp: 114,710; ansic: 1,455; sh: 430; java: 240; xml: 140; sed: 64; makefile: 28
file content (256 lines) | stat: -rw-r--r-- 8,248 bytes parent folder | download | duplicates (4)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *    Copyright 2018-2019 Kai Pastor
 *
 *    This file is part of OpenOrienteering.
 *
 *    OpenOrienteering is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OpenOrienteering is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <numeric>
#include <stdexcept>
#include <type_traits>

#include <QtGlobal>
#include <QtTest>
#include <QByteArray>
#include <QChar>
#include <QObject>
#include <QString>

#include "fileformats/ocd_types.h"
#include "fileformats/ocd_types_v8.h"
#include "fileformats/ocd_types_v9.h"



/**
 * @test Unit test for the generic Ocd format support
 * 
 * The tests of the various string implementations use a string length of four
 * elements. Test data and test implementation are designed to ensure that
 * - assignments of shorter, max length, and more than max length characters
 *   are covered,
 * - unused storage is filled with zero (for privacy)
 * - data following the string storage is not accidentally overwritten.
 */
class OcdTest : public QObject
{
Q_OBJECT
	
private slots:
	void initTestCase()
	{
		// nothing yet
	}
	
	
	/**
	 * Tests basic properties of OCD V8 icon compression.
	 */
	void compressedOcdIconTest()
	{
		using std::begin;
		using std::end;
		
		{
			Ocd::IconV9 icon;
			
			// White background
			std::fill(begin(icon.bits), end(icon.bits), 124);
			QCOMPARE(icon.compress().uncompress(), icon);
			
			// Black bar on white background
			std::fill(begin(icon.bits)+44, begin(icon.bits)+88, 0);
			QCOMPARE(icon.compress().uncompress(), icon);
			
			// 250 non-repeating bytes of input, followed by white
			// => cannot compress to less than 256 bytes
			std::iota(begin(icon.bits), begin(icon.bits)+125, 0);
			std::transform(begin(icon.bits), begin(icon.bits)+63, begin(icon.bits)+125, [](auto value) {
				return value * 2;
			});
			std::transform(begin(icon.bits), begin(icon.bits)+62, begin(icon.bits)+188, [](auto value) {
				return value * 2 + 1;
			});
			QVERIFY_EXCEPTION_THROWN(icon.compress(), std::length_error);
			
			// 197 non-repeating bytes of input, followed by white
			// => cannot compress to less than 256 bytes
			std::fill(begin(icon.bits)+197, end(icon.bits), 124);
			QVERIFY_EXCEPTION_THROWN(icon.compress(), std::length_error);
			
			// 196 non-repeating bytes of input, followed by white
			std::fill(begin(icon.bits)+196, end(icon.bits), 124);
			QCOMPARE(icon.compress().uncompress(), icon);
			
			QCOMPARE(int(*std::max_element(begin(icon.bits), end(icon.bits))), 124);
		}		
		{
			Ocd::IconV8 icon {};
			QVERIFY_EXCEPTION_THROWN(icon.uncompress(), std::domain_error);
		}
	}
	
	
	
	/*
	 * Ocd::PascalString<N> represents a string of N bytes, preceded by a byte
	 * giving the length. So a trailing zero is not required.
	 */
	void pascalStringTest_data()
	{
		QTest::addColumn<QByteArray>("input");
		QTest::addColumn<QByteArray>("expected");
		
		QTest::newRow("1")         << QByteArray("1")         << QByteArray("1");
		QTest::newRow("123")       << QByteArray("123")       << QByteArray("123");
		QTest::newRow("1234")      << QByteArray("1234")      << QByteArray("1234");
		QTest::newRow("123456789") << QByteArray("123456789") << QByteArray("1234");
		QTest::newRow("257x A")    << QByteArray(257, 'A')    << QByteArray(4, 'A');
	}
	
	/**
	 * Tests the legacy pascal string implementation, Ocd::PascalString<N>.
	 */
	void pascalStringTest()
	{
		struct {
			Ocd::PascalString<4> string;
			char trailing[4];
		} t;
		
		auto* const expected_trailing = "xyz";
		qstrncpy(t.trailing, expected_trailing, 4);
		
		QFETCH(QByteArray, input);
		QFETCH(QByteArray, expected);
		
		t.string = input;
		QCOMPARE(t.string.length, static_cast<unsigned char>(expected.length()));
		QVERIFY(qstrncmp(t.string.data, expected.data(), t.string.length) == 0);
		
		using std::begin; using std::end;
		QVERIFY(std::all_of(begin(t.string.data)+t.string.length, end(t.string.data), [](auto c){ return c == 0; }));
		
		QCOMPARE(t.trailing, expected_trailing);
	}
	
	
	
	void Utf8PascalStringTest_data()
	{
		QTest::addColumn<QByteArray>("input");
		QTest::addColumn<QByteArray>("expected");
		
		QTest::newRow("1")         << QByteArray("1")         << QByteArray("1");
		QTest::newRow("123")       << QByteArray("123")       << QByteArray("123");
		QTest::newRow("1234")      << QByteArray("1234")      << QByteArray("1234");
		QTest::newRow("123456789") << QByteArray("123456789") << QByteArray("1234");
		QTest::newRow("257x A")    << QByteArray(257, 'A')    << QByteArray(4, 'A');
		
		// trailing two-byte character
		QTest::newRow("123Cent")   << QByteArray("123¢")      << QByteArray("123");
		QTest::newRow("12Cent")    << QByteArray("12¢")       << QByteArray("12¢");
		
		// trailing three-byte character
		QTest::newRow("123Euro")   << QByteArray("123€")      << QByteArray("123");
		QTest::newRow("12Euro")    << QByteArray("12€")       << QByteArray("12");
		QTest::newRow("1Euro")     << QByteArray("1€")        << QByteArray("1€");
		
		// trailing four-byte character
		QTest::newRow("123Hwair")  << QByteArray("123𐍈")      << QByteArray("123");
		QTest::newRow("12Hwair")   << QByteArray("12𐍈")       << QByteArray("12");
		QTest::newRow("1Hwair")    << QByteArray("1𐍈")        << QByteArray("1");
		QTest::newRow("Hwair")     << QByteArray("𐍈")         << QByteArray("𐍈");
	}
	
	/**
	 * Tests the UTF-8 pascal string implementation, Ocd::Utf8PascalString<N>.
	 */
	void Utf8PascalStringTest()
	{
		struct {
			Ocd::Utf8PascalString<4> string;
			char trailing[4];
		} t;
		
		auto* const expected_trailing = "xyz";
		qstrncpy(t.trailing, expected_trailing, 4);
		
		QFETCH(QByteArray, input);
		QFETCH(QByteArray, expected);
		
		t.string = QString::fromUtf8(input);
		QCOMPARE(t.string.length, static_cast<unsigned char>(expected.length()));
		QVERIFY(qstrncmp(t.string.data, expected.data(), t.string.length) == 0);
		
		using std::begin; using std::end;
		QVERIFY(std::all_of(begin(t.string.data)+t.string.length, end(t.string.data), [](auto c){ return c == 0; }));
		
		QCOMPARE(t.trailing, expected_trailing);
	}
	
	
	
	void Utf16PascalStringTest_data()
	{
		QTest::addColumn<QString>("input");
		QTest::addColumn<QString>("expected");
		
		QTest::newRow("1")         << QString::fromUtf8("1")         << QString::fromUtf8("1");
		QTest::newRow("123")       << QString::fromUtf8("123")       << QString::fromUtf8("123");
		QTest::newRow("1234")      << QString::fromUtf8("1234")      << QString::fromUtf8("123");
		QTest::newRow("123456789") << QString::fromUtf8("123456789") << QString::fromUtf8("123");
		
		// trailing surrogate pair
		QTest::newRow("12Yee")     << QString::fromUtf8("12𐐷")       << QString::fromUtf8("12");
		QTest::newRow("1Yee")      << QString::fromUtf8("1𐐷")        << QString::fromUtf8("1𐐷");
	}
	
	/**
	 * Tests the UTF-16 pascal string implementation, Ocd::Utf16PascalString<N>.
	 */
	void Utf16PascalStringTest()
	{
		struct {
			Ocd::Utf16PascalString<4> string;
			char trailing[4];
		} t;
		
		auto* const expected_trailing = "xyz";
		qstrncpy(t.trailing, expected_trailing, 4);
		
		QFETCH(QString, input);
		QFETCH(QString, expected);
		
		Q_ASSERT(static_cast<std::size_t>(expected.length()) < std::extent<decltype(t.string.data)>::value);
		
		t.string = input;
		QCOMPARE(QString::fromRawData(t.string.data, expected.length()), expected);
		
		using std::begin; using std::end;
		QVERIFY(std::all_of(begin(t.string.data)+expected.length(), end(t.string.data), [](auto c){ return c == 0; }));
		
		QCOMPARE(t.trailing, expected_trailing);
	}
	
};  // class OcdTest



QTEST_APPLESS_MAIN(OcdTest)
#include "ocd_t.moc"  // IWYU pragma: keep