Shared set of hashids tests
For each of the 40 implementations listed on the hashids website, has someone made sure they give the same results as each other? I'm sure several of them have been checked against others. But I wouldn't be surprised if at least one of them gives different results.
I feel like the website should have resources for implementers, and one of the resources should be a file containing a long list of unit tests. Possibly in JSON, possibly some other format that's easy to parse. Then implementers can include that file in their own test suites, and just write a (hopefully fairly simple) test runner for them. It would presumably have tests like:
- Encoding this input list with (salt, min length, alphabet) gives this output string.
- Decoding this (correctly encoded) string with (salt, min length, alphabet) gives this output list.
- Decoding this (not a correctly encoded, but might look like one) string with (salt, min length, alphabet) fails, but doesn't crash unexpectedly.
- Include edge cases. What's supposed to happen if you encode an empty list? Or one with negative numbers? Numbers that can't be represented as a float/double/64-bit int/whatever?[1] What if you use a too-short alphabet, or one with duplicate letters? Are the salt and/or alphabet allowed to include multi-byte characters? (If so, what if you have an alphabet with more than 255 letters, does that work fine?) Some of these might be left up to implementations to decide what fits in with their local ecosystem, but including them in the list of tests encourages implementers to think about them.
[1]: If using JSON, such numbers might need to be written as strings. Otherwise the parsing step might not preserve them correctly.
Indeed, it seems the postgres implementation is not consistent: https://github.com/array-analytics/plpg_hashids/issues/13
@ChickenProp Sorry for the late response and I agree - the tests should be the same across implementations. We're fixing that as we're moving to the new algorithm.
Closing since the project is moving.