From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 35919 invoked by alias); 2 Jun 2016 20:41:18 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 35754 invoked by uid 89); 2 Jun 2016 20:41:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=our X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 02 Jun 2016 20:41:06 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B035AD4028 for ; Thu, 2 Jun 2016 20:41:05 +0000 (UTC) Received: from c64.redhat.com (vpn-237-222.phx2.redhat.com [10.3.237.222]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u52Kf2Wv007154; Thu, 2 Jun 2016 16:41:05 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: Bernd Schmidt , Jeff Law , David Malcolm Subject: [PATCH 03/16] spellcheck.c: convert Levenshtein test from a plugin to a selftest Date: Thu, 02 Jun 2016 20:41:00 -0000 Message-Id: <1464901625-54547-4-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1464901625-54547-1-git-send-email-dmalcolm@redhat.com> References: <1464874868.11895.41.camel@redhat.com> <1464901625-54547-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes X-SW-Source: 2016-06/txt/msg00211.txt.bz2 This is an example of converting one of our existing plugin-based tests to run within -fself-test instead. gcc/ChangeLog: * spellcheck.c: Include "selftest.h". (levenshtein_distance_unit_test_oneway): New function, adapted from testsuite/gcc.dg/plugin/levenshtein_plugin.c. (levenshtein_distance_unit_test): Likewise. (selftest::spellcheck_c_tests): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/plugin/levenshtein-test-1.c: Delete. * gcc.dg/plugin/levenshtein_plugin.c: Delete. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove the above. --- gcc/spellcheck.c | 45 +++++++++++++++++ gcc/testsuite/gcc.dg/plugin/levenshtein-test-1.c | 9 ---- gcc/testsuite/gcc.dg/plugin/levenshtein_plugin.c | 64 ------------------------ gcc/testsuite/gcc.dg/plugin/plugin.exp | 1 - 4 files changed, 45 insertions(+), 74 deletions(-) delete mode 100644 gcc/testsuite/gcc.dg/plugin/levenshtein-test-1.c delete mode 100644 gcc/testsuite/gcc.dg/plugin/levenshtein_plugin.c diff --git a/gcc/spellcheck.c b/gcc/spellcheck.c index e4e83a5..07c033a 100644 --- a/gcc/spellcheck.c +++ b/gcc/spellcheck.c @@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see #include "tm.h" #include "tree.h" #include "spellcheck.h" +#include "selftest.h" /* The Levenshtein distance is an "edit-distance": the minimal number of one-character insertions, removals or substitutions @@ -165,3 +166,47 @@ find_closest_string (const char *target, return best_candidate; } + +#if CHECKING_P + +static void +levenshtein_distance_unit_test_oneway (const char *a, const char *b, + edit_distance_t expected) +{ + edit_distance_t actual = levenshtein_distance (a, b); + ASSERT_EQ (actual, expected); +} + +static void +levenshtein_distance_unit_test (const char *a, const char *b, + edit_distance_t expected) +{ + /* Run every test both ways to ensure it's symmetric. */ + levenshtein_distance_unit_test_oneway (a, b, expected); + levenshtein_distance_unit_test_oneway (b, a, expected); +} + +namespace selftest { + +void +spellcheck_c_tests () +{ + levenshtein_distance_unit_test ("", "nonempty", strlen ("nonempty")); + levenshtein_distance_unit_test ("saturday", "sunday", 3); + levenshtein_distance_unit_test ("foo", "m_foo", 2); + levenshtein_distance_unit_test ("hello_world", "HelloWorld", 3); + levenshtein_distance_unit_test + ("the quick brown fox jumps over the lazy dog", "dog", 40); + levenshtein_distance_unit_test + ("the quick brown fox jumps over the lazy dog", + "the quick brown dog jumps over the lazy fox", + 4); + levenshtein_distance_unit_test + ("Lorem ipsum dolor sit amet, consectetur adipiscing elit,", + "All your base are belong to us", + 44); +} + +} // namespace selftest + +#endif /* #if CHECKING_P */ diff --git a/gcc/testsuite/gcc.dg/plugin/levenshtein-test-1.c b/gcc/testsuite/gcc.dg/plugin/levenshtein-test-1.c deleted file mode 100644 index ac49992..0000000 --- a/gcc/testsuite/gcc.dg/plugin/levenshtein-test-1.c +++ /dev/null @@ -1,9 +0,0 @@ -/* Placeholder C source file for unit-testing gcc/spellcheck.c. */ -/* { dg-do compile } */ -/* { dg-options "-O" } */ - -int -main (int argc, char **argv) -{ - return 0; -} diff --git a/gcc/testsuite/gcc.dg/plugin/levenshtein_plugin.c b/gcc/testsuite/gcc.dg/plugin/levenshtein_plugin.c deleted file mode 100644 index 3e7dc78..0000000 --- a/gcc/testsuite/gcc.dg/plugin/levenshtein_plugin.c +++ /dev/null @@ -1,64 +0,0 @@ -/* Plugin for unittesting gcc/spellcheck.h. */ - -#include "config.h" -#include "gcc-plugin.h" -#include "system.h" -#include "coretypes.h" -#include "spellcheck.h" -#include "diagnostic.h" - -int plugin_is_GPL_compatible; - -static void -levenshtein_distance_unit_test_oneway (const char *a, const char *b, - edit_distance_t expected) -{ - edit_distance_t actual = levenshtein_distance (a, b); - if (actual != expected) - error ("levenshtein_distance (\"%s\", \"%s\") : expected: %i got %i", - a, b, expected, actual); -} - - -static void -levenshtein_distance_unit_test (const char *a, const char *b, - edit_distance_t expected) -{ - /* Run every test both ways to ensure it's symmetric. */ - levenshtein_distance_unit_test_oneway (a, b, expected); - levenshtein_distance_unit_test_oneway (b, a, expected); -} - -/* Callback handler for the PLUGIN_FINISH event; run - levenshtein_distance unit tests here. */ - -static void -on_finish (void */*gcc_data*/, void */*user_data*/) -{ - levenshtein_distance_unit_test ("", "nonempty", strlen ("nonempty")); - levenshtein_distance_unit_test ("saturday", "sunday", 3); - levenshtein_distance_unit_test ("foo", "m_foo", 2); - levenshtein_distance_unit_test ("hello_world", "HelloWorld", 3); - levenshtein_distance_unit_test - ("the quick brown fox jumps over the lazy dog", "dog", 40); - levenshtein_distance_unit_test - ("the quick brown fox jumps over the lazy dog", - "the quick brown dog jumps over the lazy fox", - 4); - levenshtein_distance_unit_test - ("Lorem ipsum dolor sit amet, consectetur adipiscing elit,", - "All your base are belong to us", - 44); -} - -int -plugin_init (struct plugin_name_args *plugin_info, - struct plugin_gcc_version */*version*/) -{ - register_callback (plugin_info->base_name, - PLUGIN_FINISH, - on_finish, - NULL); /* void *user_data */ - - return 0; -} diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp index fd1e98e..dfcdea2 100644 --- a/gcc/testsuite/gcc.dg/plugin/plugin.exp +++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp @@ -70,7 +70,6 @@ set plugin_test_list [list \ diagnostic-test-expressions-1.c } \ { diagnostic_plugin_show_trees.c \ diagnostic-test-show-trees-1.c } \ - { levenshtein_plugin.c levenshtein-test-1.c } \ { location_overflow_plugin.c \ location-overflow-test-1.c \ location-overflow-test-2.c } \ -- 1.8.5.3