[单元测试]_[制作单元测试(gtest)辅助动态库接口之获取指定目录的直接文件路径]
来源:互联网 发布:淘宝老板 编辑:程序博客网 时间:2024/04/30 12:09
1.有时候单元测试需要对大批量的文件进行压力测试,这时候,代码里写死,xml配置文件,main的参数传递都不够方便。
2.这时候如果有一个动态库可以自动获取指定目录下的直接文件路径并返回一个C链表结构对象,那么就可以给各种语言调用了,非UNICODE版本。
3.以下在windows上使用MinGW的g++ 4.4编译过.
4.参考了msdn的win32的api例子.
文件1:Makefile
CP="cp -u"gtest_main:gtest_main.cpp tt_testg++ gtest_main.cpp -o gtest_main.exe -IE:/software/Lib/tests/gtest-1.5.0/win32/release/static/include -LE:/software/Lib/tests/gtest-1.5.0/win32/release/static -lgtest -L. -ltt_testtt_test:tt_test.o Makefileg++ -shared -o tt_test.dll -s tt_test.o -Wl,--out-implib,libtt_test.dll.att_test.o:tt_test.cpp Makefileg++ -DBUILDING_TT_TEST_DLL -c tt_test.cpp -o tt_test.o
文件2:tt_test.cpp
#include "tt_test.h"#define _WIN32_WINNT 0x0501//#define UNICODE#include <windows.h>#include <string.h>#include <stdio.h>#include <string>#include <iostream>using namespace std;TargetFile* GetTargetFiles(const char* dir,const char* extension){ WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH]; // directory specification DWORD dwError; size_t dir_len = strlen(dir); strncpy (DirSpec, dir, dir_len+1); strncat (DirSpec, extension, strlen(extension)+1); TargetFile *target = NULL; TargetFile *first = NULL; hFind = FindFirstFile(DirSpec, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %lu \n", GetLastError()); return NULL; } else { target = (TargetFile*)malloc(sizeof(TargetFile)); memset(target,0,sizeof(TargetFile)); size_t file_name_len = strlen(FindFileData.cFileName); char* file_path = (char*)malloc(file_name_len+dir_len+1); strncpy(file_path,dir,dir_len+1); strncat(file_path,FindFileData.cFileName,file_name_len); target->path = file_path; TargetFile *previous = target; first = target; while (FindNextFile(hFind, &FindFileData) != 0) { file_name_len = strlen(FindFileData.cFileName); char* temp_path = (char*)malloc(file_name_len+dir_len+1); strncpy(temp_path,dir,dir_len+1); strncat(temp_path,FindFileData.cFileName,file_name_len); target = (TargetFile*)malloc(sizeof(TargetFile)); memset(target,0,sizeof(TargetFile)); target->path = temp_path; previous->next = target; previous = target; } dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %lu \n", dwError); return first; } } return first;}void FreeTargetFiles(TargetFile* first){while(first) { TargetFile *temp = first; first = first->next; free(temp->path); temp->path = NULL; free(temp); }}
文件3: tt_test.h
#ifndef TT_TEST_H#defineTT_TEST_H#ifdef BUILDING_TT_TEST_DLL #define TT_TEST_DLL __declspec(dllexport)#else #define TT_TEST_DLL __declspec(dllimport)#endif#ifdef__cplusplusextern "C"{#endiftypedef struct TargetFile TargetFile; struct TargetFile { char *path; TargetFile *next; };TT_TEST_DLL TargetFile* GetTargetFiles(const char* dir,const char* extension);TT_TEST_DLL void FreeTargetFiles(TargetFile* first);#ifdef__cplusplus}#endif#endif/* TT_TEST_H */
文件4:gtest_main.cpp
#include <string.h>#include "gtest/gtest.h"#include "tt_test.h"using namespace std;int main(int argc, char **argv){setbuf(stdout, (char*) 0);setbuf(stderr, (char*) 0);testing::InitGoogleTest(&argc, argv);return RUN_ALL_TESTS();}TEST(gtest_main,testTargetFile){ TargetFile* first = GetTargetFiles("E:\\software\\TestData\\pdf\\","*.pdf"); ASSERT_TRUE(first); TargetFile* temp = first; while(temp) { ASSERT_TRUE(strstr(temp->path,".pdf")); temp = temp->next; } FreeTargetFiles(first); cout << "finish" << endl;}
输出结果:
备注:
1.判断是否是目录使用&运算法. FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes
- [单元测试]_[制作单元测试(gtest)辅助动态库接口之获取指定目录的直接文件路径]
- [单元测试]_[VC2010使用gtest单元测试入门]
- 写一个单元测试辅助动态库
- gtest单元测试
- gtest单元测试
- [单元测试]_[Gtest中级使用教程]
- c++单元测试工具之——gtest
- c++单元测试工具之——gtest
- 一键执行更新密文密码到指定目录下的所有文件, 附单元测试
- 获取指定文件目录路径下的所有文件
- gtest C/C++单元测试
- GTest进行单元测试
- linux 安装gtest 单元测试
- c++单元测试gtest
- vs2010结合gtest单元测试
- Linux C: 获取指定路径下的文件目录
- 获取指定路径下目录或文件的大小
- 通过文件选择框获取指定的目录路径
- 适合程序员听得歌曲
- 文档在线阅读(类似百度文库)的实现
- GUI 剖析之 U盘退出问题的解决
- 何谓大数据
- Qt之一
- [单元测试]_[制作单元测试(gtest)辅助动态库接口之获取指定目录的直接文件路径]
- HOJ 1637 HangOver
- VS环境设置问题
- 谈谈离职和跳槽
- 2013年Web设计的三大趋势
- login shell和non-login shell
- 大数据时代:如何节约存储成本
- 初窥wireshark fiddler等抓包工具及部分实现分析
- HOJ 1610 Twenty Questions