add mock entrypath structure for testing
This commit is contained in:
parent
460bcd5e7e
commit
5b220fdf46
169
test/dir_API.cpp
169
test/dir_API.cpp
@ -9,6 +9,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <iostream>
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
#include "direntry.h"
|
#include "direntry.h"
|
||||||
|
|
||||||
@ -17,6 +18,20 @@ const char* d;
|
|||||||
//global can be taken
|
//global can be taken
|
||||||
TreeNode *root;
|
TreeNode *root;
|
||||||
const char* target_filepath;
|
const char* target_filepath;
|
||||||
|
typedef struct file_test{
|
||||||
|
const char* name;
|
||||||
|
file_test* next;//use linked-list to know the file at the same level directory
|
||||||
|
}file_test;
|
||||||
|
typedef struct dir_test{
|
||||||
|
const char* name;
|
||||||
|
file_test* inFile;
|
||||||
|
dir_test* subdir;
|
||||||
|
dir_test* next;//use linked-list to know the other dir at the same parent dir.
|
||||||
|
}dir_test;
|
||||||
|
|
||||||
|
int total_number = 0;
|
||||||
|
int total_free_dir = 0;
|
||||||
|
int total_free_file = 0;
|
||||||
|
|
||||||
const char* get_baseName(const char *filename){
|
const char* get_baseName(const char *filename){
|
||||||
const char* base_name = strrchr(filename, '/');
|
const char* base_name = strrchr(filename, '/');
|
||||||
@ -28,6 +43,145 @@ const char* get_baseName(const char *filename){
|
|||||||
return base_name;
|
return base_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* generateRandomName(size_t length) {
|
||||||
|
const std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
std::string randomString;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < length; ++i) {
|
||||||
|
randomString += chars[rand() % chars.size()];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate memory and copy the string
|
||||||
|
char* name = new char[randomString.length() + 1];
|
||||||
|
strcpy(name, randomString.c_str());
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive function to create directory hierarchy
|
||||||
|
dir_test* createDirHierarchy(int level, int maxLevel) {
|
||||||
|
if (level > maxLevel) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
dir_test* head = nullptr;
|
||||||
|
dir_test* current = nullptr;
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
dir_test* newDir = new dir_test;
|
||||||
|
newDir->name = generateRandomName(6); // Generate a random name for the directory
|
||||||
|
newDir->inFile = nullptr; // Initialize file list to nullptr
|
||||||
|
newDir->subdir = createDirHierarchy(level + 1, maxLevel); // Recursively create subdirectories
|
||||||
|
newDir->next = nullptr;
|
||||||
|
|
||||||
|
// Create file list for this directory
|
||||||
|
file_test* fileHead = nullptr;
|
||||||
|
file_test* fileCurrent = nullptr;
|
||||||
|
for (int j = 0; j < 3; ++j) {
|
||||||
|
file_test* newFile = new file_test;
|
||||||
|
newFile->name = generateRandomName(6); // Generate a random name for the file
|
||||||
|
newFile->next = nullptr;
|
||||||
|
|
||||||
|
if (!fileHead) {
|
||||||
|
fileHead = newFile;
|
||||||
|
} else {
|
||||||
|
fileCurrent->next = newFile;
|
||||||
|
}
|
||||||
|
fileCurrent = newFile;
|
||||||
|
}
|
||||||
|
newDir->inFile = fileHead;
|
||||||
|
|
||||||
|
// Add the new directory to the list
|
||||||
|
if (!head) {
|
||||||
|
head = newDir;
|
||||||
|
} else {
|
||||||
|
current->next = newDir;
|
||||||
|
}
|
||||||
|
current = newDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup function for the test directory
|
||||||
|
void setupTestDirectory(dir_test** root) {
|
||||||
|
fprintf(stderr,"[%s ,%d]\n",__func__,__LINE__);
|
||||||
|
// Allocate memory for root
|
||||||
|
*root = new dir_test;
|
||||||
|
(*root)->name = strdup("/"); // Generate a random name for the directory
|
||||||
|
(*root)->inFile = nullptr; // Initialize file list to nullptr
|
||||||
|
fprintf(stderr,"[%s ,%d]\n",__func__,__LINE__);
|
||||||
|
(*root)->subdir = createDirHierarchy(0, 1);
|
||||||
|
(*root)->next = nullptr;
|
||||||
|
fprintf(stderr,"[%s ,%d]\n",__func__,__LINE__);
|
||||||
|
file_test* fileHead = nullptr;
|
||||||
|
file_test* fileCurrent = nullptr;
|
||||||
|
for (int j = 0; j < 3; ++j) {
|
||||||
|
file_test* newFile = new file_test;
|
||||||
|
newFile->name = generateRandomName(6); // Generate a random name for the file
|
||||||
|
newFile->next = nullptr;
|
||||||
|
|
||||||
|
if (!fileHead) {
|
||||||
|
fileHead = newFile;
|
||||||
|
} else {
|
||||||
|
fileCurrent->next = newFile;
|
||||||
|
}
|
||||||
|
fileCurrent = newFile;
|
||||||
|
}
|
||||||
|
(*root)->inFile = fileHead;
|
||||||
|
}
|
||||||
|
void setupTestDirectory_1(dir_test** root) {
|
||||||
|
|
||||||
|
*root = createDirHierarchy(0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to free a list of files
|
||||||
|
void freeFileList(file_test* fileList) {
|
||||||
|
while (fileList != nullptr) {
|
||||||
|
file_test* temp = fileList;
|
||||||
|
fileList = fileList->next;
|
||||||
|
total_free_file++;//for debug
|
||||||
|
delete[] temp->name; // Free the name string
|
||||||
|
delete temp; // Free the current file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive function to free the directory hierarchy
|
||||||
|
void freeDirHierarchy(dir_test* dir) {
|
||||||
|
while (dir != nullptr) {
|
||||||
|
dir_test* temp = dir;
|
||||||
|
dir = dir->next;
|
||||||
|
total_free_dir++;//for debug
|
||||||
|
freeFileList(temp->inFile); // Free the list of files in the directory
|
||||||
|
freeDirHierarchy(temp->subdir); // Recursively free subdirectories
|
||||||
|
delete[] temp->name; // Free the name string
|
||||||
|
delete temp; // Free the current directory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to print the list of files in a directory
|
||||||
|
void printFileList(const file_test* fileList) {
|
||||||
|
const file_test* currentFile = fileList;
|
||||||
|
while (currentFile != nullptr) {
|
||||||
|
// std::cout << " File: " << currentFile->name << std::endl;
|
||||||
|
currentFile = currentFile->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void traverseDirHierarchy(const dir_test* dir, int depth = 0) {
|
||||||
|
while (dir != nullptr) {
|
||||||
|
// std::cout << "Depth " << depth << ", Directory: " << dir->name << std::endl;
|
||||||
|
total_number++;//for debug
|
||||||
|
|
||||||
|
// Print files in this directory
|
||||||
|
printFileList(dir->inFile);
|
||||||
|
// Recursively traverse subdirectories
|
||||||
|
traverseDirHierarchy(dir->subdir, depth + 1);
|
||||||
|
|
||||||
|
// Go to the next directory at the same level
|
||||||
|
dir = dir->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST(DirTest, root_test) {
|
TEST(DirTest, root_test) {
|
||||||
//Init fake root directory
|
//Init fake root directory
|
||||||
INode inode_root;
|
INode inode_root;
|
||||||
@ -131,10 +285,23 @@ TEST(DirTest, Add_FindFile_test) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
d = (argc < 2) ? "/dev/vdc" : argv[1];//how to do with this?
|
srand(time(NULL)); // Seed the random number generator
|
||||||
|
d = (argc < 2) ? "/dev/vdc" : argv[1];
|
||||||
|
|
||||||
|
dir_test* mock_root = nullptr;
|
||||||
|
setupTestDirectory(&mock_root);
|
||||||
|
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
int result = RUN_ALL_TESTS();
|
int result = RUN_ALL_TESTS();
|
||||||
|
|
||||||
|
total_number = 0;
|
||||||
|
traverseDirHierarchy(mock_root);//mock_root
|
||||||
|
printf("Traverse Dir total %d\n",total_number);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
|
freeDirHierarchy(mock_root);//mock_root
|
||||||
|
printf("Free Dir total %d\n",total_free_dir);
|
||||||
|
printf("Free File total %d\n",total_free_file);
|
||||||
freeTree(root);
|
freeTree(root);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user