-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/add sort #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add sort #35
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,137 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*----------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The MIT License (MIT) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright (c) 2014-2025 Kim Kulling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this software and associated documentation files (the "Software"), to deal in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
the Software without restriction, including without limitation the rights to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
the Software, and to permit persons to whom the Software is furnished to do so, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
subject to the following conditions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The above copyright notice and this permission notice shall be included in all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
copies or substantial portions of the Software. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-----------------------------------------------------------------------------------------------*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#pragma once | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <cppcore/CPPCoreCommon.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace cppcore { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template<class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline int32_t compAscending(const void *lhs, const void *rhs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const T _lhs = *static_cast<const T *>(lhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const T _rhs = *static_cast<const T *>(rhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return (_lhs > _rhs) - (_lhs < _rhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template <typename Ty> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline int32_t compDescending(const void *_lhs, const void *_rhs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return compAscending<Ty>(_rhs, _lhs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template<class T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void swap(T& v1, T& v2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T tmp = v1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
v1 = v2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
v2 = tmp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void swap(uint8_t &lhs, uint8_t &rhs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t tmp = lhs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lhs = rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rhs = tmp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void swap(void *v1, void *v2, size_t stride) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t *lhs = (uint8_t*) v1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t *rhs = (uint8_t*) v2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const uint8_t *end = rhs + stride; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (rhs != end) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
swap(*lhs++, *rhs++); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void quicksortImpl(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (num < 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (_data == nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t *data = (uint8_t*) _data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
memcpy(pivot, &data[0], stride); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t l = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t g = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (size_t i=1; i<num;) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int32_t result = func(&data[i*stride], pivot); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (result > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
swap(&data[l*stride], &data[i*stride], stride); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
++l; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (result == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
swap(&data[g*stride], &data[i*stride], stride); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
++g; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
++i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
++i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
quicksortImpl(pivot, &data[0], l, stride, func); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
quicksortImpl(pivot, &data[g*stride], num - g, stride, func); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline void quicksort(void *_data, size_t num, size_t stride, ComparisonFn func) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t *pivot = (uint8_t*) CPPCORE_STACK_ALLOC(stride); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
quicksortImpl(pivot, _data, num, stride, func); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (num < 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t *data_ = (uint8_t *)data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (size_t i=1; i<num; ++i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const int32_t result = func(&data_[(i-1)*stride], &data_[i * stride]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (result == -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inline int32_t binSearchImpl(void *key, void *data, size_t num, size_t stride, ComparisonFn func) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t offset = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t *_data = (uint8_t *)data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (size_t i = num; offset < i;) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t idx = (offset + i) / 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int32_t result = func(key, &_data[i * stride]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (result < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
i = idx; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (result > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
offset = idx + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return idx; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ~offset; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+117
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix critical bug in binary search implementation. There's a serious bug in the binary search implementation: the comparison is done with inline int32_t binSearchImpl(void *key, void *data, size_t num, size_t stride, ComparisonFn func) {
size_t offset = 0;
uint8_t *_data = (uint8_t *)data;
for (size_t i = num; offset < i;) {
size_t idx = (offset + i) / 2;
- int32_t result = func(key, &_data[i * stride]);
+ int32_t result = func(key, &_data[idx * stride]);
if (result < 0) {
i = idx;
} else if (result > 0) {
offset = idx + 1;
} else {
return idx;
}
}
return ~offset;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int32_t binSearch(int32_t key, int32_t* array, size_t num, ComparisonFn func) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return binSearchImpl(&key, &array[0], num, sizeof(int32_t), func); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} // namespace cppcore |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||
------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
The MIT License (MIT) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Copyright (c) 2014-2025 Kim Kulling | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||||||||||||||||||||||||||||||||||
this software and associated documentation files (the "Software"), to deal in | ||||||||||||||||||||||||||||||||||
the Software without restriction, including without limitation the rights to | ||||||||||||||||||||||||||||||||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||||||||||||||||||||||||||||||||||
the Software, and to permit persons to whom the Software is furnished to do so, | ||||||||||||||||||||||||||||||||||
subject to the following conditions: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
The above copyright notice and this permission notice shall be included in all | ||||||||||||||||||||||||||||||||||
copies or substantial portions of the Software. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||||||||||||||||||||||||||||||||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||||||||||||||||||||||||||||||||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||||||||||||||||||||||||||||||||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||||||||||||||||||||||||||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||||||||||||||
------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
#include <cppcore/Common/Sort.h> | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
#include "gtest/gtest.h" | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
using namespace cppcore; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
class SortTest : public testing::Test {}; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
TEST_F(SortTest, swapTest) { | ||||||||||||||||||||||||||||||||||
int32_t i1 = 1; | ||||||||||||||||||||||||||||||||||
int32_t i2 = 2; | ||||||||||||||||||||||||||||||||||
swap(i1, i2); | ||||||||||||||||||||||||||||||||||
EXPECT_EQ(i1, 2); | ||||||||||||||||||||||||||||||||||
EXPECT_EQ(i2, 1); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
TEST_F(SortTest, isSortedTest ) { | ||||||||||||||||||||||||||||||||||
int32_t arr[] = {1,2,3,4,5}; | ||||||||||||||||||||||||||||||||||
bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>); | ||||||||||||||||||||||||||||||||||
EXPECT_TRUE(sorted); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+41
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify comparison function usage in The test is using - bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
+ bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
TEST_F(SortTest, isNotSortedTest) { | ||||||||||||||||||||||||||||||||||
int32_t arr[] = { 1, 2, 3, 5, 4 }; | ||||||||||||||||||||||||||||||||||
bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>); | ||||||||||||||||||||||||||||||||||
EXPECT_FALSE(sorted); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+47
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify comparison function usage in Similar to the previous test, this is using - bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
+ bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
TEST_F(SortTest, quicksortTest) { | ||||||||||||||||||||||||||||||||||
int32_t arr[] = { 1, 2, 3, 5, 4 }; | ||||||||||||||||||||||||||||||||||
quicksort(arr, 5, sizeof(int32_t), compDescending<int32_t>); | ||||||||||||||||||||||||||||||||||
bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>); | ||||||||||||||||||||||||||||||||||
EXPECT_TRUE(sorted); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
TEST_F(SortTest, binSearchTest) { | ||||||||||||||||||||||||||||||||||
int32_t arr[] = { 1, 2, 3, 5, 4 }; | ||||||||||||||||||||||||||||||||||
quicksort(arr, 5, sizeof(int32_t), compDescending<int32_t>); | ||||||||||||||||||||||||||||||||||
int32_t idx = binSearch(3, arr, 5, compDescending<int32_t>); | ||||||||||||||||||||||||||||||||||
EXPECT_EQ(idx, 2); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+60
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix and enhance the binary search test. The current test has several issues:
TEST_F(SortTest, binSearchTest) {
int32_t arr[] = { 1, 2, 3, 5, 4 };
- quicksort(arr, 5, sizeof(int32_t), compDescending<int32_t>);
- int32_t idx = binSearch(3, arr, 5, compDescending<int32_t>);
+ quicksort(arr, 5, sizeof(int32_t), compAscending<int32_t>);
+ int32_t idx = binSearch(3, arr, 5, compAscending<int32_t>);
EXPECT_EQ(idx, 2);
+
+ // Test searching for a value not in the array
+ idx = binSearch(6, arr, 5, compAscending<int32_t>);
+ EXPECT_LT(idx, 0);
} 📝 Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Make the
isSorted
check more robust.The function checks if
result == -1
to determine if an array is not sorted, but this assumes the comparison function returns specific values. It would be more robust to check ifresult < 0
.📝 Committable suggestion