-
Notifications
You must be signed in to change notification settings - Fork 76
Review the C++ implementation of the binary search function #53
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
Open
gshiroma
wants to merge
16
commits into
isce-framework:develop
Choose a base branch
from
gshiroma:prevent_infinite_loop_in_binary_search
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
108a19e
limit the maximum number of iterations for binary search
gshiroma d396562
limit the maximum number of iterations for binary search
gshiroma e19052c
raise an error if array contains NaN
gshiroma 15d38d3
add imports
gshiroma feef376
simplify code
gshiroma 1b2e7e9
limit the number of iterations in the binary search
gshiroma a3684da
use size_t instead of long long
gshiroma 96915ae
update the binary search `binarySearch()`
gshiroma 8893ba8
update the binary search `binarySearch()`
gshiroma 0da209a
add unit tests for `binarySearch()`
gshiroma e8f5f7f
remove comments
gshiroma 563fea9
add argument `always_pick_left` to `binarySearch()`
gshiroma 00a33f7
fix argument position
gshiroma d6ef793
fix argument position
gshiroma 5f3c682
handle values outside array; improve docstrings; fix logic for array[…
gshiroma f314eaa
improve docstrings
gshiroma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <isce3/core/Utilities.h> | ||
|
|
||
| using isce3::core::binarySearch; | ||
|
|
||
| TEST(BinarySearchTest, SingleElementArray) { | ||
| std::valarray<double> arr = {2.0}; | ||
| EXPECT_EQ(binarySearch(arr, 0.0), 0); | ||
| EXPECT_EQ(binarySearch(arr, 100.0), 0); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, SingleElementArrayPickLeft) { | ||
| const bool always_pick_left = true; | ||
| std::valarray<double> arr = {2.0}; | ||
| EXPECT_EQ(binarySearch(arr, 0.0, always_pick_left), 0); | ||
| EXPECT_EQ(binarySearch(arr, 100.0, always_pick_left), 0); | ||
| } | ||
|
|
||
|
|
||
| TEST(BinarySearchTest, ClosestMatchExact) { | ||
| std::valarray<double> arr = {1.0, 3.0, 5.0, 7.0}; | ||
| EXPECT_EQ(binarySearch(arr, 3.0), 1); | ||
| EXPECT_EQ(binarySearch(arr, 7.0), 3); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ClosestMatchExactPickLeft) { | ||
| const bool always_pick_left = true; | ||
| std::valarray<double> arr = {1.0, 3.0, 5.0, 7.0}; | ||
| EXPECT_EQ(binarySearch(arr, 3.0, always_pick_left), 1); | ||
| EXPECT_EQ(binarySearch(arr, 7.0, always_pick_left), 3); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ClosestMatchInBetween) { | ||
| std::valarray<double> arr = {1.0, 3.0, 5.0, 7.0}; | ||
| EXPECT_EQ(binarySearch(arr, 2.1), 1); // closer to `3` | ||
| EXPECT_EQ(binarySearch(arr, 4.1), 2); // closer to `5` | ||
| EXPECT_EQ(binarySearch(arr, 6.4), 3); // closer to `7` | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ClosestMatchInBetweenPickLeft) { | ||
| const bool always_pick_left = true; | ||
| std::valarray<double> arr = {1.0, 3.0, 5.0, 7.0}; | ||
| EXPECT_EQ(binarySearch(arr, 2.1, always_pick_left), 0); | ||
| EXPECT_EQ(binarySearch(arr, 4.1, always_pick_left), 1); | ||
| EXPECT_EQ(binarySearch(arr, 6.4, always_pick_left), 2); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ValueBeforeFirstElement) { | ||
| std::valarray<double> arr = {1.0, 2.0, 3.0}; | ||
| EXPECT_EQ(binarySearch(arr, -1.0), 0); // `-1` closest to `1` | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ValueBeforeFirstElementPickLeft) { | ||
| const bool always_pick_left = true; | ||
| std::valarray<double> arr = {1.0, 2.0, 3.0}; | ||
| EXPECT_EQ(binarySearch(arr, -1.0, always_pick_left), 0); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ValueAfterLastElement) { | ||
| std::valarray<double> arr = {1.0, 2.0, 3.0}; | ||
| EXPECT_EQ(binarySearch(arr, 5.0), 2); // `5` closest to `3` | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ValueAfterLastElementPickLeft) { | ||
| const bool always_pick_left = true; | ||
| std::valarray<double> arr = {1.0, 2.0, 3.0}; | ||
| EXPECT_EQ(binarySearch(arr, 5.0, always_pick_left), 2); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, EqualDistanceChoosesLeft) { | ||
| std::valarray<double> arr = {1.0, 2.0, 3.0, 4.0}; | ||
|
|
||
| // `2` and `3` equally distant; picks left (`2`) | ||
| EXPECT_EQ(binarySearch(arr, 2.5), 1); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, EqualDistanceChoosesLeftPickLeft) { | ||
| const bool always_pick_left = true; | ||
| std::valarray<double> arr = {1.0, 2.0, 3.0, 4.0}; | ||
|
|
||
| // `2` and `3` equally distant; picks left (`2`) | ||
| EXPECT_EQ(binarySearch(arr, 2.5, always_pick_left), 1); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ThrowsOnEmptyArray) { | ||
| std::valarray<double> arr; | ||
| EXPECT_THROW(binarySearch(arr, 0.0), std::invalid_argument); | ||
| } | ||
|
|
||
| TEST(BinarySearchTest, ThrowsOnNaN) { | ||
| std::valarray<double> arr = {1.0, 2.0, NAN, 4.0}; | ||
| EXPECT_THROW(binarySearch(arr, 3.0), std::invalid_argument); | ||
| } | ||
|
|
||
| int main(int argc, char * argv[]) | ||
| { | ||
| testing::InitGoogleTest(&argc, argv); | ||
| return RUN_ALL_TESTS(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.