excelutils_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 Yunion
  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 excelutils
  15. import "testing"
  16. func arrayEqual(a1, a2 []int) bool {
  17. if len(a1) != len(a2) {
  18. return false
  19. }
  20. for i := 0; i < len(a1); i += 1 {
  21. if a1[i] != a2[i] {
  22. return false
  23. }
  24. }
  25. return true
  26. }
  27. func TestDecimalBaseMaxWidth(t *testing.T) {
  28. cases := []struct {
  29. decIn int
  30. baseIn int
  31. want int
  32. }{
  33. {100, 10, 3},
  34. {16, 16, 2},
  35. {15, 16, 1},
  36. }
  37. for _, c := range cases {
  38. got := decimalBaseMaxWidth(c.decIn, c.baseIn)
  39. if got != c.want {
  40. t.Errorf("decimalBaseMaxWidth(%d, %d) = %d != %d", c.decIn, c.baseIn, got, c.want)
  41. }
  42. }
  43. cases2 := []struct {
  44. decIn int
  45. baseIn int
  46. width int
  47. want int
  48. want2 int
  49. }{
  50. {100, 10, 3, 1, 100},
  51. {16, 16, 2, 1, 16},
  52. {15, 16, 1, 15, 1},
  53. }
  54. for _, c := range cases2 {
  55. got1, got2 := decimalBaseN(c.decIn, c.baseIn, c.width)
  56. if got1 != c.want || got2 != c.want2 {
  57. t.Errorf("decimalBaseN(%d %d %d) = %d %d != %d %d", c.decIn, c.baseIn, c.width, got1, got2, c.want, c.want2)
  58. }
  59. }
  60. cases3 := []struct {
  61. decIn int
  62. baseIn int
  63. want []int
  64. }{
  65. {100, 10, []int{1, 0, 0}},
  66. {16, 16, []int{1, 0}},
  67. {0, 16, []int{0}},
  68. {15, 16, []int{15}},
  69. {0, 26, []int{0}},
  70. {1, 26, []int{1}},
  71. {25, 26, []int{25}},
  72. {26, 26, []int{1, 0}},
  73. {27, 26, []int{1, 1}},
  74. {676, 26, []int{1, 0, 0}},
  75. }
  76. for _, c := range cases3 {
  77. got := decimal2Base(c.decIn, c.baseIn)
  78. if !arrayEqual(got, c.want) {
  79. t.Errorf("decimal2Base(%d %d) = %#v != %#v", c.decIn, c.baseIn, got, c.want)
  80. }
  81. }
  82. cases4 := []struct {
  83. decIn int
  84. want string
  85. }{
  86. {0, "A"},
  87. {1, "B"},
  88. {25, "Z"},
  89. {26, "AA"},
  90. {27, "AB"},
  91. {676, "AAA"},
  92. }
  93. for _, c := range cases4 {
  94. got := decimal2Alphabet(c.decIn)
  95. if got != c.want {
  96. t.Errorf("decimal2Alphabet(%d) = %s != %s", c.decIn, got, c.want)
  97. }
  98. }
  99. }