将gtest压缩文件下载放到指定位置,进行解压缩,可看到如下结果;安装源码:https://github.com/smarr/googletest
将动态库进行链接
项目属性——-链接器—–附加依赖项—–编辑—–添加动态库的路径
动态库所在位置
在你的gtest配置文件下的msvc/gtest中有两个文件,一个是Debug,一个是Release,将其中一个文件中的gtest.lib 和gtest_main.lib的路径添加到附加依赖项,我这里添加的是Release版本
属性—–C/C++ 下的代码生成 —— 运行库 —– 选择方式
这里的方式有四种,为下表,选择时,应对应起来,如果运行中出现如下图情况,则选择对应的运行库
名称 | 对应项 |
---|---|
多线程调试Dll (/MDd) | MD_DynamicDebug |
多线程Dll (/MD) | MD_DynamicRelease |
多线程(/MT) | MD_StaticRelease |
多线程(/MTd) | MD_StaticDebug |
- 引入gtest头文件,
#include
- 采用宏来进行测试
TEST(test_case_name,test_name)
理解:将一组有关联的测试用例放在了一组测试里面
test_case_name: 针对谁的测试,这里是HuffmanTree的创建
test_name:针对某一个部分的测试,例如:针对创建节点的测试等
对于文件压缩各个单元的测试:
1.对Huffman节点的测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "HuffmanTree.h"
#include //引入gtest库
//构造哈夫曼树的节点
TEST(huffmantest, node)
{
typedef HuffmanTreeNode<int> Node;
Node* newnode = new Node(1);
ASSERT_EQ(newnode->_w, 1);
ASSERT_FALSE(newnode->_left);
ASSERT_FALSE(newnode->_right);
}
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
int a = RUN_ALL_TESTS();
//Start();
system("pause");
return 0;
}
结果:
正确结果:
错误结果:
2.Huffman的创建
TEST(huffmantest, constructhuffmantree)
{
HuffmanTree tree1;
ASSERT_FALSE(tree1.GetRoot());//开始为空树
int a[] = { 4, 3, 2, 1 };
int valiad = 0;
HuffmanTree tree(a, sizeof(a) / sizeof(a[0]), valiad);
typedef HuffmanTreeNode Node;
Node* root = tree.GetRoot();
ASSERT_EQ(root->_w, 10);
ASSERT_EQ(root->_left->_w, 4);
ASSERT_EQ(root->_right->_w, 6);
Node* Left = root->_left;
Node* Right = root->_right;
ASSERT_FALSE(Left->_left);
ASSERT_FALSE(Left->_right);
ASSERT_EQ(Right->_left->_w, 3);
ASSERT_EQ(Right->_right->_w, 3);
Node* pRight = Right->_right;
ASSERT_EQ(pRight->_left->_w, 1);
ASSERT_EQ(pRight->_right->_w, 2);
}
测试结果:
3. 统计文件中的次数
//统计次数
TEST(FileProcessTestCase, GetCountChar)
{
FileCompress fc;
CharInfo info;
for (size_t i = 0; i < 256; ++i)
{
info = fc.Get(i);
ASSERT_EQ(info._ch, (char)i);
}
}
4. 压缩之前确定压缩文件名
//压缩前的准备工作
TEST(FileProcessTestCase, CompressDo)
{
FileCompress fc;
char* file = "C:\\Users\\lenovo\\Desktop\\1.txt";
//压缩文件名
char compressfile[100] = { 0 };
//string name = fc.CompressDo(file, compressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.huffman");//覆盖
//string name1 = fc.CompressDo(file, compressfile);;
//ASSERT_EQ(name1, "C:\\Users\\lenovo\\Desktop\\2.huffman");//重命名
//string name = fc.CompressDo(file, compressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.huffman");//指定路径存在替换
//string name = fc.CompressDo(file, compressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.huffman");//指定路径存在重命名
//string name = fc.CompressDo(file, compressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.huffman");//指定路径不存在
}
5. 解压缩之前确定解压后的文件名
//解压缩前的准备工作
TEST(FileProcessTestCase, UnCompressDo)
{
FileCompress fc;
char* file = "C:\\Users\\lenovo\\Desktop\\1.huffman";
//压缩文件名
char Uncompressfile[100] = { ".txt" };
//string name = fc.UnCompressDo(file, Uncompressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.txt");//覆盖
//string name1 = fc.UnCompressDo(file, Uncompressfile);;
//ASSERT_EQ(name1, "C:\\Users\\lenovo\\Desktop\\2.txt");//重命名
//string name = fc.UnCompressDo(file, Uncompressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.txt");//指定路径存在替换
//string name = fc.CompressDo(file, compressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\2.txt");//指定路径存在重命名
//string name = fc.CompressDo(file, Uncompressfile);;
//ASSERT_EQ(name, "C:\\Users\\lenovo\\Desktop\\1.txt");//指定路径不存在
}
6. 压缩与解压缩
TEST(FileProcessTestCase, Compress)
{
FileCompress fc;
char* file = "C:\\Users\\lenovo\\Desktop\\1.huffman";
string newfile = fc.UnCompress(file);
FILE* fnew;
fopen_s(&fnew, newfile.c_str(), "rb");
ifstream ifs(newfile.c_str(), ios_base::in | ios_base::binary);//打开文件,aaabbbccd
char ch;
ifs.get(ch);
ASSERT_EQ(ch, 'a');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'a');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'a');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'a');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'b');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'b');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'b');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'c');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'c');
ch = ' ';
ifs.get(ch);
ASSERT_EQ(ch, 'd');
}
文件压缩源码及测试源码:https://github.com/zy01093017/HuffmanFileCompress/tree/master