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

Fix Company.luhn_algorithm and add missing tests. #1335

Merged
merged 2 commits into from
Sep 4, 2018

Conversation

01max
Copy link
Contributor

@01max 01max commented Aug 17, 2018

There is some issues with Faker::Company.luhn_algorithm(number) :

  • Generates non "luhn-compliant" strings for even length number
  • Not tested

The problem was that luhn_algorithm didn't reverse the number submitted argument before splitting it (as it should).

This forgotten reverse resulted in the generation of non "luhn-compliant" strings for even length number.

index 0 1 2 3 4 5
odd_base 9 8 4 5 6
reserved_odd_base 6 5 4 8 9
selected numbers for odd_base *2 by luh_algorithm 9 4 6
right numbers for odd_base *2 by luh_algorithm 6 4 9
even_base 8 7 6 9 4 3
reserved_even_base 3 4 9 6 7 8
selected numbers for even_base *2 by luh_algorithm 8 6 4
right numbers for even_base *2 by luh_algorithm 3 9 7

No issue for odd length arguments ([9,4,6] vs [6,4,9]), only with even ([8,6,4] vs [3,9,7]).

This commit fixes the issue and add test_luhn_algorithm to avoid future issues.

@01max 01max force-pushed the fixes/luhn_algorithm branch from a04f250 to 1739ce2 Compare September 4, 2018 09:08
@01max
Copy link
Contributor Author

01max commented Sep 4, 2018

I rebased on master since some new commits added conflicts since my PR has been submitted.

Can someone review this ?

test/test_faker_company.rb Outdated Show resolved Hide resolved
@vbrazo
Copy link
Member

vbrazo commented Sep 4, 2018

Thanks for adding the missing test. I just asked you to rename the a variable because a doesn't mean anything in that context. I tested locally. Overall looks good. Thanks @01max

@01max 01max force-pushed the fixes/luhn_algorithm branch from 1739ce2 to 0e8d10c Compare September 4, 2018 11:44
@01max
Copy link
Contributor Author

01max commented Sep 4, 2018

I improved the last commit.

Thanks for the review @vbrazo !

@vbrazo vbrazo merged commit 3160293 into faker-ruby:master Sep 4, 2018
davidmorton0 pushed a commit to davidmorton0/faker that referenced this pull request Jul 12, 2021
* Fix Company.luhn_algorithm and add missing tests.

* Fix TestFakerCompany::luhn_checksum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants