/** * @file basic_test.cpp * @brief 基于标准库 assert 的排序算法单元测试 */ #include "app.hpp" #include #include // std::memcpy #include #include // std::is_sorted (仅用于验证,不用于排序) // ============================================================================ // 辅助函数 // ============================================================================ /** * @brief 验证数组已按升序排列 * @param arr 数组指针 * @param n 数组长度 * @return true 升序;false 非升序 */ static bool isSorted(const int arr[], int n) { for (int i = 1; i < n; ++i) { if (arr[i - 1] > arr[i]) return false; } return true; } /** * @brief 复制数组(长度不超过 1024) */ static void copyArray(const int src[], int dst[], int n) { std::memcpy(dst, src, static_cast(n) * sizeof(int)); } // ============================================================================ // 测试用例 // ============================================================================ /** * @brief 测试空数组和单元素数组(边界情况) */ static void testEmptyAndSingle() { std::cout << "[TEST] 空数组 & 单元素数组 ... "; // 空数组 sort::bubbleSort(nullptr, 0); sort::quickSort(nullptr, 0); sort::selectionSort(nullptr, 0); // 单元素 int a1[] = {42}; sort::bubbleSort(a1, 1); assert(a1[0] == 42); int a2[] = {99}; sort::quickSort(a2, 1); assert(a2[0] == 99); int a3[] = {7}; sort::selectionSort(a3, 1); assert(a3[0] == 7); std::cout << "通过" << std::endl; } // ============================================================================ /** * @brief 测试冒泡排序 */ static void testBubbleSort() { std::cout << "[TEST] 冒泡排序 ... "; // 普通数组 int data[] = {64, 34, 25, 12, 22, 11, 90}; int arr[7]; copyArray(data, arr, 7); sort::bubbleSort(arr, 7); assert(isSorted(arr, 7)); assert(arr[0] == 11); assert(arr[6] == 90); // 已有序 int sorted[] = {1, 2, 3, 4, 5}; sort::bubbleSort(sorted, 5); assert(isSorted(sorted, 5)); // 逆序 int reversed[] = {5, 4, 3, 2, 1}; sort::bubbleSort(reversed, 5); assert(isSorted(reversed, 5)); // 含重复元素 int duplicates[] = {3, 1, 2, 3, 1, 2}; sort::bubbleSort(duplicates, 6); assert(isSorted(duplicates, 6)); std::cout << "通过" << std::endl; } // ============================================================================ /** * @brief 测试快速排序 */ static void testQuickSort() { std::cout << "[TEST] 快速排序 ... "; int data[] = {38, 27, 43, 3, 9, 82, 10}; int arr[7]; copyArray(data, arr, 7); sort::quickSort(arr, 7); assert(isSorted(arr, 7)); // 已有序 int sorted[] = {1, 2, 3, 4, 5}; sort::quickSort(sorted, 5); assert(isSorted(sorted, 5)); // 逆序 int reversed[] = {5, 4, 3, 2, 1}; sort::quickSort(reversed, 5); assert(isSorted(reversed, 5)); // 含重复元素 int duplicates[] = {3, 1, 2, 3, 1, 2}; sort::quickSort(duplicates, 6); assert(isSorted(duplicates, 6)); std::cout << "通过" << std::endl; } // ============================================================================ /** * @brief 测试选择排序 */ static void testSelectionSort() { std::cout << "[TEST] 选择排序 ... "; int data[] = {29, 10, 14, 37, 13, 33, 48}; int arr[7]; copyArray(data, arr, 7); sort::selectionSort(arr, 7); assert(isSorted(arr, 7)); // 已有序 int sorted[] = {1, 2, 3, 4, 5}; sort::selectionSort(sorted, 5); assert(isSorted(sorted, 5)); // 逆序 int reversed[] = {5, 4, 3, 2, 1}; sort::selectionSort(reversed, 5); assert(isSorted(reversed, 5)); // 含重复元素 int duplicates[] = {3, 1, 2, 3, 1, 2}; sort::selectionSort(duplicates, 6); assert(isSorted(duplicates, 6)); std::cout << "通过" << std::endl; } // ============================================================================ // 主测试入口 // ============================================================================ /** * @brief 测试主函数 * @return 0 全部通过;非 0 表示有测试失败 */ int main() { testEmptyAndSingle(); testBubbleSort(); testQuickSort(); testSelectionSort(); std::cout << "\n所有测试通过!" << std::endl; return 0; }