util_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2014 Couchbase, Inc.
  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 analysis
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestDeleteRune(t *testing.T) {
  20. tests := []struct {
  21. in []rune
  22. delPos int
  23. out []rune
  24. }{
  25. {
  26. in: []rune{'a', 'b', 'c'},
  27. delPos: 1,
  28. out: []rune{'a', 'c'},
  29. },
  30. }
  31. for _, test := range tests {
  32. actual := DeleteRune(test.in, test.delPos)
  33. if !reflect.DeepEqual(actual, test.out) {
  34. t.Errorf("expected %#v, got %#v", test.out, actual)
  35. }
  36. }
  37. }
  38. func TestInsertRune(t *testing.T) {
  39. tests := []struct {
  40. in []rune
  41. insPos int
  42. insRune rune
  43. out []rune
  44. }{
  45. {
  46. in: []rune{'a', 'b', 'c'},
  47. insPos: 1,
  48. insRune: 'x',
  49. out: []rune{'a', 'x', 'b', 'c'},
  50. },
  51. {
  52. in: []rune{'a', 'b', 'c'},
  53. insPos: 0,
  54. insRune: 'x',
  55. out: []rune{'x', 'a', 'b', 'c'},
  56. },
  57. {
  58. in: []rune{'a', 'b', 'c'},
  59. insPos: 3,
  60. insRune: 'x',
  61. out: []rune{'a', 'b', 'c', 'x'},
  62. },
  63. }
  64. for _, test := range tests {
  65. actual := InsertRune(test.in, test.insPos, test.insRune)
  66. if !reflect.DeepEqual(actual, test.out) {
  67. t.Errorf("expected %#v, got %#v", test.out, actual)
  68. }
  69. }
  70. }
  71. func TestBuildTermFromRunes(t *testing.T) {
  72. tests := []struct {
  73. in []rune
  74. }{
  75. {
  76. in: []rune{'a', 'b', 'c'},
  77. },
  78. {
  79. in: []rune{'こ', 'ん', 'に', 'ち', 'は', '世', '界'},
  80. },
  81. }
  82. for _, test := range tests {
  83. out := BuildTermFromRunes(test.in)
  84. back := []rune(string(out))
  85. if !reflect.DeepEqual(back, test.in) {
  86. t.Errorf("expected %v to convert back to %v", out, test.in)
  87. }
  88. }
  89. }
  90. func TestBuildTermFromRunesOptimistic(t *testing.T) {
  91. tests := []struct {
  92. buf []byte
  93. in []rune
  94. }{
  95. {
  96. buf: []byte("abc"),
  97. in: []rune{'a', 'b', 'c'},
  98. },
  99. {
  100. buf: []byte("こんにちは世界"),
  101. in: []rune{'こ', 'ん', 'に', 'ち', 'は', '世', '界'},
  102. },
  103. // same, but don't give enough buffer
  104. {
  105. buf: []byte("ab"),
  106. in: []rune{'a', 'b', 'c'},
  107. },
  108. {
  109. buf: []byte("こ"),
  110. in: []rune{'こ', 'ん', 'に', 'ち', 'は', '世', '界'},
  111. },
  112. }
  113. for _, test := range tests {
  114. out := BuildTermFromRunesOptimistic(test.buf, test.in)
  115. back := []rune(string(out))
  116. if !reflect.DeepEqual(back, test.in) {
  117. t.Errorf("expected %v to convert back to %v", out, test.in)
  118. }
  119. }
  120. }
  121. func BenchmarkBuildTermFromRunes(b *testing.B) {
  122. input := [][]rune{
  123. {'a', 'b', 'c'},
  124. {'こ', 'ん', 'に', 'ち', 'は', '世', '界'},
  125. }
  126. for i := 0; i < b.N; i++ {
  127. for _, i := range input {
  128. BuildTermFromRunes(i)
  129. }
  130. }
  131. }