convert_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2014 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com
  15. import (
  16. "testing"
  17. . "github.com/smartystreets/goconvey/convey"
  18. )
  19. func TestHexStr2int(t *testing.T) {
  20. Convey("Convert hex format string to decimal", t, func() {
  21. hexDecs := map[string]int{
  22. "1": 1,
  23. "002": 2,
  24. "011": 17,
  25. "0a1": 161,
  26. "35e": 862,
  27. }
  28. for hex, dec := range hexDecs {
  29. val, err := HexStr2int(hex)
  30. So(err, ShouldBeNil)
  31. So(val, ShouldEqual, dec)
  32. }
  33. })
  34. }
  35. func TestInt2HexStr(t *testing.T) {
  36. Convey("Convert decimal to hex format string", t, func() {
  37. decHexs := map[int]string{
  38. 1: "1",
  39. 2: "2",
  40. 17: "11",
  41. 161: "a1",
  42. 862: "35e",
  43. }
  44. for dec, hex := range decHexs {
  45. val := Int2HexStr(dec)
  46. So(val, ShouldEqual, hex)
  47. }
  48. })
  49. }