Merge remote-tracking branch 'origin/wangziao/layer1test' into connorg/layer1tests

This commit is contained in:
Connor 2023-11-15 23:23:54 -08:00
commit b921cadf92
2 changed files with 136 additions and 17 deletions

View File

@ -1,17 +1,23 @@
set(TARGET_LAYER0 test_layer0)
set(TARGET_LAYER1_API test_layer1_API)
set(DIR_PLACE /dev/vdb)
# add test sources here ...
add_executable(${TARGET_LAYER0}
# add need lib and source code here
layer0.cpp
)
add_executable(${TARGET_LAYER1_API}
# add need lib and source code here
layer1_API.cpp
)
# 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})
set(TARGET_LAYER0 test_layer0)
set(TARGET_LAYER1_API test_layer1_API)
set(TARGET_LAYER1_TEST1 test_layer1_test1)
set(DIR_PLACE /dev/vdb)
# add test sources here ...
add_executable(${TARGET_LAYER0}
# add need lib and source code here
layer0.cpp
)
add_executable(${TARGET_LAYER1_API}
# add need lib and source code here
layer1_API.cpp
)
add_executable(${TARGET_LAYER1_TEST1}
# add need lib and source code here
layer1_test1.cpp
)
# 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})
add_test(NAME ${TARGET_LAYER1_TEST1} COMMAND sudo ./${TARGET_LAYER1_TEST1} ${DIR_PLACE})

113
test/layer1_test1.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "fs.h"
#include <inttypes.h>
// in fs.h:
// #define MAX_INODE 2048
// #define MAX_BLOCKNUM 51200
// 51200 Sectors = 2048 * 25 Sectors = 25MB
// Free List Heads: 2048*512, 2048*9*512, 2048*17*512
// Available INodes: 2047 (-1 because superblock)
// Available DataBlocks (including Indirect Blocks): 2047 * 3 = 6141
int main(int argc, char *argv[]) {
const char* d = (argc < 2) ? "/dev/vdc" : argv[1];
RawDisk *H = new RawDisk(d);
printf("=== INode Alloc/Dealloc Test ===\n");
INodeOperation inop;
inop.initialize(*H);
// Test INode alloc and dealloc
int inode_list[2046] = {0}; // if we allocate 2047 inodes head will be 0 (faulty)
printf("freeInodeHead: %d \n", SuperBlock::getFreeINodeHead(*H)); // this impl should give 1
for(int i=0;i<2046;i++){
inode_list[i] = inop.inode_allocate(*H);
if (SuperBlock::getFreeINodeHead(*H) == 0) {
printf("%d\n",i);
assert(false);
}
}
printf("freeInodeHead: %d \n", SuperBlock::getFreeINodeHead(*H)); // this impl should give 2047
for(int i=0;i<1024;i++){
inop.inode_free(*H,inode_list[i]);
}
for(int i=0;i<1022;i++){
inode_list[i] = inop.inode_allocate(*H);
assert(SuperBlock::getFreeINodeHead(*H) != 0);
}
printf("freeInodeHead: %d \n", SuperBlock::getFreeINodeHead(*H)); // this impl should give 2
inode_list[1022] = inop.inode_allocate(*H);
printf("freeInodeHead: %d \n", SuperBlock::getFreeINodeHead(*H)); // this impl should give 1
inode_list[1023] = inop.inode_allocate(*H);
printf("freeInodeHead: %d \n", SuperBlock::getFreeINodeHead(*H)); // this impl should give 2047
// Test Many Files
printf("=== Many Files Test ===\n");
INode inode_inside[100];
for(int i=0;i<100;i++){
inode_inside[i].inode_construct(inode_list[i],*H);
for(int j=0;j<60;j++) { // Note that 1 indirect block is used for each file
u_int64_t allcBlockNum = inode_inside[i].datablock_allocate(*H);
if (SuperBlock::getFreeListHead(*H) >= (u_int64_t) 51200*512) {
printf("Bad FreeListHead: %d, %d, %llu\n", i, j, SuperBlock::getFreeListHead(*H));
assert(false);
}
if (allcBlockNum % 2048 != 0 || allcBlockNum < 2048*512 || allcBlockNum >= 25*2048*512) {
printf("Bad Allocated Block Number: %d, %d, %llu\n", i, j, allcBlockNum);
assert(false);
}
}
}
printf("Finished Allocating\n");
// in this impl should give 17*2048*512 = 17825792
printf("freeListHead: %llu \n", SuperBlock::getFreeListHead(*H)); // if all 6141 blocks allocated, would give 51200*512 (faulty)
for(int i=0;i<100;i++){
for(int j=0;j<59;j++){
u_int64_t freedBlkNum = inode_inside[i].datablock_deallocate(*H);
u_int64_t fh = SuperBlock::getFreeListHead(*H);
if (freedBlkNum % 2048 != 0 || freedBlkNum < 2048*512 || freedBlkNum >= 25*2048*512 || fh >= 51200*512) {
printf("%d, %d, Freed Block Number: %llu\n", i, j, freedBlkNum);
printf("FreeListHead is %llu\n", fh);
assert(false);
}
}
}
printf("Finished Deallocating\n");
printf("freeListHead: %d \n", SuperBlock::getFreeListHead(*H));
// Test Big File (Use direct, single indirect, double indirect)
printf("=== Big File Test ===\n");
u_int64_t lastAllc = 0;
for(int j=0;j<5000;j++){
u_int64_t allcBlockNum = inode_inside[0].datablock_allocate(*H);
lastAllc = allcBlockNum;
u_int64_t fh = SuperBlock::getFreeListHead(*H);
if (allcBlockNum % 2048 != 0 || allcBlockNum < 2048*512 || allcBlockNum >= 25*2048*512 || fh >= 51200*512) {
printf("%d, Alloc Block Number: %llu\n", j, allcBlockNum);
printf("FreeListHead is %llu\n", fh);
assert(false);
}
}
printf("last allocate for big file: %llu\n", lastAllc);
printf("Finished Allocating\n");
printf("freeListHead: %d \n", SuperBlock::getFreeListHead(*H));
for(int j=0;j<5000;j++){
u_int64_t freedBlkNum = inode_inside[0].datablock_deallocate(*H);
u_int64_t fh = SuperBlock::getFreeListHead(*H);
if (freedBlkNum % 2048 != 0 || freedBlkNum < 2048*512 || freedBlkNum >= 25*2048*512 || fh >= 51200*512) {
printf("%d, Freed Block Number: %llu\n", j, freedBlkNum);
printf("FreeListHead is %llu\n", fh);
assert(false);
}
}
printf("Finished Deallocating\n");
printf("freeListHead: %d \n", SuperBlock::getFreeListHead(*H));
delete H; // Delete the RawDisk object
return 0;
}