-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TESTS: Add unit tests for ASCII encoding functions
- Loading branch information
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* xoreos - A reimplementation of BioWare's Aurora engine | ||
* | ||
* xoreos is the legal property of its developers, whose names | ||
* can be found in the AUTHORS file distributed with this source | ||
* distribution. | ||
* | ||
* xoreos 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. | ||
* | ||
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file | ||
* Common utility functions used by the encoding-related unit tests. | ||
*/ | ||
|
||
#ifndef TESTS_ENCODING_H | ||
#define TESTS_ENCODING_H | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
#include "src/common/encoding.h" | ||
|
||
#ifndef SKIP_RETURN_CODE | ||
#define SKIP_RETURN_CODE 77 | ||
#endif | ||
|
||
static const int kSkipTest = SKIP_RETURN_CODE; | ||
|
||
static void testSupport(Common::Encoding encoding) { | ||
if (Common::hasSupportEncoding(encoding)) | ||
return; | ||
|
||
std::fprintf(stderr, "No support to transcode UTF-8 <-> %s\n", Common::getEncodingName(encoding).c_str()); | ||
std::exit(kSkipTest); // Skip the test if we don't have relevant support | ||
} | ||
|
||
#endif // TESTS_ENCODING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* xoreos - A reimplementation of BioWare's Aurora engine | ||
* | ||
* xoreos is the legal property of its developers, whose names | ||
* can be found in the AUTHORS file distributed with this source | ||
* distribution. | ||
* | ||
* xoreos 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. | ||
* | ||
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file | ||
* Unit tests for ASCII encoding functions. | ||
*/ | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "src/common/error.h" | ||
#include "src/common/encoding.h" | ||
#include "src/common/memreadstream.h" | ||
#include "src/common/memwritestream.h" | ||
|
||
#include "tests/common/encoding.h" | ||
|
||
// The name and enum of the encoding we're testing | ||
#define XOREOS_ENCODINGNAME EncodingASCII | ||
static const Common::Encoding kEncoding = Common::kEncodingASCII; | ||
|
||
// -- General encoding feature tests, can't be generalized -- | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, getBytesPerCodepoint) { | ||
testSupport(kEncoding); | ||
|
||
EXPECT_EQ(Common::getBytesPerCodepoint(kEncoding), 1); | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, isValidCodePoint) { | ||
testSupport(kEncoding); | ||
|
||
EXPECT_TRUE(Common::isValidCodepoint(kEncoding, 0x20)); | ||
EXPECT_FALSE(Common::isValidCodepoint(kEncoding, 0x80)); | ||
} | ||
|
||
// -- Generalized encoding function tests -- | ||
|
||
// Example string with terminating 0 | ||
static const byte stringData0 [] = { 'F', 'o', 'o', 'b', 'a', 'r', '\0', }; | ||
// Example string with terminating 0 and garbage following | ||
static const byte stringData0X [] = { 'F', 'o', 'o', 'b', 'a', 'r', '\0', 'x' }; | ||
// Example string without terminating 0 and with garbage following | ||
static const byte stringDataX [] = { 'F', 'o', 'o', 'b', 'a', 'r', 'x' }; | ||
// Example string with line end and garbage following | ||
static const byte stringDataLineX[] = { 'F', 'o', 'o', 'b', 'a', 'r', '\r', '\n', 'x' }; | ||
|
||
// Number of bytes in the example string without terminating 0 and without garbage | ||
static const size_t stringBytes = 6; | ||
// Number of characters in the example string without terminating 0 and without garbage | ||
static const size_t stringChars = 6; | ||
|
||
// The example string encoded as UTF-8 | ||
static const Common::UString stringUString = Common::UString("Foobar"); | ||
|
||
// The actual tests live here | ||
#include "tests/common/encoding_tests.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* xoreos - A reimplementation of BioWare's Aurora engine | ||
* | ||
* xoreos is the legal property of its developers, whose names | ||
* can be found in the AUTHORS file distributed with this source | ||
* distribution. | ||
* | ||
* xoreos 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. | ||
* | ||
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file | ||
* Encoding tests, included by the various encoding_* files. | ||
*/ | ||
|
||
#ifndef TESTS_ENCODING_TESTS_H | ||
#define TESTS_ENCODING_TESTS_H | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, readString) { | ||
testSupport(kEncoding); | ||
|
||
Common::MemoryReadStream stream(stringData0X); | ||
|
||
const Common::UString string = Common::readString(stream, kEncoding); | ||
|
||
EXPECT_EQ(string.size(), stringChars); | ||
EXPECT_STREQ(string.c_str(), stringUString.c_str()); | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, readStringFixed) { | ||
testSupport(kEncoding); | ||
|
||
Common::MemoryReadStream stream(stringDataX); | ||
|
||
const Common::UString string = Common::readStringFixed(stream, kEncoding, stringBytes); | ||
|
||
EXPECT_EQ(string.size(), stringChars); | ||
EXPECT_STREQ(string.c_str(), stringUString.c_str()); | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, readStringLine) { | ||
testSupport(kEncoding); | ||
|
||
Common::MemoryReadStream stream(stringDataLineX); | ||
|
||
const Common::UString string = Common::readStringLine(stream, kEncoding); | ||
|
||
EXPECT_EQ(string.size(), stringChars); | ||
EXPECT_STREQ(string.c_str(), stringUString.c_str()); | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, readStringBuf) { | ||
testSupport(kEncoding); | ||
|
||
const Common::UString string = Common::readString(stringDataX, stringBytes, kEncoding); | ||
|
||
EXPECT_EQ(string.size(), stringChars); | ||
EXPECT_STREQ(string.c_str(), stringUString.c_str()); | ||
} | ||
|
||
static void compareData(Common::SeekableReadStream &stream, const byte *data, size_t n, size_t t) { | ||
for (size_t i = 0; i < n; i++) | ||
EXPECT_EQ(stream.readByte(), data[i]) << "At case " << t << ", index " << i; | ||
} | ||
|
||
static void compareData(const byte *data1, const byte *data2, size_t n, size_t t, size_t offset1 = 0) { | ||
for (size_t i = 0; i < n; i++) | ||
EXPECT_EQ(data1[offset1 + i], data2[i]) << "At case " << t << ", index " << i; | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, convertString) { | ||
testSupport(kEncoding); | ||
|
||
Common::MemoryReadStream *stream = 0; | ||
|
||
stream = convertString(stringUString, kEncoding, false); | ||
ASSERT_NE(stream, static_cast<Common::MemoryReadStream *>(0)); | ||
|
||
EXPECT_EQ(stream->size(), stringBytes); | ||
compareData(*stream, stringData0, stringBytes, 0); | ||
|
||
delete stream; | ||
|
||
stream = convertString(stringUString, kEncoding, true); | ||
ASSERT_NE(stream, static_cast<Common::MemoryReadStream *>(0)); | ||
|
||
EXPECT_EQ(stream->size(), sizeof(stringData0)); | ||
compareData(*stream, stringData0, sizeof(stringData0), 1); | ||
|
||
delete stream; | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, writeString) { | ||
testSupport(kEncoding); | ||
|
||
byte writeData[sizeof(stringData0) * 2] = { 0 }; | ||
Common::MemoryWriteStream stream(writeData); | ||
|
||
const size_t written1 = Common::writeString(stream, stringUString, kEncoding, false); | ||
ASSERT_EQ(written1, stringBytes); | ||
|
||
compareData(writeData, stringData0, stringBytes, 0); | ||
|
||
const size_t written2 = Common::writeString(stream, stringUString, kEncoding, true); | ||
ASSERT_EQ(written2, sizeof(stringData0)); | ||
|
||
compareData(writeData, stringData0, sizeof(stringData0), 1, written1); | ||
} | ||
|
||
GTEST_TEST(XOREOS_ENCODINGNAME, writeStringFixed) { | ||
testSupport(kEncoding); | ||
|
||
byte writeData[sizeof(stringData0) * 2] = { 0 }; | ||
Common::MemoryWriteStream stream(writeData); | ||
|
||
Common::writeStringFixed(stream, stringUString, kEncoding, stringBytes); | ||
|
||
compareData(writeData, stringData0, stringBytes, 0); | ||
|
||
Common::writeStringFixed(stream, stringUString, kEncoding, sizeof(stringData0)); | ||
|
||
compareData(writeData, stringData0, sizeof(stringData0), 1, stringBytes); | ||
} | ||
|
||
#endif // TESTS_ENCODING_TESTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters