From dd9021e3128af332b472450ba407f345e5dbdb02 Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Wed, 26 Jan 2022 12:55:05 +0100 Subject: [PATCH] fix incorrect editops validation --- src/cython/distance/_initialize.pyx | 10 ++++------ tests/distance/test_init.py | 4 ++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cython/distance/_initialize.pyx b/src/cython/distance/_initialize.pyx index a16d6cd..f4db3e2 100644 --- a/src/cython/distance/_initialize.pyx +++ b/src/cython/distance/_initialize.pyx @@ -75,12 +75,10 @@ cdef RfEditops list_to_editops(ops, Py_ssize_t src_len, Py_ssize_t dest_len) exc if src_pos > src_len or dest_pos > dest_len: raise ValueError("List of edit operations invalid") - if edit_type == EditType.Insert: - if src_pos == src_len: - raise ValueError("List of edit operations invalid") - elif edit_type == EditType.Delete: - if dest_pos == dest_len: - raise ValueError("List of edit operations invalid") + if src_pos == src_len and edit_type != EditType.Insert: + raise ValueError("List of edit operations invalid") + elif dest_pos == dest_len and edit_type != EditType.Delete: + raise ValueError("List of edit operations invalid") result.emplace_back(edit_type, src_pos, dest_pos) diff --git a/tests/distance/test_init.py b/tests/distance/test_init.py index 28b858a..063fe2d 100644 --- a/tests/distance/test_init.py +++ b/tests/distance/test_init.py @@ -119,5 +119,9 @@ def test_list_initialization(): 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 + if __name__ == '__main__': unittest.main() \ No newline at end of file