sortedstrings_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 stringutils2
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestSortedString(t *testing.T) {
  20. input := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
  21. // Alpha Bravo Delta Go Gopher Grin
  22. // 0 1 2 3 4 5
  23. ss := NewSortedStrings(input)
  24. cases := []struct {
  25. Needle string
  26. Index int
  27. Want bool
  28. }{
  29. {"Go", 3, true},
  30. {"Bravo", 1, true},
  31. {"Gopher", 4, true},
  32. {"Alpha", 0, true},
  33. {"Grin", 5, true},
  34. {"Delta", 2, true},
  35. {"Go1", 4, false},
  36. {"G", 3, false},
  37. {"A", 0, false},
  38. {"T", 6, false},
  39. }
  40. for _, c := range cases {
  41. idx, find := ss.Index(c.Needle)
  42. if idx != c.Index || find != c.Want {
  43. t.Errorf("%s: want: %d %v got: %d %v", c.Needle, c.Index, c.Want, idx, find)
  44. }
  45. }
  46. }
  47. func TestSplitStrings(t *testing.T) {
  48. input := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
  49. input2 := []string{"Go2", "Bravo", "Gopher", "Alpha1", "Grin", "Delt"}
  50. ss1 := NewSortedStrings(input)
  51. ss2 := NewSortedStrings(input2)
  52. a, b, c := Split(ss1, ss2)
  53. t.Logf("A: %s", ss1)
  54. t.Logf("B: %s", ss2)
  55. t.Logf("A-B: %s", a)
  56. t.Logf("AnB: %s", b)
  57. t.Logf("B-A: %s", c)
  58. }
  59. func TestMergeStrings(t *testing.T) {
  60. input := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
  61. input2 := []string{"Go2", "Bravo", "Gopher", "Alpha1", "Grin", "Delt"}
  62. ss1 := NewSortedStrings(input)
  63. ss2 := NewSortedStrings(input2)
  64. m := Merge(ss1, ss2)
  65. t.Logf("A: %s", ss1)
  66. t.Logf("B: %s", ss2)
  67. t.Logf("%s", m)
  68. }
  69. func TestSortedStringsAppend(t *testing.T) {
  70. cases := []struct {
  71. in []string
  72. ele []string
  73. want SSortedStrings
  74. }{
  75. {
  76. in: []string{"Alpha", "Bravo", "Go"},
  77. ele: []string{"Go2"},
  78. want: []string{"Alpha", "Bravo", "Go", "Go2"},
  79. },
  80. {
  81. in: []string{"Alpha", "Bravo", "Go2"},
  82. ele: []string{"Go"},
  83. want: []string{"Alpha", "Bravo", "Go", "Go2"},
  84. },
  85. {
  86. in: []string{"Alpha", "Bravo", "Go2"},
  87. ele: []string{"Aaaa", "Go"},
  88. want: []string{"Aaaa", "Alpha", "Bravo", "Go", "Go2"},
  89. },
  90. }
  91. for _, c := range cases {
  92. got := NewSortedStrings(c.in).Append(c.ele...)
  93. if !reflect.DeepEqual(c.want, got) {
  94. t.Errorf("want: %s got: %s", c.want, got)
  95. }
  96. }
  97. }
  98. func TestSortedStringsRemove(t *testing.T) {
  99. cases := []struct {
  100. in []string
  101. ele []string
  102. want SSortedStrings
  103. }{
  104. {
  105. in: []string{"Alpha", "Bravo", "Go"},
  106. ele: []string{"Go", "Go2"},
  107. want: []string{"Alpha", "Bravo"},
  108. },
  109. {
  110. in: []string{"Alpha", "Bravo", "Go2"},
  111. ele: []string{"Go"},
  112. want: []string{"Alpha", "Bravo", "Go2"},
  113. },
  114. {
  115. in: []string{"Alpha", "Bravo", "Go", "Go2"},
  116. ele: []string{"Aaaa", "Alpha"},
  117. want: []string{"Bravo", "Go", "Go2"},
  118. },
  119. }
  120. for _, c := range cases {
  121. got := NewSortedStrings(c.in).Remove(c.ele...)
  122. if !reflect.DeepEqual(c.want, got) {
  123. t.Errorf("want: %s got: %s", c.want, got)
  124. }
  125. }
  126. }