Skip to content

Commit af3d126

Browse files
committed
Add more tests
1 parent ed3f57b commit af3d126

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

include/cppcore/Common/Sort.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ namespace cppcore {
66

77
typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs);
88

9-
int32_t int_comp(const void *lhs, const void *rhs) {
10-
int32_t _lhs=0, _rhs=0;
11-
memcpy(&_lhs, lhs, sizeof(int32_t));
12-
memcpy(&_rhs, rhs, sizeof(int32_t));
13-
if (_lhs > _rhs) {
14-
return 1;
15-
} else if (lhs == rhs) {
16-
return 0;
17-
}
18-
return -1;
9+
template<class T>
10+
int32_t compAscending(const void *lhs, const void *rhs) {
11+
const T _lhs = *static_cast<const T *>(lhs);
12+
const T _rhs = *static_cast<const T *>(rhs);
13+
return (_lhs > _rhs) - (_lhs < _rhs);
1914
}
2015

21-
template<class T>
22-
void swap(T *v1, T *v2) {
23-
T tmp = *v1;
24-
*v1 = *v2;
25-
*v2 = *tmp;
16+
inline void swap(uint8_t &lhs, uint8_t &rhs) {
17+
uint8_t tmp = lhs;
18+
lhs = rhs;
19+
rhs = tmp;
2620
}
2721

28-
inline void quicksort(void *pivot, void *_data, size_t num, ComparisonFn func=int_comp) {
22+
inline void swap(void *v1, void *v2, size_t stride) {
23+
uint8_t *lhs = (uint8_t*) v1;
24+
uint8_t *rhs = (uint8_t*) v2;
25+
const uint8_t *end = rhs + stride;
26+
while (rhs != end) {
27+
swap(*lhs++, *rhs++);
28+
}
29+
}
30+
31+
inline void quicksort(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) {
2932
if (num < 2) {
3033
return;
3134
}
@@ -40,28 +43,28 @@ namespace cppcore {
4043
for (size_t i=1; i<num;) {
4144
int32_t result = func(&data[i], pivot);
4245
if (result > 0) {
43-
swap(&data[l], &data[i]);
46+
swap(&data[l*stride], &data[i*stride], stride);
4447
++l;
4548
} else if (result == 0) {
46-
swap(&data[g], &data[i]);
49+
swap(&data[g*stride], &data[i*stride], stride);
4750
++g;
4851
++i;
4952
} else {
5053
++i;
5154
}
5255
}
5356

54-
quicksort(pivot, &data[0], l, func);
55-
quicksort(pivot, &data[g], num-g, func);
57+
quicksort(pivot, &data[0], l, stride, func);
58+
quicksort(pivot, &data[g], num - g, stride, func);
5659
}
5760

58-
bool isSorted(void *data, size_t num, ComparisonFn func) {
61+
bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) {
5962
if (num < 2) {
6063
return true;
6164
}
62-
63-
for (size_t i=0; i<num-1; ++i) {
64-
const int32_t result = func(&data[i], &data[i+1]);
65+
uint8_t *data_ = (uint8_t *)data;
66+
for (size_t i=1; i<num; ++i) {
67+
const int32_t result = func(&data_[(i-1)*stride], &data_[i * stride]);
6568
if (result == -1) {
6669
return false;
6770
}

test/common/SortTest.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,19 @@ class SortTest : public testing::Test {
3737

3838
TEST_F(SortTest, isSortedTest ) {
3939
int32_t arr[] = {1,2,3,4,5};
40-
bool sorted = isSorted(arr, 5, int_comp);
40+
bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
4141
EXPECT_TRUE(sorted);
4242
}
4343

44+
TEST_F(SortTest, isNotSortedTest) {
45+
int32_t arr[] = { 1, 2, 3, 5, 4 };
46+
bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
47+
EXPECT_FALSE(sorted);
48+
}
49+
50+
TEST_F(SortTest, quicksortTest) {
51+
int32_t arr[] = { 1, 2, 3, 5, 4 };
52+
quicksort(&arr[0], &arr[0], 5, sizeof(int32_t), compAscending<int32_t>);
53+
bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
54+
EXPECT_TRUE(sorted);
55+
}

0 commit comments

Comments
 (0)