Files
test/source/blender/blenlib/tests/BLI_string_search_test.cc
Campbell Barton c434782e3a File headers: SPDX License migration
Use a shorter/simpler license convention, stops the header taking so
much space.

Follow the SPDX license specification: https://spdx.org/licenses

- C/C++/objc/objc++
- Python
- Shell Scripts
- CMake, GNUmakefile

While most of the source tree has been included

- `./extern/` was left out.
- `./intern/cycles` & `./intern/atomic` are also excluded because they
  use different header conventions.

doc/license/SPDX-license-identifiers.txt has been added to list SPDX all
used identifiers.

See P2788 for the script that automated these edits.

Reviewed By: brecht, mont29, sergey

Ref D14069
2022-02-11 09:14:36 +11:00

57 lines
2.0 KiB
C++

/* SPDX-License-Identifier: Apache-2.0 */
#include "testing/testing.h"
#include "BLI_array.hh"
#include "BLI_string_search.h"
#include "BLI_vector.hh"
namespace blender::string_search::tests {
/* Right arrow, keep in sync with #UI_MENU_ARROW_SEP in `UI_interface.h`. */
#define UI_MENU_ARROW_SEP "\xe2\x96\xb6"
TEST(string_search, damerau_levenshtein_distance)
{
EXPECT_EQ(damerau_levenshtein_distance("test", "test"), 0);
EXPECT_EQ(damerau_levenshtein_distance("hello", "ell"), 2);
EXPECT_EQ(damerau_levenshtein_distance("hello", "hel"), 2);
EXPECT_EQ(damerau_levenshtein_distance("ell", "hello"), 2);
EXPECT_EQ(damerau_levenshtein_distance("hell", "hello"), 1);
EXPECT_EQ(damerau_levenshtein_distance("hello", "hallo"), 1);
EXPECT_EQ(damerau_levenshtein_distance("test", ""), 4);
EXPECT_EQ(damerau_levenshtein_distance("", "hello"), 5);
EXPECT_EQ(damerau_levenshtein_distance("Test", "test"), 1);
EXPECT_EQ(damerau_levenshtein_distance("ab", "ba"), 1);
EXPECT_EQ(damerau_levenshtein_distance("what", "waht"), 1);
EXPECT_EQ(damerau_levenshtein_distance("what", "ahwt"), 2);
}
TEST(string_search, get_fuzzy_match_errors)
{
EXPECT_EQ(get_fuzzy_match_errors("a", "b"), -1);
EXPECT_EQ(get_fuzzy_match_errors("", "abc"), 0);
EXPECT_EQ(get_fuzzy_match_errors("hello", "hallo"), 1);
EXPECT_EQ(get_fuzzy_match_errors("hap", "hello"), -1);
EXPECT_EQ(get_fuzzy_match_errors("armature", UI_MENU_ARROW_SEP "restore"), -1);
}
TEST(string_search, extract_normalized_words)
{
LinearAllocator<> allocator;
Vector<StringRef, 64> words;
extract_normalized_words("hello world" UI_MENU_ARROW_SEP "test another test" UI_MENU_ARROW_SEP
" 3",
allocator,
words);
EXPECT_EQ(words.size(), 6);
EXPECT_EQ(words[0], "hello");
EXPECT_EQ(words[1], "world");
EXPECT_EQ(words[2], "test");
EXPECT_EQ(words[3], "another");
EXPECT_EQ(words[4], "test");
EXPECT_EQ(words[5], "3");
}
} // namespace blender::string_search::tests