From b34b1a70fa79cf806ffd3d1a70ecb575a9a0de9c Mon Sep 17 00:00:00 2001 From: Daniel Buckmaster Date: Tue, 29 Jul 2014 09:16:11 +1000 Subject: [PATCH] Port file test. --- .../platform/test/platformFileIOTest.cpp | 118 ++++++++++++++ Engine/source/platform/test/testFile.cpp | 150 ------------------ 2 files changed, 118 insertions(+), 150 deletions(-) create mode 100644 Engine/source/platform/test/platformFileIOTest.cpp delete mode 100644 Engine/source/platform/test/testFile.cpp diff --git a/Engine/source/platform/test/platformFileIOTest.cpp b/Engine/source/platform/test/platformFileIOTest.cpp new file mode 100644 index 000000000..c27278431 --- /dev/null +++ b/Engine/source/platform/test/platformFileIOTest.cpp @@ -0,0 +1,118 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2012 GarageGames, LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +//----------------------------------------------------------------------------- + +#ifdef TORQUE_TESTS_ENABLED +#include "testing/unitTesting.h" +#include "platform/platform.h" +#include "core/fileio.h" +#include "core/util/tVector.h" +#include "console/console.h" + +TEST(Platform, ExcludedDirectories) +{ + // Just dump everything under the current directory. We should + // find at least one file. + + // Exclude .svn and CVS + Platform::clearExcludedDirectories(); + Platform::addExcludedDirectory(".svn"); + Platform::addExcludedDirectory("CVS"); + + EXPECT_TRUE(Platform::isExcludedDirectory(".svn")) + << "On list, should be excluded."; + EXPECT_TRUE(Platform::isExcludedDirectory("CVS")) + << "On list, should be excluded."; + EXPECT_FALSE(Platform::isExcludedDirectory("foo")) + << "Doesn't match list, shouldn't be excluded."; + EXPECT_FALSE(Platform::isExcludedDirectory(".svnCVS")) + << "Looks like a duck, but it shouldn't be excluded cuz it's distinct from all entries on the exclusion list."; + + // Ok, now our exclusion list is setup, so let's dump some paths. + Vector pathInfo; + Platform::dumpPath(Platform::getCurrentDirectory(), pathInfo, 2); + EXPECT_GT(pathInfo.size(), 0) + << "Should find at least SOMETHING in the current directory!"; + + // This'll nuke info if we run it in a live situation... so don't run unit + // tests in a live situation. ;) + Platform::clearExcludedDirectories(); +}; + +TEST(File, TouchAndTime) +{ + FileTime create[2], modify[2]; + + // Create a file and sleep for a second. + File f; + f.open("testTouch.file", File::WriteAppend); + f.close(); + + // Touch a file and note its last-modified. + dFileTouch("testTouch.file"); + EXPECT_TRUE(Platform::isFile("testTouch.file")) + << "We just touched this file - it should exist."; + EXPECT_TRUE(Platform::getFileTimes("testTouch.file", &create[0], &modify[0])) + << "Failed to get filetimes for a file we just created."; + + // Sleep for a tick + Platform::sleep(10); + + // Touch it again, and compare the last-modifieds. + EXPECT_TRUE(Platform::isFile("testTouch.file")) + << "We just touched this file - it should exist."; + dFileTouch("testTouch.file"); + EXPECT_TRUE(Platform::isFile("testTouch.file")) + << "We just touched this file - it should exist."; + EXPECT_TRUE(Platform::getFileTimes("testTouch.file", &create[1], &modify[1])) + << "Failed to get filetimes for a file we just created."; + + // Now compare the times... + EXPECT_LT(Platform::compareFileTimes(modify[0], modify[1]), 0) + << "Timestamps are wrong - modify[0] should be before modify[1]!"; + EXPECT_EQ(Platform::compareFileTimes(create[0], create[1]), 0) + << "Create timestamps should match - we didn't delete the file during this test."; + + // Clean up.. + dFileDelete("testTouch.file"); + EXPECT_FALSE(Platform::isFile("testTouch.file")) + << "Somehow failed to delete our test file."; +}; + +// Mac has no implementations for these functions, so we 'def it out for now. +#ifndef __MACOSX__ +TEST(Platform, Volumes) +{ + Vector names; + Platform::getVolumeNamesList(names); + + EXPECT_GT(names.size(), 0) + << "We should have at least one volume..."; + + Vector info; + Platform::getVolumeInformationList(info); + + EXPECT_EQ(names.size(), info.size()) + << "Got inconsistent number of volumes back from info vs. name list functions!"; +}; +#endif + +#endif diff --git a/Engine/source/platform/test/testFile.cpp b/Engine/source/platform/test/testFile.cpp deleted file mode 100644 index 667f9fbee..000000000 --- a/Engine/source/platform/test/testFile.cpp +++ /dev/null @@ -1,150 +0,0 @@ -//----------------------------------------------------------------------------- -// Copyright (c) 2012 GarageGames, LLC -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. -//----------------------------------------------------------------------------- - -#include "platform/platform.h" -#include "core/fileio.h" -#include "unit/test.h" -#include "core/util/tVector.h" -#include "console/console.h" - -using namespace UnitTesting; - -CreateUnitTest(CheckFileListingAndExclusion, "File/ListDirectoryAndExclusions") -{ - void run() - { - // Just dump everything under the current directory. We should - // find at least one file. - - // Exclude .svn and CVS - Platform::clearExcludedDirectories(); - Platform::addExcludedDirectory(".svn"); - Platform::addExcludedDirectory("CVS"); - - test(Platform::isExcludedDirectory("foo") == false, "Doesn't match list, shouldn't be excluded."); - test(Platform::isExcludedDirectory(".svn") == true, "On list, should be excluded."); - test(Platform::isExcludedDirectory("CVS") == true, "On list, should be excluded."); - test(Platform::isExcludedDirectory(".svnCVS") == false, "Looks like a duck, but it shouldn't be excluded cuz it's distinct from all entries on the exclusion list."); - - // Ok, now our exclusion list is setup, so let's dump some paths. - Vector < Platform::FileInfo > pathInfo; - Platform::dumpPath (Platform::getCurrentDirectory(), pathInfo, 2); - - Con::printf("Dump of files in '%s', up to 2 levels deep...", Platform::getCurrentDirectory()); - for(S32 i=0; i 0, "Should find at least SOMETHING in the current directory!"); - - // This'll nuke info if we run it in a live situation... so don't run unit - // tests in a live situation. ;) - Platform::clearExcludedDirectories(); - } -}; - -CreateUnitTest(CheckFileTouchAndTime, "File/TouchAndTime") -{ - void run() - { - FileTime create[2], modify[2]; - - // Create a file and sleep for a second. - File f; - f.open("testTouch.file", File::WriteAppend); - f.close(); - - Platform::sleep(2000); - - // Touch a file and note its last-modified. - dFileTouch("testTouch.file"); - test(Platform::isFile("testTouch.file"), "We just touched this file - it should exist."); - test(Platform::getFileTimes("testTouch.file", &create[0], &modify[0]), "Failed to get filetimes for a file we just created."); - - // Sleep for a few seconds... - Platform::sleep(5000); - - // Touch it again, and compare the last-modifieds. - test(Platform::isFile("testTouch.file"), "We just touched this file - it should exist."); - dFileTouch("testTouch.file"); - test(Platform::isFile("testTouch.file"), "We just touched this file - it should exist."); - test(Platform::getFileTimes("testTouch.file", &create[1], &modify[1]), "Failed to get filetimes for a file we just created."); - - // Now compare the times... - test(Platform::compareFileTimes(modify[0], modify[1]) < 0, "Timestamps are wrong - modify[0] should be before modify[1]!"); - - // This seems to fail even on a valid case... - // test(Platform::compareFileTimes(create[0], create[1]) == 0, "Create timestamps should match - we didn't delete the file during this test."); - - // Clean up.. - dFileDelete("testTouch.file"); - test(!Platform::isFile("testTouch.file"), "Somehow failed to delete our test file."); - } -}; - -// Mac has no implementations for these functions, so we 'def it out for now. -#if 0 -CreateUnitTest(CheckVolumes, "File/Volumes") -{ - void run() - { - Con::printf("Dumping volumes by name:"); - - Vector names; - Platform::getVolumeNamesList(names); - - test(names.size() > 0, "We should have at least one volume..."); - - for(S32 i=0; i info; - Platform::getVolumeInformationList(info); - - test(names.size() == info.size(), "Got inconsistent number of volumes back from info vs. name list functions!"); - - for(S32 i=0; i