Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement