Skip to content

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

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ SET ( cppcore_common_src
include/cppcore/Common/Hash.h
include/cppcore/Common/TStringBase.h
include/cppcore/Common/Variant.h
include/cppcore/Common/Sort.h
include/cppcore/Common/TBitField.h
include/cppcore/Common/TOptional.h
)
Expand Down Expand Up @@ -133,6 +134,7 @@ IF( CPPCORE_BUILD_UNITTESTS )
SET( cppcore_common_test_src
test/common/HashTest.cpp
test/common/VariantTest.cpp
test/common/SortTest.cpp
test/common/TBitFieldTest.cpp
test/common/TOptionalTest.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion code/cppcore.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
3 changes: 3 additions & 0 deletions include/cppcore/CPPCoreCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>
#include <malloc.h>

namespace cppcore {

Expand All @@ -53,8 +54,10 @@ namespace cppcore {
# endif
// All disabled warnings for windows
# pragma warning( disable : 4251 ) // <class> needs to have dll-interface to be used by clients of class <class>
# define CPPCORE_STACK_ALLOC(size) ::alloca(size)
#else
# define DLL_CPPCORE_EXPORT __attribute__((visibility("default")))
# define CPPCORE_STACK_ALLOC(size) __builtin_alloca(size)
#endif

//-------------------------------------------------------------------------------------------------
Expand Down
137 changes: 137 additions & 0 deletions include/cppcore/Common/Sort.h
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;
}
Comment on lines +102 to +115
Copy link
Contributor

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 if result < 0.

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) {
+        if (result < 0) {
            return false;
        }
    }

    return true;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
}
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 < 0) {
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix critical bug in binary search implementation.

There's a serious bug in the binary search implementation: the comparison is done with &_data[i * stride] where i can be as large as num, potentially causing out-of-bounds memory access.

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
}
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[idx * stride]);
if (result < 0) {
i = idx;
} else if (result > 0) {
offset = idx + 1;
} else {
return idx;
}
}
return ~offset;
}


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
2 changes: 1 addition & 1 deletion include/cppcore/Common/TBitField.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Common/TOptional.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Common/TStringBase.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Common/Variant.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Container/TArray.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Container/THashMap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Container/TList.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Container/TQueue.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Container/TStaticArray.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Memory/MemUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Memory/TDefaultAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Memory/TPoolAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Memory/TStackAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion include/cppcore/Random/RandomGenerator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion test/CPPCoreCommonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-------------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
2 changes: 1 addition & 1 deletion test/common/HashTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-------------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2024 Kim Kulling
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
Expand Down
65 changes: 65 additions & 0 deletions test/common/SortTest.cpp
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Verify comparison function usage in isSortedTest.

The test is using compDescending but expecting a sorted array in ascending order to return true. This seems contradictory and may lead to unexpected behavior.

-    bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
+    bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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);
}
TEST_F(SortTest, isSortedTest ) {
int32_t arr[] = {1,2,3,4,5};
bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
EXPECT_TRUE(sorted);
}


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Verify comparison function usage in isNotSortedTest.

Similar to the previous test, this is using compDescending on an array that appears to be in ascending order. Ensure this is the intended behavior.

-    bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
+    bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

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);
}
TEST_F(SortTest, isNotSortedTest) {
int32_t arr[] = { 1, 2, 3, 5, 4 };
bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending<int32_t>);
EXPECT_FALSE(sorted);
}


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
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. No verification that the search returns -1 for values not in the array
  2. Uses the same comparison function for sorting and searching
 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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);
}
TEST_F(SortTest, binSearchTest) {
int32_t arr[] = { 1, 2, 3, 5, 4 };
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);
}

Loading