Skip to content

Commit

Permalink
TESTS: Add unit tests for ReadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Sep 3, 2016
1 parent 785b6e0 commit 75969e5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/common/readfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* 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 our file read stream.
*/

#include <cstdio>

#include "gtest/gtest.h"

#include "src/common/util.h"
#include "src/common/readfile.h"

static const char *kFilename = "tests/common/testfile_readfile.file";

GTEST_TEST(ReadFile, write) {
static const byte data[5] = { 0x12, 0x34, 0x56, 0x78, 0x90 };

// Create the input file

FILE *testFile = std::fopen(kFilename, "wb");
ASSERT_NE(testFile, static_cast<FILE *>(0));

const size_t writeCount = std::fwrite(data, 1, ARRAYSIZE(data), testFile);
EXPECT_EQ(writeCount, ARRAYSIZE(data));

const int flushSuccess = std::fflush(testFile);
ASSERT_EQ(flushSuccess, 0);

std::fclose(testFile);
testFile = 0;

// Read the file with our ReadFile class

Common::ReadFile file(kFilename);
ASSERT_TRUE(file.isOpen());

EXPECT_EQ(file.size(), ARRAYSIZE(data));

byte readData[ARRAYSIZE(data)];
const size_t readCount = file.read(readData, sizeof(readData));
EXPECT_EQ(readCount, ARRAYSIZE(readData));

file.close();
ASSERT_FALSE(file.isOpen());

// Compare

for (size_t i = 0; i < ARRAYSIZE(data); i++)
EXPECT_EQ(readData[i], data[i]) << "At index " << i;
}
6 changes: 6 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ check_PROGRAMS += tests/common/test_memwritestream
tests_common_test_memwritestream_SOURCES = tests/common/memwritestream.cpp
tests_common_test_memwritestream_LDADD = $(common_LIBS)
tests_common_test_memwritestream_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_readfile
tests_common_test_readfile_SOURCES = tests/common/readfile.cpp
tests_common_test_readfile_LDADD = $(common_LIBS)
tests_common_test_readfile_CXXFLAGS = $(test_CXXFLAGS)
CLEANFILES += tests/common/testfile_readfile.file

0 comments on commit 75969e5

Please sign in to comment.