From 7d85479f573841a646c2d2879e50e36a06a051ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Thu, 28 Mar 2024 13:51:46 +0000 Subject: [PATCH 01/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils_mapped_file_test.cpp | 590 +++++++----------- 1 file changed, 230 insertions(+), 360 deletions(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index 765ba6a..322deba 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -111,24 +111,18 @@ bool SaveStringToFile(const std::string& filePath, const std::string& content, o return true; } -/* - * @tc.name: testDefaultMapping001 - * @tc.desc: Test file mapping with default params. - */ -HWTEST_F(UtilsMappedFileTest, testDefaultMapping001, TestSize.Level0) + +void RecreateFile(std::string& filename, const std::string& content) { - // 1. Create a new file - std::string filename = "test_read_write_1.txt"; - std::string content = "Test for normal use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); + filename.insert(0, UtilsMappedFileTest::SUITE_PATH).insert(0, UtilsMappedFileTest::BASE_PATH); RemoveTestFile(filename); ASSERT_TRUE(CreateTestFile(filename, content)); +} - // 2. map file - MappedFile mf(filename); +void CheckStatusAndSize(MappedFile& mf, const std::string& filename) +{ ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // check status ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); @@ -137,27 +131,189 @@ HWTEST_F(UtilsMappedFileTest, testDefaultMapping001, TestSize.Level0) struct stat stb = {0}; stat(filename.c_str(), &stb); ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); +} - // check map-mode - ASSERT_EQ(MapMode::DEFAULT, mf.GetMode()); +void CheckStatusAndSizeAndRead(MappedFile& mf, const std::string& filename, const std::string& content, struct stat* stb) +{ + ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // check offset - ASSERT_EQ(mf.StartOffset(), 0u); + // 3. check status after mapping + ASSERT_TRUE(mf.IsMapped()); + ASSERT_TRUE(mf.IsNormed()); - // 3. read from mapped file + // 4. check size + stat(filename.c_str(), stb); + ASSERT_TRUE(stb->st_size == mf.Size() || mf.PageSize() == mf.Size()); + + // 5. read from Mapped File + std::string readout; + char* cur = mf.Begin(); + for (; cur <= mf.End(); cur++) { + readout.push_back(*cur); + } + EXPECT_EQ(readout, content); + + // 9. write to the extended region + *(cur) = 'E'; + EXPECT_EQ((*cur), 'E'); +} + +void CheckReadAndWriteMappedFile(const MappedFile& mf, const std::string& content) +{ + // 4. read from mapped file + std::string readout; + for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { + readout.push_back(*cur); + } + EXPECT_EQ(readout, content); + + // 5. write to mapped file + std::string toWrite("Complete."); + char* newCur = mf.Begin(); + for (std::string::size_type i = 0; i < toWrite.length(); i++) { + (*newCur) = toWrite[i]; + newCur++; + } +} + +void CheckSizeAndRead(MappedFile& mf, const std::string& filename, const std::string& content, struct stat* stb) +{ + ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); + + // 3. check status after mapping + ASSERT_TRUE(mf.IsMapped()); + ASSERT_TRUE(mf.IsNormed()); + + // 4. check size + stat(filename.c_str(), stb); + ASSERT_TRUE(stb->st_size == mf.Size() || mf.PageSize() == mf.Size()); + + // 5. read from Mapped File std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { readout.push_back(*cur); } EXPECT_EQ(readout, content); - // 4. write to mapped file +} + +void CheckFileEquals(const MappedFile& mf, const std::string& filename, std::string& filename1, std::string& content1, struct stat* stb) +{ + // 9. check status after remapping + EXPECT_TRUE(mf.IsMapped()); + EXPECT_TRUE(mf.IsNormed()); + + // 10. check size + stat(filename1.c_str(), stb); + EXPECT_TRUE(stb->st_size == mf.Size()); + + // 11. read from Mapped File + std::string readout1; + for (char* cur1 = mf.Begin(); cur1 <= mf.End(); cur1++) { + readout1.push_back(*cur1); + } + EXPECT_EQ(readout1, content1); + + RemoveTestFile(filename); + RemoveTestFile(filename1); +} + +void CheckOffset(MappedFile& mf, const off_t& orig) +{ + off_t endOff; + // 6. keep turnNext within a page + for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first + // TunrNext() calling in consideration. + endOff = mf.EndOffset(); + EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); + EXPECT_EQ(mf.StartOffset(), endOff + 1); + EXPECT_EQ(mf.Size(), orig); + } + std::cout << "==Last TurnNext() with The Same Size==" << std::endl; + PrintStatus(mf); + + // 7. this turn will reach the bottom of a page + endOff = mf.EndOffset(); + char* rEnd = mf.RegionEnd(); + char* end = mf.End(); + EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); + EXPECT_EQ(mf.StartOffset(), endOff + 1); + EXPECT_EQ(mf.Size(), static_cast(rEnd - end)); + std::cout << "==Reached Bottom of A Page==" << std::endl; + PrintStatus(mf); +} + +void CheckReadComplete(const MappedFile& mfNew, const std::string& filename, const std::string& content) +{ + // 6. read from mapped file + std::string readout; + for (char* cur = mfNew.Begin(); cur <= mfNew.End(); cur++) { + readout.push_back(*cur); + } + EXPECT_EQ(readout, content); + + // 7. write to mapped file + std::string toWrite("Complete."); + char* newCur = mfNew.Begin(); + for (std::string::size_type i = 0; i < toWrite.length(); i++) { + (*newCur) = toWrite[i]; + newCur++; + } + std::string res; + LoadStringFromFile(filename, res); + EXPECT_EQ(res, "Complete.move use."); + + RemoveTestFile(filename); +} + +void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, const std::string& filename1, const std::string& content1) +{ + std::string readout; + for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { + readout.push_back(*cur); + } + EXPECT_EQ(readout, content1); + + //write to mapped file std::string toWrite("Complete."); char* newCur = mf.Begin(); for (std::string::size_type i = 0; i < toWrite.length(); i++) { (*newCur) = toWrite[i]; newCur++; } + std::string res; + LoadStringFromFile(filename1, res); + EXPECT_EQ(res, "Complete.move use."); + + RemoveTestFile(filename); + RemoveTestFile(filename1); + +} + +/* + * @tc.name: testDefaultMapping001 + * @tc.desc: Test file mapping with default params. + */ +HWTEST_F(UtilsMappedFileTest, testDefaultMapping001, TestSize.Level0) +{ + // 1. Create a new file + std::string filename = "test_read_write_1.txt"; + std::string content = "Test for normal use."; + RecreateFile(filename, content); + + // 2. map file + MappedFile mf(filename); + + CheckStatusAndSize(mf, filename); + + // check map-mode + ASSERT_EQ(MapMode::DEFAULT, mf.GetMode()); + + // check offset + ASSERT_EQ(mf.StartOffset(), 0u); + + CheckReadAndWriteMappedFile(mf, content); + std::string res; LoadStringFromFile(filename, res); EXPECT_EQ(res, "Complete.normal use."); @@ -310,10 +466,7 @@ HWTEST_F(UtilsMappedFileTest, testPrivateMapping001, TestSize.Level0) // 1. create a new file std::string filename = "test_read_write_4.txt"; std::string content = "Test for private use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename, MapMode::DEFAULT | MapMode::PRIVATE); @@ -323,20 +476,8 @@ HWTEST_F(UtilsMappedFileTest, testPrivateMapping001, TestSize.Level0) ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); - // 4. read from mapped file - std::string readout; - for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); + CheckReadAndWriteMappedFile(mf, content); - // 5. write to mapped file - std::string toWrite("Complete."); - char* newCur = mf.Begin(); - for (std::string::size_type i = 0; i < toWrite.length(); i++) { - (*newCur) = toWrite[i]; - newCur++; - } std::string res; LoadStringFromFile(filename, res); EXPECT_EQ(res, content); // changes to private mapped file will not write back to the original file @@ -353,10 +494,8 @@ HWTEST_F(UtilsMappedFileTest, testSharedReadOnlyMapping001, TestSize.Level0) // 1. create a new file std::string filename = "test_read_write_5.txt"; std::string content = "Test for readonly use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename, MapMode::DEFAULT | MapMode::READ_ONLY); @@ -386,10 +525,7 @@ HWTEST_F(UtilsMappedFileTest, testReMap001, TestSize.Level0) // 1. create a new file std::string filename = "test_remap_1.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -428,16 +564,11 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for default use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); + RecreateFile(filename, content); std::string filename1 = "test_remap_1.txt"; std::string content1 = "Test for remapping use."; - filename1.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename1); - - ASSERT_TRUE(CreateTestFile(filename, content)); - ASSERT_TRUE(CreateTestFile(filename1, content1)); + RecreateFile(filename1, content1); MappedFile mf(filename); @@ -451,24 +582,10 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) ASSERT_TRUE(mf.ChangeHint(reinterpret_cast(0x89000))); // 0x89000: random address. ASSERT_TRUE(mf.ChangeMode(MapMode::READ_ONLY)); - // 2. map file - ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - - // 3. check status after mapping - ASSERT_TRUE(mf.IsMapped()); - ASSERT_TRUE(mf.IsNormed()); - // 4. check size struct stat stb = {0}; - stat(filename.c_str(), &stb); - ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); - // 5. read from Mapped File - std::string readout; - for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); + CheckSizeAndRead(mf, filename, content, &stb); // 6. change params ASSERT_TRUE(mf.ChangePath(filename1)); @@ -482,23 +599,9 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) // 8. remap file ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // 9. check status after remapping - EXPECT_TRUE(mf.IsMapped()); - EXPECT_TRUE(mf.IsNormed()); - // 10. check size - stat(filename1.c_str(), &stb); - EXPECT_TRUE(stb.st_size == mf.Size()); + CheckFileEquals(mf, filename, filename1, content1, &stb); - // 11. read from Mapped File - std::string readout1; - for (char* cur1 = mf.Begin(); cur1 <= mf.End(); cur1++) { - readout1.push_back(*cur1); - } - EXPECT_EQ(readout1, content1); - - RemoveTestFile(filename); - RemoveTestFile(filename1); } /* @@ -510,36 +613,18 @@ HWTEST_F(UtilsMappedFileTest, testReMap003, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for default use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); std::string filename1 = "test_remap_1.txt"; std::string content1 = "Test for remapping use."; - filename1.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename1); - - ASSERT_TRUE(CreateTestFile(filename, content)); - ASSERT_TRUE(CreateTestFile(filename1, content1)); + RecreateFile(filename, content); + RecreateFile(filename1, content1); // 2. map file MappedFile mf(filename); - ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // 3. check status after mapping - ASSERT_TRUE(mf.IsMapped()); - ASSERT_TRUE(mf.IsNormed()); - - // 4. check size struct stat stb = {0}; - stat(filename.c_str(), &stb); - ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); - // 5. read from Mapped File - std::string readout; - for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); + CheckSizeAndRead(mf, filename, content, &stb); // 6. change params mf.ChangePath(filename1); @@ -552,22 +637,7 @@ HWTEST_F(UtilsMappedFileTest, testReMap003, TestSize.Level0) // 8. remap file ASSERT_EQ(mf.Resize(), MAPPED_FILE_ERR_OK); // 9. check status after remapping - EXPECT_TRUE(mf.IsMapped()); - EXPECT_TRUE(mf.IsNormed()); - - // 10. check size - stat(filename1.c_str(), &stb); - EXPECT_TRUE(stb.st_size == mf.Size()); - - // 11. read from Mapped File - std::string readout1; - for (char* cur1 = mf.Begin(); cur1 <= mf.End(); cur1++) { - readout1.push_back(*cur1); - } - EXPECT_EQ(readout1, content1); - - RemoveTestFile(filename); - RemoveTestFile(filename1); + CheckFileEquals(mf, filename, filename1, content1, &stb); } /* @@ -577,33 +647,15 @@ HWTEST_F(UtilsMappedFileTest, testReMap003, TestSize.Level0) HWTEST_F(UtilsMappedFileTest, testReMap004, TestSize.Level0) { // 1. create a new file - std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - - ASSERT_TRUE(CreateTestFile(filename, content)); + std::string filename = "test_remap.txt"; + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); - ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - - // 3. check status after mapping - ASSERT_TRUE(mf.IsMapped()); - ASSERT_TRUE(mf.IsNormed()); - - // 4. check size struct stat stb = {0}; - stat(filename.c_str(), &stb); - ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); - // 5. read from Mapped File - std::string readout; - char* cur = mf.Begin(); - for (; cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); + CheckStatusAndSizeAndRead(mf, filename, content, &stb); // 6. Remap to extend region ASSERT_EQ(mf.Resize(mf.Size() + 10), MAPPED_FILE_ERR_OK); @@ -615,10 +667,6 @@ HWTEST_F(UtilsMappedFileTest, testReMap004, TestSize.Level0) stat(filename.c_str(), &stb); EXPECT_TRUE(stb.st_size < mf.Size()); - // 9. write to the extended region - *(cur) = 'E'; - EXPECT_EQ((*cur), 'E'); - std::string res; LoadStringFromFile(filename, res); EXPECT_EQ(res, content); // No changes will be sync in the original file, since mapped region @@ -636,31 +684,13 @@ HWTEST_F(UtilsMappedFileTest, testReMap005, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); - ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - - // 3. check status after mapping - ASSERT_TRUE(mf.IsMapped()); - ASSERT_TRUE(mf.IsNormed()); - - // 4. check size struct stat stb = {0}; - stat(filename.c_str(), &stb); - ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); - // 5. read from Mapped File - std::string readout; - char* cur = mf.Begin(); - for (; cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); + CheckStatusAndSizeAndRead(mf, filename, content, &stb); // 6. remap to extend region ASSERT_EQ(mf.Resize(mf.Size() + 10, true), MAPPED_FILE_ERR_OK); @@ -672,10 +702,6 @@ HWTEST_F(UtilsMappedFileTest, testReMap005, TestSize.Level0) stat(filename.c_str(), &stb); EXPECT_TRUE(stb.st_size == mf.Size()); // File size will sync to that of the mapped region. - // 8. write to the extended region - *(cur) = 'E'; - EXPECT_EQ((*cur), 'E'); - std::string res; LoadStringFromFile(filename, res); EXPECT_STREQ(res.c_str(), content.append("E").c_str()); // Changes will be sync in the original file. @@ -690,10 +716,8 @@ HWTEST_F(UtilsMappedFileTest, testReMap006, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - - ASSERT_TRUE(CreateTestFile(filename, content)); + + RecreateFile(filename, content); // 2. map file off_t size = 20; @@ -730,10 +754,8 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext001, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); struct stat stb = {0}; ASSERT_EQ(stat(filename.c_str(), &stb), 0); @@ -761,30 +783,10 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext001, TestSize.Level0) LoadStringFromFile(filename, res); EXPECT_STREQ(res.c_str(), content.append("N").c_str()); - off_t endOff; - // 6. keep turnNext within a page - for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first - // TunrNext() calling in consideration. - endOff = mf.EndOffset(); - EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - EXPECT_EQ(mf.StartOffset(), endOff + 1); - EXPECT_EQ(mf.Size(), orig); - } - std::cout << "==Last TurnNext() with The Same Size==" << std::endl; - PrintStatus(mf); - - // 7. this turn will reach the bottom of a page - endOff = mf.EndOffset(); - char* rEnd = mf.RegionEnd(); - char* end = mf.End(); - EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - EXPECT_EQ(mf.StartOffset(), endOff + 1); - EXPECT_EQ(mf.Size(), static_cast(rEnd - end)); - std::cout << "==Reached Bottom of A Page==" << std::endl; - PrintStatus(mf); + CheckOffset(mf, orig); // 8. this turn will trigger a remapping - endOff = mf.EndOffset(); + off_t endOff = mf.EndOffset(); off_t curSize = mf.Size(); EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); EXPECT_TRUE(mf.IsMapped()); @@ -818,10 +820,8 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext002, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -851,20 +851,18 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext002, TestSize.Level0) HWTEST_F(UtilsMappedFileTest, testTurnNext003, TestSize.Level0) { // 1. create a new file - std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); + std::string filename = "test_remap.txt"; - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); // 3. check status after mapping - ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); + ASSERT_TRUE(mf.IsMapped()); // 4. recommand to unmap first before other operations on the file. ASSERT_EQ(mf.Unmap(), MAPPED_FILE_ERR_OK); @@ -895,10 +893,8 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext004, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -923,10 +919,8 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext005, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use.00"; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); struct stat stb = {0}; ASSERT_EQ(stat(filename.c_str(), &stb), 0); @@ -948,30 +942,10 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext005, TestSize.Level0) // 5. turn next mapped region with the same size as the file's initial size. EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - off_t endOff; - // 6. keep turnNext within a page - for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first - // TunrNext() calling in consideration. - endOff = mf.EndOffset(); - EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - EXPECT_EQ(mf.StartOffset(), endOff + 1); - EXPECT_EQ(mf.Size(), orig); - } - std::cout << "==Last TurnNext() with The Same Size==" << std::endl; - PrintStatus(mf); - - // 7. this turn will reach the bottom of a page - endOff = mf.EndOffset(); - char* rEnd = mf.RegionEnd(); - char* end = mf.End(); - EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - EXPECT_EQ(mf.StartOffset(), endOff + 1); - EXPECT_EQ(mf.Size(), static_cast(rEnd - end)); - std::cout << "==Reached Bottom of A Page==" << std::endl; - PrintStatus(mf); + CheckOffset(mf, orig); // 8. this turn will trigger a remapping - endOff = mf.EndOffset(); + off_t endOff = mf.EndOffset(); EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); EXPECT_TRUE(mf.IsMapped()); EXPECT_EQ(mf.StartOffset(), endOff + 1); @@ -991,10 +965,8 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap001, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_1.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file off_t offset = 100; // Specify offset that is not multiple of page-size. @@ -1022,10 +994,8 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap002, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_2.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file off_t offset = 4 * 1024; // Specify offset excessing the substantial size of the file. @@ -1070,10 +1040,9 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap004, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_4.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); + // 2. map file MappedFile mf(filename, MapMode::DEFAULT, 0, -2); // -2: less than DEFAULT_LENGTH(-1) @@ -1101,10 +1070,8 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap005, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_6.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1127,10 +1094,8 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap006, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_7.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1161,10 +1126,8 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap007, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_8.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1189,10 +1152,8 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap008, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_9.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1224,10 +1185,9 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap009, TestSize.Level0) // 1. create a new file std::string filename = "test_invalid_10.txt"; std::string content = "Test for invalid use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); + // 2. create MappedFile MappedFile mf(filename); @@ -1258,10 +1218,8 @@ HWTEST_F(UtilsMappedFileTest, testAutoAdjustedMode001, TestSize.Level0) // 1. create a new file std::string filename = "test_adjmod_1.txt"; std::string content = "Test for auto adj use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MapMode mode = static_cast(1) | static_cast(16) | @@ -1288,10 +1246,8 @@ HWTEST_F(UtilsMappedFileTest, testAutoAdjustedSize001, TestSize.Level0) // 1. create a new file std::string filename = "test_adjsize_1.txt"; std::string content = "Test for auto adj use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file off_t size = 5 * 1024; // Specified size excessing the last page of the file. @@ -1320,10 +1276,8 @@ HWTEST_F(UtilsMappedFileTest, testAutoAdjustedSize002, TestSize.Level0) // 1. create a new file std::string filename = "test_adjsize_2.txt"; std::string content = "Test for auto adj use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. Extend size manually int fd = open(filename.c_str(), O_RDWR | O_CLOEXEC); @@ -1362,10 +1316,8 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile001, TestSize.Level0) // 1. create a new file std::string filename = "test_move_1.txt"; std::string content = "Test for move use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1399,25 +1351,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile001, TestSize.Level0) EXPECT_EQ(mfNew.GetHint(), hint); EXPECT_EQ(mfNew.GetPath(), filename); - // 5. read from mapped file - std::string readout; - for (char* cur = mfNew.Begin(); cur <= mfNew.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); - - // 6. write to mapped file - std::string toWrite("Complete."); - char* newCur = mfNew.Begin(); - for (std::string::size_type i = 0; i < toWrite.length(); i++) { - (*newCur) = toWrite[i]; - newCur++; - } - std::string res; - LoadStringFromFile(filename, res); - EXPECT_EQ(res, "Complete.move use."); - - RemoveTestFile(filename); + CheckReadComplete(mfNew, filename, content); } /* @@ -1429,10 +1363,8 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile002, TestSize.Level0) // 1. create a new file std::string filename = "test_move_2.txt"; std::string content = "Test for move use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); - ASSERT_TRUE(CreateTestFile(filename, content)); + RecreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1468,25 +1400,8 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile002, TestSize.Level0) // 5. Map again ASSERT_EQ(mfNew.Map(), MAPPED_FILE_ERR_OK); - // 6. read from mapped file - std::string readout; - for (char* cur = mfNew.Begin(); cur <= mfNew.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content); - // 7. write to mapped file - std::string toWrite("Complete."); - char* newCur = mfNew.Begin(); - for (std::string::size_type i = 0; i < toWrite.length(); i++) { - (*newCur) = toWrite[i]; - newCur++; - } - std::string res; - LoadStringFromFile(filename, res); - EXPECT_EQ(res, "Complete.move use."); - - RemoveTestFile(filename); + CheckReadComplete(mfNew, filename, content); } /* @@ -1498,16 +1413,13 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile003, TestSize.Level0) // 1. create a new file std::string filename = "test_move_3.txt"; std::string content = "Test for move use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); std::string filename1 = "test_move_4.txt"; std::string content1 = "Test for move use."; - filename1.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename1); - ASSERT_TRUE(CreateTestFile(filename, content)); - ASSERT_TRUE(CreateTestFile(filename1, content1)); + RecreateFile(filename, content); + RecreateFile(filename1, content1); + // 2. map file MappedFile mf(filename); @@ -1534,26 +1446,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile003, TestSize.Level0) EXPECT_EQ(mf.GetHint(), hint); EXPECT_EQ(mf.GetPath(), filename1); - // 5. read from mapped file - std::string readout; - for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content1); - - // 6. write to mapped file - std::string toWrite("Complete."); - char* newCur = mf.Begin(); - for (std::string::size_type i = 0; i < toWrite.length(); i++) { - (*newCur) = toWrite[i]; - newCur++; - } - std::string res; - LoadStringFromFile(filename1, res); - EXPECT_EQ(res, "Complete.move use."); - - RemoveTestFile(filename); - RemoveTestFile(filename1); + CheckReadCompleteMove(mf, filename, filename1, content1); } /* @@ -1565,21 +1458,16 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile004, TestSize.Level0) // 1. create a new file std::string filename = "test_move_4.txt"; std::string content = "Test for move use."; - filename.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename); + RecreateFile(filename, content); std::string filename1 = "test_move_5.txt"; std::string content1 = "Test for move use."; - filename1.insert(0, SUITE_PATH).insert(0, BASE_PATH); - RemoveTestFile(filename1); - - ASSERT_TRUE(CreateTestFile(filename, content)); - ASSERT_TRUE(CreateTestFile(filename1, content1)); + RecreateFile(filename1, content1); // 2. map file MappedFile mf(filename); - ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); MappedFile mf1(filename1); + ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); ASSERT_EQ(mf1.Map(), MAPPED_FILE_ERR_OK); off_t size = mf1.Size(); @@ -1602,26 +1490,8 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile004, TestSize.Level0) EXPECT_EQ(mf.GetPath(), filename1); ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // 6. read from mapped file - std::string readout; - for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { - readout.push_back(*cur); - } - EXPECT_EQ(readout, content1); - - // 7. write to mapped file - std::string toWrite("Complete."); - char* newCur = mf.Begin(); - for (std::string::size_type i = 0; i < toWrite.length(); i++) { - (*newCur) = toWrite[i]; - newCur++; - } - std::string res; - LoadStringFromFile(filename1, res); - EXPECT_EQ(res, "Complete.move use."); - - RemoveTestFile(filename); - RemoveTestFile(filename1); + + CheckReadCompleteMove(mf, filename, filename1, content1); } } // namespace -- Gitee From 5a4a875eb370475e2052df96dee8462a25754819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Fri, 29 Mar 2024 03:50:19 +0000 Subject: [PATCH 02/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unittest/common/utils_mapped_file_test.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index 322deba..3fdfbb2 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -120,7 +120,7 @@ void RecreateFile(std::string& filename, const std::string& content) ASSERT_TRUE(CreateTestFile(filename, content)); } -void CheckStatusAndSize(MappedFile& mf, const std::string& filename) +void CheckStatusAndSize(MappedFile& mf, const std::string& filename) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); // check status @@ -133,7 +133,8 @@ void CheckStatusAndSize(MappedFile& mf, const std::string& filename) ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); } -void CheckStatusAndSizeAndRead(MappedFile& mf, const std::string& filename, const std::string& content, struct stat* stb) +void CheckStatusAndSizeAndRead(MappedFile& mf, const std::string& filename, + const std::string& content, struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); @@ -176,7 +177,8 @@ void CheckReadAndWriteMappedFile(const MappedFile& mf, const std::string& conten } } -void CheckSizeAndRead(MappedFile& mf, const std::string& filename, const std::string& content, struct stat* stb) +void CheckSizeAndRead(MappedFile& mf, const std::string& filename, + const std::string& content, struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); @@ -194,10 +196,10 @@ void CheckSizeAndRead(MappedFile& mf, const std::string& filename, const std::st readout.push_back(*cur); } EXPECT_EQ(readout, content); - } -void CheckFileEquals(const MappedFile& mf, const std::string& filename, std::string& filename1, std::string& content1, struct stat* stb) +void CheckFileEquals(const MappedFile& mf, const std::string& filename, + std::string& filename1, std::string& content1, struct stat* stb) { // 9. check status after remapping EXPECT_TRUE(mf.IsMapped()); @@ -220,6 +222,7 @@ void CheckFileEquals(const MappedFile& mf, const std::string& filename, std::str void CheckOffset(MappedFile& mf, const off_t& orig) { + ASSERT_NE(orig, 0); off_t endOff; // 6. keep turnNext within a page for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first @@ -266,7 +269,8 @@ void CheckReadComplete(const MappedFile& mfNew, const std::string& filename, con RemoveTestFile(filename); } -void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, const std::string& filename1, const std::string& content1) +void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, + const std::string& filename1, const std::string& content1) { std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { @@ -287,7 +291,6 @@ void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, co RemoveTestFile(filename); RemoveTestFile(filename1); - } /* @@ -601,7 +604,6 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); CheckFileEquals(mf, filename, filename1, content1, &stb); - } /* -- Gitee From 0eeecb545ba70dee81436a3b4d9e53ff66d034a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Fri, 29 Mar 2024 06:32:35 +0000 Subject: [PATCH 03/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils_mapped_file_test.cpp | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index 3fdfbb2..2af4fed 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -133,8 +133,8 @@ void CheckStatusAndSize(MappedFile& mf, const std::string& filename) ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); } -void CheckStatusAndSizeAndRead(MappedFile& mf, const std::string& filename, - const std::string& content, struct stat* stb) +void CheckStatusAndSizeAndRead(MappedFile& mf, const std::string& filename, + const std::string& content, struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); @@ -177,8 +177,8 @@ void CheckReadAndWriteMappedFile(const MappedFile& mf, const std::string& conten } } -void CheckSizeAndRead(MappedFile& mf, const std::string& filename, - const std::string& content, struct stat* stb) +void CheckSizeAndRead(MappedFile& mf, const std::string& filename, + const std::string& content, struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); @@ -198,8 +198,8 @@ void CheckSizeAndRead(MappedFile& mf, const std::string& filename, EXPECT_EQ(readout, content); } -void CheckFileEquals(const MappedFile& mf, const std::string& filename, - std::string& filename1, std::string& content1, struct stat* stb) +void CheckFileEquals(const MappedFile& mf, const std::string& filename, + std::string& filename1, std::string& content1, struct stat* stb) { // 9. check status after remapping EXPECT_TRUE(mf.IsMapped()); @@ -222,15 +222,16 @@ void CheckFileEquals(const MappedFile& mf, const std::string& filename, void CheckOffset(MappedFile& mf, const off_t& orig) { - ASSERT_NE(orig, 0); off_t endOff; // 6. keep turnNext within a page - for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first - // TunrNext() calling in consideration. - endOff = mf.EndOffset(); - EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - EXPECT_EQ(mf.StartOffset(), endOff + 1); - EXPECT_EQ(mf.Size(), orig); + if (orig != 0) { + for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first + // TunrNext() calling in consideration. + endOff = mf.EndOffset(); + EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); + EXPECT_EQ(mf.StartOffset(), endOff + 1); + EXPECT_EQ(mf.Size(), orig); + } } std::cout << "==Last TurnNext() with The Same Size==" << std::endl; PrintStatus(mf); @@ -269,8 +270,8 @@ void CheckReadComplete(const MappedFile& mfNew, const std::string& filename, con RemoveTestFile(filename); } -void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, - const std::string& filename1, const std::string& content1) +void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, + const std::string& filename1, const std::string& content1) { std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { -- Gitee From 098cdd66e543532466c8c1104ee0a54f4d234671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Fri, 29 Mar 2024 06:37:03 +0000 Subject: [PATCH 04/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/test/unittest/common/utils_mapped_file_test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index 2af4fed..3ab053a 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -224,6 +224,7 @@ void CheckOffset(MappedFile& mf, const off_t& orig) { off_t endOff; // 6. keep turnNext within a page + ASSERT_NE(orig, 0); if (orig != 0) { for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first // TunrNext() calling in consideration. -- Gitee From fbf048a055c23e0c34832586af81ef53a51d5918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Fri, 29 Mar 2024 07:26:39 +0000 Subject: [PATCH 05/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils_mapped_file_test.cpp | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index 3ab053a..c655a98 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -133,8 +133,10 @@ void CheckStatusAndSize(MappedFile& mf, const std::string& filename) ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); } -void CheckStatusAndSizeAndRead(MappedFile& mf, const std::string& filename, - const std::string& content, struct stat* stb) +void CheckStatusAndSizeAndRead(MappedFile& mf, + const std::string& filename, + const std::string& content, + struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); @@ -177,8 +179,10 @@ void CheckReadAndWriteMappedFile(const MappedFile& mf, const std::string& conten } } -void CheckSizeAndRead(MappedFile& mf, const std::string& filename, - const std::string& content, struct stat* stb) +void CheckSizeAndRead(MappedFile& mf, + const std::string& filename, + const std::string& content, + struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); @@ -198,8 +202,11 @@ void CheckSizeAndRead(MappedFile& mf, const std::string& filename, EXPECT_EQ(readout, content); } -void CheckFileEquals(const MappedFile& mf, const std::string& filename, - std::string& filename1, std::string& content1, struct stat* stb) +void CheckFileEquals(const MappedFile& mf, + const std::string& filename, + std::string& filename1, + std::string& content1, + struct stat* stb) { // 9. check status after remapping EXPECT_TRUE(mf.IsMapped()); @@ -271,8 +278,10 @@ void CheckReadComplete(const MappedFile& mfNew, const std::string& filename, con RemoveTestFile(filename); } -void CheckReadCompleteMove(const MappedFile& mf, const std::string& filename, - const std::string& filename1, const std::string& content1) +void CheckReadCompleteMove(const MappedFile& mf, + const std::string& filename, + const std::string& filename1, + const std::string& content1) { std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { -- Gitee From bfcdce4d6f077043c2055d95b3ce79160b27aa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Sat, 30 Mar 2024 07:27:00 +0000 Subject: [PATCH 06/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils_mapped_file_test.cpp | 258 ++++++++++-------- 1 file changed, 146 insertions(+), 112 deletions(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index c655a98..b2e60d6 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -112,7 +112,7 @@ bool SaveStringToFile(const std::string& filePath, const std::string& content, o } -void RecreateFile(std::string& filename, const std::string& content) +void ReCreateFile(std::string& filename, const std::string& content) { filename.insert(0, UtilsMappedFileTest::SUITE_PATH).insert(0, UtilsMappedFileTest::BASE_PATH); RemoveTestFile(filename); @@ -120,35 +120,23 @@ void RecreateFile(std::string& filename, const std::string& content) ASSERT_TRUE(CreateTestFile(filename, content)); } -void CheckStatusAndSize(MappedFile& mf, const std::string& filename) -{ - ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // check status - ASSERT_TRUE(mf.IsMapped()); - ASSERT_TRUE(mf.IsNormed()); - - // check size - struct stat stb = {0}; - stat(filename.c_str(), &stb); - ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); -} -void CheckStatusAndSizeAndRead(MappedFile& mf, - const std::string& filename, - const std::string& content, - struct stat* stb) +void TestFileStatusAndRead(MappedFile& mf, + const std::string& filename, + const std::string& content, + struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // 3. check status after mapping + // check status after mapping ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); - // 4. check size + // check size stat(filename.c_str(), stb); ASSERT_TRUE(stb->st_size == mf.Size() || mf.PageSize() == mf.Size()); - // 5. read from Mapped File + // read from Mapped File std::string readout; char* cur = mf.Begin(); for (; cur <= mf.End(); cur++) { @@ -156,21 +144,21 @@ void CheckStatusAndSizeAndRead(MappedFile& mf, } EXPECT_EQ(readout, content); - // 9. write to the extended region + // write to the extended region *(cur) = 'E'; EXPECT_EQ((*cur), 'E'); } -void CheckReadAndWriteMappedFile(const MappedFile& mf, const std::string& content) +void TestFileReadAndWrite(const MappedFile& mf, const std::string& content) { - // 4. read from mapped file + // read from mapped file std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { readout.push_back(*cur); } EXPECT_EQ(readout, content); - // 5. write to mapped file + // write to mapped file std::string toWrite("Complete."); char* newCur = mf.Begin(); for (std::string::size_type i = 0; i < toWrite.length(); i++) { @@ -179,22 +167,22 @@ void CheckReadAndWriteMappedFile(const MappedFile& mf, const std::string& conten } } -void CheckSizeAndRead(MappedFile& mf, - const std::string& filename, - const std::string& content, - struct stat* stb) +void TestFileStat(MappedFile& mf, + const std::string& filename, + const std::string& content, + struct stat* stb) { ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - // 3. check status after mapping + // check status after mapping ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); - // 4. check size + // check size stat(filename.c_str(), stb); ASSERT_TRUE(stb->st_size == mf.Size() || mf.PageSize() == mf.Size()); - // 5. read from Mapped File + // read from Mapped File std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { readout.push_back(*cur); @@ -202,21 +190,21 @@ void CheckSizeAndRead(MappedFile& mf, EXPECT_EQ(readout, content); } -void CheckFileEquals(const MappedFile& mf, - const std::string& filename, - std::string& filename1, - std::string& content1, - struct stat* stb) +void TestFileContentEqual(const MappedFile& mf, + const std::string& filename, + std::string& filename1, + std::string& content1, + struct stat* stb) { - // 9. check status after remapping + // check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); - // 10. check size + // check size stat(filename1.c_str(), stb); EXPECT_TRUE(stb->st_size == mf.Size()); - // 11. read from Mapped File + // read from Mapped File std::string readout1; for (char* cur1 = mf.Begin(); cur1 <= mf.End(); cur1++) { readout1.push_back(*cur1); @@ -227,10 +215,10 @@ void CheckFileEquals(const MappedFile& mf, RemoveTestFile(filename1); } -void CheckOffset(MappedFile& mf, const off_t& orig) +void TestFileRegion(MappedFile& mf, const off_t& orig) { off_t endOff; - // 6. keep turnNext within a page + // keep turnNext within a page ASSERT_NE(orig, 0); if (orig != 0) { for (unsigned int cnt = 2; cnt < (MappedFile::PageSize() / orig); cnt++) { // 2: start from 2 to take the first @@ -244,7 +232,7 @@ void CheckOffset(MappedFile& mf, const off_t& orig) std::cout << "==Last TurnNext() with The Same Size==" << std::endl; PrintStatus(mf); - // 7. this turn will reach the bottom of a page + // this turn will reach the bottom of a page endOff = mf.EndOffset(); char* rEnd = mf.RegionEnd(); char* end = mf.End(); @@ -255,16 +243,16 @@ void CheckOffset(MappedFile& mf, const off_t& orig) PrintStatus(mf); } -void CheckReadComplete(const MappedFile& mfNew, const std::string& filename, const std::string& content) +void TestFileWrite(const MappedFile& mfNew, const std::string& filename, const std::string& content) { - // 6. read from mapped file + // read from mapped file std::string readout; for (char* cur = mfNew.Begin(); cur <= mfNew.End(); cur++) { readout.push_back(*cur); } EXPECT_EQ(readout, content); - // 7. write to mapped file + // write to mapped file std::string toWrite("Complete."); char* newCur = mfNew.Begin(); for (std::string::size_type i = 0; i < toWrite.length(); i++) { @@ -278,10 +266,10 @@ void CheckReadComplete(const MappedFile& mfNew, const std::string& filename, con RemoveTestFile(filename); } -void CheckReadCompleteMove(const MappedFile& mf, - const std::string& filename, - const std::string& filename1, - const std::string& content1) +void TestTwoFileWrite(const MappedFile& mf, + const std::string& filename, + const std::string& filename1, + const std::string& content1) { std::string readout; for (char* cur = mf.Begin(); cur <= mf.End(); cur++) { @@ -313,12 +301,20 @@ HWTEST_F(UtilsMappedFileTest, testDefaultMapping001, TestSize.Level0) // 1. Create a new file std::string filename = "test_read_write_1.txt"; std::string content = "Test for normal use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); + ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - CheckStatusAndSize(mf, filename); + // check status + ASSERT_TRUE(mf.IsMapped()); + ASSERT_TRUE(mf.IsNormed()); + + // check size + struct stat stb = {0}; + stat(filename.c_str(), &stb); + ASSERT_TRUE(stb.st_size == mf.Size() || mf.PageSize() == mf.Size()); // check map-mode ASSERT_EQ(MapMode::DEFAULT, mf.GetMode()); @@ -326,7 +322,10 @@ HWTEST_F(UtilsMappedFileTest, testDefaultMapping001, TestSize.Level0) // check offset ASSERT_EQ(mf.StartOffset(), 0u); - CheckReadAndWriteMappedFile(mf, content); + // 3. read from mapped file + // 4. write to mapped file + // extract 3-4 step into a common method + TestFileReadAndWrite(mf, content); std::string res; LoadStringFromFile(filename, res); @@ -480,7 +479,7 @@ HWTEST_F(UtilsMappedFileTest, testPrivateMapping001, TestSize.Level0) // 1. create a new file std::string filename = "test_read_write_4.txt"; std::string content = "Test for private use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename, MapMode::DEFAULT | MapMode::PRIVATE); @@ -490,7 +489,10 @@ HWTEST_F(UtilsMappedFileTest, testPrivateMapping001, TestSize.Level0) ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); - CheckReadAndWriteMappedFile(mf, content); + // 4. read from mapped file + // 5. write to mapped file + // extract 4-5 step into a common method + TestFileReadAndWrite(mf, content); std::string res; LoadStringFromFile(filename, res); @@ -509,7 +511,7 @@ HWTEST_F(UtilsMappedFileTest, testSharedReadOnlyMapping001, TestSize.Level0) std::string filename = "test_read_write_5.txt"; std::string content = "Test for readonly use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename, MapMode::DEFAULT | MapMode::READ_ONLY); @@ -539,7 +541,7 @@ HWTEST_F(UtilsMappedFileTest, testReMap001, TestSize.Level0) // 1. create a new file std::string filename = "test_remap_1.txt"; std::string content = "Test for remapping use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -578,11 +580,11 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for default use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); std::string filename1 = "test_remap_1.txt"; std::string content1 = "Test for remapping use."; - RecreateFile(filename1, content1); + ReCreateFile(filename1, content1); MappedFile mf(filename); @@ -596,10 +598,13 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) ASSERT_TRUE(mf.ChangeHint(reinterpret_cast(0x89000))); // 0x89000: random address. ASSERT_TRUE(mf.ChangeMode(MapMode::READ_ONLY)); - // 4. check size struct stat stb = {0}; - CheckSizeAndRead(mf, filename, content, &stb); + // 3. check status after mapping + // 4. check size + // 5. read from Mapped File + // extract 3-5 step into a common method + TestFileStat(mf, filename, content, &stb); // 6. change params ASSERT_TRUE(mf.ChangePath(filename1)); @@ -614,7 +619,7 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) // 8. remap file ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - CheckFileEquals(mf, filename, filename1, content1, &stb); + TestFileContentEqual(mf, filename, filename1, content1, &stb); } /* @@ -629,15 +634,19 @@ HWTEST_F(UtilsMappedFileTest, testReMap003, TestSize.Level0) std::string filename1 = "test_remap_1.txt"; std::string content1 = "Test for remapping use."; - RecreateFile(filename, content); - RecreateFile(filename1, content1); + ReCreateFile(filename, content); + ReCreateFile(filename1, content1); // 2. map file MappedFile mf(filename); struct stat stb = {0}; - - CheckSizeAndRead(mf, filename, content, &stb); + + // 3. check status after mapping + // 4. check size + // 5. read from Mapped File + // extract 3-5 step into a common method + TestFileStat(mf, filename, content, &stb); // 6. change params mf.ChangePath(filename1); @@ -650,7 +659,7 @@ HWTEST_F(UtilsMappedFileTest, testReMap003, TestSize.Level0) // 8. remap file ASSERT_EQ(mf.Resize(), MAPPED_FILE_ERR_OK); // 9. check status after remapping - CheckFileEquals(mf, filename, filename1, content1, &stb); + TestFileContentEqual(mf, filename, filename1, content1, &stb); } /* @@ -662,21 +671,26 @@ HWTEST_F(UtilsMappedFileTest, testReMap004, TestSize.Level0) // 1. create a new file std::string content = "Test for remapping use."; std::string filename = "test_remap.txt"; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); struct stat stb = {0}; - CheckStatusAndSizeAndRead(mf, filename, content, &stb); + // 3. check status after mapping + // 4. check size + // 5. read from Mapped File + // 6. write to the extended region + // extract 3-6 step into a common method + TestFileStatusAndRead(mf, filename, content, &stb); - // 6. Remap to extend region + // 7. Remap to extend region ASSERT_EQ(mf.Resize(mf.Size() + 10), MAPPED_FILE_ERR_OK); - // 7. check status after remapping + // 8. check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); - // 8. check size after remapping + // 9. check size after remapping stat(filename.c_str(), &stb); EXPECT_TRUE(stb.st_size < mf.Size()); @@ -697,21 +711,26 @@ HWTEST_F(UtilsMappedFileTest, testReMap005, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); struct stat stb = {0}; - CheckStatusAndSizeAndRead(mf, filename, content, &stb); + // 3. check status after mapping + // 4. check size + // 5. read from Mapped File + // 6. write to the extended region + // extract 3-6 step into a common method + TestFileStatusAndRead(mf, filename, content, &stb); - // 6. remap to extend region + // 7. remap to extend region ASSERT_EQ(mf.Resize(mf.Size() + 10, true), MAPPED_FILE_ERR_OK); // check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); - // 7. check size after remapping + // 8. check size after remapping stat(filename.c_str(), &stb); EXPECT_TRUE(stb.st_size == mf.Size()); // File size will sync to that of the mapped region. @@ -729,8 +748,8 @@ HWTEST_F(UtilsMappedFileTest, testReMap006, TestSize.Level0) // 1. create a new file std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - - RecreateFile(filename, content); + + ReCreateFile(filename, content); // 2. map file off_t size = 20; @@ -768,7 +787,7 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext001, TestSize.Level0) std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); struct stat stb = {0}; ASSERT_EQ(stat(filename.c_str(), &stb), 0); @@ -796,7 +815,10 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext001, TestSize.Level0) LoadStringFromFile(filename, res); EXPECT_STREQ(res.c_str(), content.append("N").c_str()); - CheckOffset(mf, orig); + // 6. keep turnNext within a page + // 7. this turn will reach the bottom of a page + // extract 6-7 step into a common method + TestFileRegion(mf, orig); // 8. this turn will trigger a remapping off_t endOff = mf.EndOffset(); @@ -834,7 +856,7 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext002, TestSize.Level0) std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -867,7 +889,7 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext003, TestSize.Level0) std::string content = "Test for remapping use."; std::string filename = "test_remap.txt"; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -907,7 +929,7 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext004, TestSize.Level0) std::string filename = "test_remap.txt"; std::string content = "Test for remapping use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -933,7 +955,7 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext005, TestSize.Level0) std::string filename = "test_remap.txt"; std::string content = "Test for remapping use.00"; - RecreateFile(filename, content); + ReCreateFile(filename, content); struct stat stb = {0}; ASSERT_EQ(stat(filename.c_str(), &stb), 0); @@ -955,7 +977,10 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext005, TestSize.Level0) // 5. turn next mapped region with the same size as the file's initial size. EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); - CheckOffset(mf, orig); + // 6. keep turnNext within a page + // 7. this turn will reach the bottom of a page + // extract 6-7 step into a common method + TestFileRegion(mf, orig); // 8. this turn will trigger a remapping off_t endOff = mf.EndOffset(); @@ -979,7 +1004,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap001, TestSize.Level0) std::string filename = "test_invalid_1.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file off_t offset = 100; // Specify offset that is not multiple of page-size. @@ -1008,7 +1033,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap002, TestSize.Level0) std::string filename = "test_invalid_2.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file off_t offset = 4 * 1024; // Specify offset excessing the substantial size of the file. @@ -1054,7 +1079,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap004, TestSize.Level0) std::string filename = "test_invalid_4.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file @@ -1084,7 +1109,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap005, TestSize.Level0) std::string filename = "test_invalid_6.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1108,7 +1133,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap006, TestSize.Level0) std::string filename = "test_invalid_7.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1140,7 +1165,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap007, TestSize.Level0) std::string filename = "test_invalid_8.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1166,7 +1191,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap008, TestSize.Level0) std::string filename = "test_invalid_9.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1176,14 +1201,14 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap008, TestSize.Level0) ASSERT_TRUE(mf.IsMapped()); ASSERT_TRUE(mf.IsNormed()); - // 5. Change params + // 4. Change params ASSERT_TRUE(mf.ChangeSize(mf.Size() + 1)); - // 6. check status + // 5. check status ASSERT_FALSE(mf.IsMapped()); ASSERT_FALSE(mf.IsNormed()); - // 4. turn next. + // 6. turn next. EXPECT_EQ(mf.TurnNext(), ERR_INVALID_OPERATION); RemoveTestFile(filename); @@ -1199,7 +1224,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap009, TestSize.Level0) std::string filename = "test_invalid_10.txt"; std::string content = "Test for invalid use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. create MappedFile @@ -1232,7 +1257,7 @@ HWTEST_F(UtilsMappedFileTest, testAutoAdjustedMode001, TestSize.Level0) std::string filename = "test_adjmod_1.txt"; std::string content = "Test for auto adj use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MapMode mode = static_cast(1) | static_cast(16) | @@ -1260,7 +1285,7 @@ HWTEST_F(UtilsMappedFileTest, testAutoAdjustedSize001, TestSize.Level0) std::string filename = "test_adjsize_1.txt"; std::string content = "Test for auto adj use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file off_t size = 5 * 1024; // Specified size excessing the last page of the file. @@ -1290,7 +1315,7 @@ HWTEST_F(UtilsMappedFileTest, testAutoAdjustedSize002, TestSize.Level0) std::string filename = "test_adjsize_2.txt"; std::string content = "Test for auto adj use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. Extend size manually int fd = open(filename.c_str(), O_RDWR | O_CLOEXEC); @@ -1330,7 +1355,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile001, TestSize.Level0) std::string filename = "test_move_1.txt"; std::string content = "Test for move use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1364,7 +1389,10 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile001, TestSize.Level0) EXPECT_EQ(mfNew.GetHint(), hint); EXPECT_EQ(mfNew.GetPath(), filename); - CheckReadComplete(mfNew, filename, content); + // 5. read from mapped file + // 6. write to mapped file + // extract 5-6 step into a common method + TestFileWrite(mfNew, filename, content); } /* @@ -1377,7 +1405,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile002, TestSize.Level0) std::string filename = "test_move_2.txt"; std::string content = "Test for move use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); // 2. map file MappedFile mf(filename); @@ -1413,8 +1441,10 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile002, TestSize.Level0) // 5. Map again ASSERT_EQ(mfNew.Map(), MAPPED_FILE_ERR_OK); - - CheckReadComplete(mfNew, filename, content); + // 6. read from mapped file + // 7. write to mapped file + // extract 6-7 step into a common method + TestFileWrite(mfNew, filename, content); } /* @@ -1430,9 +1460,8 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile003, TestSize.Level0) std::string filename1 = "test_move_4.txt"; std::string content1 = "Test for move use."; - RecreateFile(filename, content); - RecreateFile(filename1, content1); - + ReCreateFile(filename, content); + ReCreateFile(filename1, content1); // 2. map file MappedFile mf(filename); @@ -1458,8 +1487,11 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile003, TestSize.Level0) EXPECT_EQ(mf.GetMode(), mode); EXPECT_EQ(mf.GetHint(), hint); EXPECT_EQ(mf.GetPath(), filename1); - - CheckReadCompleteMove(mf, filename, filename1, content1); + + // 5. read from mapped file + // 6. write to mapped file + // extract 5-6 step into a common method + TestTwoFileWrite(mf, filename, filename1, content1); } /* @@ -1471,11 +1503,11 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile004, TestSize.Level0) // 1. create a new file std::string filename = "test_move_4.txt"; std::string content = "Test for move use."; - RecreateFile(filename, content); + ReCreateFile(filename, content); std::string filename1 = "test_move_5.txt"; std::string content1 = "Test for move use."; - RecreateFile(filename1, content1); + ReCreateFile(filename1, content1); // 2. map file MappedFile mf(filename); @@ -1503,8 +1535,10 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile004, TestSize.Level0) EXPECT_EQ(mf.GetPath(), filename1); ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); - - CheckReadCompleteMove(mf, filename, filename1, content1); + // 6. read from mapped file + // 7. write to mapped file + // extract 6-7 step into a common method + TestTwoFileWrite(mf, filename, filename1, content1); } } // namespace -- Gitee From fe45ffc4927f27c4f41c810c48810910b7ffec59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Sat, 30 Mar 2024 07:36:03 +0000 Subject: [PATCH 07/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/test/unittest/common/utils_mapped_file_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index b2e60d6..d402180 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -1487,7 +1487,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile003, TestSize.Level0) EXPECT_EQ(mf.GetMode(), mode); EXPECT_EQ(mf.GetHint(), hint); EXPECT_EQ(mf.GetPath(), filename1); - + // 5. read from mapped file // 6. write to mapped file // extract 5-6 step into a common method -- Gitee From 15e9912479dbb4fed6308601c3c44f7690b93e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Sat, 30 Mar 2024 12:05:08 +0000 Subject: [PATCH 08/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils_mapped_file_test.cpp | 93 ++++++++----------- 1 file changed, 41 insertions(+), 52 deletions(-) diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index d402180..bccebd3 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -126,6 +126,7 @@ void TestFileStatusAndRead(MappedFile& mf, const std::string& content, struct stat* stb) { + ASSERT_NE(stb, nullptr); ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); // check status after mapping @@ -172,6 +173,7 @@ void TestFileStat(MappedFile& mf, const std::string& content, struct stat* stb) { + ASSERT_NE(stb, nullptr); ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); // check status after mapping @@ -196,6 +198,7 @@ void TestFileContentEqual(const MappedFile& mf, std::string& content1, struct stat* stb) { + ASSERT_NE(stb, nullptr); // check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); @@ -215,7 +218,7 @@ void TestFileContentEqual(const MappedFile& mf, RemoveTestFile(filename1); } -void TestFileRegion(MappedFile& mf, const off_t& orig) +void TestFileRegion(MappedFile& mf, const off_t orig) { off_t endOff; // keep turnNext within a page @@ -323,8 +326,7 @@ HWTEST_F(UtilsMappedFileTest, testDefaultMapping001, TestSize.Level0) ASSERT_EQ(mf.StartOffset(), 0u); // 3. read from mapped file - // 4. write to mapped file - // extract 3-4 step into a common method + // write to mapped file TestFileReadAndWrite(mf, content); std::string res; @@ -490,8 +492,7 @@ HWTEST_F(UtilsMappedFileTest, testPrivateMapping001, TestSize.Level0) ASSERT_TRUE(mf.IsNormed()); // 4. read from mapped file - // 5. write to mapped file - // extract 4-5 step into a common method + // write to mapped file TestFileReadAndWrite(mf, content); std::string res; @@ -601,22 +602,21 @@ HWTEST_F(UtilsMappedFileTest, testReMap002, TestSize.Level0) struct stat stb = {0}; // 3. check status after mapping - // 4. check size - // 5. read from Mapped File - // extract 3-5 step into a common method + // check size + // read from Mapped File TestFileStat(mf, filename, content, &stb); - // 6. change params + // 4. change params ASSERT_TRUE(mf.ChangePath(filename1)); ASSERT_TRUE(mf.ChangeSize(MappedFile::DEFAULT_LENGTH)); ASSERT_TRUE(mf.ChangeHint(reinterpret_cast(0x80000))); // 0x80000: random address. ASSERT_TRUE(mf.ChangeMode(MapMode::DEFAULT | MapMode::CREATE_IF_ABSENT)); - // 7. check status after changing + // 5. check status after changing EXPECT_FALSE(mf.IsMapped()); EXPECT_FALSE(mf.IsNormed()); - // 8. remap file + // 6. remap file ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); TestFileContentEqual(mf, filename, filename1, content1, &stb); @@ -641,24 +641,23 @@ HWTEST_F(UtilsMappedFileTest, testReMap003, TestSize.Level0) MappedFile mf(filename); struct stat stb = {0}; - + // 3. check status after mapping - // 4. check size - // 5. read from Mapped File - // extract 3-5 step into a common method + // check size + // read from Mapped File TestFileStat(mf, filename, content, &stb); - // 6. change params + // 4. change params mf.ChangePath(filename1); mf.ChangeSize(MappedFile::DEFAULT_LENGTH); - // 7. check status after changing + // 5. check status after changing EXPECT_FALSE(mf.IsMapped()); EXPECT_FALSE(mf.IsNormed()); - // 8. remap file + // 6. remap file ASSERT_EQ(mf.Resize(), MAPPED_FILE_ERR_OK); - // 9. check status after remapping + // 7. check status after remapping TestFileContentEqual(mf, filename, filename1, content1, &stb); } @@ -678,19 +677,18 @@ HWTEST_F(UtilsMappedFileTest, testReMap004, TestSize.Level0) struct stat stb = {0}; // 3. check status after mapping - // 4. check size - // 5. read from Mapped File - // 6. write to the extended region - // extract 3-6 step into a common method + // check size + // read from Mapped File + // write to the extended region TestFileStatusAndRead(mf, filename, content, &stb); - // 7. Remap to extend region + // 4. Remap to extend region ASSERT_EQ(mf.Resize(mf.Size() + 10), MAPPED_FILE_ERR_OK); - // 8. check status after remapping + // 5. check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); - // 9. check size after remapping + // 6. check size after remapping stat(filename.c_str(), &stb); EXPECT_TRUE(stb.st_size < mf.Size()); @@ -718,19 +716,18 @@ HWTEST_F(UtilsMappedFileTest, testReMap005, TestSize.Level0) struct stat stb = {0}; // 3. check status after mapping - // 4. check size - // 5. read from Mapped File - // 6. write to the extended region - // extract 3-6 step into a common method + // check size + // read from Mapped File + // write to the extended region TestFileStatusAndRead(mf, filename, content, &stb); - // 7. remap to extend region + // 4. remap to extend region ASSERT_EQ(mf.Resize(mf.Size() + 10, true), MAPPED_FILE_ERR_OK); // check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); - // 8. check size after remapping + // 5. check size after remapping stat(filename.c_str(), &stb); EXPECT_TRUE(stb.st_size == mf.Size()); // File size will sync to that of the mapped region. @@ -767,11 +764,11 @@ HWTEST_F(UtilsMappedFileTest, testReMap006, TestSize.Level0) ASSERT_EQ(mf.Resize(MappedFile::DEFAULT_LENGTH, true), MAPPED_FILE_ERR_OK); off_t lessSize = mf.Size() - 8; ASSERT_EQ(mf.Resize(lessSize, true), MAPPED_FILE_ERR_OK); - // check status after remapping + // 6. check status after remapping EXPECT_TRUE(mf.IsMapped()); EXPECT_TRUE(mf.IsNormed()); - // 6. check size after remapping + // 7. check size after remapping struct stat stb = {0}; stat(filename.c_str(), &stb); EXPECT_EQ(lessSize, mf.Size()); @@ -816,11 +813,10 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext001, TestSize.Level0) EXPECT_STREQ(res.c_str(), content.append("N").c_str()); // 6. keep turnNext within a page - // 7. this turn will reach the bottom of a page - // extract 6-7 step into a common method + // this turn will reach the bottom of a page TestFileRegion(mf, orig); - // 8. this turn will trigger a remapping + // 7. this turn will trigger a remapping off_t endOff = mf.EndOffset(); off_t curSize = mf.Size(); EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); @@ -832,7 +828,7 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext001, TestSize.Level0) std::cout << "==Remap A New Page==" << std::endl; PrintStatus(mf); - // 9. keep turnNext within a page + // 8. keep turnNext within a page for (off_t cnt = 1; cnt < (MappedFile::PageSize() / 100LL / curSize); cnt++) { endOff = mf.EndOffset(); EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); @@ -978,11 +974,10 @@ HWTEST_F(UtilsMappedFileTest, testTurnNext005, TestSize.Level0) EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); // 6. keep turnNext within a page - // 7. this turn will reach the bottom of a page - // extract 6-7 step into a common method + // this turn will reach the bottom of a page TestFileRegion(mf, orig); - // 8. this turn will trigger a remapping + // 7. this turn will trigger a remapping off_t endOff = mf.EndOffset(); EXPECT_EQ(mf.TurnNext(), MAPPED_FILE_ERR_OK); EXPECT_TRUE(mf.IsMapped()); @@ -1081,7 +1076,6 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap004, TestSize.Level0) ReCreateFile(filename, content); - // 2. map file MappedFile mf(filename, MapMode::DEFAULT, 0, -2); // -2: less than DEFAULT_LENGTH(-1) ASSERT_EQ(mf.Map(), ERR_INVALID_VALUE); @@ -1090,7 +1084,7 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap004, TestSize.Level0) MappedFile mf1(filename, MapMode::DEFAULT, 0, 0); ASSERT_EQ(mf1.Map(), ERR_INVALID_VALUE); - // 3. check status + // 4. check status EXPECT_FALSE(mf.IsMapped()); EXPECT_FALSE(mf.IsNormed()); // mapping will fail in normalize stage. EXPECT_FALSE(mf1.IsMapped()); @@ -1226,7 +1220,6 @@ HWTEST_F(UtilsMappedFileTest, testInvalidMap009, TestSize.Level0) ReCreateFile(filename, content); - // 2. create MappedFile MappedFile mf(filename); @@ -1390,8 +1383,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile001, TestSize.Level0) EXPECT_EQ(mfNew.GetPath(), filename); // 5. read from mapped file - // 6. write to mapped file - // extract 5-6 step into a common method + // write to mapped file TestFileWrite(mfNew, filename, content); } @@ -1442,8 +1434,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile002, TestSize.Level0) // 5. Map again ASSERT_EQ(mfNew.Map(), MAPPED_FILE_ERR_OK); // 6. read from mapped file - // 7. write to mapped file - // extract 6-7 step into a common method + // write to mapped file TestFileWrite(mfNew, filename, content); } @@ -1489,8 +1480,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile003, TestSize.Level0) EXPECT_EQ(mf.GetPath(), filename1); // 5. read from mapped file - // 6. write to mapped file - // extract 5-6 step into a common method + // write to mapped file TestTwoFileWrite(mf, filename, filename1, content1); } @@ -1536,8 +1526,7 @@ HWTEST_F(UtilsMappedFileTest, testMoveMappedFile004, TestSize.Level0) ASSERT_EQ(mf.Map(), MAPPED_FILE_ERR_OK); // 6. read from mapped file - // 7. write to mapped file - // extract 6-7 step into a common method + // write to mapped file TestTwoFileWrite(mf, filename, filename1, content1); } -- Gitee From 9074371e22904044638752026e9dcef4db91e17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Sat, 30 Mar 2024 12:20:25 +0000 Subject: [PATCH 09/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9CWVH=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/test/unittest/common/utils_file_test.cpp | 34 +++++------ .../unittest/common/utils_safe_map_test.cpp | 61 +++++++++---------- .../common/utils_sorted_vector_test.cpp | 51 ++++------------ .../unittest/common/utils_thread_test.cpp | 57 ++++++----------- 4 files changed, 76 insertions(+), 127 deletions(-) diff --git a/base/test/unittest/common/utils_file_test.cpp b/base/test/unittest/common/utils_file_test.cpp index 21c9c73..ff98966 100644 --- a/base/test/unittest/common/utils_file_test.cpp +++ b/base/test/unittest/common/utils_file_test.cpp @@ -382,25 +382,31 @@ HWTEST_F(UtilsFileTest, testSaveStringToFd001, TestSize.Level0) EXPECT_EQ(ret, false); } -/* - * @tc.name: testSaveStringToFd002 - * @tc.desc: Test writting an empty string to a file specified by its fd - */ -HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) + +void OpenAndLoadFileContent(string& content, string& loadResult) { - string content; - string filename = FILE_PATH; + string filename = UtilsFileTest::FILE_PATH; int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); bool ret = SaveStringToFd(fd, content); close(fd); EXPECT_EQ(ret, true); - string loadResult; fd = open(filename.c_str(), O_RDONLY); ret = LoadStringFromFd(fd, loadResult); close(fd); RemoveTestFile(filename); EXPECT_EQ(ret, true); +} + +/* + * @tc.name: testSaveStringToFd002 + * @tc.desc: Test writting an empty string to a file specified by its fd + */ +HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) +{ + string content; + string loadResult; + OpenAndLoadFileContent(content, loadResult); EXPECT_EQ(loadResult, ""); } @@ -411,18 +417,8 @@ HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) HWTEST_F(UtilsFileTest, testSaveStringToFd003, TestSize.Level0) { string content = CONTENT_STR; - string filename = FILE_PATH; - int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - bool ret = SaveStringToFd(fd, content); - close(fd); - EXPECT_EQ(ret, true); - string loadResult; - fd = open(filename.c_str(), O_RDONLY); - ret = LoadStringFromFd(fd, loadResult); - close(fd); - RemoveTestFile(filename); - EXPECT_EQ(ret, true); + OpenAndLoadFileContent(content, loadResult); EXPECT_EQ(loadResult, content); } diff --git a/base/test/unittest/common/utils_safe_map_test.cpp b/base/test/unittest/common/utils_safe_map_test.cpp index 0b90909..d9b3b38 100644 --- a/base/test/unittest/common/utils_safe_map_test.cpp +++ b/base/test/unittest/common/utils_safe_map_test.cpp @@ -311,6 +311,26 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndRead001, TestSize.Level0) }); } +void TestThreadRunResult(std::thread (&threads)[THREAD_NUM], + vector &result, + std::vector> &vcfi) +{ + std::this_thread::sleep_for(std::chrono::seconds(4)); + for (auto& t : threads) { + t.join(); + } + + for (auto& t : vcfi) { + result.push_back(t.get()); + } + + std::sort(result.begin(), result.end()); + + for (int i = 0; i < THREAD_NUM; ++i) { + ASSERT_EQ(i, result[i]); + } +} + /* * @tc.name: testUtilsConcurrentWriteAndFind001 * @tc.desc: 100 threads test in writein to the corresponding key of the map, @@ -323,12 +343,6 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) std::vector> vcfi; ASSERT_NO_THROW({ - auto lamfuncInsert = [](SafeMap& data, const string& key, - const int& value, const std::chrono::system_clock::time_point& absTime) { - std::this_thread::sleep_until(absTime); - data.EnsureInsert(key, value); - }; - auto lamfuncCheckLoop = [](SafeMap& data, const string& key, std::chrono::system_clock::time_point absTime) { std::this_thread::sleep_until(absTime); @@ -339,6 +353,12 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) return i; }; + auto lamfuncInsert = [](SafeMap& data, const string& key, + const int& value, const std::chrono::system_clock::time_point& absTime) { + std::this_thread::sleep_until(absTime); + data.EnsureInsert(key, value); + }; + using std::chrono::system_clock; std::time_t timeT = system_clock::to_time_t(system_clock::now()); @@ -352,22 +372,9 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) std::ref(demoData), key + std::to_string(i), system_clock::from_time_t(timeT))); } - std::this_thread::sleep_for(std::chrono::seconds(4)); - for (auto& t : threads) { - t.join(); - } - vector result; - for (auto& t : vcfi) { - result.push_back(t.get()); - } - - std::sort(result.begin(), result.end()); - - for (int i = 0; i < THREAD_NUM; ++i) { - ASSERT_EQ(i, result[i]); - } + TestThreadRunResult(threads, result, vcfi); }); } @@ -412,21 +419,9 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFindAndSet001, TestSize.Level0 std::ref(demoData), key + std::to_string(i), i + 1, system_clock::from_time_t(timeT))); } - std::this_thread::sleep_for(std::chrono::seconds(4)); - for (auto& t : threads) { - t.join(); - } - vector result; - for (auto& t : vcfi) { - result.push_back(t.get()); - } - - std::sort(result.begin(), result.end()); - for (int i = 0; i < THREAD_NUM; ++i) { - ASSERT_EQ(i, result[i]); - } + TestThreadRunResult(threads, result, vcfi); int t = 0; result.clear(); diff --git a/base/test/unittest/common/utils_sorted_vector_test.cpp b/base/test/unittest/common/utils_sorted_vector_test.cpp index 8316b54..54878da 100644 --- a/base/test/unittest/common/utils_sorted_vector_test.cpp +++ b/base/test/unittest/common/utils_sorted_vector_test.cpp @@ -68,11 +68,8 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedAllowDup, TestSize.Level0) } } -HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowDuplicate, TestSize.Level0) -{ - SortedVector svec; - std::vector vec; - +template +void PushItem(SortedVector& svec, std::vector& vec) { for (int i = 0; i < 20; i++) { vec.push_back(i); } @@ -84,6 +81,14 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowDuplicate, TestSize.Level0 for (int i = 0; i < 20; i++) { svec.Add(i); } +} + +HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowDuplicate, TestSize.Level0) +{ + SortedVector svec; + std::vector vec; + + PushItem(svec, vec); ASSERT_EQ(static_cast(30), svec.Size()); @@ -101,17 +106,7 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowToAlloworNotAllow, TestSiz SortedVector svec; std::vector vec; - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } - - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } + PushItem(svec, vec); ASSERT_EQ(static_cast(20), svec.Size()); @@ -159,17 +154,7 @@ HWTEST_F(UtilsSortedVector, testOperatorEqAllowToNotAllow, TestSize.Level0) SortedVector svec; std::vector vec; - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } - - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } + PushItem(svec, vec); ASSERT_EQ(static_cast(30), svec.Size()); SortedVector newSvec = svec; @@ -186,17 +171,7 @@ HWTEST_F(UtilsSortedVector, testOperatorEqNotAllowToAllowOrNotAllow, TestSize.Le SortedVector svec; std::vector vec; - for (int i = 0; i < 20; i++) { - vec.push_back(i); - } - - for (int i = 9; i >= 0; i--) { - svec.Add(i); - } - - for (int i = 0; i < 20; i++) { - svec.Add(i); - } + PushItem(svec, vec); ASSERT_EQ(static_cast(20), svec.Size()); SortedVector newSvecFalse = svec; diff --git a/base/test/unittest/common/utils_thread_test.cpp b/base/test/unittest/common/utils_thread_test.cpp index 7da8ede..07b35ac 100644 --- a/base/test/unittest/common/utils_thread_test.cpp +++ b/base/test/unittest/common/utils_thread_test.cpp @@ -115,11 +115,24 @@ bool TestThread::Run() prctl(PR_GET_NAME, threadName, 0, 0); name_ = threadName; - if (runFunc_ != nullptr) { - return (*runFunc_)(data_); - } + return (runFunc_ != nullptr) ? (*runFunc_)(data_) : false; +} - return false; +void TestThreadRun(std::unique_ptr &test, int times) { + pthread_t thread = test->GetThread(); + + // pthread_equal return non-zero if equal + EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); + + // ReadyToWork return false, RUN will not be called! + EXPECT_EQ(test->priority_, DEFAULT_PRIO); + EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); + + EXPECT_EQ(test->data_, 0); + EXPECT_EQ(times, 0); + test->NotifyExitSync(); + sleep(1); + EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); } /* @@ -189,20 +202,7 @@ HWTEST_F(UtilsThreadTest, testThread003, TestSize.Level0) ThreadStatus status = test->Start("test_thread_03", THREAD_PROI_LOW, 1024); EXPECT_EQ(status == ThreadStatus::OK, true); - pthread_t thread = test->GetThread(); - - // pthread_equal return non-zero if equal - EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); - - // ReadyToWork return false, RUN will not be called! - EXPECT_EQ(test->priority_, DEFAULT_PRIO); - EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); - - EXPECT_EQ(test->data_, 0); - EXPECT_EQ(times, 0); - test->NotifyExitSync(); - sleep(1); - EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); + TestThreadRun(test, times); } /* @@ -244,20 +244,7 @@ HWTEST_F(UtilsThreadTest, testThread005, TestSize.Level0) ThreadStatus status = test->Start("test_thread_05", THREAD_PROI_LOW, 1024); EXPECT_EQ(status == ThreadStatus::OK, true); - pthread_t thread = test->GetThread(); - - // pthread_equal return non-zero if equal - EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); - - // ReadyToWork return false, RUN will not be called! - EXPECT_EQ(test->priority_, DEFAULT_PRIO); - EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); - - EXPECT_EQ(test->data_, 0); - EXPECT_EQ(times, 0); - test->NotifyExitSync(); - sleep(1); - EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); + TestThreadRun(test, times); } /* @@ -365,11 +352,7 @@ bool TestThread2::Run() prctl(PR_GET_NAME, threadName, 0, 0); name_ = threadName; - if (runFunc_ != nullptr) { - return (*runFunc_)(data_); - } - - return false; + return (runFunc_ != nullptr) ? (*runFunc_)(data_) : false; } /* -- Gitee From a13d5580aa2e4e4e7084c72ec7673e15c595bc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=AE=9D=E7=9B=B8?= Date: Sat, 30 Mar 2024 13:22:05 +0000 Subject: [PATCH 10/10] =?UTF-8?q?issue:https://gitee.com/openharmony/commo?= =?UTF-8?q?nlibrary=5Fc=5Futils/issues/I9AGBB=3Ffrom=3Dproject-issue=20Sig?= =?UTF-8?q?ned-off-by:=20=E9=9F=A6=E5=AE=9D=E7=9B=B8=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/test/unittest/common/utils_file_test.cpp | 34 ++++++----- .../common/utils_mapped_file_test.cpp | 3 +- .../unittest/common/utils_safe_map_test.cpp | 61 ++++++++++--------- .../common/utils_sorted_vector_test.cpp | 51 ++++++++++++---- .../unittest/common/utils_thread_test.cpp | 57 +++++++++++------ 5 files changed, 128 insertions(+), 78 deletions(-) diff --git a/base/test/unittest/common/utils_file_test.cpp b/base/test/unittest/common/utils_file_test.cpp index ff98966..21c9c73 100644 --- a/base/test/unittest/common/utils_file_test.cpp +++ b/base/test/unittest/common/utils_file_test.cpp @@ -382,31 +382,25 @@ HWTEST_F(UtilsFileTest, testSaveStringToFd001, TestSize.Level0) EXPECT_EQ(ret, false); } - -void OpenAndLoadFileContent(string& content, string& loadResult) +/* + * @tc.name: testSaveStringToFd002 + * @tc.desc: Test writting an empty string to a file specified by its fd + */ +HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) { - string filename = UtilsFileTest::FILE_PATH; + string content; + string filename = FILE_PATH; int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); bool ret = SaveStringToFd(fd, content); close(fd); EXPECT_EQ(ret, true); + string loadResult; fd = open(filename.c_str(), O_RDONLY); ret = LoadStringFromFd(fd, loadResult); close(fd); RemoveTestFile(filename); EXPECT_EQ(ret, true); -} - -/* - * @tc.name: testSaveStringToFd002 - * @tc.desc: Test writting an empty string to a file specified by its fd - */ -HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) -{ - string content; - string loadResult; - OpenAndLoadFileContent(content, loadResult); EXPECT_EQ(loadResult, ""); } @@ -417,8 +411,18 @@ HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) HWTEST_F(UtilsFileTest, testSaveStringToFd003, TestSize.Level0) { string content = CONTENT_STR; + string filename = FILE_PATH; + int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + bool ret = SaveStringToFd(fd, content); + close(fd); + EXPECT_EQ(ret, true); + string loadResult; - OpenAndLoadFileContent(content, loadResult); + fd = open(filename.c_str(), O_RDONLY); + ret = LoadStringFromFd(fd, loadResult); + close(fd); + RemoveTestFile(filename); + EXPECT_EQ(ret, true); EXPECT_EQ(loadResult, content); } diff --git a/base/test/unittest/common/utils_mapped_file_test.cpp b/base/test/unittest/common/utils_mapped_file_test.cpp index bccebd3..4d30773 100644 --- a/base/test/unittest/common/utils_mapped_file_test.cpp +++ b/base/test/unittest/common/utils_mapped_file_test.cpp @@ -111,7 +111,6 @@ bool SaveStringToFile(const std::string& filePath, const std::string& content, o return true; } - void ReCreateFile(std::string& filename, const std::string& content) { filename.insert(0, UtilsMappedFileTest::SUITE_PATH).insert(0, UtilsMappedFileTest::BASE_PATH); @@ -120,7 +119,6 @@ void ReCreateFile(std::string& filename, const std::string& content) ASSERT_TRUE(CreateTestFile(filename, content)); } - void TestFileStatusAndRead(MappedFile& mf, const std::string& filename, const std::string& content, @@ -198,6 +196,7 @@ void TestFileContentEqual(const MappedFile& mf, std::string& content1, struct stat* stb) { + // check pointer not null ASSERT_NE(stb, nullptr); // check status after remapping EXPECT_TRUE(mf.IsMapped()); diff --git a/base/test/unittest/common/utils_safe_map_test.cpp b/base/test/unittest/common/utils_safe_map_test.cpp index d9b3b38..0b90909 100644 --- a/base/test/unittest/common/utils_safe_map_test.cpp +++ b/base/test/unittest/common/utils_safe_map_test.cpp @@ -311,26 +311,6 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndRead001, TestSize.Level0) }); } -void TestThreadRunResult(std::thread (&threads)[THREAD_NUM], - vector &result, - std::vector> &vcfi) -{ - std::this_thread::sleep_for(std::chrono::seconds(4)); - for (auto& t : threads) { - t.join(); - } - - for (auto& t : vcfi) { - result.push_back(t.get()); - } - - std::sort(result.begin(), result.end()); - - for (int i = 0; i < THREAD_NUM; ++i) { - ASSERT_EQ(i, result[i]); - } -} - /* * @tc.name: testUtilsConcurrentWriteAndFind001 * @tc.desc: 100 threads test in writein to the corresponding key of the map, @@ -343,6 +323,12 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) std::vector> vcfi; ASSERT_NO_THROW({ + auto lamfuncInsert = [](SafeMap& data, const string& key, + const int& value, const std::chrono::system_clock::time_point& absTime) { + std::this_thread::sleep_until(absTime); + data.EnsureInsert(key, value); + }; + auto lamfuncCheckLoop = [](SafeMap& data, const string& key, std::chrono::system_clock::time_point absTime) { std::this_thread::sleep_until(absTime); @@ -353,12 +339,6 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) return i; }; - auto lamfuncInsert = [](SafeMap& data, const string& key, - const int& value, const std::chrono::system_clock::time_point& absTime) { - std::this_thread::sleep_until(absTime); - data.EnsureInsert(key, value); - }; - using std::chrono::system_clock; std::time_t timeT = system_clock::to_time_t(system_clock::now()); @@ -372,9 +352,22 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFind001, TestSize.Level0) std::ref(demoData), key + std::to_string(i), system_clock::from_time_t(timeT))); } + std::this_thread::sleep_for(std::chrono::seconds(4)); + for (auto& t : threads) { + t.join(); + } + vector result; - TestThreadRunResult(threads, result, vcfi); + for (auto& t : vcfi) { + result.push_back(t.get()); + } + + std::sort(result.begin(), result.end()); + + for (int i = 0; i < THREAD_NUM; ++i) { + ASSERT_EQ(i, result[i]); + } }); } @@ -419,9 +412,21 @@ HWTEST_F(UtilsSafeMap, testUtilsConcurrentWriteAndFindAndSet001, TestSize.Level0 std::ref(demoData), key + std::to_string(i), i + 1, system_clock::from_time_t(timeT))); } + std::this_thread::sleep_for(std::chrono::seconds(4)); + for (auto& t : threads) { + t.join(); + } + vector result; + for (auto& t : vcfi) { + result.push_back(t.get()); + } + + std::sort(result.begin(), result.end()); - TestThreadRunResult(threads, result, vcfi); + for (int i = 0; i < THREAD_NUM; ++i) { + ASSERT_EQ(i, result[i]); + } int t = 0; result.clear(); diff --git a/base/test/unittest/common/utils_sorted_vector_test.cpp b/base/test/unittest/common/utils_sorted_vector_test.cpp index 54878da..8316b54 100644 --- a/base/test/unittest/common/utils_sorted_vector_test.cpp +++ b/base/test/unittest/common/utils_sorted_vector_test.cpp @@ -68,8 +68,11 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedAllowDup, TestSize.Level0) } } -template -void PushItem(SortedVector& svec, std::vector& vec) { +HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowDuplicate, TestSize.Level0) +{ + SortedVector svec; + std::vector vec; + for (int i = 0; i < 20; i++) { vec.push_back(i); } @@ -81,14 +84,6 @@ void PushItem(SortedVector& svec, std::vector& vec) { for (int i = 0; i < 20; i++) { svec.Add(i); } -} - -HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowDuplicate, TestSize.Level0) -{ - SortedVector svec; - std::vector vec; - - PushItem(svec, vec); ASSERT_EQ(static_cast(30), svec.Size()); @@ -106,7 +101,17 @@ HWTEST_F(UtilsSortedVector, testConsFromSortedNotAllowToAlloworNotAllow, TestSiz SortedVector svec; std::vector vec; - PushItem(svec, vec); + for (int i = 0; i < 20; i++) { + vec.push_back(i); + } + + for (int i = 9; i >= 0; i--) { + svec.Add(i); + } + + for (int i = 0; i < 20; i++) { + svec.Add(i); + } ASSERT_EQ(static_cast(20), svec.Size()); @@ -154,7 +159,17 @@ HWTEST_F(UtilsSortedVector, testOperatorEqAllowToNotAllow, TestSize.Level0) SortedVector svec; std::vector vec; - PushItem(svec, vec); + for (int i = 0; i < 20; i++) { + vec.push_back(i); + } + + for (int i = 9; i >= 0; i--) { + svec.Add(i); + } + + for (int i = 0; i < 20; i++) { + svec.Add(i); + } ASSERT_EQ(static_cast(30), svec.Size()); SortedVector newSvec = svec; @@ -171,7 +186,17 @@ HWTEST_F(UtilsSortedVector, testOperatorEqNotAllowToAllowOrNotAllow, TestSize.Le SortedVector svec; std::vector vec; - PushItem(svec, vec); + for (int i = 0; i < 20; i++) { + vec.push_back(i); + } + + for (int i = 9; i >= 0; i--) { + svec.Add(i); + } + + for (int i = 0; i < 20; i++) { + svec.Add(i); + } ASSERT_EQ(static_cast(20), svec.Size()); SortedVector newSvecFalse = svec; diff --git a/base/test/unittest/common/utils_thread_test.cpp b/base/test/unittest/common/utils_thread_test.cpp index 07b35ac..7da8ede 100644 --- a/base/test/unittest/common/utils_thread_test.cpp +++ b/base/test/unittest/common/utils_thread_test.cpp @@ -115,24 +115,11 @@ bool TestThread::Run() prctl(PR_GET_NAME, threadName, 0, 0); name_ = threadName; - return (runFunc_ != nullptr) ? (*runFunc_)(data_) : false; -} - -void TestThreadRun(std::unique_ptr &test, int times) { - pthread_t thread = test->GetThread(); - - // pthread_equal return non-zero if equal - EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); - - // ReadyToWork return false, RUN will not be called! - EXPECT_EQ(test->priority_, DEFAULT_PRIO); - EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); + if (runFunc_ != nullptr) { + return (*runFunc_)(data_); + } - EXPECT_EQ(test->data_, 0); - EXPECT_EQ(times, 0); - test->NotifyExitSync(); - sleep(1); - EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); + return false; } /* @@ -202,7 +189,20 @@ HWTEST_F(UtilsThreadTest, testThread003, TestSize.Level0) ThreadStatus status = test->Start("test_thread_03", THREAD_PROI_LOW, 1024); EXPECT_EQ(status == ThreadStatus::OK, true); - TestThreadRun(test, times); + pthread_t thread = test->GetThread(); + + // pthread_equal return non-zero if equal + EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); + + // ReadyToWork return false, RUN will not be called! + EXPECT_EQ(test->priority_, DEFAULT_PRIO); + EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); + + EXPECT_EQ(test->data_, 0); + EXPECT_EQ(times, 0); + test->NotifyExitSync(); + sleep(1); + EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); } /* @@ -244,7 +244,20 @@ HWTEST_F(UtilsThreadTest, testThread005, TestSize.Level0) ThreadStatus status = test->Start("test_thread_05", THREAD_PROI_LOW, 1024); EXPECT_EQ(status == ThreadStatus::OK, true); - TestThreadRun(test, times); + pthread_t thread = test->GetThread(); + + // pthread_equal return non-zero if equal + EXPECT_EQ(pthread_equal(thread , -1) != 0, (test->IsRunning() ? false : true)); + + // ReadyToWork return false, RUN will not be called! + EXPECT_EQ(test->priority_, DEFAULT_PRIO); + EXPECT_EQ(test->name_, DEFAULT_THREAD_NAME); + + EXPECT_EQ(test->data_, 0); + EXPECT_EQ(times, 0); + test->NotifyExitSync(); + sleep(1); + EXPECT_EQ(pthread_equal(test->GetThread(), -1) != 0, (test->IsRunning() ? false : true)); } /* @@ -352,7 +365,11 @@ bool TestThread2::Run() prctl(PR_GET_NAME, threadName, 0, 0); name_ = threadName; - return (runFunc_ != nullptr) ? (*runFunc_)(data_) : false; + if (runFunc_ != nullptr) { + return (*runFunc_)(data_); + } + + return false; } /* -- Gitee