Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(704)

Delta Between Two Patch Sets: src/pkg/strings/replace_test.go

Issue 6492076: code review 6492076: strings: implement a faster generic Replacer (Closed)
Left Patch Set: diff -r cdee8bf43694 https://code.google.com/p/go Created 11 years, 6 months ago
Right Patch Set: diff -r cdee8bf43694 https://code.google.com/p/go Created 11 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« src/pkg/strings/replace.go ('K') | « src/pkg/strings/replace.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package strings_test 5 package strings_test
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 . "strings" 10 . "strings"
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 testCases = append(testCases, 212 testCases = append(testCases,
213 testCase{genAll, allString, "[all]"}, 213 testCase{genAll, allString, "[all]"},
214 testCase{genAll, "a\xff" + allString + "\x00", "a[ff][all][00]"} , 214 testCase{genAll, "a\xff" + allString + "\x00", "a[ff][all][00]"} ,
215 testCase{genAll, "", ""}, 215 testCase{genAll, "", ""},
216 ) 216 )
217 217
218 // Test cases with empty old strings. 218 // Test cases with empty old strings.
219 219
220 blankToX1 := NewReplacer("", "X") 220 blankToX1 := NewReplacer("", "X")
221 blankToX2 := NewReplacer("", "X", "", "") 221 blankToX2 := NewReplacer("", "X", "", "")
222 » blankToXOToO := NewReplacer("", "X", "o", "O") 222 » blankHighPriority := NewReplacer("", "X", "o", "O")
223 » blankLowPriority := NewReplacer("o", "O", "", "X")
223 blankNoOp1 := NewReplacer("", "") 224 blankNoOp1 := NewReplacer("", "")
224 blankNoOp2 := NewReplacer("", "", "", "A") 225 blankNoOp2 := NewReplacer("", "", "", "A")
225 blankFoo := NewReplacer("", "X", "foobar", "R", "foobaz", "Z") 226 blankFoo := NewReplacer("", "X", "foobar", "R", "foobaz", "Z")
226 blankLowPriority := NewReplacer("a", "A", "", "X")
227 testCases = append(testCases, 227 testCases = append(testCases,
228 testCase{blankToX1, "foo", "XfXoXoX"}, 228 testCase{blankToX1, "foo", "XfXoXoX"},
229 testCase{blankToX1, "", "X"}, 229 testCase{blankToX1, "", "X"},
230 230
231 testCase{blankToX2, "foo", "XfXoXoX"}, 231 testCase{blankToX2, "foo", "XfXoXoX"},
232 testCase{blankToX2, "", "X"}, 232 testCase{blankToX2, "", "X"},
233 233
234 » » testCase{blankToXOToO, "oo", "XOXOX"}, 234 » » testCase{blankHighPriority, "oo", "XOXOX"},
235 » » testCase{blankToXOToO, "ii", "XiXiX"}, 235 » » testCase{blankHighPriority, "ii", "XiXiX"},
236 » » testCase{blankToXOToO, "iooi", "XiXOXOXiX"}, 236 » » testCase{blankHighPriority, "iooi", "XiXOXOXiX"},
nigeltao 2012/09/17 01:49:24 I'd also add an "oiio" test.
237 » » testCase{blankToXOToO, "", "X"}, 237 » » testCase{blankHighPriority, "", "X"},
238
239 » » testCase{blankLowPriority, "oo", "OOX"},
240 » » testCase{blankLowPriority, "ii", "XiXiX"},
241 » » testCase{blankLowPriority, "iooi", "XiOOXiX"},
242 » » testCase{blankLowPriority, "", "X"},
238 243
239 testCase{blankNoOp1, "foo", "foo"}, 244 testCase{blankNoOp1, "foo", "foo"},
240 testCase{blankNoOp1, "", ""}, 245 testCase{blankNoOp1, "", ""},
241 246
242 testCase{blankNoOp2, "foo", "foo"}, 247 testCase{blankNoOp2, "foo", "foo"},
243 testCase{blankNoOp2, "", ""}, 248 testCase{blankNoOp2, "", ""},
244 249
245 testCase{blankFoo, "foobarfoobaz", "XRXZX"}, 250 testCase{blankFoo, "foobarfoobaz", "XRXZX"},
246 testCase{blankFoo, "foobar-foobaz", "XRX-XZX"}, 251 testCase{blankFoo, "foobar-foobaz", "XRX-XZX"},
247 testCase{blankFoo, "", "X"}, 252 testCase{blankFoo, "", "X"},
248
249 testCase{blankLowPriority, "abba", "AXbXbAX"},
250 testCase{blankLowPriority, "", "X"},
251 ) 253 )
252 254
253 // No-arg test cases. 255 // No-arg test cases.
254 256
255 nop := NewReplacer() 257 nop := NewReplacer()
256 testCases = append(testCases, 258 testCases = append(testCases,
257 testCase{nop, "abc", "abc"}, 259 testCase{nop, "abc", "abc"},
258 testCase{nop, "", ""}, 260 testCase{nop, "", ""},
259 ) 261 )
260 262
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 } 354 }
353 355
354 for _, tc := range testCases { 356 for _, tc := range testCases {
355 keys := Split(tc.in, ";") 357 keys := Split(tc.in, ";")
356 args := make([]string, len(keys)*2) 358 args := make([]string, len(keys)*2)
357 for i, key := range keys { 359 for i, key := range keys {
358 args[i*2] = key 360 args[i*2] = key
359 } 361 }
360 362
361 got := NewReplacer(args...).PrintTrie() 363 got := NewReplacer(args...).PrintTrie()
362 » » want := Replace(tc.out, "\t", "", -1) 364 » » // Remove tabs from tc.out
365 » » wantbuf := make([]byte, 0, len(tc.out))
366 » » for i := 0; i < len(tc.out); i++ {
367 » » » if tc.out[i] != '\t' {
368 » » » » wantbuf = append(wantbuf, tc.out[i])
369 » » » }
370 » » }
371 » » want := string(wantbuf)
363 372
364 if got != want { 373 if got != want {
365 t.Errorf("PrintTrie(%q)\ngot\n%swant\n%s", tc.in, got, w ant) 374 t.Errorf("PrintTrie(%q)\ngot\n%swant\n%s", tc.in, got, w ant)
366 } 375 }
367 } 376 }
368 } 377 }
369 378
370 func BenchmarkGenericNoMatch(b *testing.B) { 379 func BenchmarkGenericNoMatch(b *testing.B) {
371 str := Repeat("A", 100) + Repeat("B", 100) 380 str := Repeat("A", 100) + Repeat("B", 100)
372 generic := NewReplacer("a", "A", "b", "B", "12", "123") // varying lengt hs forces generic 381 generic := NewReplacer("a", "A", "b", "B", "12", "123") // varying lengt hs forces generic
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 return 'A' 451 return 'A'
443 case 'b': 452 case 'b':
444 return 'B' 453 return 'B'
445 } 454 }
446 return r 455 return r
447 } 456 }
448 for i := 0; i < b.N; i++ { 457 for i := 0; i < b.N; i++ {
449 Map(fn, str) 458 Map(fn, str)
450 } 459 }
451 } 460 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b