Skip to content

Commit b27a220

Browse files
committed
More file tests
1 parent 4188094 commit b27a220

File tree

2 files changed

+546
-252
lines changed

2 files changed

+546
-252
lines changed

modules/yup_core/files/yup_File.cpp

Lines changed: 0 additions & 252 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,256 +1099,4 @@ MemoryMappedFile::MemoryMappedFile (const File& file, const Range<int64>& fileRa
10991099
openInternal (file, mode, exclusive);
11001100
}
11011101

1102-
//==============================================================================
1103-
//==============================================================================
1104-
#if YUP_UNIT_TESTS
1105-
1106-
class FileTests final : public UnitTest
1107-
{
1108-
public:
1109-
FileTests()
1110-
: UnitTest ("Files", UnitTestCategories::files)
1111-
{
1112-
}
1113-
1114-
void runTest() override
1115-
{
1116-
beginTest ("Reading");
1117-
1118-
const File home (File::getSpecialLocation (File::userHomeDirectory));
1119-
const File temp (File::getSpecialLocation (File::tempDirectory));
1120-
1121-
expect (! File().exists());
1122-
expect (! File().existsAsFile());
1123-
expect (! File().isDirectory());
1124-
#if ! YUP_WINDOWS
1125-
expect (File ("/").isDirectory());
1126-
#endif
1127-
expect (home.isDirectory());
1128-
expect (home.exists());
1129-
expect (! home.existsAsFile());
1130-
expect (File::getSpecialLocation (File::userApplicationDataDirectory).isDirectory());
1131-
expect (File::getSpecialLocation (File::currentExecutableFile).exists());
1132-
expect (File::getSpecialLocation (File::currentApplicationFile).exists());
1133-
expect (File::getSpecialLocation (File::invokedExecutableFile).exists());
1134-
expect (home.getVolumeTotalSize() > 1024 * 1024);
1135-
expect (home.getBytesFreeOnVolume() > 0);
1136-
expect (! home.isHidden());
1137-
expect (home.isOnHardDisk());
1138-
expect (! home.isOnCDRomDrive());
1139-
expect (File::getCurrentWorkingDirectory().exists());
1140-
expect (home.setAsCurrentWorkingDirectory());
1141-
1142-
{
1143-
auto homeParent = home;
1144-
bool noSymlinks = true;
1145-
1146-
while (! homeParent.isRoot())
1147-
{
1148-
if (homeParent.isSymbolicLink())
1149-
{
1150-
noSymlinks = false;
1151-
break;
1152-
}
1153-
1154-
homeParent = homeParent.getParentDirectory();
1155-
}
1156-
1157-
if (noSymlinks)
1158-
expect (File::getCurrentWorkingDirectory() == home);
1159-
}
1160-
1161-
{
1162-
Array<File> roots;
1163-
File::findFileSystemRoots (roots);
1164-
expect (roots.size() > 0);
1165-
1166-
int numRootsExisting = 0;
1167-
for (int i = 0; i < roots.size(); ++i)
1168-
if (roots[i].exists())
1169-
++numRootsExisting;
1170-
1171-
// (on windows, some of the drives may not contain media, so as long as at least one is ok..)
1172-
expect (numRootsExisting > 0);
1173-
}
1174-
1175-
beginTest ("Writing");
1176-
1177-
auto random = getRandom();
1178-
const auto tempFolderName = "YUP UnitTests Temp Folder "
1179-
+ String::toHexString (random.nextInt())
1180-
+ ".folder";
1181-
File demoFolder (temp.getChildFile (tempFolderName));
1182-
expect (demoFolder.deleteRecursively());
1183-
expect (demoFolder.createDirectory());
1184-
expect (demoFolder.isDirectory());
1185-
expect (demoFolder.getParentDirectory() == temp);
1186-
expect (temp.isDirectory());
1187-
expect (temp.findChildFiles (File::findFilesAndDirectories, false, "*").contains (demoFolder));
1188-
expect (temp.findChildFiles (File::findDirectories, true, "*.folder").contains (demoFolder));
1189-
1190-
File tempFile (demoFolder.getNonexistentChildFile ("test", ".txt", false));
1191-
1192-
expect (tempFile.getFileExtension() == ".txt");
1193-
expect (tempFile.hasFileExtension (".txt"));
1194-
expect (tempFile.hasFileExtension ("txt"));
1195-
expect (tempFile.withFileExtension ("xyz").hasFileExtension (".xyz"));
1196-
expect (tempFile.withFileExtension ("xyz").hasFileExtension ("abc;xyz;foo"));
1197-
expect (tempFile.withFileExtension ("xyz").hasFileExtension ("xyz;foo"));
1198-
expect (! tempFile.withFileExtension ("h").hasFileExtension ("bar;foo;xx"));
1199-
expect (tempFile.getSiblingFile ("foo").isAChildOf (temp));
1200-
expect (tempFile.hasWriteAccess());
1201-
1202-
expect (home.getChildFile (".") == home);
1203-
expect (home.getChildFile ("..") == home.getParentDirectory());
1204-
expect (home.getChildFile (".xyz").getFileName() == ".xyz");
1205-
expect (home.getChildFile ("..xyz").getFileName() == "..xyz");
1206-
expect (home.getChildFile ("...xyz").getFileName() == "...xyz");
1207-
expect (home.getChildFile ("./xyz") == home.getChildFile ("xyz"));
1208-
expect (home.getChildFile ("././xyz") == home.getChildFile ("xyz"));
1209-
expect (home.getChildFile ("../xyz") == home.getParentDirectory().getChildFile ("xyz"));
1210-
expect (home.getChildFile (".././xyz") == home.getParentDirectory().getChildFile ("xyz"));
1211-
expect (home.getChildFile (".././xyz/./abc") == home.getParentDirectory().getChildFile ("xyz/abc"));
1212-
expect (home.getChildFile ("./../xyz") == home.getParentDirectory().getChildFile ("xyz"));
1213-
expect (home.getChildFile ("a1/a2/a3/./../../a4") == home.getChildFile ("a1/a4"));
1214-
1215-
expect (! File().hasReadAccess());
1216-
expect (! File().hasWriteAccess());
1217-
1218-
expect (! tempFile.hasReadAccess());
1219-
1220-
{
1221-
FileOutputStream fo (tempFile);
1222-
fo.write ("0123456789", 10);
1223-
}
1224-
1225-
expect (tempFile.hasReadAccess());
1226-
1227-
expect (tempFile.exists());
1228-
expect (tempFile.getSize() == 10);
1229-
expect (std::abs ((int) (tempFile.getLastModificationTime().toMilliseconds() - Time::getCurrentTime().toMilliseconds())) < 3000);
1230-
expectEquals (tempFile.loadFileAsString(), String ("0123456789"));
1231-
expect (! demoFolder.containsSubDirectories());
1232-
1233-
expectEquals (tempFile.getRelativePathFrom (demoFolder.getParentDirectory()), demoFolder.getFileName() + File::getSeparatorString() + tempFile.getFileName());
1234-
expectEquals (demoFolder.getParentDirectory().getRelativePathFrom (tempFile), ".." + File::getSeparatorString() + ".." + File::getSeparatorString() + demoFolder.getParentDirectory().getFileName());
1235-
1236-
expect (demoFolder.getNumberOfChildFiles (File::findFiles) == 1);
1237-
expect (demoFolder.getNumberOfChildFiles (File::findFilesAndDirectories) == 1);
1238-
expect (demoFolder.getNumberOfChildFiles (File::findDirectories) == 0);
1239-
demoFolder.getNonexistentChildFile ("tempFolder", "", false).createDirectory();
1240-
expect (demoFolder.getNumberOfChildFiles (File::findDirectories) == 1);
1241-
expect (demoFolder.getNumberOfChildFiles (File::findFilesAndDirectories) == 2);
1242-
expect (demoFolder.containsSubDirectories());
1243-
1244-
expect (tempFile.hasWriteAccess());
1245-
tempFile.setReadOnly (true);
1246-
expect (! tempFile.hasWriteAccess());
1247-
tempFile.setReadOnly (false);
1248-
expect (tempFile.hasWriteAccess());
1249-
1250-
Time t (Time::getCurrentTime());
1251-
tempFile.setLastModificationTime (t);
1252-
Time t2 = tempFile.getLastModificationTime();
1253-
expect (std::abs ((int) (t2.toMilliseconds() - t.toMilliseconds())) <= 1000);
1254-
1255-
{
1256-
MemoryBlock mb;
1257-
tempFile.loadFileAsData (mb);
1258-
expect (mb.getSize() == 10);
1259-
expect (mb[0] == '0');
1260-
}
1261-
1262-
{
1263-
expect (tempFile.getSize() == 10);
1264-
FileOutputStream fo (tempFile);
1265-
expect (fo.openedOk());
1266-
1267-
expect (fo.setPosition (7));
1268-
expect (fo.truncate().wasOk());
1269-
expect (tempFile.getSize() == 7);
1270-
fo.write ("789", 3);
1271-
fo.flush();
1272-
expect (tempFile.getSize() == 10);
1273-
}
1274-
1275-
beginTest ("Memory-mapped files");
1276-
1277-
{
1278-
MemoryMappedFile mmf (tempFile, MemoryMappedFile::readOnly);
1279-
expect (mmf.getSize() == 10);
1280-
expect (mmf.getData() != nullptr);
1281-
expect (memcmp (mmf.getData(), "0123456789", 10) == 0);
1282-
}
1283-
1284-
{
1285-
const File tempFile2 (tempFile.getNonexistentSibling (false));
1286-
expect (tempFile2.create());
1287-
expect (tempFile2.appendData ("xxxxxxxxxx", 10));
1288-
1289-
{
1290-
MemoryMappedFile mmf (tempFile2, MemoryMappedFile::readWrite);
1291-
expect (mmf.getSize() == 10);
1292-
expect (mmf.getData() != nullptr);
1293-
memcpy (mmf.getData(), "abcdefghij", 10);
1294-
}
1295-
1296-
{
1297-
MemoryMappedFile mmf (tempFile2, MemoryMappedFile::readWrite);
1298-
expect (mmf.getSize() == 10);
1299-
expect (mmf.getData() != nullptr);
1300-
expect (memcmp (mmf.getData(), "abcdefghij", 10) == 0);
1301-
}
1302-
1303-
expect (tempFile2.deleteFile());
1304-
}
1305-
1306-
beginTest ("More writing");
1307-
1308-
expect (tempFile.appendData ("abcdefghij", 10));
1309-
expect (tempFile.getSize() == 20);
1310-
expect (tempFile.replaceWithData ("abcdefghij", 10));
1311-
expect (tempFile.getSize() == 10);
1312-
1313-
File tempFile2 (tempFile.getNonexistentSibling (false));
1314-
expect (tempFile.copyFileTo (tempFile2));
1315-
expect (tempFile2.exists());
1316-
expect (tempFile2.hasIdenticalContentTo (tempFile));
1317-
expect (tempFile.deleteFile());
1318-
expect (! tempFile.exists());
1319-
expect (tempFile2.moveFileTo (tempFile));
1320-
expect (tempFile.exists());
1321-
expect (! tempFile2.exists());
1322-
1323-
expect (demoFolder.deleteRecursively());
1324-
expect (! demoFolder.exists());
1325-
1326-
{
1327-
URL url ("https://audio.dev/foo/bar/");
1328-
expectEquals (url.toString (false), String ("https://audio.dev/foo/bar/"));
1329-
expectEquals (url.getChildURL ("x").toString (false), String ("https://audio.dev/foo/bar/x"));
1330-
expectEquals (url.getParentURL().toString (false), String ("https://audio.dev/foo"));
1331-
expectEquals (url.getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
1332-
expectEquals (url.getParentURL().getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
1333-
expectEquals (url.getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/foo/x"));
1334-
expectEquals (url.getParentURL().getParentURL().getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/x"));
1335-
}
1336-
1337-
{
1338-
URL url ("https://audio.dev/foo/bar");
1339-
expectEquals (url.toString (false), String ("https://audio.dev/foo/bar"));
1340-
expectEquals (url.getChildURL ("x").toString (false), String ("https://audio.dev/foo/bar/x"));
1341-
expectEquals (url.getParentURL().toString (false), String ("https://audio.dev/foo"));
1342-
expectEquals (url.getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
1343-
expectEquals (url.getParentURL().getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
1344-
expectEquals (url.getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/foo/x"));
1345-
expectEquals (url.getParentURL().getParentURL().getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/x"));
1346-
}
1347-
}
1348-
};
1349-
1350-
static FileTests fileUnitTests;
1351-
1352-
#endif
1353-
13541102
} // namespace yup

0 commit comments

Comments
 (0)