Skip to content

Add validity tests to cover edge cases and make validation uniform across languages#750

Open
willbeason wants to merge 2 commits intogoogle:mainfrom
willbeason:add-tests
Open

Add validity tests to cover edge cases and make validation uniform across languages#750
willbeason wants to merge 2 commits intogoogle:mainfrom
willbeason:add-tests

Conversation

@willbeason
Copy link
Contributor

@willbeason willbeason commented Mar 7, 2026

Add validity tests to cover edge cases

I added these tests to improve Go coverage for optimizations I want to be sure are safe. Previously only Check() was tested, but not CheckShort(), CheckFull() and Decode(), each of which have their own (previously untested) validation logic. In adding these, I found two cases where the libraries of various language handle validation logic inconsistently. In cases of inconsistency, I defaulted to the behavior used by most language in the repository.

  1. Automatically expanding requested short code length if odd. Most of the language implementations automatically add 1 to the length of requested codes if the length indicates it is in the pair part of the code.

  2. Validate latitude and longitude bounds within validation logic for full codes. Most of the language implementations only check the first two characters for latitude/longitude bounds when within the validation logic for full codes, not when generally checking if codes are valid. This does introduce a strange case where a code is "valid", but is neither a valid short nor a valid long code. As this was the preexisting behavior, I have defaulted to this.

go: Additionally, I removed the duplicate check for len(code) == 1 in CheckFull(). This path cannot be reached if Check() passes. I've added a test to verify this is the case.

This improves Go coverage for optimizations I want to be sure are safe.
I added these tests to improve Go coverage for optimizations I want to be sure
are safe. In adding these, I found two cases where the libraries of various
language handle validation logic inconsistently. In cases of inconsistency, I
defaulted to the behavior used by most language in the repository.

1. Automatically expanding requested short code length if odd. Most of the
language implementations automatically add 1 to the length of requested codes
if the length indicates it is in the pair part of the code.

2. Validate latitude and longitude bounds within validation logic for full
codes. Most of the language implementations only check the first two characters
for latitude/longitude bounds when within the validation logic for full codes,
not when generally checking if codes are valid. This does introduce a strange
case where a code is "valid", but is neither a valid short nor a valid long
code. As this was the preexisting behavior, I have defaulted to this.
@willbeason
Copy link
Contributor Author

Opened Issue to document this discrepancy: #751

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.

1 participant