util_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package parse // import "github.com/tdewolff/parse"
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "regexp"
  6. "testing"
  7. "github.com/tdewolff/test"
  8. )
  9. func helperRand(n, m int, chars []byte) [][]byte {
  10. r := make([][]byte, n)
  11. for i := range r {
  12. for j := 0; j < m; j++ {
  13. r[i] = append(r[i], chars[rand.Intn(len(chars))])
  14. }
  15. }
  16. return r
  17. }
  18. ////////////////////////////////////////////////////////////////
  19. var wsSlices [][]byte
  20. func init() {
  21. wsSlices = helperRand(100, 20, []byte("abcdefg \n\r\f\t"))
  22. }
  23. func TestCopy(t *testing.T) {
  24. foo := []byte("abc")
  25. bar := Copy(foo)
  26. foo[0] = 'b'
  27. test.String(t, string(foo), "bbc")
  28. test.String(t, string(bar), "abc")
  29. }
  30. func TestToLower(t *testing.T) {
  31. foo := []byte("Abc")
  32. bar := ToLower(foo)
  33. bar[1] = 'B'
  34. test.String(t, string(foo), "aBc")
  35. test.String(t, string(bar), "aBc")
  36. }
  37. func TestEqualFold(t *testing.T) {
  38. test.That(t, EqualFold([]byte("Abc"), []byte("abc")))
  39. test.That(t, !EqualFold([]byte("Abcd"), []byte("abc")))
  40. test.That(t, !EqualFold([]byte("Bbc"), []byte("abc")))
  41. }
  42. func TestWhitespace(t *testing.T) {
  43. test.That(t, IsAllWhitespace([]byte("\t \r\n\f")))
  44. test.That(t, !IsAllWhitespace([]byte("\t \r\n\fx")))
  45. }
  46. func TestReplaceMultipleWhitespace(t *testing.T) {
  47. wsRegexp := regexp.MustCompile("[ \t\f]+")
  48. wsNewlinesRegexp := regexp.MustCompile("[ ]*[\r\n][ \r\n]*")
  49. for _, e := range wsSlices {
  50. reference := wsRegexp.ReplaceAll(e, []byte(" "))
  51. reference = wsNewlinesRegexp.ReplaceAll(reference, []byte("\n"))
  52. test.Bytes(t, ReplaceMultipleWhitespace(e), reference, "must remove all multiple whitespace but keep newlines")
  53. }
  54. }
  55. func TestTrim(t *testing.T) {
  56. test.Bytes(t, TrimWhitespace([]byte("a")), []byte("a"))
  57. test.Bytes(t, TrimWhitespace([]byte(" a")), []byte("a"))
  58. test.Bytes(t, TrimWhitespace([]byte("a ")), []byte("a"))
  59. test.Bytes(t, TrimWhitespace([]byte(" ")), []byte(""))
  60. }
  61. ////////////////////////////////////////////////////////////////
  62. func BenchmarkBytesTrim(b *testing.B) {
  63. for i := 0; i < b.N; i++ {
  64. for _, e := range wsSlices {
  65. bytes.TrimSpace(e)
  66. }
  67. }
  68. }
  69. func BenchmarkTrim(b *testing.B) {
  70. for i := 0; i < b.N; i++ {
  71. for _, e := range wsSlices {
  72. TrimWhitespace(e)
  73. }
  74. }
  75. }
  76. func BenchmarkReplace(b *testing.B) {
  77. for i := 0; i < b.N; i++ {
  78. for _, e := range wsSlices {
  79. ReplaceMultipleWhitespace(e)
  80. }
  81. }
  82. }
  83. func BenchmarkWhitespaceTable(b *testing.B) {
  84. n := 0
  85. for i := 0; i < b.N; i++ {
  86. for _, e := range wsSlices {
  87. for _, c := range e {
  88. if IsWhitespace(c) {
  89. n++
  90. }
  91. }
  92. }
  93. }
  94. }
  95. func BenchmarkWhitespaceIf1(b *testing.B) {
  96. n := 0
  97. for i := 0; i < b.N; i++ {
  98. for _, e := range wsSlices {
  99. for _, c := range e {
  100. if c == ' ' {
  101. n++
  102. }
  103. }
  104. }
  105. }
  106. }
  107. func BenchmarkWhitespaceIf2(b *testing.B) {
  108. n := 0
  109. for i := 0; i < b.N; i++ {
  110. for _, e := range wsSlices {
  111. for _, c := range e {
  112. if c == ' ' || c == '\n' {
  113. n++
  114. }
  115. }
  116. }
  117. }
  118. }
  119. func BenchmarkWhitespaceIf3(b *testing.B) {
  120. n := 0
  121. for i := 0; i < b.N; i++ {
  122. for _, e := range wsSlices {
  123. for _, c := range e {
  124. if c == ' ' || c == '\n' || c == '\r' {
  125. n++
  126. }
  127. }
  128. }
  129. }
  130. }
  131. func BenchmarkWhitespaceIf4(b *testing.B) {
  132. n := 0
  133. for i := 0; i < b.N; i++ {
  134. for _, e := range wsSlices {
  135. for _, c := range e {
  136. if c == ' ' || c == '\n' || c == '\r' || c == '\t' {
  137. n++
  138. }
  139. }
  140. }
  141. }
  142. }
  143. func BenchmarkWhitespaceIf5(b *testing.B) {
  144. n := 0
  145. for i := 0; i < b.N; i++ {
  146. for _, e := range wsSlices {
  147. for _, c := range e {
  148. if c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' {
  149. n++
  150. }
  151. }
  152. }
  153. }
  154. }