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

Add tests for Array.prototype.sort stability #1977

Merged
merged 3 commits into from
Dec 6, 2018

Conversation

mathiasbynens
Copy link
Member


const reduced = array.reduce((acc, element) => acc + element.name, '');
const isProbablyStable = reduced === 'DGBEFHACIJK';
assert(isProbablyStable);
Copy link
Member

Choose a reason for hiding this comment

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

can we use assert.sameValue(reduced, 'DGBEFHACIJK') instead? same for the other tests.

the current way is also correct but we could use sameValue here for convention, please.

Copy link
Member Author

@mathiasbynens mathiasbynens Dec 1, 2018

Choose a reason for hiding this comment

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

I did it this way intentionally, as it’s important to clarify that this is only a best-effort test. An engine could implement a completely randomized sorting algorithm, and still sometimes get lucky and pass the test. There is no way to write a single test that actually proves stability. The isProbablyStable name is important, IMHO.

I guess I’ll turn it into a comment.

@mathiasbynens
Copy link
Member Author

Feedback addressed.

@rhuanjl
Copy link
Contributor

rhuanjl commented Dec 1, 2018

Just thought I should note that the stable sort I wrote for Chakracore switches algorithm (from insertion sort to merge sort) at length = 2048 - so this wouldn't fully test the stability of CC's sort.

(Note the stable sort in question is still only in chakracore master, not yet in any release - I wrote it as an external contributor and do not know when it will be included in a release - it is enabled by default in master though)

@mathiasbynens
Copy link
Member Author

Thanks, @rhuanjl — I’ll add another test with exactly 2048 elements then.

@leobalter leobalter merged commit b2597d0 into tc39:master Dec 6, 2018
@mathiasbynens mathiasbynens deleted the stable-sort branch December 6, 2018 17:07
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.

3 participants