1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
// https://github.com/sergi/go-diff
// See the included LICENSE file for license details.
//
// go-diff is a Go implementation of Google's Diff, Match, and Patch library
// Original library is Copyright (c) 2006 Google Inc.
// http://code.google.com/p/google-diff-match-patch/
package diffmatchpatch
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRunesIndexOf(t *testing.T) {
type TestCase struct {
Pattern string
Start int
Expected int
}
for i, tc := range []TestCase{
{"abc", 0, 0},
{"cde", 0, 2},
{"e", 0, 4},
{"cdef", 0, -1},
{"abcdef", 0, -1},
{"abc", 2, -1},
{"cde", 2, 2},
{"e", 2, 4},
{"cdef", 2, -1},
{"abcdef", 2, -1},
{"e", 6, -1},
} {
actual := runesIndexOf([]rune("abcde"), []rune(tc.Pattern), tc.Start)
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}
func TestIndexOf(t *testing.T) {
type TestCase struct {
String string
Pattern string
Position int
Expected int
}
for i, tc := range []TestCase{
{"hi world", "world", -1, 3},
{"hi world", "world", 0, 3},
{"hi world", "world", 1, 3},
{"hi world", "world", 2, 3},
{"hi world", "world", 3, 3},
{"hi world", "world", 4, -1},
{"abbc", "b", -1, 1},
{"abbc", "b", 0, 1},
{"abbc", "b", 1, 1},
{"abbc", "b", 2, 2},
{"abbc", "b", 3, -1},
{"abbc", "b", 4, -1},
// The greek letter beta is the two-byte sequence of "\u03b2".
{"a\u03b2\u03b2c", "\u03b2", -1, 1},
{"a\u03b2\u03b2c", "\u03b2", 0, 1},
{"a\u03b2\u03b2c", "\u03b2", 1, 1},
{"a\u03b2\u03b2c", "\u03b2", 3, 3},
{"a\u03b2\u03b2c", "\u03b2", 5, -1},
{"a\u03b2\u03b2c", "\u03b2", 6, -1},
} {
actual := indexOf(tc.String, tc.Pattern, tc.Position)
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}
func TestLastIndexOf(t *testing.T) {
type TestCase struct {
String string
Pattern string
Position int
Expected int
}
for i, tc := range []TestCase{
{"hi world", "world", -1, -1},
{"hi world", "world", 0, -1},
{"hi world", "world", 1, -1},
{"hi world", "world", 2, -1},
{"hi world", "world", 3, -1},
{"hi world", "world", 4, -1},
{"hi world", "world", 5, -1},
{"hi world", "world", 6, -1},
{"hi world", "world", 7, 3},
{"hi world", "world", 8, 3},
{"abbc", "b", -1, -1},
{"abbc", "b", 0, -1},
{"abbc", "b", 1, 1},
{"abbc", "b", 2, 2},
{"abbc", "b", 3, 2},
{"abbc", "b", 4, 2},
// The greek letter beta is the two-byte sequence of "\u03b2".
{"a\u03b2\u03b2c", "\u03b2", -1, -1},
{"a\u03b2\u03b2c", "\u03b2", 0, -1},
{"a\u03b2\u03b2c", "\u03b2", 1, 1},
{"a\u03b2\u03b2c", "\u03b2", 3, 3},
{"a\u03b2\u03b2c", "\u03b2", 5, 3},
{"a\u03b2\u03b2c", "\u03b2", 6, 3},
} {
actual := lastIndexOf(tc.String, tc.Pattern, tc.Position)
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}
// Exhaustive check for all ints from 0 to 1112060 for correctness of implementation
// of `intToRune() -> runeToInt()`.
// This test is slow and runs longer than 5 seconds but it does provide a safety
// guarantee that these 2 functions are correct for the ranges we support.
func TestRuneToInt(t *testing.T) {
for i := uint32(0); i <= UNICODE_RANGE_MAX-UNICODE_INVALID_RANGE_DELTA-3; i += 1 {
r := intToRune(i)
ic := runeToInt(r)
assert.Equal(t, i, ic, fmt.Sprintf("intToRune(%d)=%d and runeToInt(%d)=%d", i, r, r, ic))
}
assert.Panics(t, func() {
intToRune(UNICODE_RANGE_MAX - UNICODE_INVALID_RANGE_DELTA - 2)
})
}
|