test with google test framwork, will keep running the whole test even if the asserstion is wrong

This commit is contained in:
Victor 2023-11-12 12:18:39 -08:00
parent 46ce866c57
commit 292c59c77b
6 changed files with 53 additions and 34 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "googletest"]
path = googletest
url = https://github.com/google/googletest.git

View File

@ -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
@ -19,3 +20,4 @@ add_executable(fischl
enable_testing()
add_subdirectory(test)
add_subdirectory(googletest)

1
googletest Submodule

@ -0,0 +1 @@
Subproject commit b10fad38c4026a29ea6561ab15fc4818170d1c10

View File

@ -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})

View File

@ -1,29 +1,33 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <gtest/gtest.h>
#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
H->rawdisk_write(i*512, buf, strlen(buf));
}
//use number to substitute H->getnumSector(), getnumSectors() are not yest implemented
// Read and verify test
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_read(i*512, readBuffer, sizeof(readBuffer));
EXPECT_EQ(strncmp(readBuffer, buf, strlen(buf)), 0);
}
delete H; // Delete the RawDisk object
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();
}

View File

@ -1,12 +1,13 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <gtest/gtest.h>
#include "fs.h"
#include <inttypes.h>
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();
}