sortDemo/tests/basic_test.cpp

183 lines
4.4 KiB
C++
Raw Normal View History

2026-06-02 01:53:58 +00:00
/**
* @file basic_test.cpp
* @brief assert
*/
#include "app.hpp"
#include <cassert>
#include <cstring> // std::memcpy
#include <iostream>
#include <algorithm> // 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<std::size_t>(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;
}