diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/CMakeLists.txt b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/CMakeLists.txt old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/Lib/redbud/io/color.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/Lib/redbud/io/color.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/Lib/redbud/platform.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/Lib/redbud/platform.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/README.md b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/README.md old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h old mode 100644 new mode 100755 index 675e5b2..f6c9b97 --- a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h +++ b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h @@ -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 +// 包含自定义的算法头文件 #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(static_cast(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(static_cast(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(static_cast(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(static_cast(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_ - diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_test.h old mode 100644 new mode 100755 index 92eccdc..3160b8d --- a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_test.h +++ b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_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 #include #include +// 包含自定义的算法和向量容器头文件 #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 个函数的简单测试 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/deque_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/deque_test.h old mode 100644 new mode 100755 index 0793bf6..e77b5d4 --- a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/deque_test.h +++ b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/deque_test.h @@ -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头文件和测试框架头文件 #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 d1; - mystl::deque d2(5); - mystl::deque d3(5, 1); - mystl::deque d4(a, a + 5); - mystl::deque d5(d2); - mystl::deque d6(std::move(d2)); + int a[] = { 1,2,3,4,5 }; // 用于初始化的数组 + mystl::deque d1; // 空的deque + mystl::deque d2(5); // 指定大小的deque + mystl::deque d3(5, 1); // 指定大小和值的deque + mystl::deque d4(a, a + 5); // 由数组初始化的deque + mystl::deque d5(d2); // 拷贝构造的deque + mystl::deque d6(std::move(d2)); // 移动构造的deque mystl::deque d7; - d7 = d3; + d7 = d3; // 赋值拷贝 mystl::deque d8; - d8 = std::move(d3); - mystl::deque d9{ 1,2,3,4,5,6,7,8,9 }; + d8 = std::move(d3); // 赋值移动 + mystl::deque d9{ 1,2,3,4,5,6,7,8,9 }; // 列表初始化的deque mystl::deque 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, push_front, rand(), SCALE_LL(LEN1), SCALE_LL(LEN2), SCALE_LL(LEN3)); #else CON_TEST_P1(deque, 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_ - diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/list_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/list_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/map_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/map_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/queue_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/queue_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/set_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/set_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/stack_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/stack_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/string_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/string_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/test.cpp b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/test.cpp old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/unordered_map_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/unordered_map_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/unordered_set_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/unordered_set_test.h old mode 100644 new mode 100755 diff --git a/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/vector_test.h b/MyTinySTL/src/MyTinySTL-master/MyTinySTL-master/Test/vector_test.h old mode 100644 new mode 100755