Advertisement
Guest User

Untitled

a guest
May 26th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. package ids
  2.  
  3. import (
  4. "fmt"
  5. "testing"
  6. "testing/quick"
  7. )
  8.  
  9. func ExampleMarshalString() {
  10. fmt.Print("\n")
  11.  
  12. s := marshalString(9223372036854775807)
  13. fmt.Println(s)
  14.  
  15. s = marshalString(19821031)
  16. fmt.Print(s)
  17.  
  18. // Output:
  19. // 7ZZZZZZZZZZZZ
  20. // JWWF7
  21. }
  22.  
  23. func ExampleUnmarshalString() {
  24. fmt.Print("\n")
  25.  
  26. i, err := unmarshalString("7ZZZZZZZZZZZZ")
  27. if err != nil {
  28. fmt.Println(err)
  29. }
  30. fmt.Println(i)
  31.  
  32. i, err = unmarshalString("JWWF7")
  33. if err != nil {
  34. fmt.Println(err)
  35. }
  36. fmt.Println(i)
  37.  
  38. // Output:
  39. // 9223372036854775807
  40. // 19821031
  41. }
  42.  
  43. // Positive int64 values should be isomorphic to encoded strings
  44. // For any postive int64 value we should be able to encode it, decode it and have the same value
  45. func TestIsomorphism(t *testing.T) {
  46. abs := func(i int64) int64 {
  47. if i < 0 {
  48. return i * -1
  49. }
  50. return i
  51. }
  52.  
  53. absMarshalUnmarshal := func(i int64) int64 {
  54. // fmt.Println("DEBUG: ", i, abs(i))
  55. enc := marshalString(abs(i))
  56. dec, err := unmarshalString(enc)
  57. if err != nil {
  58. return -1
  59. }
  60. return dec
  61. }
  62.  
  63. err := quick.CheckEqual(abs, absMarshalUnmarshal, nil)
  64. if err != nil {
  65. t.Error(err)
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement