增加了Test分支的部分注释

main
Khara0 8 months ago
parent 86994bc791
commit 01c71207be

@ -1,108 +1,107 @@
#ifndef MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_
#define MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_
// 仅仅针对 sort, binary_search 做了性能测试
#ifndef MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ // 预处理指令,防止头文件被重复包含
#define MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ // 定义宏,用于防止头文件被重复包含
// 包含标准库中的算法头文件
#include <algorithm>
// 包含自定义的算法头文件
#include "../MyTinySTL/algorithm.h"
// 包含测试相关的头文件
#include "test.h"
namespace mystl
{
namespace test
namespace mystl // 命名空间mystl
{
namespace algorithm_performance_test
namespace test // 命名空间test
{
namespace algorithm_performance_test // 命名空间algorithm_performance_test用于算法性能测试
// 函数性能测试宏定义
{
// 函数性能测试宏定义,用于测试单个函数的性能
#define FUN_TEST1(mode, fun, count) do { \
std::string fun_name = #fun; \
srand((int)time(0)); \
char buf[10]; \
clock_t start, end; \
int *arr = new int[count]; \
for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); \
start = clock(); \
mode::fun(arr, arr + count); \
end = clock(); \
int n = static_cast<int>(static_cast<double>(end - start) \
/ CLOCKS_PER_SEC * 1000); \
std::snprintf(buf, sizeof(buf), "%d", n); \
std::string t = buf; \
t += "ms |"; \
std::cout << std::setw(WIDE) << t; \
delete []arr; \
} while(0)
std::string fun_name = #fun; // \
srand((int)time(0)); // \
char buf[10]; // \
clock_t start, end; // \
int *arr = new int[count]; // \
for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); // \
start = clock(); // \
mode::fun(arr, arr + count); // \
end = clock(); // \
int n = static_cast<int>(static_cast<double>(end - start) // \
/ CLOCKS_PER_SEC * 1000); // \
std::snprintf(buf, sizeof(buf), "%d", n); // \
std::string t = buf; // \
t += "ms |"; // \
std::cout << std::setw(WIDE) << t; // WIDE \
delete []arr; // \
} while(0)
// 函数性能测试宏定义,用于测试需要两个参数的函数的性能
#define FUN_TEST2(mode, fun, count) do { \
std::string fun_name = #fun; \
srand((int)time(0)); \
char buf[10]; \
clock_t start, end; \
int *arr = new int[count]; \
for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); \
start = clock(); \
for(size_t i = 0; i < count; ++i) \
mode::fun(arr, arr + count, rand()); \
end = clock(); \
int n = static_cast<int>(static_cast<double>(end - start) \
/ CLOCKS_PER_SEC * 1000); \
std::snprintf(buf, sizeof(buf), "%d", n); \
std::string t = buf; \
t += "ms |"; \
std::cout << std::setw(WIDE) << t; \
delete []arr; \
} while(0)
std::string fun_name = #fun; // \
srand((int)time(0)); // \
char buf[10]; // \
clock_t start, end; // \
int *arr = new int[count]; // \
for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); // \
start = clock(); // \
for(size_t i = 0; i < count; ++i) \
mode::fun(arr, arr + count, rand()); // \
end = clock(); // \
int n = static_cast<int>(static_cast<double>(end - start) // \
/ CLOCKS_PER_SEC * 1000); // \
std::snprintf(buf, sizeof(buf), "%d", n); // \
std::string t = buf; // \
t += "ms |"; // \
std::cout << std::setw(WIDE) << t; // WIDE \
delete []arr; // \
} while(0)
void binary_search_test()
{
std::cout << "[------------------- function : binary_search ------------------]" << std::endl;
std::cout << "| orders of magnitude |";
TEST_LEN(LEN1, LEN2, LEN3, WIDE);
std::cout << "| std |";
FUN_TEST2(std, binary_search, LEN1);
FUN_TEST2(std, binary_search, LEN2);
FUN_TEST2(std, binary_search, LEN3);
std::cout << std::endl << "| mystl |";
FUN_TEST2(mystl, binary_search, LEN1);
FUN_TEST2(mystl, binary_search, LEN2);
FUN_TEST2(mystl, binary_search, LEN3);
std::cout << std::endl;
}
void binary_search_test() // 二分查找性能测试函数
{
std::cout << "[------------------- function : binary_search ------------------]" << std::endl;
std::cout << "| orders of magnitude |";
TEST_LEN(LEN1, LEN2, LEN3, WIDE); // 输出测试长度的表头
std::cout << "| std |"; // 输出标准库性能测试的表头
FUN_TEST2(std, binary_search, LEN1); // 测试标准库的二分查找性能
FUN_TEST2(std, binary_search, LEN2);
FUN_TEST2(std, binary_search, LEN3);
std::cout << std::endl << "| mystl |"; // 输出自定义库性能测试的表头
FUN_TEST2(mystl, binary_search, LEN1); // 测试自定义库的二分查找性能
FUN_TEST2(mystl, binary_search, LEN2);
FUN_TEST2(mystl, binary_search, LEN3);
std::cout << std::endl; // 输出换行
}
void sort_test()
{
std::cout << "[----------------------- function : sort -----------------------]" << std::endl;
std::cout << "| orders of magnitude |";
TEST_LEN(LEN1, LEN2, LEN3, WIDE);
std::cout << "| std |";
FUN_TEST1(std, sort, LEN1);
FUN_TEST1(std, sort, LEN2);
FUN_TEST1(std, sort, LEN3);
std::cout << std::endl << "| mystl |";
FUN_TEST1(mystl, sort, LEN1);
FUN_TEST1(mystl, sort, LEN2);
FUN_TEST1(mystl, sort, LEN3);
std::cout << std::endl;
}
void sort_test() // 排序性能测试函数
{
std::cout << "[----------------------- function : sort -----------------------]" << std::endl;
std::cout << "| orders of magnitude |"; // 输出测试长度的表头
TEST_LEN(LEN1, LEN2, LEN3, WIDE);
std::cout << "| std |"; // 输出标准库性能测试的表头
FUN_TEST1(std, sort, LEN1); // 测试标准库的排序性能
FUN_TEST1(std, sort, LEN2);
FUN_TEST1(std, sort, LEN3);
std::cout << std::endl << "| mystl |"; // 输出自定义库性能测试的表头
FUN_TEST1(mystl, sort, LEN1); // 测试自定义库的排序性能
FUN_TEST1(mystl, sort, LEN2);
FUN_TEST1(mystl, sort, LEN3);
std::cout << std::endl; // 输出换行
}
void algorithm_performance_test()
{
#if PERFORMANCE_TEST_ON
std::cout << "[===============================================================]" << std::endl;
std::cout << "[--------------- Run algorithm performance test ----------------]" << std::endl;
sort_test();
binary_search_test();
std::cout << "[--------------- End algorithm performance test ----------------]" << std::endl;
std::cout << "[===============================================================]" << std::endl;
void algorithm_performance_test() // 算法性能测试入口函数
{
#if PERFORMANCE_TEST_ON // 如果定义了性能测试宏,则执行以下代码
std::cout << "[===============================================================]" << std::endl;
std::cout << "[--------------- Run algorithm performance test ----------------]" << std::endl;
sort_test(); // 执行排序性能测试
binary_search_test(); // 执行二分查找性能测试
std::cout << "[--------------- End algorithm performance test ----------------]" << std::endl;
std::cout << "[===============================================================]" << std::endl;
#endif // PERFORMANCE_TEST_ON
}
}
} // namespace algorithm_performance_test
} // namespace test
} // namespace mystl
#endif // !MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_

@ -1,21 +1,21 @@
#ifndef MYTINYSTL_ALGORITHM_TEST_H_
#define MYTINYSTL_ALGORITHM_TEST_H_
// 算法测试: 包含了 mystl 的 81 个算法测试
#ifndef MYTINYSTL_ALGORITHM_TEST_H_ // 防止头文件被重复包含
#define MYTINYSTL_ALGORITHM_TEST_H_ // 同上
// 包含标准库中的算法、函数和数值操作头文件
#include <algorithm>
#include <functional>
#include <numeric>
// 包含自定义的算法和向量容器头文件
#include "../MyTinySTL/algorithm.h"
#include "../MyTinySTL/vector.h"
#include "test.h"
namespace mystl
namespace mystl // 命名空间mystl
{
namespace test
namespace test // 命名空间test
{
// 取消宏定义max和min以避免与标准库中的max和min冲突
#ifdef max
#pragma message("#undefing marco max")
#undef max
@ -26,25 +26,27 @@ namespace test
#undef min
#endif // min
// 针对_MSC_VER微软编译器的特殊处理用于禁用特定的编译器警告
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4389)
#endif // _MSC_VER
namespace algorithm_test
{
// 一些可能会用到的辅助数据和函数
int for_each_sum = 0;
int gen() { return 5; }
int r(int i) { return (i * 5 + 1) % 9; }
bool is_odd(int i) { return i & 1; }
bool is_even(int i) { return !(i & 1); }
void arr_sum(int i) { for_each_sum += i; }
bool cmp(const int& a, const int& b) { return b < a; }
int unary_op(const int& x) { return x + 1; }
int binary_op(const int& x, const int& y) { return x + y; }
namespace algorithm_test // 命名空间algorithm_test用于算法测试
{
// 一些辅助数据和函数,用于算法测试
int for_each_sum = 0; // 用于累计求和的全局变量
int gen() { return 5; } // 生成固定值的函数
int r(int i) { return (i * 5 + 1) % 9; } // 一个简单的函数,用于生成测试数据
bool is_odd(int i) { return i & 1; } // 判断奇数的函数
bool is_even(int i) { return !(i & 1); } // 判断偶数的函数
void arr_sum(int i) { for_each_sum += i; } // 累计求和的函数
bool cmp(const int& a, const int& b) { return b < a; } // 用于比较的函数
int unary_op(const int& x) { return x + 1; } // 一元操作函数
int binary_op(const int& x, const int& y) { return x + y; } // 二元操作函数
// 以下是80个算法测试函数的简单测试
// algobase test: 基础算法测试
// 以下为 80 个函数的简单测试

@ -1,40 +1,40 @@
#ifndef MYTINYSTL_DEQUE_TEST_H_
#define MYTINYSTL_DEQUE_TEST_H_
// deque test : 测试 deque 的接口和 push_front/push_back 的性能
#ifndef MYTINYSTL_DEQUE_TEST_H_ // 防止头文件被重复包含
#define MYTINYSTL_DEQUE_TEST_H_ // 同上
// 包含标准库中的deque头文件
#include <deque>
// 包含自定义的deque头文件和测试框架头文件
#include "../MyTinySTL/deque.h"
#include "test.h"
namespace mystl
namespace mystl // 命名空间mystl
{
namespace test
namespace test // 命名空间test
{
namespace deque_test
namespace deque_test // 命名空间deque_test用于deque的测试
{
void deque_test()
void deque_test() // 测试deque的接口和push_front/push_back的性能
{
std::cout << "[===============================================================]" << std::endl;
std::cout << "[----------------- Run container test : deque ------------------]" << std::endl;
std::cout << "[-------------------------- API test ---------------------------]" << std::endl;
int a[] = { 1,2,3,4,5 };
mystl::deque<int> d1;
mystl::deque<int> d2(5);
mystl::deque<int> d3(5, 1);
mystl::deque<int> d4(a, a + 5);
mystl::deque<int> d5(d2);
mystl::deque<int> d6(std::move(d2));
int a[] = { 1,2,3,4,5 }; // 用于初始化的数组
mystl::deque<int> d1; // 空的deque
mystl::deque<int> d2(5); // 指定大小的deque
mystl::deque<int> d3(5, 1); // 指定大小和值的deque
mystl::deque<int> d4(a, a + 5); // 由数组初始化的deque
mystl::deque<int> d5(d2); // 拷贝构造的deque
mystl::deque<int> d6(std::move(d2)); // 移动构造的deque
mystl::deque<int> d7;
d7 = d3;
d7 = d3; // 赋值拷贝
mystl::deque<int> d8;
d8 = std::move(d3);
mystl::deque<int> d9{ 1,2,3,4,5,6,7,8,9 };
d8 = std::move(d3); // 赋值移动
mystl::deque<int> d9{ 1,2,3,4,5,6,7,8,9 }; // 列表初始化的deque
mystl::deque<int> d10;
d10 = { 1,2,3,4,5,6,7,8,9 };
d10 = { 1,2,3,4,5,6,7,8,9 }; // 赋值列表初始化
// 对deque的成员函数进行测试FUN_AFTER宏用于打印函数执行后deque的状态
FUN_AFTER(d1, d1.assign(5, 1));
FUN_AFTER(d1, d1.assign(8, 8));
FUN_AFTER(d1, d1.assign(a, a + 5));
@ -57,6 +57,7 @@ void deque_test()
FUN_AFTER(d1, d1.clear());
FUN_AFTER(d1, d1.shrink_to_fit());
FUN_AFTER(d1, d1.swap(d4));
// 测试访问元素的函数
FUN_VALUE(*(d1.begin()));
FUN_VALUE(*(d1.end() - 1));
FUN_VALUE(*(d1.rbegin()));
@ -65,17 +66,19 @@ void deque_test()
FUN_VALUE(d1.back());
FUN_VALUE(d1.at(1));
FUN_VALUE(d1[2]);
// 测试状态查询函数
std::cout << std::boolalpha;
FUN_VALUE(d1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(d1.size());
FUN_VALUE(d1.max_size());
PASSED;
#if PERFORMANCE_TEST_ON
#if PERFORMANCE_TEST_ON // 如果定义了性能测试宏,则执行以下代码
std::cout << "[--------------------- Performance Testing ---------------------]" << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
std::cout << "| push_front |";
#if LARGER_TEST_DATA_ON
#if LARGER_TEST_DATA_ON // 如果定义了更大的测试数据宏,则执行以下代码
CON_TEST_P1(deque<int>, push_front, rand(), SCALE_LL(LEN1), SCALE_LL(LEN2), SCALE_LL(LEN3));
#else
CON_TEST_P1(deque<int>, push_front, rand(), SCALE_L(LEN1), SCALE_L(LEN2), SCALE_L(LEN3));
@ -94,9 +97,7 @@ void deque_test()
#endif
std::cout << "[----------------- End container test : deque ------------------]" << std::endl;
}
} // namespace deque_test
} // namespace test
} // namespace mystl
#endif // !MYTINYSTL_DEQUE_TEST_H_

Loading…
Cancel
Save