File: test_init.py

package info (click to toggle)
rapidfuzz 3.12.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,436 kB
  • sloc: python: 7,571; cpp: 7,481; sh: 30; makefile: 23
file content (364 lines) | stat: -rw-r--r-- 10,165 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from __future__ import annotations

import random

import hypothesis.strategies as st
import pytest
from hypothesis import given, settings

import rapidfuzz.distance._initialize_cpp as distance_cpp
import rapidfuzz.distance._initialize_py as distance_py
from rapidfuzz.distance import Editops, Levenshtein, Opcodes


def test_editops_comparison():
    """
    test comparison with Editops
    """
    ops = Levenshtein.editops("aaabaaa", "abbaaabba")
    assert ops == ops
    assert not (ops != ops)  # noqa: SIM202
    assert ops == ops.copy()
    assert not (ops != ops.copy())  # noqa: SIM202


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_editops_get_index(module):
    """
    test __getitem__ with index of Editops
    """
    ops = module.Editops(
        [
            ("delete", 1, 1),
            ("replace", 2, 1),
            ("insert", 6, 5),
            ("insert", 6, 6),
            ("insert", 6, 7),
        ],
        7,
        9,
    )

    ops_list = [
        ("delete", 1, 1),
        ("replace", 2, 1),
        ("insert", 6, 5),
        ("insert", 6, 6),
        ("insert", 6, 7),
    ]

    assert ops[0] == ops_list[0]
    assert ops[1] == ops_list[1]
    assert ops[2] == ops_list[2]
    assert ops[3] == ops_list[3]
    assert ops[4] == ops_list[4]

    assert ops[-1] == ops_list[-1]
    assert ops[-2] == ops_list[-2]
    assert ops[-3] == ops_list[-3]
    assert ops[-4] == ops_list[-4]
    assert ops[-5] == ops_list[-5]

    with pytest.raises(IndexError):
        ops[5]
    with pytest.raises(IndexError):
        ops[-6]


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_editops_get_slice(module):
    """
    test __getitem__ with slice of Editops
    """
    ops = module.Editops(
        [
            ("delete", 1, 1),
            ("replace", 2, 1),
            ("insert", 6, 5),
            ("insert", 6, 6),
            ("insert", 6, 7),
        ],
        7,
        9,
    )

    ops_list = [
        ("delete", 1, 1),
        ("replace", 2, 1),
        ("insert", 6, 5),
        ("insert", 6, 6),
        ("insert", 6, 7),
    ]

    assert ops[::].as_list() == ops_list[::]
    assert ops[::] is not ops
    assert ops[1:].as_list() == ops_list[1:]
    assert ops[:3].as_list() == ops_list[:3]
    assert ops[1:3].as_list() == ops_list[1:3]
    assert ops[:-1].as_list() == ops_list[:-1]
    assert ops[-3:].as_list() == ops_list[-3:]
    assert ops[-3:-1].as_list() == ops_list[-3:-1]

    with pytest.raises(ValueError, match="slice step cannot be zero"):
        ops[::0]

    with pytest.raises(ValueError, match="step sizes below 0 lead to an invalid order of editops"):
        ops[::-1]


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_editops_del_slice(module):
    """
    test __delitem__ with slice of Editops
    """
    ops = module.Editops(
        [
            ("delete", 1, 1),
            ("replace", 2, 1),
            ("insert", 6, 5),
            ("insert", 6, 6),
            ("insert", 6, 7),
        ],
        7,
        9,
    )

    ops_list = [
        ("delete", 1, 1),
        ("replace", 2, 1),
        ("insert", 6, 5),
        ("insert", 6, 6),
        ("insert", 6, 7),
    ]

    def del_test(key):
        _ops = ops[::]
        _ops_list = ops_list[::]
        del _ops[key]
        del _ops_list[key]
        assert _ops.as_list() == _ops_list

    del_test(slice(None, 4, None))
    del_test(slice(1, None, None))
    del_test(slice(1, 4, None))
    del_test(slice(None, 4, 2))
    del_test(slice(1, None, 2))
    del_test(slice(1, 4, 2))

    del_test(slice(None, -1, None))
    del_test(slice(-4, None, None))
    del_test(slice(-4, -1, None))
    del_test(slice(None, -1, 2))
    del_test(slice(-4, None, 2))
    del_test(slice(-4, -1, 2))


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_editops_inversion(module):
    """
    test correct inversion of Editops
    """
    ops = module.Editops(
        [
            ("delete", 1, 1),
            ("replace", 2, 1),
            ("insert", 6, 5),
            ("insert", 6, 6),
            ("insert", 6, 7),
        ],
        7,
        9,
    )

    assert ops.inverse().as_list() == [
        ("insert", 1, 1),
        ("replace", 1, 2),
        ("delete", 5, 6),
        ("delete", 6, 6),
        ("delete", 7, 6),
    ]


def test_opcodes_comparison():
    """
    test comparison with Opcodes
    """
    ops = Levenshtein.opcodes("aaabaaa", "abbaaabba")
    assert ops == ops
    assert not (ops != ops)  # noqa: SIM202
    assert ops == ops.copy()
    assert not (ops != ops.copy())  # noqa: SIM202


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_opcode_py_get_index(module):
    """
    test __getitem__ with index of Opcodes
    """
    ops = module.Opcodes(
        [
            ("equal", 0, 1, 0, 1),
            ("delete", 1, 2, 1, 1),
            ("replace", 2, 3, 1, 2),
            ("equal", 3, 6, 2, 5),
            ("insert", 6, 6, 5, 8),
            ("equal", 6, 7, 8, 9),
        ],
        7,
        9,
    )

    ops_list = [
        ("equal", 0, 1, 0, 1),
        ("delete", 1, 2, 1, 1),
        ("replace", 2, 3, 1, 2),
        ("equal", 3, 6, 2, 5),
        ("insert", 6, 6, 5, 8),
        ("equal", 6, 7, 8, 9),
    ]

    assert ops[0] == ops_list[0]
    assert ops[1] == ops_list[1]
    assert ops[2] == ops_list[2]
    assert ops[3] == ops_list[3]
    assert ops[4] == ops_list[4]
    assert ops[5] == ops_list[5]

    assert ops[-1] == ops_list[-1]
    assert ops[-2] == ops_list[-2]
    assert ops[-3] == ops_list[-3]
    assert ops[-4] == ops_list[-4]
    assert ops[-5] == ops_list[-5]
    assert ops[-6] == ops_list[-6]

    with pytest.raises(IndexError):
        ops[6]
    with pytest.raises(IndexError):
        ops[-7]


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_opcode_inversion(module):
    """
    test correct inversion of Opcodes
    """
    ops = module.Opcodes(
        [
            ("equal", 0, 1, 0, 1),
            ("delete", 1, 2, 1, 1),
            ("replace", 2, 3, 1, 2),
            ("equal", 3, 6, 2, 5),
            ("insert", 6, 6, 5, 8),
            ("equal", 6, 7, 8, 9),
        ],
        7,
        9,
    )

    assert ops.inverse().as_list() == [
        ("equal", 0, 1, 0, 1),
        ("insert", 1, 1, 1, 2),
        ("replace", 1, 2, 2, 3),
        ("equal", 2, 5, 3, 6),
        ("delete", 5, 8, 6, 6),
        ("equal", 8, 9, 6, 7),
    ]


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_opcodes_empty(module):
    """
    test behavior of conversion between empty list and Editops
    """
    ops = module.Opcodes([], 0, 0)
    assert ops.as_list() == []
    assert ops.src_len == 0
    assert ops.dest_len == 0

    ops = module.Opcodes([], 0, 3)
    assert ops.as_list() == [module.Opcode(tag="equal", src_start=0, src_end=0, dest_start=0, dest_end=3)]
    assert ops.src_len == 0
    assert ops.dest_len == 3


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_editops_empty(module):
    """
    test behavior of conversion between empty list and Opcodes
    """
    ops = module.Editops([], 0, 0)
    assert ops.as_list() == []
    assert ops.src_len == 0
    assert ops.dest_len == 0

    ops = module.Editops([], 0, 3)
    assert ops.as_list() == []
    assert ops.src_len == 0
    assert ops.dest_len == 3


def test_list_initialization():
    """
    test whether list initialization works correctly
    """
    ops = Levenshtein.opcodes("aaabaaa", "abbaaabba")
    ops2 = Opcodes(ops.as_list(), ops.src_len, ops.dest_len)
    assert ops == ops2

    ops = Levenshtein.editops("aaabaaa", "abbaaabba")
    ops2 = Editops(ops.as_list(), ops.src_len, ops.dest_len)
    assert ops == ops2

    ops = Levenshtein.opcodes("aaabaaa", "abbaaabba")
    ops2 = Editops(ops.as_list(), ops.src_len, ops.dest_len)
    assert ops.as_editops() == ops2

    ops = Levenshtein.editops("aaabaaa", "abbaaabba")
    ops2 = Opcodes(ops.as_list(), ops.src_len, ops.dest_len)
    assert ops.as_opcodes() == ops2

    ops = Levenshtein.editops("skdsakldsakdlasda", "djkajkdfkdgkhdfjrmecsidjf")
    ops2 = Opcodes(ops.as_list(), ops.src_len, ops.dest_len)
    assert ops.as_opcodes() == ops2


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_merge_adjacent_blocks(module):
    """
    test whether adjacent blocks are merged
    """
    ops1 = [module.Opcode(tag="equal", src_start=0, src_end=3, dest_start=0, dest_end=3)]
    ops2 = [
        module.Opcode(tag="equal", src_start=0, src_end=1, dest_start=0, dest_end=1),
        module.Opcode(tag="equal", src_start=1, src_end=3, dest_start=1, dest_end=3),
    ]
    assert module.Opcodes(ops1, 3, 3) == module.Opcodes(ops2, 3, 3)
    assert module.Opcodes(ops2, 3, 3) == module.Opcodes(ops2, 3, 3).as_editops().as_opcodes()


@pytest.mark.parametrize("module", [distance_py, distance_cpp])
def test_empty_matching_blocks(module):
    """
    test behavior for empty matching blocks
    """
    assert module.Editops([], 0, 0).as_matching_blocks() == [module.MatchingBlock(a=0, b=0, size=0)]
    assert module.Editops([], 0, 3).as_matching_blocks() == [module.MatchingBlock(a=0, b=3, size=0)]
    assert module.Editops([], 3, 0).as_matching_blocks() == [module.MatchingBlock(a=3, b=0, size=0)]

    assert module.Opcodes([], 0, 0).as_matching_blocks() == [module.MatchingBlock(a=0, b=0, size=0)]
    assert module.Opcodes([], 0, 3).as_matching_blocks() == [module.MatchingBlock(a=0, b=3, size=0)]
    assert module.Opcodes([], 3, 0).as_matching_blocks() == [module.MatchingBlock(a=3, b=0, size=0)]


@given(s1=st.text(), s2=st.text())
@settings(max_examples=100, deadline=None)
def test_editops_reversible(s1, s2):
    """
    test whether conversions between editops and opcodes are reversible
    """
    ops = Levenshtein.editops(s1, s2)
    assert ops == ops.as_opcodes().as_editops()
    while ops:
        del ops[random.randrange(len(ops))]
        assert Opcodes(ops.as_list(), ops.src_len, ops.dest_len) == ops.as_opcodes()
        assert ops == ops.as_opcodes().as_editops()