luhn: Add more tests#522
Conversation
Why are strings of zeros invalid?
What is this testing? |
Looking at the README I somehow got the impression that they were invalid. I haven't found it clearly stated anywhere, but I'm willing to believe 0 is a valid answer.
The formula works by reversing the sequence of numbers and going through them doubling every second digit. The current set of answers all pass even if you forget to reverse the sequence. So I guess zero strings should pass. I'll update. |
|
@gibfahn "but I'm willing to believe 0 is a valid answer" The readme states that strings of length 1 or less are invalid. |
0 as in the result of the formula is zero, talking about strings of zeros (i.e. |
|
Having tests for both valid and invalid zero strings would be useful.
|
|
Why are 4 zeros missing the checkdigit? I take all but the last one, sum
them up and get a sum of 0, now I substract 0 from 10 and get 10. 10 % 10
is 0, so my checkdigit is 0, which again is equal to what I took away
before summing up. so 0000 is valid as well.
Geoff Hubbard <notifications@github.com> schrieb am Di., 31. Jan. 2017 um
10:18 Uhr:
… Having tests for both valid and invalid zero strings would be useful.
000 valid
0000 invalid - missing check digit.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#522 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADmR2yWKX5WTtgm_WXZnNGtE8ir7MT-ks5rXvxQgaJpZM4LxJNP>
.
|
|
My (current) interpretation is that a single zero is invalid, but multiple zeros are always valid. |
| { | ||
| "description": "lots of zeros are invalid", | ||
| "input": " 00000", | ||
| "expected": false |
There was a problem hiding this comment.
don't forget to change this one (I think we want this one to be valid, right?)
| "expected": true | ||
| }, | ||
| { | ||
| "description": "simple valid sin", |
There was a problem hiding this comment.
probably worth moving this up such that it becomes the first true case - let the student solve the simplest case first.
There was a problem hiding this comment.
also another note that this is another case that will reveal doubling from the wrong side - if doubling the wrong digits here we will get 14 instead of 10 and falsely think this is invalid.
This could possibly make the "must double from right" case redundant.
There was a problem hiding this comment.
Makes sense, I've moved this up and removed the "must double from right" case
The luhn test suite didn't cover a lot of cases, including: - Strings of zeros - Non-alphabetic characters - Numbers which are only valid if you reverse the string
There was a problem hiding this comment.
These cases seem suitable for inclusion. I have one question about what the "another valid sin" is doing.
It's acknowledged by me that some of the added cases will be removed if #523 decides in favour of no validation, but that is an orthogonal concern to this PR. My approval on this PR is not meant to be construed as a vote in either direction on #523.
| "expected": true | ||
| }, | ||
| { | ||
| "description": "nine doubled is nine", |
There was a problem hiding this comment.
in case anyone wonder, this caught a bug in mine (I was using mod 9 after doubling, which turns nine into zero instead of nine)
| }, | ||
| { | ||
| "description": "another valid sin", | ||
| "input": "055 444 285", |
There was a problem hiding this comment.
can you confirm - what does this test that above cases do not? is it just here to contrast with 055-444-285? What is an example of an implementation that would pass the above cases but fail this one?
There was a problem hiding this comment.
Part of it was the contrast with 0555-444-285, and part of it was the thought that having a couple of different longer valid numbers might help someone who was struggling to work out how the algorithm needed to be implemented.
I can remove it if necessary.
| }, | ||
| { | ||
| "description": "symbols are not allowed", | ||
| "input": "055£ 444$ 285", |
There was a problem hiding this comment.
can I get a confirmation on whether the £ is in ASCII? I am seeing bytes 0xc2 0xa3 for it
* bin/generator: Big rewrite Can now generate tests without updating the test version number. New command line options: ``` Usage: bin/generate [options] exercise-generator -f, --freeze Don't update test version -h, --help Prints this help -v, --verbose Display progress messages ``` `sha1` in the "Common test data version: <%= sha1 %>" comment is now the commit ID of the relevant canonical-data.json file Split code up into classes. 100% test coverage. Added dependency: `require_all` gem Just requiring files in (potentially random) directory order was not sufficient to satisfy all class dependencies. The `require_all` gem ensures dependencies are met. * Rename 'new_content' to 'content' * Rename `isogram` to `beta` There's nothing special about isogram as a name, and `beta` continues the greek-letter fixture exercise naming pattern. * Rename `save_if_changed` to `save` * Use << rather than + for string concatenation * Remove redundant comment
The luhn test suite didn't cover a lot of cases, including:
This updates the test suite to cover these.
Let me know if I'm doing anything wrong.
Refs: exercism/rust#253