diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8cf8b5e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "googletest"] + path = googletest + url = https://github.com/google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index bb170e4..81bcbfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 14) include_directories( # fischl include files ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/googletest/googletest/include ) add_executable(fischl @@ -18,4 +19,5 @@ add_executable(fischl ) enable_testing() -add_subdirectory(test) \ No newline at end of file +add_subdirectory(test) +add_subdirectory(googletest) \ No newline at end of file diff --git a/googletest b/googletest new file mode 160000 index 0000000..b10fad3 --- /dev/null +++ b/googletest @@ -0,0 +1 @@ +Subproject commit b10fad38c4026a29ea6561ab15fc4818170d1c10 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5bfe19f..09bdc38 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,10 @@ add_executable(${TARGET_LAYER2_API} layer2_API.cpp ) +# Link Google Test to your test executables +target_link_libraries(${TARGET_LAYER0} gtest gtest_main) +target_link_libraries(${TARGET_LAYER1_API} gtest gtest_main) + # add test to activate ctest -VV add_test(NAME ${TARGET_LAYER0} COMMAND sudo ./${TARGET_LAYER0} ${DIR_PLACE}) add_test(NAME ${TARGET_LAYER1_API} COMMAND sudo ./${TARGET_LAYER1_API} ${DIR_PLACE}) diff --git a/test/layer0.cpp b/test/layer0.cpp index 8cc358a..03d4f69 100644 --- a/test/layer0.cpp +++ b/test/layer0.cpp @@ -1,29 +1,33 @@ -#include -#include -#include +#include #include "rawdisk.h" - -int main(int argc, char *argv[]) { - const char* d = (argc < 2) ? "/dev/vdc" : argv[1]; - +const char* d; +TEST(RawDiskTest, WriteReadTest) { RawDisk *H = new RawDisk(d); - + char *buf = "iloveosdfjlseirfnerig"; - char readBuffer[512] = {0}; // Initialize to zeros + char readBuffer[512] = {0}; - //printf("dir %s, numSectors %lld, diskSize %lld \n", H->dir, H->numSectors, H->diskSize); - - //use number to substitute H->getnumSector(), getnumSectors() are not yest implemented + // Write test for(u_int64_t i = 0; i < 10; i++) { - H->rawdisk_write(i*512, buf, strlen(buf));//Change write_API - } - //use number to substitute H->getnumSector(), getnumSectors() are not yest implemented - for(u_int64_t i = 0; i < 10; i++) { - H->rawdisk_read(i*512, readBuffer, sizeof(readBuffer));//Change read_API - assert(strncmp(readBuffer, buf, strlen(buf)) == 0); + H->rawdisk_write(i*512, buf, strlen(buf)); } - delete H; // Delete the RawDisk object + // Read and verify test + for(u_int64_t i = 0; i < 10; i++) { + H->rawdisk_read(i*512, readBuffer, sizeof(readBuffer)); + EXPECT_EQ(strncmp(readBuffer, buf, strlen(buf)), 0); + } - return 0; + delete H; +} + +TEST(RawDiskTest, AssertionFailureTest) { + EXPECT_EQ(2, 3); // Intentional failure + EXPECT_EQ(4, 1); // Another intentional failure +} + +int main(int argc, char **argv) { + d = (argc < 2) ? "/dev/vdc" : argv[1];//how to do with this? + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/test/layer1_API.cpp b/test/layer1_API.cpp index 0c63001..5190bd4 100644 --- a/test/layer1_API.cpp +++ b/test/layer1_API.cpp @@ -1,12 +1,13 @@ #include #include #include +#include #include "fs.h" #include -int main(int argc, char *argv[]) { - const char* d = (argc < 2) ? "/dev/vdc" : argv[1]; - +const char* d; + +TEST(Layer1Test, APItest) { RawDisk *H = new RawDisk(d); printf("test inode\n"); @@ -20,21 +21,21 @@ int main(int argc, char *argv[]) { for (int j = 0; j < 8; j++) t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); - assert(t == 2);//the first 1th unused inode will store the next unused inode 2th + EXPECT_EQ(t, 2);//the first 1th unused inode will store the next unused inode 2th //test the number before end of inode 524286th H->rawdisk_read((MAX_INODE - 2) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); - assert(t == MAX_INODE - 1);//store the maximun th inode + EXPECT_EQ(t, MAX_INODE - 1);//store the maximun th inode //test the end of inode 524287th H->rawdisk_read((MAX_INODE - 1) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); - assert(t == 0);//the end of inode(524287th inode) do not have the next inode address + EXPECT_EQ(t, 0);//the end of inode(524287th inode) do not have the next inode address /**************************test datablock Initialization***************************/ //we separate 2048 4kB I/O block(1+2047) as a group and the first I/O block will manage the following 2047 I/O block usage. //the first 8 bytes(0~7) in first I/O block store the address of next first I/O block, the following 256(8~263) bytes record 2047 I/O block usage. @@ -44,14 +45,14 @@ int main(int argc, char *argv[]) { for (int j = 0; j < 8; j++) t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); - assert(t == (MAX_INODE+2048*8)*SECTOR_SIZE);//the first 8 bytes of 4k I/O block will store the next address(after 2048*4k I/O block) + EXPECT_EQ(t, (MAX_INODE+2048*8)*SECTOR_SIZE);//the first 8 bytes of 4k I/O block will store the next address(after 2048*4k I/O block) //test the end of the datablock H->rawdisk_read((MAX_BLOCKNUM - 2048*8) * SECTOR_SIZE, buffer, sizeof(buffer)); t = 0; for (int j = 0; j < 8; j++) t |= ((u_int64_t)(unsigned char)buffer[j]) << (8 * j); - assert(t == (MAX_BLOCKNUM)*SECTOR_SIZE); + EXPECT_EQ(t, (MAX_BLOCKNUM)*SECTOR_SIZE); /***************************test inode de/allocation**********************************/ //when requesting an inode, the inode_allocation will give you the inode number, we use inode_list to store the sequence allocate inode @@ -62,7 +63,7 @@ int main(int argc, char *argv[]) { //printf("Allocate 20 inode num:{"); for(int i=0;i<20;i++){ inode_list[i] = inop.inode_allocate(*H); - assert(inode_list[i] == i+1); + EXPECT_EQ(inode_list[i], i+1); //printf(" %d", inode_list[i]); } //printf("}\n"); @@ -75,7 +76,7 @@ int main(int argc, char *argv[]) { for(int i=10;i<20;i++){ inode_list[i] = inop.inode_allocate(*H); //printf("inode %d, rec_f %d\n,", inode_list[i],record_free[rec]); - assert(inode_list[i] == record_free[rec]); + EXPECT_EQ(inode_list[i], record_free[rec]); rec--; } printf("}\n"); @@ -105,7 +106,7 @@ int main(int argc, char *argv[]) { for(int i=0;i<10;i++){ //printf("%dth data block allocate again addres: ", i); for(int j=0;j<3;j++){ - assert(inode_inside[i].datablock_allocate(*H) == rec_datablock_free[i][j]); + EXPECT_EQ(inode_inside[i].datablock_allocate(*H), rec_datablock_free[i][j]); //printf("%d," ,inode_inside[i].datablock_allocate(*H)); } //printf("\n"); @@ -113,6 +114,10 @@ int main(int argc, char *argv[]) { //printf("}\n"); delete H; // Delete the RawDisk object - - return 0; } + +int main(int argc, char **argv) { + d = (argc < 2) ? "/dev/vdc" : argv[1];//how to do with this? + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file