|
| 1 | +#include "TestFilesystemFunc.h" |
| 2 | +#include "filesystemfunc.h" |
| 3 | +#include "gtesthelper.h" |
| 4 | + |
| 5 | +using namespace filesystem; |
| 6 | + |
| 7 | +namespace filesystem { namespace test |
| 8 | +{ |
| 9 | + bool createDummyFile(const char * iPath) |
| 10 | + { |
| 11 | + FILE * f = fopen(iPath, "w"); |
| 12 | + if (f == NULL) |
| 13 | + return false; |
| 14 | + fputs("FOO!\n", f); |
| 15 | + fputs("&\n", f); |
| 16 | + fputs("BAR\n", f); |
| 17 | + fclose(f); |
| 18 | + |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + int countValues(const std::vector<std::string> & iList, const std::string & iValue) |
| 23 | + { |
| 24 | + int count = 0; |
| 25 | + for(size_t i=0; i<iList.size(); i++) |
| 26 | + { |
| 27 | + const std::string & value = iList[i]; |
| 28 | + if (value == iValue) |
| 29 | + count++; |
| 30 | + } |
| 31 | + return count; |
| 32 | + } |
| 33 | + |
| 34 | + //-------------------------------------------------------------------------------------------------- |
| 35 | + void TestFilesystemFunc::SetUp() |
| 36 | + { |
| 37 | + } |
| 38 | + //-------------------------------------------------------------------------------------------------- |
| 39 | + void TestFilesystemFunc::TearDown() |
| 40 | + { |
| 41 | + } |
| 42 | + //-------------------------------------------------------------------------------------------------- |
| 43 | + TEST_F(TestFilesystemFunc, testGetFileSize) |
| 44 | + { |
| 45 | + //test NULL |
| 46 | + { |
| 47 | + const char * path = NULL; |
| 48 | + uint32_t size = filesystem::getFileSize(path); |
| 49 | + ASSERT_EQ(0, size); |
| 50 | + } |
| 51 | + |
| 52 | + //test actual value |
| 53 | + { |
| 54 | + //create dummy file |
| 55 | + std::string filename = gTestHelper::getInstance().getTestQualifiedName(); |
| 56 | + ASSERT_TRUE( createDummyFile(filename.c_str()) ); |
| 57 | + |
| 58 | +#ifdef WIN32 |
| 59 | + static const uint32_t EXPECTED = 14; |
| 60 | +#elif UNIX |
| 61 | + static const uint32_t EXPECTED = 11; |
| 62 | +#endif |
| 63 | + |
| 64 | + //test `const char *` api |
| 65 | + uint32_t size = filesystem::getFileSize(filename.c_str()); |
| 66 | + ASSERT_EQ(EXPECTED, size); |
| 67 | + size = 0; |
| 68 | + |
| 69 | + //test `FILE*` api |
| 70 | + FILE * ptr = fopen(filename.c_str(), "r"); |
| 71 | + ASSERT_TRUE(ptr != NULL); |
| 72 | + size = filesystem::getFileSize(ptr); |
| 73 | + fclose(ptr); |
| 74 | + ASSERT_EQ(EXPECTED, size); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + //-------------------------------------------------------------------------------------------------- |
| 79 | + TEST_F(TestFilesystemFunc, testGetFilename) |
| 80 | + { |
| 81 | + //test NULL |
| 82 | + { |
| 83 | + static const std::string EXPECTED = ""; |
| 84 | + std::string filename = filesystem::getFilename(NULL); |
| 85 | + ASSERT_EQ(EXPECTED, filename); |
| 86 | + } |
| 87 | + |
| 88 | + //test filename only |
| 89 | + { |
| 90 | + static const std::string EXPECTED = "foo.bar"; |
| 91 | + std::string filename = filesystem::getFilename("foo.bar"); |
| 92 | + ASSERT_EQ(EXPECTED, filename); |
| 93 | + } |
| 94 | + |
| 95 | + //test full path (unix style) |
| 96 | + { |
| 97 | + static const std::string EXPECTED = "foo.bar"; |
| 98 | + std::string filename = filesystem::getFilename("/home/myFolder/foo.bar"); |
| 99 | + ASSERT_EQ(EXPECTED, filename); |
| 100 | + } |
| 101 | + |
| 102 | + //test full path (windows style) |
| 103 | + { |
| 104 | + static const std::string EXPECTED = "foo.bar"; |
| 105 | + std::string filename = filesystem::getFilename("C:\\Users\\Antoine\\Desktop\\myFolder\\foo.bar"); |
| 106 | + ASSERT_EQ(EXPECTED, filename); |
| 107 | + } |
| 108 | + } |
| 109 | + //-------------------------------------------------------------------------------------------------- |
| 110 | + TEST_F(TestFilesystemFunc, testFileExists) |
| 111 | + { |
| 112 | + //test NULL |
| 113 | + { |
| 114 | + bool exists = filesystem::fileExists(NULL); |
| 115 | + ASSERT_FALSE(exists); |
| 116 | + } |
| 117 | + |
| 118 | + //test not found |
| 119 | + { |
| 120 | + bool exists = filesystem::fileExists("foo.bar.notfound.bang"); |
| 121 | + ASSERT_FALSE(exists); |
| 122 | + } |
| 123 | + |
| 124 | + //test found |
| 125 | + { |
| 126 | + //create dummy file |
| 127 | + std::string filename = gTestHelper::getInstance().getTestQualifiedName(); |
| 128 | + ASSERT_TRUE( createDummyFile(filename.c_str()) ); |
| 129 | + |
| 130 | + bool exists = filesystem::fileExists(filename.c_str()); |
| 131 | + ASSERT_TRUE(exists); |
| 132 | + } |
| 133 | + } |
| 134 | + //-------------------------------------------------------------------------------------------------- |
| 135 | + TEST_F(TestFilesystemFunc, testFolderExists) |
| 136 | + { |
| 137 | + //test NULL |
| 138 | + { |
| 139 | + bool exists = filesystem::folderExists(NULL); |
| 140 | + ASSERT_FALSE(exists); |
| 141 | + } |
| 142 | + |
| 143 | + //test not found |
| 144 | + { |
| 145 | + bool exists = filesystem::folderExists("/home/fooBAR"); |
| 146 | + ASSERT_FALSE(exists); |
| 147 | + } |
| 148 | + |
| 149 | + //test found |
| 150 | + { |
| 151 | + //create dummy file |
| 152 | + std::string currentFolder = filesystem::getCurrentFolder(); |
| 153 | + ASSERT_TRUE( !currentFolder.empty() ); |
| 154 | + |
| 155 | + bool exists = filesystem::folderExists(currentFolder.c_str()); |
| 156 | + ASSERT_TRUE(exists); |
| 157 | + } |
| 158 | + } |
| 159 | + //-------------------------------------------------------------------------------------------------- |
| 160 | + TEST_F(TestFilesystemFunc, testGetTemporaryFileName) |
| 161 | + { |
| 162 | + //test not empty |
| 163 | + { |
| 164 | + std::string filename = filesystem::getTemporaryFileName(); |
| 165 | + ASSERT_TRUE( !filename.empty() ); |
| 166 | + } |
| 167 | + |
| 168 | + //test repetitive |
| 169 | + { |
| 170 | + std::vector<std::string> filenames; |
| 171 | + static const size_t numTest = 20; |
| 172 | + for(size_t i=0; i<numTest; i++) |
| 173 | + { |
| 174 | + std::string filename = filesystem::getTemporaryFileName(); |
| 175 | + filenames.push_back(filename); |
| 176 | + } |
| 177 | + |
| 178 | + //assert that all values are unique |
| 179 | + for(size_t i=0; i<numTest; i++) |
| 180 | + { |
| 181 | + const std::string & filename = filenames[i]; |
| 182 | + int count = countValues(filenames, filename); |
| 183 | + ASSERT_EQ(1, count) << "Found value '" << filename << "' " << count << " times in the list."; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + //-------------------------------------------------------------------------------------------------- |
| 188 | + TEST_F(TestFilesystemFunc, testGetTemporaryFilePath) |
| 189 | + { |
| 190 | + //test not empty |
| 191 | + { |
| 192 | + std::string path = filesystem::getTemporaryFilePath(); |
| 193 | + ASSERT_TRUE( !path.empty() ); |
| 194 | + } |
| 195 | + |
| 196 | + //test repetitive |
| 197 | + { |
| 198 | + std::vector<std::string> paths; |
| 199 | + static const size_t numTest = 20; |
| 200 | + for(size_t i=0; i<numTest; i++) |
| 201 | + { |
| 202 | + std::string path = filesystem::getTemporaryFilePath(); |
| 203 | + paths.push_back(path); |
| 204 | + } |
| 205 | + |
| 206 | + //assert that all values are unique |
| 207 | + for(size_t i=0; i<numTest; i++) |
| 208 | + { |
| 209 | + const std::string & path = paths[i]; |
| 210 | + int count = countValues(paths, path); |
| 211 | + ASSERT_EQ(1, count) << "Found value '" << path << "' " << count << " times in the list."; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + //-------------------------------------------------------------------------------------------------- |
| 216 | + TEST_F(TestFilesystemFunc, testGetParentPath) |
| 217 | + { |
| 218 | + //test no folder |
| 219 | + { |
| 220 | + static const std::string EXPECTED = ""; |
| 221 | + std::string parent = filesystem::getParentPath("filename.bar"); |
| 222 | + ASSERT_EQ(EXPECTED, parent); |
| 223 | + } |
| 224 | + |
| 225 | + //test unix style |
| 226 | + { |
| 227 | + static const std::string EXPECTED = "/home/myFolder"; |
| 228 | + std::string parent = filesystem::getParentPath("/home/myFolder/foo.bar"); |
| 229 | + ASSERT_EQ(EXPECTED, parent); |
| 230 | + } |
| 231 | + |
| 232 | + //test windows style |
| 233 | + { |
| 234 | + static const std::string EXPECTED = "C:\\Users\\Antoine\\Desktop\\myFolder"; |
| 235 | + std::string parent = filesystem::getParentPath("C:\\Users\\Antoine\\Desktop\\myFolder\\foo.bar"); |
| 236 | + ASSERT_EQ(EXPECTED, parent); |
| 237 | + } |
| 238 | + } |
| 239 | + //-------------------------------------------------------------------------------------------------- |
| 240 | + TEST_F(TestFilesystemFunc, testGetShortPathForm) |
| 241 | + { |
| 242 | + //test spaces in filename |
| 243 | + { |
| 244 | + static const std::string EXPECTED = "ABC~1.TXT"; |
| 245 | + std::string shortPath = filesystem::getShortPathForm("a b c.txt"); |
| 246 | + ASSERT_EQ(EXPECTED, shortPath); |
| 247 | + } |
| 248 | + |
| 249 | + //test too long file extension |
| 250 | + { |
| 251 | + static const std::string EXPECTED = "ABCDEF~1.TEX"; |
| 252 | + std::string shortPath = filesystem::getShortPathForm("abcdefgh.text"); |
| 253 | + ASSERT_EQ(EXPECTED, shortPath); |
| 254 | + } |
| 255 | + |
| 256 | + //test too long filename |
| 257 | + { |
| 258 | + static const std::string EXPECTED = "ABCDEF~1.TXT"; |
| 259 | + std::string shortPath = filesystem::getShortPathForm("abcdefghijklmnopqrstuvwxyz.txt"); |
| 260 | + ASSERT_EQ(EXPECTED, shortPath); |
| 261 | + } |
| 262 | + |
| 263 | + //test spaces in file extension |
| 264 | + { |
| 265 | + static const std::string EXPECTED = "ABCDE~1.TXT"; |
| 266 | + std::string shortPath = filesystem::getShortPathForm("abcde.t x t"); |
| 267 | + ASSERT_EQ(EXPECTED, shortPath); |
| 268 | + } |
| 269 | + |
| 270 | + //test program files (windows style) |
| 271 | + { |
| 272 | + static const std::string EXPECTED = "PROGRA~1"; |
| 273 | + std::string shortPath = filesystem::getShortPathForm("Program Files (x86)"); |
| 274 | + ASSERT_EQ(EXPECTED, shortPath); |
| 275 | + } |
| 276 | + } |
| 277 | + //-------------------------------------------------------------------------------------------------- |
| 278 | +} // End namespace test |
| 279 | +} // End namespace filesystem |
0 commit comments