levenshtein_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2015, Arbo von Monkiewitsch All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license.
  4. package levenshtein
  5. import (
  6. "fmt"
  7. "testing"
  8. )
  9. var distanceTests = []struct {
  10. first string
  11. second string
  12. wanted int
  13. }{
  14. {"a", "a", 0},
  15. {"ab", "ab", 0},
  16. {"ab", "aa", 1},
  17. {"ab", "aa", 1},
  18. {"ab", "aaa", 2},
  19. {"bbb", "a", 3},
  20. {"kitten", "sitting", 3},
  21. {"a", "", 1},
  22. {"", "a", 1},
  23. {"aa", "aü", 1},
  24. {"Fön", "Föm", 1},
  25. }
  26. func TestDistance(t *testing.T) {
  27. lev := &Context{}
  28. for index, distanceTest := range distanceTests {
  29. result := lev.Distance(distanceTest.first, distanceTest.second)
  30. if result != distanceTest.wanted {
  31. output := fmt.Sprintf("%v \t distance of %v and %v should be %v but was %v.",
  32. index, distanceTest.first, distanceTest.second, distanceTest.wanted, result)
  33. t.Errorf(output)
  34. }
  35. }
  36. }
  37. func BenchmarkDistance(b *testing.B) {
  38. s1 := "frederick"
  39. s2 := "fredelstick"
  40. total := 0
  41. b.ReportAllocs()
  42. b.ResetTimer()
  43. c := &Context{}
  44. for i := 0; i < b.N; i++ {
  45. total += c.Distance(s1, s2)
  46. }
  47. if total == 0 {
  48. b.Logf("total is %d", total)
  49. }
  50. }
  51. func BenchmarkDistanceOriginal(b *testing.B) {
  52. s1 := "frederick"
  53. s2 := "fredelstick"
  54. total := 0
  55. b.ReportAllocs()
  56. b.ResetTimer()
  57. ctx := Context{}
  58. for i := 0; i < b.N; i++ {
  59. total += ctx.Distance(s1, s2)
  60. }
  61. if total == 0 {
  62. b.Logf("total is %d", total)
  63. }
  64. }