hex_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package strutil
  14. import (
  15. "testing"
  16. )
  17. func TestIDAsHex(t *testing.T) {
  18. tests := []struct {
  19. input uint64
  20. want string
  21. }{
  22. {
  23. input: uint64(12),
  24. want: "c",
  25. },
  26. {
  27. input: uint64(4918257920282737594),
  28. want: "444129853c343bba",
  29. },
  30. }
  31. for i, tt := range tests {
  32. got := IDAsHex(tt.input)
  33. if tt.want != got {
  34. t.Errorf("#%d: IDAsHex failure: want=%v, got=%v", i, tt.want, got)
  35. }
  36. }
  37. }
  38. func TestIDFromHex(t *testing.T) {
  39. tests := []struct {
  40. input string
  41. want uint64
  42. }{
  43. {
  44. input: "17",
  45. want: uint64(23),
  46. },
  47. {
  48. input: "612840dae127353",
  49. want: uint64(437557308098245459),
  50. },
  51. }
  52. for i, tt := range tests {
  53. got, err := IDFromHex(tt.input)
  54. if err != nil {
  55. t.Errorf("#%d: IDFromHex failure: err=%v", i, err)
  56. continue
  57. }
  58. if tt.want != got {
  59. t.Errorf("#%d: IDFromHex failure: want=%v, got=%v", i, tt.want, got)
  60. }
  61. }
  62. }
  63. func TestIDFromHexFail(t *testing.T) {
  64. tests := []string{
  65. "",
  66. "XXX",
  67. "612840dae127353612840dae127353",
  68. }
  69. for i, tt := range tests {
  70. _, err := IDFromHex(tt)
  71. if err == nil {
  72. t.Fatalf("#%d: IDFromHex expected error, but err=nil", i)
  73. }
  74. }
  75. }