github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/runes/runes_test.go (about) 1 // Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package runes_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/re" 7 "github.com/TeaOSLab/EdgeNode/internal/utils/runes" 8 "github.com/iwind/TeaGo/assert" 9 "regexp" 10 "runtime" 11 "sort" 12 "strings" 13 "testing" 14 ) 15 16 func TestContainsAllWords(t *testing.T) { 17 var a = assert.NewAssertion(t) 18 a.IsTrue(runes.ContainsAllWords("How are you?", []string{"are", "you"}, false)) 19 a.IsFalse(runes.ContainsAllWords("How are you?", []string{"how", "are", "you"}, false)) 20 a.IsTrue(runes.ContainsAllWords("How are you?", []string{"how", "are", "you"}, true)) 21 } 22 23 func TestContainsAnyWord(t *testing.T) { 24 var a = assert.NewAssertion(t) 25 a.IsTrue(runes.ContainsAnyWord("How are you?", []string{"are", "you"}, false)) 26 a.IsTrue(runes.ContainsAnyWord("How are you?", []string{"are", "you", "ok"}, false)) 27 a.IsFalse(runes.ContainsAnyWord("How are you?", []string{"how", "ok"}, false)) 28 a.IsTrue(runes.ContainsAnyWord("How are you?", []string{"how"}, true)) 29 a.IsTrue(runes.ContainsAnyWord("How are you?", []string{"how", "ok"}, true)) 30 a.IsTrue(runes.ContainsAnyWord("How-are you?", []string{"how", "ok"}, true)) 31 } 32 33 func TestContainsAnyWord_Sort(t *testing.T) { 34 var a = assert.NewAssertion(t) 35 a.IsTrue(runes.ContainsAnyWord("How are you?", []string{"abc", "ant", "arm", "Hit", "Hi", "Pet", "pie", "are"}, false)) 36 } 37 38 func TestContainsWordRunes(t *testing.T) { 39 var a = assert.NewAssertion(t) 40 a.IsFalse(runes.ContainsWordRunes([]rune(""), []rune("How"), true)) 41 a.IsFalse(runes.ContainsWordRunes([]rune("How are you?"), []rune(""), true)) 42 a.IsTrue(runes.ContainsWordRunes([]rune("How are you?"), []rune("How"), true)) 43 a.IsFalse(runes.ContainsWordRunes([]rune("How are you?"), []rune("how"), false)) 44 a.IsTrue(runes.ContainsWordRunes([]rune("How are you?"), []rune("you"), false)) 45 a.IsTrue(runes.ContainsWordRunes([]rune("How are you?"), []rune("are"), false)) 46 a.IsFalse(runes.ContainsWordRunes([]rune("How are you?"), []rune("re"), false)) 47 a.IsTrue(runes.ContainsWordRunes([]rune("How are you w?"), []rune("w"), false)) 48 a.IsTrue(runes.ContainsWordRunes([]rune("w How are you?"), []rune("w"), false)) 49 a.IsTrue(runes.ContainsWordRunes([]rune("How are w you?"), []rune("w"), false)) 50 a.IsTrue(runes.ContainsWordRunes([]rune("How are how you?"), []rune("how"), false)) 51 a.IsTrue(runes.ContainsWordRunes([]rune("How are you?"), []rune("how"), true)) 52 a.IsTrue(runes.ContainsWordRunes([]rune("How are you?"), []rune("ARE"), true)) 53 a.IsTrue(runes.ContainsWordRunes([]rune("How are you"), []rune("you"), false)) 54 a.IsTrue(runes.ContainsWordRunes([]rune("How are you"), []rune("YOU"), true)) 55 a.IsTrue(runes.ContainsWordRunes([]rune("How are you?"), []rune("YOU"), true)) 56 a.IsFalse(runes.ContainsWordRunes([]rune("How are you1?"), []rune("YOU"), true)) 57 a.IsFalse(runes.ContainsWordRunes([]rune("How are you1?"), []rune("YOU YOU YOU YOU YOU YOU YOU"), true)) 58 } 59 60 func TestContainsSubRunes(t *testing.T) { 61 var a = assert.NewAssertion(t) 62 a.IsFalse(runes.ContainsSubRunes([]rune(""), []rune("How"), true)) 63 a.IsFalse(runes.ContainsSubRunes([]rune("How are you?"), []rune(""), true)) 64 a.IsTrue(runes.ContainsSubRunes([]rune("How are you1?"), []rune("YOU"), true)) 65 a.IsTrue(runes.ContainsSubRunes([]rune("How are you1?"), []rune("ow"), false)) 66 a.IsTrue(runes.ContainsSubRunes([]rune("How are you1?"), []rune("H"), false)) 67 a.IsTrue(runes.ContainsSubRunes([]rune("How are you1?"), []rune("How"), false)) 68 a.IsTrue(runes.ContainsSubRunes([]rune("How are you doing"), []rune("oi"), false)) 69 a.IsTrue(runes.ContainsSubRunes([]rune("How are you doing"), []rune("g"), false)) 70 a.IsTrue(runes.ContainsSubRunes([]rune("How are you doing"), []rune("ing"), false)) 71 a.IsFalse(runes.ContainsSubRunes([]rune("How are you doing"), []rune("int"), false)) 72 } 73 74 func TestEqualRune(t *testing.T) { 75 var a = assert.NewAssertion(t) 76 a.IsTrue(runes.EqualRune('a', 'a', false)) 77 a.IsTrue(runes.EqualRune('a', 'a', true)) 78 a.IsFalse(runes.EqualRune('a', 'A', false)) 79 a.IsTrue(runes.EqualRune('a', 'A', true)) 80 a.IsFalse(runes.EqualRune('c', 'C', false)) 81 a.IsTrue(runes.EqualRune('c', 'C', true)) 82 a.IsTrue(runes.EqualRune('C', 'C', true)) 83 a.IsTrue(runes.EqualRune('C', 'c', true)) 84 a.IsTrue(runes.EqualRune('Z', 'z', true)) 85 a.IsTrue(runes.EqualRune('z', 'Z', true)) 86 a.IsFalse(runes.EqualRune('z', 'z'+('a'-'A'), true)) 87 } 88 89 func BenchmarkContainsWordRunes(b *testing.B) { 90 runtime.GOMAXPROCS(4) 91 92 b.RunParallel(func(pb *testing.PB) { 93 for pb.Next() { 94 _, _ = runes.ContainsWordRunes([]rune("How are you"), []rune("YOU"), true) 95 } 96 }) 97 } 98 99 func BenchmarkContainsAnyWord(b *testing.B) { 100 runtime.GOMAXPROCS(4) 101 102 var words = strings.Split("python\npycurl\nhttp-client\nhttpclient\napachebench\nnethttp\nhttp_request\njava\nperl\nruby\nscrapy\nphp\nrust", "\n") 103 sort.Strings(words) 104 105 var wordRunes = [][]rune{} 106 for _, word := range words { 107 wordRunes = append(wordRunes, []rune(word)) 108 } 109 110 b.ResetTimer() 111 112 b.RunParallel(func(pb *testing.PB) { 113 for pb.Next() { 114 _ = runes.ContainsAnyWord("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_0_0) AppleWebKit/500.00 (KHTML, like Gecko) Chrome/100.0.0.0", words, true) 115 } 116 }) 117 } 118 119 func BenchmarkContainsAnyWordRunes(b *testing.B) { 120 runtime.GOMAXPROCS(4) 121 122 var words = strings.Split("python\npycurl\nhttp-client\nhttpclient\napachebench\nnethttp\nhttp_request\njava\nperl\nruby\nscrapy\nphp\nrust", "\n") 123 sort.Strings(words) 124 125 var wordRunes = [][]rune{} 126 for _, word := range words { 127 wordRunes = append(wordRunes, []rune(word)) 128 } 129 130 b.ResetTimer() 131 132 b.RunParallel(func(pb *testing.PB) { 133 for pb.Next() { 134 _ = runes.ContainsAnyWordRunes("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_0_0) AppleWebKit/500.00 (KHTML, like Gecko) Chrome/100.0.0.0", wordRunes, true) 135 } 136 }) 137 } 138 139 140 func BenchmarkContainsAnyWord_Regexp(b *testing.B) { 141 runtime.GOMAXPROCS(4) 142 var reg = regexp.MustCompile("(?i)" + strings.ReplaceAll("python\npycurl\nhttp-client\nhttpclient\napachebench\nnethttp\nhttp_request\njava\nperl\nruby\nscrapy\nphp\nrust", "\n", "|")) 143 b.ResetTimer() 144 145 b.RunParallel(func(pb *testing.PB) { 146 for pb.Next() { 147 _ = reg.MatchString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_0_0) AppleWebKit/500.00 (KHTML, like Gecko) Chrome/100.0.0.0") 148 } 149 }) 150 } 151 152 func BenchmarkContainsAnyWord_Re(b *testing.B) { 153 runtime.GOMAXPROCS(4) 154 var reg = re.MustCompile("(?i)" + strings.ReplaceAll("python\npycurl\nhttp-client\nhttpclient\napachebench\nnethttp\nhttp_request\njava\nperl\nruby\nscrapy\nphp\nrust", "\n", "|")) 155 b.ResetTimer() 156 157 b.RunParallel(func(pb *testing.PB) { 158 for pb.Next() { 159 _ = reg.MatchString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_0_0) AppleWebKit/500.00 (KHTML, like Gecko) Chrome/100.0.0.0") 160 } 161 }) 162 } 163 164 func BenchmarkContainsSubRunes(b *testing.B) { 165 runtime.GOMAXPROCS(4) 166 167 b.RunParallel(func(pb *testing.PB) { 168 for pb.Next() { 169 _ = runes.ContainsSubRunes([]rune("How are you"), []rune("YOU"), true) 170 } 171 }) 172 }