How to assert list of values in ascending order?
Here is a list of values in an array:
[463, 246, 216, 194, 154, 152, 147, 140, 129, 128, 123, 118, 118, 102, 102, 101, 97, 96, 93, 85]
How can I ensure/assert through RSpec that the array list is in ascending order?
ruby rspec
add a comment |
Here is a list of values in an array:
[463, 246, 216, 194, 154, 152, 147, 140, 129, 128, 123, 118, 118, 102, 102, 101, 97, 96, 93, 85]
How can I ensure/assert through RSpec that the array list is in ascending order?
ruby rspec
3
list == list.sort
?
– Aleksei Matiushkin
May 31 '17 at 7:13
Providing an example in a question is generally helpful, but the inclusion of an arbitrary numeric array that is not referenced by the question serves no purpose.
– Cary Swoveland
May 31 '17 at 7:44
add a comment |
Here is a list of values in an array:
[463, 246, 216, 194, 154, 152, 147, 140, 129, 128, 123, 118, 118, 102, 102, 101, 97, 96, 93, 85]
How can I ensure/assert through RSpec that the array list is in ascending order?
ruby rspec
Here is a list of values in an array:
[463, 246, 216, 194, 154, 152, 147, 140, 129, 128, 123, 118, 118, 102, 102, 101, 97, 96, 93, 85]
How can I ensure/assert through RSpec that the array list is in ascending order?
ruby rspec
ruby rspec
edited Jun 5 '17 at 14:52
Prashanth Sams
asked May 31 '17 at 7:10
Prashanth SamsPrashanth Sams
7,13685176
7,13685176
3
list == list.sort
?
– Aleksei Matiushkin
May 31 '17 at 7:13
Providing an example in a question is generally helpful, but the inclusion of an arbitrary numeric array that is not referenced by the question serves no purpose.
– Cary Swoveland
May 31 '17 at 7:44
add a comment |
3
list == list.sort
?
– Aleksei Matiushkin
May 31 '17 at 7:13
Providing an example in a question is generally helpful, but the inclusion of an arbitrary numeric array that is not referenced by the question serves no purpose.
– Cary Swoveland
May 31 '17 at 7:44
3
3
list == list.sort
?– Aleksei Matiushkin
May 31 '17 at 7:13
list == list.sort
?– Aleksei Matiushkin
May 31 '17 at 7:13
Providing an example in a question is generally helpful, but the inclusion of an arbitrary numeric array that is not referenced by the question serves no purpose.
– Cary Swoveland
May 31 '17 at 7:44
Providing an example in a question is generally helpful, but the inclusion of an arbitrary numeric array that is not referenced by the question serves no purpose.
– Cary Swoveland
May 31 '17 at 7:44
add a comment |
2 Answers
2
active
oldest
votes
The simplest way is probably:
expect(array.sort).to eq(array)
1
To assert that it is in strictly ascending order, add a separate testexpect(array.uniq.size) == array.size
.
– Cary Swoveland
May 31 '17 at 7:18
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
1
Array#sort
is not stable, so this does not work if there are equal elements.
– Jörg W Mittag
May 31 '17 at 10:34
2
Array#sort
is stable in the context of this question (array of ints).
– Ilya
May 31 '17 at 10:48
add a comment |
"Ascending" means "the next element is not smaller than the current". You can encode that into a predicate easily:
expect(array.each_cons(2).all? {|a, b| a <= b }).to be_truthy
Note that Array#sort
is not stable, so something like
expect(array.sort).to eq(array)
does not work!
Could you please provide an instance, when sort does not work, but your answer works? Also,be_truthy
is not nil or false,be true
is enough.
– Ilya
May 31 '17 at 10:39
sort
is unstable, which means that if there are elements where<=>
returns0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where<=>
returns0
,==
returnstrue
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.
– Jörg W Mittag
May 31 '17 at 10:43
1
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44277994%2fhow-to-assert-list-of-values-in-ascending-order%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The simplest way is probably:
expect(array.sort).to eq(array)
1
To assert that it is in strictly ascending order, add a separate testexpect(array.uniq.size) == array.size
.
– Cary Swoveland
May 31 '17 at 7:18
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
1
Array#sort
is not stable, so this does not work if there are equal elements.
– Jörg W Mittag
May 31 '17 at 10:34
2
Array#sort
is stable in the context of this question (array of ints).
– Ilya
May 31 '17 at 10:48
add a comment |
The simplest way is probably:
expect(array.sort).to eq(array)
1
To assert that it is in strictly ascending order, add a separate testexpect(array.uniq.size) == array.size
.
– Cary Swoveland
May 31 '17 at 7:18
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
1
Array#sort
is not stable, so this does not work if there are equal elements.
– Jörg W Mittag
May 31 '17 at 10:34
2
Array#sort
is stable in the context of this question (array of ints).
– Ilya
May 31 '17 at 10:48
add a comment |
The simplest way is probably:
expect(array.sort).to eq(array)
The simplest way is probably:
expect(array.sort).to eq(array)
edited Nov 26 '18 at 4:43
answered May 31 '17 at 7:13
IlyaIlya
11.5k42441
11.5k42441
1
To assert that it is in strictly ascending order, add a separate testexpect(array.uniq.size) == array.size
.
– Cary Swoveland
May 31 '17 at 7:18
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
1
Array#sort
is not stable, so this does not work if there are equal elements.
– Jörg W Mittag
May 31 '17 at 10:34
2
Array#sort
is stable in the context of this question (array of ints).
– Ilya
May 31 '17 at 10:48
add a comment |
1
To assert that it is in strictly ascending order, add a separate testexpect(array.uniq.size) == array.size
.
– Cary Swoveland
May 31 '17 at 7:18
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
1
Array#sort
is not stable, so this does not work if there are equal elements.
– Jörg W Mittag
May 31 '17 at 10:34
2
Array#sort
is stable in the context of this question (array of ints).
– Ilya
May 31 '17 at 10:48
1
1
To assert that it is in strictly ascending order, add a separate test
expect(array.uniq.size) == array.size
.– Cary Swoveland
May 31 '17 at 7:18
To assert that it is in strictly ascending order, add a separate test
expect(array.uniq.size) == array.size
.– Cary Swoveland
May 31 '17 at 7:18
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
Yes @IIya that makes sense :)
– Prashanth Sams
May 31 '17 at 7:30
1
1
Array#sort
is not stable, so this does not work if there are equal elements.– Jörg W Mittag
May 31 '17 at 10:34
Array#sort
is not stable, so this does not work if there are equal elements.– Jörg W Mittag
May 31 '17 at 10:34
2
2
Array#sort
is stable in the context of this question (array of ints).– Ilya
May 31 '17 at 10:48
Array#sort
is stable in the context of this question (array of ints).– Ilya
May 31 '17 at 10:48
add a comment |
"Ascending" means "the next element is not smaller than the current". You can encode that into a predicate easily:
expect(array.each_cons(2).all? {|a, b| a <= b }).to be_truthy
Note that Array#sort
is not stable, so something like
expect(array.sort).to eq(array)
does not work!
Could you please provide an instance, when sort does not work, but your answer works? Also,be_truthy
is not nil or false,be true
is enough.
– Ilya
May 31 '17 at 10:39
sort
is unstable, which means that if there are elements where<=>
returns0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where<=>
returns0
,==
returnstrue
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.
– Jörg W Mittag
May 31 '17 at 10:43
1
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
add a comment |
"Ascending" means "the next element is not smaller than the current". You can encode that into a predicate easily:
expect(array.each_cons(2).all? {|a, b| a <= b }).to be_truthy
Note that Array#sort
is not stable, so something like
expect(array.sort).to eq(array)
does not work!
Could you please provide an instance, when sort does not work, but your answer works? Also,be_truthy
is not nil or false,be true
is enough.
– Ilya
May 31 '17 at 10:39
sort
is unstable, which means that if there are elements where<=>
returns0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where<=>
returns0
,==
returnstrue
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.
– Jörg W Mittag
May 31 '17 at 10:43
1
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
add a comment |
"Ascending" means "the next element is not smaller than the current". You can encode that into a predicate easily:
expect(array.each_cons(2).all? {|a, b| a <= b }).to be_truthy
Note that Array#sort
is not stable, so something like
expect(array.sort).to eq(array)
does not work!
"Ascending" means "the next element is not smaller than the current". You can encode that into a predicate easily:
expect(array.each_cons(2).all? {|a, b| a <= b }).to be_truthy
Note that Array#sort
is not stable, so something like
expect(array.sort).to eq(array)
does not work!
answered May 31 '17 at 10:34
Jörg W MittagJörg W Mittag
294k63359557
294k63359557
Could you please provide an instance, when sort does not work, but your answer works? Also,be_truthy
is not nil or false,be true
is enough.
– Ilya
May 31 '17 at 10:39
sort
is unstable, which means that if there are elements where<=>
returns0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where<=>
returns0
,==
returnstrue
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.
– Jörg W Mittag
May 31 '17 at 10:43
1
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
add a comment |
Could you please provide an instance, when sort does not work, but your answer works? Also,be_truthy
is not nil or false,be true
is enough.
– Ilya
May 31 '17 at 10:39
sort
is unstable, which means that if there are elements where<=>
returns0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where<=>
returns0
,==
returnstrue
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.
– Jörg W Mittag
May 31 '17 at 10:43
1
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
Could you please provide an instance, when sort does not work, but your answer works? Also,
be_truthy
is not nil or false, be true
is enough.– Ilya
May 31 '17 at 10:39
Could you please provide an instance, when sort does not work, but your answer works? Also,
be_truthy
is not nil or false, be true
is enough.– Ilya
May 31 '17 at 10:39
sort
is unstable, which means that if there are elements where <=>
returns 0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where <=>
returns 0
, ==
returns true
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.– Jörg W Mittag
May 31 '17 at 10:43
sort
is unstable, which means that if there are elements where <=>
returns 0
, there is no guarantee that they will keep their relative order. There is also no guarantee that for elements where <=>
returns 0
, ==
returns true
(although I would consider that very strange if not a bug, but hey, people do strange things). So, if there are two elements which have the same order but are not equal, then they may get swapped by sorting and resulting in the original array and the sorted array to not be equal.– Jörg W Mittag
May 31 '17 at 10:43
1
1
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
But there are only integers in the question, I think your answer is overhead in this context, but, of course, makes sense in some (very rare) conditions.
– Ilya
May 31 '17 at 10:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44277994%2fhow-to-assert-list-of-values-in-ascending-order%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
list == list.sort
?– Aleksei Matiushkin
May 31 '17 at 7:13
Providing an example in a question is generally helpful, but the inclusion of an arbitrary numeric array that is not referenced by the question serves no purpose.
– Cary Swoveland
May 31 '17 at 7:44