conv_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  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. //
  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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "testing"
  17. )
  18. func TestGetString(t *testing.T) {
  19. var t1 = "test1"
  20. if "test1" != GetString(t1) {
  21. t.Error("get string from string error")
  22. }
  23. var t2 = []byte("test2")
  24. if "test2" != GetString(t2) {
  25. t.Error("get string from byte array error")
  26. }
  27. var t3 = 1
  28. if "1" != GetString(t3) {
  29. t.Error("get string from int error")
  30. }
  31. var t4 int64 = 1
  32. if "1" != GetString(t4) {
  33. t.Error("get string from int64 error")
  34. }
  35. var t5 = 1.1
  36. if "1.1" != GetString(t5) {
  37. t.Error("get string from float64 error")
  38. }
  39. if "" != GetString(nil) {
  40. t.Error("get string from nil error")
  41. }
  42. }
  43. func TestGetInt(t *testing.T) {
  44. var t1 = 1
  45. if 1 != GetInt(t1) {
  46. t.Error("get int from int error")
  47. }
  48. var t2 int32 = 32
  49. if 32 != GetInt(t2) {
  50. t.Error("get int from int32 error")
  51. }
  52. var t3 int64 = 64
  53. if 64 != GetInt(t3) {
  54. t.Error("get int from int64 error")
  55. }
  56. var t4 = "128"
  57. if 128 != GetInt(t4) {
  58. t.Error("get int from num string error")
  59. }
  60. if 0 != GetInt(nil) {
  61. t.Error("get int from nil error")
  62. }
  63. }
  64. func TestGetInt64(t *testing.T) {
  65. var i int64 = 1
  66. var t1 = 1
  67. if i != GetInt64(t1) {
  68. t.Error("get int64 from int error")
  69. }
  70. var t2 int32 = 1
  71. if i != GetInt64(t2) {
  72. t.Error("get int64 from int32 error")
  73. }
  74. var t3 int64 = 1
  75. if i != GetInt64(t3) {
  76. t.Error("get int64 from int64 error")
  77. }
  78. var t4 = "1"
  79. if i != GetInt64(t4) {
  80. t.Error("get int64 from num string error")
  81. }
  82. if 0 != GetInt64(nil) {
  83. t.Error("get int64 from nil")
  84. }
  85. }
  86. func TestGetFloat64(t *testing.T) {
  87. var f = 1.11
  88. var t1 float32 = 1.11
  89. if f != GetFloat64(t1) {
  90. t.Error("get float64 from float32 error")
  91. }
  92. var t2 = 1.11
  93. if f != GetFloat64(t2) {
  94. t.Error("get float64 from float64 error")
  95. }
  96. var t3 = "1.11"
  97. if f != GetFloat64(t3) {
  98. t.Error("get float64 from string error")
  99. }
  100. var f2 float64 = 1
  101. var t4 = 1
  102. if f2 != GetFloat64(t4) {
  103. t.Error("get float64 from int error")
  104. }
  105. if 0 != GetFloat64(nil) {
  106. t.Error("get float64 from nil error")
  107. }
  108. }
  109. func TestGetBool(t *testing.T) {
  110. var t1 = true
  111. if true != GetBool(t1) {
  112. t.Error("get bool from bool error")
  113. }
  114. var t2 = "true"
  115. if true != GetBool(t2) {
  116. t.Error("get bool from string error")
  117. }
  118. if false != GetBool(nil) {
  119. t.Error("get bool from nil error")
  120. }
  121. }
  122. func byteArrayEquals(a []byte, b []byte) bool {
  123. if len(a) != len(b) {
  124. return false
  125. }
  126. for i, v := range a {
  127. if v != b[i] {
  128. return false
  129. }
  130. }
  131. return true
  132. }