-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnatural_sort_test.cpp
More file actions
95 lines (86 loc) · 2.22 KB
/
natural_sort_test.cpp
File metadata and controls
95 lines (86 loc) · 2.22 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <string>
#include <assert.h>
#include "natural_sort.hpp"
#define ADD_TEST(test_method) { \
std::cout<<"Running test " #test_method "..."<<std::endl; \
test_method(); \
std::cout<<"Test " #test_method " is successful."<<std::endl; \
}
#define ASSERT_TRUE(x) { assert(x); }
#define ASSERT_FALSE(x) { assert(!(x)); }
#define ASSERT_EQ(x, y) { assert((x) == (y)); }
void test_compare() {
ASSERT_FALSE(SI::natural::compare<std::string>("Hello 32", "Hello 023"));
ASSERT_FALSE(SI::natural::compare<std::string>("Hello 32a", "Hello 32"));
ASSERT_TRUE(SI::natural::compare<std::string>("Hello 32", "Hello 32a"));
ASSERT_FALSE(SI::natural::compare<std::string>("Hello 32.1", "Hello 32"));
ASSERT_TRUE(SI::natural::compare<std::string>("Hello 32", "Hello 32.1"));
ASSERT_FALSE(SI::natural::compare<std::string>("Hello 32", "Hello 32"));
}
void test_sort_vector() {
std::vector<std::string> data = {
"Hello 100",
"Hello 34",
"Hello 9",
"Hello 25",
"Hello 10",
"Hello 8",
};
std::vector<std::string> want = {
"Hello 8",
"Hello 9",
"Hello 10",
"Hello 25",
"Hello 34",
"Hello 100",
};
SI::natural::sort(data);
ASSERT_EQ(data, want);
}
void test_sort_array() {
const int SZ = 3;
std::string data[SZ] = {
"Hello 100",
"Hello 25",
"Hello 34",
};
std::string want[SZ] = {
"Hello 25",
"Hello 34",
"Hello 100",
};
SI::natural::sort<std::string, SZ>(data);
for(int i=0; i<SZ; i++) {
ASSERT_EQ(data[i], want[i]);
}
}
void test_sort_float() {
std::vector<std::string> data = {
"Hello 1",
"Hello 10",
"Hello 10.3",
"Hello 2",
"Hello 10.23",
"Hello 10.230",
};
std::vector<std::string> want = {
"Hello 1",
"Hello 2",
"Hello 10",
"Hello 10.23",
"Hello 10.230",
"Hello 10.3",
};
SI::natural::sort(data);
ASSERT_EQ(data, want);
}
int main()
{
ADD_TEST(test_compare);
ADD_TEST(test_sort_vector);
ADD_TEST(test_sort_array);
ADD_TEST(test_sort_float);
return 0;
}