Skip to content

luhn: Add more tests#522

Merged
petertseng merged 1 commit intoexercism:masterfrom
gibfahn:update-luhn
Feb 8, 2017
Merged

luhn: Add more tests#522
petertseng merged 1 commit intoexercism:masterfrom
gibfahn:update-luhn

Conversation

@gibfahn
Copy link
Copy Markdown
Contributor

@gibfahn gibfahn commented Jan 30, 2017

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

This updates the test suite to cover these.

Let me know if I'm doing anything wrong.

Refs: exercism/rust#253

@Insti
Copy link
Copy Markdown
Contributor

Insti commented Jan 30, 2017

Thanks @gibfahn

Your suggestions have caused me to identify something I think is an issue with this problem:#523
I don't think the string parsing tests belong in this problem.

(It's reasonable for you to have added more string parsing tests to given the current specification. ❤️ )

@Insti
Copy link
Copy Markdown
Contributor

Insti commented Jan 30, 2017

Strings of zeros

Why are strings of zeros invalid?

Numbers which are only valid if you reverse the string

What is this testing?

@gibfahn
Copy link
Copy Markdown
Contributor Author

gibfahn commented Jan 30, 2017

Why are strings of zeros invalid?

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.

Numbers which are only valid if you reverse the string

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.

@stevejb71
Copy link
Copy Markdown
Contributor

@gibfahn "but I'm willing to believe 0 is a valid answer"

The readme states that strings of length 1 or less are invalid.

@gibfahn
Copy link
Copy Markdown
Contributor Author

gibfahn commented Jan 31, 2017

Why are strings of zeros invalid?
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 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. 0000) where the calculation ends up being 0 mod 10 == 0.

@Insti
Copy link
Copy Markdown
Contributor

Insti commented Jan 31, 2017

Having tests for both valid and invalid zero strings would be useful.

000 valid
0000 invalid - missing check digit.

@NobbZ
Copy link
Copy Markdown
Member

NobbZ commented Jan 31, 2017 via email

@gibfahn
Copy link
Copy Markdown
Contributor Author

gibfahn commented Jan 31, 2017

My (current) interpretation is that a single zero is invalid, but multiple zeros are always valid.

Comment thread exercises/luhn/canonical-data.json Outdated
{
"description": "lots of zeros are invalid",
"input": " 00000",
"expected": false
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

don't forget to change this one (I think we want this one to be valid, right?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread exercises/luhn/canonical-data.json Outdated
"expected": true
},
{
"description": "simple valid sin",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

probably worth moving this up such that it becomes the first true case - let the student solve the simplest case first.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Member

@petertseng petertseng left a comment

Choose a reason for hiding this comment

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

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",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can I get a confirmation on whether the £ is in ASCII? I am seeing bytes 0xc2 0xa3 for it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is not.

@gibfahn gibfahn deleted the update-luhn branch September 17, 2017 12:10
emcoding pushed a commit that referenced this pull request Nov 19, 2018
* 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
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.

5 participants