Skip to content

Instantly share code, notes, and snippets.

@groveriffic
Created April 16, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groveriffic/eec8fddf09a336baafc4 to your computer and use it in GitHub Desktop.
Save groveriffic/eec8fddf09a336baafc4 to your computer and use it in GitHub Desktop.
package ids
import (
"fmt"
"testing"
"testing/quick"
)
func ExampleMarshalString() {
fmt.Print("\n")
s := marshalString(9223372036854775807)
fmt.Println(s)
s = marshalString(19821031)
fmt.Print(s)
// Output:
// 7ZZZZZZZZZZZZ
// JWWF7
}
func ExampleUnmarshalString() {
fmt.Print("\n")
i, err := unmarshalString("7ZZZZZZZZZZZZ")
if err != nil {
fmt.Println(err)
}
fmt.Println(i)
i, err = unmarshalString("JWWF7")
if err != nil {
fmt.Println(err)
}
fmt.Println(i)
// Output:
// 9223372036854775807
// 19821031
}
// Positive int64 values should be isomorphic to encoded strings
// For any postive int64 value we should be able to encode it, decode it and have the same value
func TestIsomorphism(t *testing.T) {
abs := func(i int64) int64 {
if i < 0 {
return i * -1
}
return i
}
absMarshalUnmarshal := func(i int64) int64 {
// fmt.Println("DEBUG: ", i, abs(i))
enc := marshalString(abs(i))
dec, err := unmarshalString(enc)
if err != nil {
return -1
}
return dec
}
err := quick.CheckEqual(abs, absMarshalUnmarshal, nil)
if err != nil {
t.Error(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment