aboutsummaryrefslogtreecommitdiff
path: root/gcc/spellcheck.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-06-15 00:12:16 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-06-15 00:12:16 +0000
commitf254671f2f9a83843b7b77b496689791bdfdb4fe (patch)
treecf61ac3757fd5bfdcfc0f5e53278cd0e3d0606c1 /gcc/spellcheck.c
parentee62e30213a72a93ebee9bfbadc3612cef175957 (diff)
downloadgcc-f254671f2f9a83843b7b77b496689791bdfdb4fe.zip
gcc-f254671f2f9a83843b7b77b496689791bdfdb4fe.tar.gz
gcc-f254671f2f9a83843b7b77b496689791bdfdb4fe.tar.bz2
Add more spellcheck selftests
gcc/ChangeLog: * selftest-run-tests.c (selftest::run_tests): Call selftest::spellcheck_tree_c_tests. * selftest.h (selftest::spellcheck_tree_c_tests): New decl. * spellcheck-tree.c: Include selftest.h and stringpool.h. (selftest::test_find_closest_identifier): New function. (selftest::spellcheck_tree_c_tests): New function. * spellcheck.c (selftest::test_find_closest_string): Verify that the order of the vec does not affect the results for this case. (selftest::test_data): New array. (selftest::test_metric_conditions): New function. (selftest::spellcheck_c_tests): Add a test of case-comparison. Call selftest::test_metric_conditions. From-SVN: r237466
Diffstat (limited to 'gcc/spellcheck.c')
-rw-r--r--gcc/spellcheck.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/gcc/spellcheck.c b/gcc/spellcheck.c
index 11018f0..e03f484 100644
--- a/gcc/spellcheck.c
+++ b/gcc/spellcheck.c
@@ -217,6 +217,69 @@ test_find_closest_string ()
ASSERT_STREQ ("banana", find_closest_string ("banyan", &candidates));
ASSERT_STREQ ("cherry", find_closest_string ("berry", &candidates));
ASSERT_EQ (NULL, find_closest_string ("not like the others", &candidates));
+
+ /* The order of the vec can matter, but it should not matter for these
+ inputs. */
+ candidates.truncate (0);
+ candidates.safe_push ("cherry");
+ candidates.safe_push ("banana");
+ candidates.safe_push ("apple");
+ ASSERT_STREQ ("apple", find_closest_string ("app", &candidates));
+ ASSERT_STREQ ("banana", find_closest_string ("banyan", &candidates));
+ ASSERT_STREQ ("cherry", find_closest_string ("berry", &candidates));
+ ASSERT_EQ (NULL, find_closest_string ("not like the others", &candidates));
+}
+
+/* Test data for test_metric_conditions. */
+
+static const char * const test_data[] = {
+ "",
+ "foo"
+ "food",
+ "boo",
+ "1234567890123456789012345678901234567890123456789012345678901234567890"
+};
+
+/* Verify that levenshtein_distance appears to be a sane distance function,
+ i.e. the conditions for being a metric. This is done directly for a
+ small set of examples, using test_data above. This is O(N^3) in the size
+ of the array, due to the test for the triangle inequality, so we keep the
+ array small. */
+
+static void
+test_metric_conditions ()
+{
+ const int num_test_cases = sizeof (test_data) / sizeof (test_data[0]);
+
+ for (int i = 0; i < num_test_cases; i++)
+ {
+ for (int j = 0; j < num_test_cases; j++)
+ {
+ edit_distance_t dist_ij
+ = levenshtein_distance (test_data[i], test_data[j]);
+
+ /* Identity of indiscernibles: d(i, j) > 0 iff i == j. */
+ if (i == j)
+ ASSERT_EQ (dist_ij, 0);
+ else
+ ASSERT_TRUE (dist_ij > 0);
+
+ /* Symmetry: d(i, j) == d(j, i). */
+ edit_distance_t dist_ji
+ = levenshtein_distance (test_data[j], test_data[i]);
+ ASSERT_EQ (dist_ij, dist_ji);
+
+ /* Triangle inequality. */
+ for (int k = 0; k < num_test_cases; k++)
+ {
+ edit_distance_t dist_ik
+ = levenshtein_distance (test_data[i], test_data[k]);
+ edit_distance_t dist_jk
+ = levenshtein_distance (test_data[j], test_data[k]);
+ ASSERT_TRUE (dist_ik <= dist_ij + dist_jk);
+ }
+ }
+ }
}
/* Verify levenshtein_distance for a variety of pairs of pre-canned
@@ -239,8 +302,10 @@ spellcheck_c_tests ()
("Lorem ipsum dolor sit amet, consectetur adipiscing elit,",
"All your base are belong to us",
44);
+ levenshtein_distance_unit_test ("foo", "FOO", 3);
test_find_closest_string ();
+ test_metric_conditions ();
}
} // namespace selftest