-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
67 lines (53 loc) · 1.85 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include "SortAlgorithm.h"
using sorting::SortingMethod;
using sorting::SortAlgorithm;
template<typename T>
void runTest(SortingMethod method) {
SortAlgorithm<T> sorter;
sorter.setSortingMethod(method);
// Gera dados aleatórios
std::vector<T> data(1000);
std::generate(data.begin(), data.end(), rand);
// Faz uma cópia dos dados e ordena usando std::sort para comparação
std::vector<T> expectedData = data;
std::sort(expectedData.begin(), expectedData.end());
// Ordena os dados usando o algoritmo de ordenação atual
sorter.sort(data, data.size());
// Verifica se os dados estão corretamente ordenados
if (data != expectedData)
std::cout << "\033[1;31mTest failed for sorting method: " << sorter.getSortingMethodName() << "\033[0m\n";
else
std::cout << "\033[1;32mTest passed for sorting method: \033[0m " << sorter.getSortingMethodName() << "\n";
}
template<typename T>
void runAllTests() {
std::vector<SortingMethod> methods = {
SortingMethod::BubbleSort,
SortingMethod::SelectionSort,
SortingMethod::InsertionSort,
SortingMethod::ShellSort,
SortingMethod::MergeSort,
SortingMethod::QuickSort
};
for (auto& method : methods) {
runTest<T>(method);
}
std::cout << "\n";
}
int main() {
std::cout << "Running tests for \033[1;34mint\033[0m...\n";
runAllTests<int>();
std::cout << "Running tests for \033[1;34mdouble\033[0m...\n";
runAllTests<double>();
std::cout << "Running tests for \033[1;34mfloat\033[0m...\n";
runAllTests<float>();
std::cout << "Running tests for \033[1;34mstring\033[0m...\n";
runAllTests<std::string>();
return 0;
}
// g++ -I.\include\ .\test\test.cpp -o .\test\test