Permalink
Browse files

Merge pull request #1384 from Montellese/test_platform_utils

[test] make TestFile.Read platform agnostic
  • Loading branch information...
Montellese committed Oct 15, 2012
2 parents 76f1504 + 0466875 commit 937ace2faccf04d50ed666aae7964f534bf499f1
@@ -27,46 +27,70 @@
TEST(TestFile, Read)
{
+ const std::string newLine = CXBMCTestUtils::Instance().getNewLineCharacters();
+ const int size = 1616;
+ const int lines = 25;
+ int addPerLine = newLine.length() - 1;
+ int realSize = size + lines * addPerLine;
+
+ const std::string firstBuf = "About" + newLine + "-----" + newLine + "XBMC is ";
+ const std::string secondBuf = "an award-winning fre";
+ const std::string thirdBuf = "ent hub for digital ";
+ const std::string fourthBuf = "rs, XBMC is a non-pr";
+ const std::string fifthBuf = "multimedia jukebox." + newLine;
+
XFILE::CFile file;
- char buf[20];
+ char buf[23];
memset(&buf, 0, sizeof(buf));
+ int currentPos;
ASSERT_TRUE(file.Open(
XBMC_REF_FILE_PATH("/xbmc/filesystem/test/reffile.txt")));
EXPECT_EQ(0, file.GetPosition());
- EXPECT_EQ(1616, file.GetLength());
- EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
+ EXPECT_EQ(realSize, file.GetLength());
+ EXPECT_EQ(firstBuf.length(), file.Read(buf, firstBuf.length()));
file.Flush();
- EXPECT_EQ(20, file.GetPosition());
- EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1));
- EXPECT_TRUE(file.ReadString(buf, sizeof(buf)));
- EXPECT_EQ(39, file.GetPosition());
- EXPECT_STREQ("an award-winning fr", buf);
- EXPECT_EQ(100, file.Seek(100));
- EXPECT_EQ(100, file.GetPosition());
- EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
+ currentPos = firstBuf.length();
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_TRUE(memcmp(firstBuf.c_str(), buf, firstBuf.length()) == 0);
+ EXPECT_TRUE(file.Read(buf, secondBuf.length()));
+ currentPos += secondBuf.length();
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_TRUE(memcmp(secondBuf.c_str(), buf, secondBuf.length()) == 0);
+ currentPos = 100 + addPerLine * 3;
+ EXPECT_EQ(currentPos, file.Seek(currentPos));
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_EQ(thirdBuf.length(), file.Read(buf, thirdBuf.length()));
file.Flush();
- EXPECT_EQ(120, file.GetPosition());
- EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1));
- EXPECT_EQ(220, file.Seek(100, SEEK_CUR));
- EXPECT_EQ(220, file.GetPosition());
- EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
+ currentPos += thirdBuf.length();
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_TRUE(memcmp(thirdBuf.c_str(), buf, thirdBuf.length()) == 0);
+ currentPos += 100 + addPerLine * 1;
+ EXPECT_EQ(currentPos, file.Seek(100 + addPerLine * 1, SEEK_CUR));
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_EQ(fourthBuf.length(), file.Read(buf, fourthBuf.length()));
file.Flush();
- EXPECT_EQ(240, file.GetPosition());
- EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1));
- EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END));
- EXPECT_EQ(1596, file.GetPosition());
- EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
+ currentPos += fourthBuf.length();
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_TRUE(memcmp(fourthBuf.c_str(), buf, fourthBuf.length()) == 0);
+ currentPos = realSize - fifthBuf.length();
+ EXPECT_EQ(currentPos, file.Seek(-(int64_t)fifthBuf.length(), SEEK_END));
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_EQ(fifthBuf.length(), file.Read(buf, fifthBuf.length()));
file.Flush();
- EXPECT_EQ(1616, file.GetPosition());
- EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1));
- EXPECT_EQ(1716, file.Seek(100, SEEK_CUR));
- EXPECT_EQ(1716, file.GetPosition());
- EXPECT_EQ(0, file.Seek(0, SEEK_SET));
- EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf)));
+ currentPos += fifthBuf.length();
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_TRUE(memcmp(fifthBuf.c_str(), buf, fifthBuf.length()) == 0);
+ currentPos += 100;
+ EXPECT_EQ(currentPos, file.Seek(100, SEEK_CUR));
+ EXPECT_EQ(currentPos, file.GetPosition());
+ currentPos = 0;
+ EXPECT_EQ(currentPos, file.Seek(currentPos, SEEK_SET));
+ EXPECT_EQ(firstBuf.length(), file.Read(buf, firstBuf.length()));
file.Flush();
- EXPECT_EQ(20, file.GetPosition());
- EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1));
+ currentPos += firstBuf.length();
+ EXPECT_EQ(currentPos, file.GetPosition());
+ EXPECT_TRUE(memcmp(firstBuf.c_str(), buf, firstBuf.length()) == 0);
EXPECT_EQ(0, file.Seek(0, SEEK_SET));
EXPECT_EQ(-1, file.Seek(-100, SEEK_SET));
file.Close();
@@ -73,17 +73,17 @@ TEST_F(TestFileFactory, Read)
std::vector<CStdString>::iterator it;
for (it = urls.begin(); it < urls.end(); it++)
{
- std::cout << "Testing URL: " << *it << "\n";
+ std::cout << "Testing URL: " << *it << std::endl;
ASSERT_TRUE(file.Open(*it));
std::cout << "file.GetLength(): " <<
- testing::PrintToString(file.GetLength()) << "\n";
+ testing::PrintToString(file.GetLength()) << std::endl;
std::cout << "file.Seek(file.GetLength() / 2, SEEK_CUR) return value: " <<
- testing::PrintToString(file.Seek(file.GetLength() / 2, SEEK_CUR)) << "\n";
+ testing::PrintToString(file.Seek(file.GetLength() / 2, SEEK_CUR)) << std::endl;
std::cout << "file.Seek(0, SEEK_END) return value: " <<
- testing::PrintToString(file.Seek(0, SEEK_END)) << "\n";
+ testing::PrintToString(file.Seek(0, SEEK_END)) << std::endl;
std::cout << "file.Seek(0, SEEK_SET) return value: " <<
- testing::PrintToString(file.Seek(0, SEEK_SET)) << "\n";
- std::cout << "File contents:\n";
+ testing::PrintToString(file.Seek(0, SEEK_SET)) << std::endl;
+ std::cout << "File contents:" << std::endl;
while ((size = file.Read(buf, sizeof(buf))) > 0)
{
str.Format(" %08X", count);
@@ -104,7 +104,7 @@ TEST_F(TestFileFactory, Read)
else
std::cout << ".";
}
- std::cout << "]\n";
+ std::cout << "]" << std::endl;
}
file.Close();
}
@@ -127,24 +127,24 @@ TEST_F(TestFileFactory, Write)
std::vector<CStdString>::iterator it;
for (it = urls.begin(); it < urls.end(); it++)
{
- std::cout << "Testing URL: " << *it << "\n";
+ std::cout << "Testing URL: " << *it << std::endl;
std::cout << "Writing...";
ASSERT_TRUE(file.OpenForWrite(*it, true));
while ((size = inputfile.Read(buf, sizeof(buf))) > 0)
{
EXPECT_GE(file.Write(buf, size), 0);
}
file.Close();
- std::cout << "done.\n";
- std::cout << "Reading...\n";
+ std::cout << "done." << std::endl;
+ std::cout << "Reading..." << std::endl;
ASSERT_TRUE(file.Open(*it));
EXPECT_EQ(inputfile.GetLength(), file.GetLength());
std::cout << "file.Seek(file.GetLength() / 2, SEEK_CUR) return value: " <<
- testing::PrintToString(file.Seek(file.GetLength() / 2, SEEK_CUR)) << "\n";
+ testing::PrintToString(file.Seek(file.GetLength() / 2, SEEK_CUR)) << std::endl;
std::cout << "file.Seek(0, SEEK_END) return value: " <<
- testing::PrintToString(file.Seek(0, SEEK_END)) << "\n";
+ testing::PrintToString(file.Seek(0, SEEK_END)) << std::endl;
std::cout << "file.Seek(0, SEEK_SET) return value: " <<
- testing::PrintToString(file.Seek(0, SEEK_SET)) << "\n";
+ testing::PrintToString(file.Seek(0, SEEK_SET)) << std::endl;
std::cout << "File contents:\n";
while ((size = file.Read(buf, sizeof(buf))) > 0)
{
@@ -166,7 +166,7 @@ TEST_F(TestFileFactory, Write)
else
std::cout << ".";
}
- std::cout << "]\n";
+ std::cout << "]" << std::endl;
}
file.Close();
}
@@ -130,8 +130,7 @@ TEST(TestRarFile, CorruptedFile)
reffilepath = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.rar");
ASSERT_TRUE((file = XBMC_CREATECORRUPTEDFILE(reffilepath, ".rar")) != NULL);
- std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) <<
- "'\n";
+ std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) << "'" << std::endl;
URIUtils::CreateArchivePath(strrarpath, "rar", XBMC_TEMPFILEPATH(file), "");
if (!XFILE::CDirectory::GetDirectory(strrarpath, itemlist, "",
@@ -156,14 +155,14 @@ TEST(TestRarFile, CorruptedFile)
return;
}
std::cout << "file->GetLength(): " <<
- testing::PrintToString(file->GetLength()) << "\n";
+ testing::PrintToString(file->GetLength()) << std::endl;
std::cout << "file->Seek(file->GetLength() / 2, SEEK_CUR) return value: " <<
- testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << "\n";
+ testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << std::endl;
std::cout << "file->Seek(0, SEEK_END) return value: " <<
- testing::PrintToString(file->Seek(0, SEEK_END)) << "\n";
+ testing::PrintToString(file->Seek(0, SEEK_END)) << std::endl;
std::cout << "file->Seek(0, SEEK_SET) return value: " <<
- testing::PrintToString(file->Seek(0, SEEK_SET)) << "\n";
- std::cout << "File contents:\n";
+ testing::PrintToString(file->Seek(0, SEEK_SET)) << std::endl;
+ std::cout << "File contents:" << std::endl;
while ((size = file->Read(buf, sizeof(buf))) > 0)
{
str.Format(" %08X", count);
@@ -184,7 +183,7 @@ TEST(TestRarFile, CorruptedFile)
else
std::cout << ".";
}
- std::cout << "]\n";
+ std::cout << "]" << std::endl;
}
file->Close();
XBMC_DELETETEMPFILE(file);
@@ -152,8 +152,7 @@ TEST_F(TestZipFile, CorruptedFile)
reffilepath = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip");
ASSERT_TRUE((file = XBMC_CREATECORRUPTEDFILE(reffilepath, ".zip")) != NULL);
- std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) <<
- "'\n";
+ std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) << "'" << std::endl;
URIUtils::CreateArchivePath(strzippath, "zip", XBMC_TEMPFILEPATH(file), "");
if (!XFILE::CDirectory::GetDirectory(strzippath, itemlist, "",
@@ -178,14 +177,14 @@ TEST_F(TestZipFile, CorruptedFile)
return;
}
std::cout << "file->GetLength(): " <<
- testing::PrintToString(file->GetLength()) << "\n";
+ testing::PrintToString(file->GetLength()) << std::endl;
std::cout << "file->Seek(file->GetLength() / 2, SEEK_CUR) return value: " <<
- testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << "\n";
+ testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << std::endl;
std::cout << "file->Seek(0, SEEK_END) return value: " <<
- testing::PrintToString(file->Seek(0, SEEK_END)) << "\n";
+ testing::PrintToString(file->Seek(0, SEEK_END)) << std::endl;
std::cout << "file->Seek(0, SEEK_SET) return value: " <<
- testing::PrintToString(file->Seek(0, SEEK_SET)) << "\n";
- std::cout << "File contents:\n";
+ testing::PrintToString(file->Seek(0, SEEK_SET)) << std::endl;
+ std::cout << "File contents:" << std::endl;
while ((size = file->Read(buf, sizeof(buf))) > 0)
{
str.Format(" %08X", count);
@@ -206,7 +205,7 @@ TEST_F(TestZipFile, CorruptedFile)
else
std::cout << ".";
}
- std::cout << "]\n";
+ std::cout << "]" << std::endl;
}
file->Close();
XBMC_DELETETEMPFILE(file);
View
@@ -358,3 +358,12 @@ void CXBMCTestUtils::ParseArgs(int argc, char **argv)
}
}
}
+
+std::string CXBMCTestUtils::getNewLineCharacters() const
+{
+#ifdef TARGET_WINDOWS
+ return "\r\n";
+#else
+ return "\n";
+#endif
+}
View
@@ -86,6 +86,9 @@ class CXBMCTestUtils
/* Function to parse command line options */
void ParseArgs(int argc, char **argv);
+
+ /* Function to return the newline characters for this platform */
+ std::string getNewLineCharacters() const;
private:
CXBMCTestUtils();
CXBMCTestUtils(CXBMCTestUtils const&);
@@ -98,5 +98,5 @@ TEST(TestCPUInfo, getUsedPercentage_output)
CCPUInfo c;
Sleep(1); /* TODO: Support option from main that sets this parameter */
int r = c.getUsedPercentage();
- std::cout << "Percentage: " << testing::PrintToString(r) << "\n";
+ std::cout << "Percentage: " << testing::PrintToString(r) << std::endl;
}
@@ -74,7 +74,7 @@ TEST_F(TestDownloadQueue, RequestContent)
count = 0;
for (it = urls.begin(); it < urls.end(); it++)
{
- std::cout << "Testing URL: " << *it << "\n";
+ std::cout << "Testing URL: " << *it << std::endl;
TICKET t = queue.RequestContent(*it, &observer);
EXPECT_EQ(count, t.dwItemId);
count++;
@@ -99,7 +99,7 @@ TEST_F(TestDownloadQueue, RequestFile)
count = 0;
for (it = urls.begin(); it < urls.end(); it++)
{
- std::cout << "Testing URL: " << *it << "\n";
+ std::cout << "Testing URL: " << *it << std::endl;
TICKET t = queue.RequestFile(*it, &observer);
EXPECT_EQ(count, t.dwItemId);
count++;
@@ -73,9 +73,9 @@ TEST_F(TestDownloadQueueManager, RequestContent)
count = 0;
for (it = urls.begin(); it < urls.end(); it++)
{
- std::cout << "Testing URL: " << *it << "\n";
+ std::cout << "Testing URL: " << *it << std::endl;
TICKET t = g_DownloadManager.RequestContent(*it, &observer);
- std::cout << " Ticket Item ID: " << t.dwItemId << "\n";
+ std::cout << " Ticket Item ID: " << t.dwItemId << std::endl;
count++;
}
@@ -95,9 +95,9 @@ TEST_F(TestDownloadQueueManager, RequestFile)
count = 0;
for (it = urls.begin(); it < urls.end(); it++)
{
- std::cout << "Testing URL: " << *it << "\n";
+ std::cout << "Testing URL: " << *it << std::endl;
TICKET t = g_DownloadManager.RequestFile(*it, &observer);
- std::cout << " Ticket Item ID: " << t.dwItemId << "\n";
+ std::cout << " Ticket Item ID: " << t.dwItemId << std::endl;
count++;
}
@@ -283,10 +283,9 @@ TEST(TestFileOperationJob, GetFunctions)
EXPECT_TRUE(XFILE::CFile::Exists(tmpfilepath));
EXPECT_TRUE(XFILE::CFile::Exists(destfile));
- fprintf(stdout, "GetAverageSpeed(): %s\n", job.GetAverageSpeed().c_str());
- fprintf(stdout, "GetCurrentOperation(): %s\n",
- job.GetCurrentOperation().c_str());
- fprintf(stdout, "GetCurrentFile(): %s\n", job.GetCurrentFile().c_str());
+ std::cout << "GetAverageSpeed(): " << job.GetAverageSpeed() << std::endl;
+ std::cout << "GetCurrentOperation(): " << job.GetCurrentOperation() << std::endl;
+ std::cout << "GetCurrentFile(): " << job.GetCurrentFile() << std::endl;
EXPECT_FALSE(job.GetItems().IsEmpty());
EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile));
@@ -47,7 +47,7 @@ TEST(TestPerformanceSample, General)
EXPECT_GT(a.getFreq(), (int64_t)0);
std::cout << "Estimated Error: " <<
- testing::PrintToString(a.GetEstimatedError()) << "\n";
- std::cout << "Start: " << testing::PrintToString(a.getStart()) << "\n";
- std::cout << "Frequency: " << testing::PrintToString(a.getFreq()) << "\n";
+ testing::PrintToString(a.GetEstimatedError()) << std::endl;
+ std::cout << "Start: " << testing::PrintToString(a.getStart()) << std::endl;
+ std::cout << "Frequency: " << testing::PrintToString(a.getFreq()) << std::endl;
}
Oops, something went wrong.

0 comments on commit 937ace2

Please sign in to comment.