From 546e443b53823d100be3775c6674cbb6ef4936f8 Mon Sep 17 00:00:00 2001 From: coderpolo Date: Wed, 22 Feb 2017 14:16:29 +0000 Subject: [PATCH 1/4] test case for file read --- test/CMakeLists.txt | 1 + test/FileSystem.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/FileSystem.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 843c5c0..dd1fbee 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,7 @@ SET(TEST_ALL Queue.cpp Sequence.cpp String.cpp + FileSystem.cpp ) ADD_EXECUTABLE(test_all ${TEST_ALL}) TARGET_LINK_LIBRARIES(test_all adbase libgtest.a libgtest_main.a pthread) diff --git a/test/FileSystem.cpp b/test/FileSystem.cpp new file mode 100644 index 0000000..4aeb7dd --- /dev/null +++ b/test/FileSystem.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +std::string generate_str(int str_len) +{ + std::string result; + for (int i = 0; i < str_len; i++) { + char ch = static_cast('a' + rand() % 26); + result.push_back(ch); + } + return result; +} + +// {{{ construct +TEST(ReadSmallFileTest, ReadSmallFileConstructorTest) +{ + //create a regular file. + char filename[255] = {0x00}; + int fd = mkstemp(filename); + adbase::ReadSmallFile read_file(filename); + close(fd); +} +// }}} + +// {{{ construct +TEST(ReadSmallFileTest, ReadFileTest) +{ + char filename[]= "template-XXXXXX"; + int str_len = 65535; + std::string str_to_write = generate_str(str_len); + + //create a regular file. + int fd = mkstemp(filename); + + //write some chars to fhe file + ssize_t write_count = write (fd, str_to_write.c_str(), (str_to_write.size())); + close(fd); + EXPECT_EQ(write_count,str_to_write.size()); + + //readTobuffer + adbase::ReadSmallFile read_file(filename); + int read_count = 0; + read_file.readToBuffer(&read_count); + EXPECT_EQ(read_count,write_count); + EXPECT_EQ(std::string(read_file.buffer()),std::string(str_to_write.c_str())); + + //readToString + int64_t file_size = 0; + int64_t modify_time = 0; + int64_t create_time = 0; + int max_read = 65535; + std::string result; + adbase::readFile(filename,max_read, &result,&file_size,&modify_time,&create_time); + EXPECT_EQ(result,str_to_write); + + //remove file + remove(filename); +} +// }}} From b098bb06bbc7c8242ae7796c0016f5e1f6dae589 Mon Sep 17 00:00:00 2001 From: coderpolo Date: Wed, 22 Feb 2017 17:17:26 +0000 Subject: [PATCH 2/4] renew test case for file read --- test/FileSystem.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/FileSystem.cpp b/test/FileSystem.cpp index 4aeb7dd..63dba7f 100644 --- a/test/FileSystem.cpp +++ b/test/FileSystem.cpp @@ -17,10 +17,11 @@ std::string generate_str(int str_len) TEST(ReadSmallFileTest, ReadSmallFileConstructorTest) { //create a regular file. - char filename[255] = {0x00}; + char filename[]= "template-XXXXXX"; int fd = mkstemp(filename); adbase::ReadSmallFile read_file(filename); close(fd); + remove(filename); } // }}} From 1b85471d193becdb705ba79eeb773005be0db40f Mon Sep 17 00:00:00 2001 From: coderpolo Date: Thu, 23 Feb 2017 04:42:16 +0000 Subject: [PATCH 3/4] test case for file append --- test/FileSystem.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/FileSystem.cpp b/test/FileSystem.cpp index 63dba7f..7b87124 100644 --- a/test/FileSystem.cpp +++ b/test/FileSystem.cpp @@ -25,7 +25,7 @@ TEST(ReadSmallFileTest, ReadSmallFileConstructorTest) } // }}} -// {{{ construct +// {{{ read TEST(ReadSmallFileTest, ReadFileTest) { char filename[]= "template-XXXXXX"; @@ -60,3 +60,35 @@ TEST(ReadSmallFileTest, ReadFileTest) remove(filename); } // }}} + +// {{{ append +TEST(AppendFileTest, appendTest) +{ + char filename[]= "template-XXXXXX"; + int str_len = 65535; + std::string str_to_write = generate_str(str_len); + + //create a regular file. + int fd = mkstemp(filename); + + //write to file + ssize_t write_count = write (fd, str_to_write.c_str(), (str_to_write.size())); + close(fd); + EXPECT_EQ(write_count,str_to_write.size()); + + //append to file + adbase::AppendFile append_file(filename); + append_file.append(str_to_write.c_str(),str_to_write.size()); + append_file.flush(); + + //read and compare + int64_t file_size = 0; + int64_t modify_time = 0; + int64_t create_time = 0; + int max_read = 65535*2; + std::string result; + adbase::readFile(filename,max_read, &result,&file_size,&modify_time,&create_time); + + EXPECT_EQ(result,str_to_write + str_to_write); +} +// }}} From 162954c4bf3b0662c294d769c0345ce1256c6f77 Mon Sep 17 00:00:00 2001 From: coderpolo Date: Thu, 23 Feb 2017 04:44:50 +0000 Subject: [PATCH 4/4] delete file after test --- test/FileSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/FileSystem.cpp b/test/FileSystem.cpp index 7b87124..364fc41 100644 --- a/test/FileSystem.cpp +++ b/test/FileSystem.cpp @@ -88,7 +88,7 @@ TEST(AppendFileTest, appendTest) int max_read = 65535*2; std::string result; adbase::readFile(filename,max_read, &result,&file_size,&modify_time,&create_time); - EXPECT_EQ(result,str_to_write + str_to_write); + remove(filename); } // }}}