Skip to content
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

test: test bottom-up merge sort in URLSearchParams #11399

Closed
wants to merge 1 commit into from

Conversation

watilde
Copy link
Contributor

@watilde watilde commented Feb 15, 2017

Following up for this commit: 02d1e32 to increase the reduced coverage. The bottom-up iterative stable merge sort is called only when the length of provided value is larger than 100. Added a test for it.

This increases the coverage of internal/url.js,

and the following lines will be covered.

  • node/lib/internal/url.js

    Lines 701 to 727 in e4e7cd5

    function merge(out, start, mid, end, lBuffer, rBuffer) {
    const sizeLeft = mid - start;
    const sizeRight = end - mid;
    var l, r, o;
    for (l = 0; l < sizeLeft; l++)
    lBuffer[l] = out[start + l];
    for (r = 0; r < sizeRight; r++)
    rBuffer[r] = out[mid + r];
    l = 0;
    r = 0;
    o = start;
    while (l < sizeLeft && r < sizeRight) {
    if (lBuffer[l] <= rBuffer[r]) {
    out[o++] = lBuffer[l++];
    out[o++] = lBuffer[l++];
    } else {
    out[o++] = rBuffer[r++];
    out[o++] = rBuffer[r++];
    }
    }
    while (l < sizeLeft)
    out[o++] = lBuffer[l++];
    while (r < sizeRight)
    out[o++] = rBuffer[r++];
    }
  • node/lib/internal/url.js

    Lines 886 to 900 in e4e7cd5

    } else {
    // Bottom-up iterative stable merge sort
    const lBuffer = new Array(len);
    const rBuffer = new Array(len);
    for (var step = 2; step < len; step *= 2) {
    for (var start = 0; start < len - 2; start += 2 * step) {
    var mid = start + step;
    var end = mid + step;
    end = end < len ? end : len;
    if (mid > end)
    continue;
    merge(a, start, mid, end, lBuffer, rBuffer);
    }
    }
    }
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

test

@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label Feb 15, 2017
@mscdex mscdex added the whatwg-url Issues and PRs related to the WHATWG URL implementation. label Feb 15, 2017
@@ -82,3 +82,34 @@ const { test, assert_array_equals } = common.WPT;
}
}, 'URL parse and sort: ' + val.input);
});

// Test bottom-up iterative stable merge sort
const params = {input: '', output: []};
Copy link
Member

@joyeecheung joyeecheung Feb 15, 2017

Choose a reason for hiding this comment

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

If I am reading correctly the test part below just duplicates the code above? Then we can probably just construct the params first and then put it in the array literal that's being forEached above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's nice. I've updated it :)

@watilde watilde force-pushed the test-internal-url-merge branch 2 times, most recently from e653c27 to cca745a Compare February 15, 2017 18:08
const tests = [{input: '', output: []}];
for (let i = 0; 50 > i; i++) {
tests[0].input += 'a=b&';
tests[0].output.push(['a', 'b']);
Copy link
Member

Choose a reason for hiding this comment

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

Hmm..so looks like this doesn't actually catch a mistake in the implementation because we are sorting identical pairs...maybe we can generate the keys on the fly, pseudo-shuffle them, then compared to another original copy to make sure they are sorted properly?

const tests = [{input: '', output: []}];
const pairs = [];
for (let i = 10; i < 60; i++) {
  pairs.push([`a${i}`, 'b']);
  tests[0].output.push([`a${i}`, 'b']);
}
tests[0].input = pairs.sort(() => Math.random() > 0.5).map((pair) => pair.join('=')).join('&');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right indeed. To use the random list to sort them actually, I've updated it with your comment. Thanks!

@watilde watilde force-pushed the test-internal-url-merge branch from cca745a to 461fea7 Compare February 15, 2017 23:32
Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

LGTM.

// Test bottom-up iterative stable merge sort
const tests = [{input: '', output: []}];
const pairs = [];
for (let i = 10; i < 60; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: I'd be more comfortable if there are more than just the threshold number of elements in pairs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It sounds good to me as well. I will replace 60 with 100 later :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've updated.

@TimothyGu
Copy link
Member

The bottom-up iterative stable merge sort is called only when
the length of provided value is larger than 100. Added a test for it.
@watilde watilde force-pushed the test-internal-url-merge branch from 461fea7 to 8176dc3 Compare February 16, 2017 13:03
@TimothyGu
Copy link
Member

@jasnell
Copy link
Member

jasnell commented Feb 16, 2017

CI failures are unrelated.

jasnell pushed a commit that referenced this pull request Feb 19, 2017
The bottom-up iterative stable merge sort is called only when
the length of provided value is larger than 100. Added a test for it.

PR-URL: #11399
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
@jasnell
Copy link
Member

jasnell commented Feb 19, 2017

Landed in 1b31ca6

@jasnell jasnell closed this Feb 19, 2017
@watilde watilde deleted the test-internal-url-merge branch February 20, 2017 00:33
italoacasas pushed a commit to italoacasas/node that referenced this pull request Feb 25, 2017
The bottom-up iterative stable merge sort is called only when
the length of provided value is larger than 100. Added a test for it.

PR-URL: nodejs#11399
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
@italoacasas italoacasas mentioned this pull request Feb 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants