Skip to content

✨ feat(mq-lang): Add bsearch function and tests#1353

Merged
harehare merged 4 commits intomainfrom
feat/bsearch
Feb 28, 2026
Merged

✨ feat(mq-lang): Add bsearch function and tests#1353
harehare merged 4 commits intomainfrom
feat/bsearch

Conversation

@harehare
Copy link
Owner

  • Implements binary search (bsearch) for sorted arrays in builtin.mq
  • Adds comprehensive tests for bsearch in builtin_tests.mq

Closes #1341

- Implements binary search (bsearch) for sorted arrays in builtin.mq
- Adds comprehensive tests for bsearch in builtin_tests.mq

Closes #1341
Copilot AI review requested due to automatic review settings February 28, 2026 00:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a bsearch (binary search) function in mq's built-in library, as requested in issue #1341. The function searches a sorted array for a target value and returns its index, or -1 if not found. The implementation uses a recursive inner lambda (_bsearch_impl) following the same idiom as the existing find_index function.

Changes:

  • Adds the bsearch function in builtin.mq with input validation (must be an array) and a recursive inner lambda for the binary search logic
  • Adds test_bserch test function in builtin_tests.mq covering found/not-found and various element types

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
crates/mq-lang/builtin.mq Implements bsearch(arr, target) using a recursive inner lambda _bsearch_impl
crates/mq-lang/builtin_tests.mq Adds test function for bsearch with 4 test cases

| assert_eq(result3, "abc ")
end

def test_bserch():
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The test function is named test_bserch, which is a misspelling of "search". It should be named test_bsearch to match the function being tested.

Suggested change
def test_bserch():
def test_bsearch():

Copilot uses AI. Check for mistakes.
Comment on lines +589 to +601
def test_bserch():
let result1 = bsearch([1, 2, 3, 4, 5], 3)
| assert_eq(result1, 2)

| let result2 = bsearch([1, 2, 3, 4, 5], 6)
| assert_eq(result2, -1)

| let result3 = bsearch([10, 20, 30], 10)
| assert_eq(result3, 0)

| let result4 = bsearch(["a", "b", "c"], "b")
| assert_eq(result4, 1)
end
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The test function test_bserch (and after fixing the typo, test_bsearch) is defined but never registered in the run_tests([...]) call. Without this registration, the tests will never be executed as part of the test suite. A corresponding test_case("bsearch", test_bsearch) entry should be added, ideally in the "# Array manipulation" section alongside similar array functions like find_index.

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

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

@copilot
Can you fix it?

| assert_eq(result3, 0)

| let result4 = bsearch(["a", "b", "c"], "b")
| assert_eq(result4, 1)
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The test suite is missing a test case for an empty array input: bsearch([], x). When an empty array is passed, len([]) - 1 evaluates to -1, so _bsearch_impl is called with low = 0 and high = -1, which should correctly return -1. This edge case is worth covering explicitly in the tests, both to document the expected behavior and to guard against regressions.

Suggested change
| assert_eq(result4, 1)
| assert_eq(result4, 1)
| let result5 = bsearch([], 1)
| assert_eq(result5, -1)

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI commented Feb 28, 2026

@harehare I've opened a new pull request, #1354, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits February 28, 2026 00:33
Co-authored-by: harehare <533078+harehare@users.noreply.github.com>
Fix typo and register bsearch test in run_tests
@harehare harehare merged commit f4b6efb into main Feb 28, 2026
4 checks passed
@harehare harehare deleted the feat/bsearch branch February 28, 2026 00:45
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 28, 2026

Merging this PR will not alter performance

✅ 29 untouched benchmarks


Comparing feat/bsearch (b748658) with main (bed1b9e)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (b748658) during the generation of this report, so bed1b9e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add bsearch function

3 participants