wildcard_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package wildcard
  20. import (
  21. "fmt"
  22. "testing"
  23. )
  24. const target = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
  25. func TestMatch(t *testing.T) {
  26. testCases := []struct {
  27. pattern string
  28. target string
  29. match bool
  30. }{
  31. {"*", target, true},
  32. {target, target, true},
  33. {"Lorem*", target, true},
  34. {"*aliqua.", target, true},
  35. {"*tempor*", target, true},
  36. {"*dolor*eiusmod*magna*", target, true},
  37. {"Lorem*dolor*eiusmod*magna*", target, true},
  38. {"*ipsum*elit*aliqua.", target, true},
  39. {"Lorem*dolor*eiusmod*dolore*aliqua.", target, true},
  40. {"", target, false},
  41. {"L-rem*", target, false},
  42. {"L-rem**", target, false},
  43. {"*aliqua-", target, false},
  44. {"*temp-r*", target, false},
  45. {"*dolor*ei-smod*magna*", target, false},
  46. {"Lorem*dolor*eiu-mod*magna*", target, false},
  47. {"*ipsum*eli-*aliqua.", target, false},
  48. {"Lorem*dolor*eiusm-d*dolore*aliqua.", target, false},
  49. {"Lorem**", target, true},
  50. {"**aliqua.", target, true},
  51. {"**tempor**", target, true},
  52. }
  53. for _, testCase := range testCases {
  54. t.Run(fmt.Sprintf("match: %+v", testCase), func(t *testing.T) {
  55. if Match(testCase.pattern, testCase.target) != testCase.match {
  56. t.Errorf("unexpected result")
  57. }
  58. })
  59. }
  60. }
  61. func BenchmarkFixedMatch(b *testing.B) {
  62. for i := 0; i < b.N; i++ {
  63. if !Match(target, target) {
  64. b.Fatalf("unexpected result")
  65. }
  66. }
  67. }
  68. func BenchmarkPrefixMatch(b *testing.B) {
  69. for i := 0; i < b.N; i++ {
  70. if !Match("Lorem*", target) {
  71. b.Fatalf("unexpected result")
  72. }
  73. }
  74. }
  75. func BenchmarkSuffixMatch(b *testing.B) {
  76. for i := 0; i < b.N; i++ {
  77. if !Match("*aliqua.", target) {
  78. b.Fatalf("unexpected result")
  79. }
  80. }
  81. }
  82. func BenchmarkMultipleMatch(b *testing.B) {
  83. for i := 0; i < b.N; i++ {
  84. if !Match("*dolor*eiusmod*magna*", target) {
  85. b.Fatalf("unexpected result")
  86. }
  87. }
  88. }