#include "../filesystem/filesystem.hpp" #include "../filesystem/path.hpp" #include <gtest/gtest.h> TEST(FilesystemTest, FilesWork) { sprawl::filesystem::File f = sprawl::filesystem::Open("./test.txt", "w"); f.Write("Hi there!"); f.Flush(); f.Sync(); EXPECT_EQ(9, f.Tell()); EXPECT_EQ(9, f.FileSize()); EXPECT_FALSE(f.IsClosed()); f.Seek(5, sprawl::filesystem::RelativeTo::Beginning); EXPECT_EQ(5, f.Tell()); EXPECT_EQ(9, f.FileSize()); f.Seek(2, sprawl::filesystem::RelativeTo::CurrentPosition); EXPECT_EQ(7, f.Tell()); EXPECT_EQ(9, f.FileSize()); f.Seek(-3, sprawl::filesystem::RelativeTo::End); EXPECT_EQ(6, f.Tell()); EXPECT_EQ(9, f.FileSize()); EXPECT_EQ(sprawl::String("w"), f.Mode()); f.Close(); EXPECT_TRUE(f.IsClosed()); f = sprawl::filesystem::Open("./test.txt", "r+"); EXPECT_FALSE(f.IsClosed()); EXPECT_FALSE(f.IsATTY()); EXPECT_EQ(9, f.FileSize()); EXPECT_EQ(sprawl::String("r+"), f.Mode()); sprawl::String str = f.Read(2); EXPECT_EQ(sprawl::String("Hi"), str); f.Seek(0, sprawl::filesystem::RelativeTo::Beginning); str = f.Read(); EXPECT_EQ(sprawl::String("Hi there!"), str); f.Truncate(2); EXPECT_EQ(2, f.Tell()); EXPECT_EQ(2, f.FileSize()); f.Seek(0, sprawl::filesystem::RelativeTo::Beginning); str = f.Read(); EXPECT_EQ(sprawl::String("Hi"), str); f.Close(); f = sprawl::filesystem::Open("./test.txt", "r+"); EXPECT_EQ(2, f.FileSize()); str = f.Read(); EXPECT_EQ(sprawl::String("Hi"), str); f.Close(); f = sprawl::filesystem::Open("./test.txt", "w"); f.Write("Hello"); f.Write(sprawl::filesystem::LineSeparator()); f.Write("World"); f.Close(); f = sprawl::filesystem::Open("./test.txt", "r"); sprawl::String hello = f.ReadLine(); sprawl::String world = f.ReadLine(); EXPECT_EQ(sprawl::String("Hello{}").format(sprawl::filesystem::LineSeparator()), hello); EXPECT_EQ(sprawl::String("World"), world); f.Close(); EXPECT_TRUE(sprawl::filesystem::Remove("test.txt")) << strerror(errno); } TEST(FilesystemTest, EnvironmentWorks) { sprawl::filesystem::PutEnv("SprawlUnitTesting", "true"); sprawl::String str1 = sprawl::filesystem::GetEnv("SprawlUnitTesting", "false"); EXPECT_EQ(sprawl::String("true"), str1); sprawl::filesystem::UnsetEnv("SprawlUnitTesting"); sprawl::String str2 = sprawl::filesystem::GetEnv("SprawlUnitTesting", "false"); EXPECT_EQ(sprawl::String("false"), str2); sprawl::filesystem::PutEnv("SprawlUnitTesting", ""); sprawl::String str3 = sprawl::filesystem::GetEnv("SprawlUnitTesting", "false"); EXPECT_EQ(sprawl::String("false"), str3); } TEST(FilesystemTest, MkDirAndRmDirWork) { sprawl::String path1 = "testdir"; ASSERT_TRUE(sprawl::filesystem::MkDir(path1)) << strerror(errno); ASSERT_TRUE(sprawl::path::Exists(path1)) << strerror(errno); ASSERT_TRUE(sprawl::path::IsDirectory(path1)) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::RmDir(path1)) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists(path1)); } TEST(FilesystemTest, MakeDirsAndRmTreeWork) { sprawl::String path2 = "testdir2/nested/directory/structure"; ASSERT_TRUE(sprawl::filesystem::MakeDirs(path2)) << strerror(errno); ASSERT_TRUE(sprawl::path::Exists(path2)) << strerror(errno); ASSERT_TRUE(sprawl::path::IsDirectory(path2)) << strerror(errno); EXPECT_FALSE(sprawl::filesystem::RmDir("testdir2")); ASSERT_TRUE(sprawl::path::Exists(path2)) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::RmTree("testdir2")) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists(path2)); EXPECT_FALSE(sprawl::path::Exists("testdir")); ASSERT_TRUE(sprawl::path::Exists(".")) << strerror(errno); } TEST(FilesystemTest, RemoveWorks) { sprawl::filesystem::File f = sprawl::filesystem::Open("test.txt", "w"); f.Write("Hello, world!"); f.Close(); ASSERT_TRUE(sprawl::path::Exists("test.txt")) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::Remove("test.txt")) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists("test.txt")); } TEST(FilesystemTest, RemoveDirsWorks) { sprawl::String path2 = "testdir2/nested/directory/structure"; ASSERT_TRUE(sprawl::filesystem::MakeDirs(path2)) << strerror(errno); ASSERT_TRUE(sprawl::path::Exists(path2)) << strerror(errno); ASSERT_TRUE(sprawl::path::IsDirectory(path2)) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::RemoveDirs(path2)) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists(path2)); EXPECT_FALSE(sprawl::path::Exists("testdir2")); ASSERT_TRUE(sprawl::path::Exists(".")) << strerror(errno); } TEST(FilesystemTest, RmTreeWorksWithFilesInTree) { sprawl::String path2 = "testdir2/nested/directory/structure"; ASSERT_TRUE(sprawl::filesystem::MakeDirs(path2)) << strerror(errno); ASSERT_TRUE(sprawl::path::Exists(path2)) << strerror(errno); ASSERT_TRUE(sprawl::path::IsDirectory(path2)) << strerror(errno); sprawl::filesystem::File f = sprawl::filesystem::Open(sprawl::path::Join(path2, "test.txt"), "w"); f.Write("Hello, world!"); f.Close(); ASSERT_TRUE(sprawl::filesystem::RmTree("testdir2")) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists(path2)); EXPECT_FALSE(sprawl::path::Exists("testdir")); ASSERT_TRUE(sprawl::path::Exists(".")) << strerror(errno); } TEST(FilesystemTest, RenameWorks) { sprawl::filesystem::File f = sprawl::filesystem::Open("test.txt", "w"); f.Write("Hello, world!"); f.Close(); ASSERT_TRUE(sprawl::path::Exists("test.txt")) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::Rename("test.txt", "test2.txt")) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists("test.txt")); ASSERT_TRUE(sprawl::path::Exists("test2.txt")) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::Remove("test2.txt")) << strerror(errno); } TEST(FilesystemTest, RenamesWorks) { sprawl::String path2 = "testdir2/nested/directory/structure"; sprawl::filesystem::File f = sprawl::filesystem::Open("test.txt", "w"); f.Write("Hello, world!"); f.Close(); ASSERT_TRUE(sprawl::path::Exists("test.txt")) << strerror(errno); sprawl::String newName = sprawl::path::Join(path2, "test2.txt"); ASSERT_TRUE(sprawl::filesystem::Renames("test.txt", newName)) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists("test.txt")); ASSERT_TRUE(sprawl::path::Exists(newName)) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::RmTree("testdir2")) << strerror(errno); } TEST(FilesystemTest, FileSymlinkWorks) { sprawl::filesystem::File f = sprawl::filesystem::Open("test.txt", "w"); f.Write("Hello, world!"); f.Close(); ASSERT_TRUE(sprawl::path::Exists("test.txt")); ASSERT_TRUE(sprawl::filesystem::MakeSymlink("test.txt", "test2.txt")) << strerror(errno); ASSERT_TRUE(sprawl::path::Exists("test2.txt")); EXPECT_TRUE(sprawl::path::IsLink("test2.txt")); EXPECT_TRUE(sprawl::path::IsFile("test2.txt")); f = sprawl::filesystem::Open("test2.txt", "r"); EXPECT_EQ(sprawl::String("Hello, world!"), f.Read()); f.Close(); ASSERT_TRUE(sprawl::filesystem::Remove("test.txt")) << strerror(errno); ASSERT_TRUE(sprawl::filesystem::Remove("test2.txt")) << strerror(errno); } TEST(FilesystemTest, DirectorySymlinkWorks) { sprawl::filesystem::MkDir("test"); sprawl::String file1 = "test/test.txt"; sprawl::String file2 = "test2/test.txt"; sprawl::filesystem::File f = sprawl::filesystem::Open(file1, "w"); f.Write("Hello, world!"); f.Close(); ASSERT_TRUE(sprawl::path::Exists(file1)); ASSERT_TRUE(sprawl::filesystem::MakeSymlink("test", "test2")) << strerror(errno); ASSERT_TRUE(sprawl::path::Exists(file2)); EXPECT_FALSE(sprawl::path::IsLink(file2)); EXPECT_TRUE(sprawl::path::IsFile(file2)); EXPECT_TRUE(sprawl::path::IsLink("test2")); EXPECT_TRUE(sprawl::path::IsDirectory("test2")); f = sprawl::filesystem::Open(file2, "r"); EXPECT_EQ(sprawl::String("Hello, world!"), f.Read()); f.Close(); EXPECT_TRUE(sprawl::filesystem::Remove(file2)) << strerror(errno); EXPECT_FALSE(sprawl::path::Exists(file1)); EXPECT_FALSE(sprawl::filesystem::RmDir("test2")); EXPECT_TRUE(sprawl::filesystem::Remove("test2")); EXPECT_FALSE(sprawl::path::Exists("test2")); EXPECT_TRUE(sprawl::path::Exists("test")); EXPECT_TRUE(sprawl::filesystem::RmDir("test")); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#5 | 16052 | ShadauxCat |
- Changed default block size for concurrent queue to a more reasonable value - Changed some memory orders to memory_order_seq_cst when they don't actually need to be that to get around a bug in visual studio 2013 - debug builds assert when memory_order_acq_rel is used for a compare_exchange_strong (this is a standard library bug and is fixed in VS2015) - Added Event API - events are an alternative to condition variables that do not require a mutex and are guaranteed not to miss any signals, even if the signal comes while the thread is not listening for it. Unlike condition variables, however, they do not support broadcasting (and in fact, in general, are not safe to use with multiple threads listening for the same event simultaneously - though notifying on the same event is fine) - Rewrote ThreadManager around ConcurrentQueue and Event API so it is now lock-free. Also improved some behaviors of the staged thread manager operation so it now supports tasks that can be run on multiple stages via a bitmask. - Fixed an issue where the Coroutine copy constructor was calling the std::function constructor instead and another where initializing with a stack might try to call the wrong constructor and vice-versa - Fixed Coroutine never calling munmap() on its stack in linux and causing a memory leak - Added default arguments to time functions - Attempted to fix some issues with BinaryTree. Fixed some but not all. It's currently not suitable for use, sadly. - Logging Improvements: - - Added thread ID to logging - - Fixed some issues with category handlers - - Added backtraces - - Added the following additional log macros: - - - LOG_IF - - - LOG_EVERY_N - - - LOG_FIRST_N - - - LOG_IF_EVERY_N - - - LOG_IF_FIRST_N - - - LOG_ASSERT - - Added the ability to set extra info callbacks to get data such as script backtraces - - Removed the thread-related handlers and replaced them with RunHandler_Threaded and RunHandler_ThreadManager, which will enable any passed-in handler to be run in a threaded fashion - Removed StaticPoolAllocator and renamed DynamicPoolAllocator to PoolAllocator; adjusted unit tests accordingly - PoolAllocator now allocates its pool with mmap and VirtualAlloc, rather than with malloc - Fixed a bug with Vector copy assignment operator - Improved performance of StringBuilder considerably for cases where there are no modifier strings - Removed Copy-On-Write behavior of JSONToken as it was broken; copies are now performed with explicit DeepCopy() and ShallowCopy() functions - Fixed some parser bugs with JSONToken - Added iteration to JSONToken to iterate its children - Fixed crash when reading a negative number of bytes from a file - Changed StringBuilder to favor speed instead of memory by default - Added some performance unit tests for JSON token #review-16053 |
||
#4 | 14822 | ShadauxCat |
Last batch of filesystem code for now, added MakeSymlink and GetPid, removed other todo functions for the time being. Also fixed some bugs: -Linux IsLink() implementation not using lstat = broken -File::IsClosed() would crash if file were created via default constructor or null handle -Remove() and RmDir() on Windows were inconsistent with Linux - in Linux all symlinks are removed with Remove() even if they point to directories. Forced windows to work the same way. -Asked RmTree to please not descend into symbolic links to directories, but just to remove them, thanks. -Removed starting \\?\ from result of RealPath() on Windows -Fixed IsFile() on Windows just not working - assuming anything that's not a directory is a file now. -Fixed StringBuilder only printing the first character if you passed it type char* instead of type char const* #review-14823 |
||
#3 | 14816 | ShadauxCat |
Filled in some more filesystem functions, added appropriate unit tests. Only a few remain. #review-14817 |
||
#2 | 14785 | ShadauxCat |
-Fixed search/replace mishap in const movement -Fixed a couple of missing windows headers -Adjusted PutEnv() so that Linux and Windows have matching functionality - empty string is equivalent to UnsetEnv. #review-14786 |
||
#1 | 14781 | ShadauxCat |
-Finished path library -Added some more functionality to filesystem library -Added a few more unit tests, still more needed for path #review-14782 |
||
//guest/ShadauxCat/Sprawl/Mainline/UnitTests/UnitTests_File.cpp | |||||
#1 | 14761 | ShadauxCat |
First drop of code for sprawl::filesystem and sprawl::path. Library will continue to grow. Also fixed a warning on linux. #review-14762 |