Permalink
Browse files

[test] make TestFile.Read platform agnostic

  • Loading branch information...
1 parent 39af002 commit 046687513034c7dd3b21a5ea3e0a19fd84a0f7e2 @Montellese Montellese committed Sep 7, 2012
Showing with 65 additions and 29 deletions.
  1. +53 −29 xbmc/filesystem/test/TestFile.cpp
  2. +9 −0 xbmc/test/TestUtils.cpp
  3. +3 −0 xbmc/test/TestUtils.h
@@ -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();
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&);

0 comments on commit 0466875

Please sign in to comment.