From 064518abd9ef9113a432acd03904d2d17915c551 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 20 May 2016 13:26:23 -0400
Subject: [PATCH 03/16] spellcheck.c: convert Levenshtein test from a plugin to
 a selftest

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

