From c3e5d75f9c8a60af42646319fcca832d5f1a55d4 Mon Sep 17 00:00:00 2001
From: TJ Saunders <tj@castaglia.org>
Date: Sun, 21 May 2017 13:44:23 -0700
Subject: [PATCH] Merge pull request #513 from pghmcfc/similars

Fix pr_str_get_similars
---
 src/str.c       |  4 ++--
 tests/api/str.c | 36 +++++++++++++++++++-----------------
 2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/src/str.c b/src/str.c
index eeed096ef..0a59f2379 100644
--- a/src/str.c
+++ b/src/str.c
@@ -725,11 +725,11 @@ static int distance_cmp(const void *a, const void *b) {
   const char *s1, *s2;
   int distance1, distance2;

-  cand1 = a;
+  cand1 = * (const struct candidate **) a;
   s1 = cand1->s;
   distance1 = cand1->distance;

-  cand2 = b;
+  cand2 = * (const struct candidate **) b;
   s2 = cand2->s;
   distance2 = cand2->distance;

diff --git a/tests/api/str.c b/tests/api/str.c
index 7c6e11000..9dce95820 100644
--- a/tests/api/str.c
+++ b/tests/api/str.c
@@ -1469,25 +1469,23 @@ START_TEST (similars_test) {
   mark_point();
   similars = (const char **) res->elts;

-  /* Note: We see different results here due to (I think) different
-   * qsort(3) implementations.
+  /*
+   * Note: expected distances are as follows:
+   *
+   * Candidate       Case-Sensitive      Case-Insensitive
+   * fools                 0                     0
+   * odd                   5                     5
+   * bar                   5                     5
+   * FOO                   5                     0
    */

-  expected = "FOO";
-  if (strcmp(similars[0], expected) != 0) {
-    expected = "fools";
-  }
+  expected = "fools";

   fail_unless(strcmp(similars[0], expected) == 0,
     "Expected similar '%s', got '%s'", expected, similars[0]);

-  expected = "fools";
-  if (strcmp(similars[1], expected) != 0) {
-    expected = "FOO";
-  }
-
-  fail_unless(strcmp(similars[1], expected) == 0,
-    "Expected similar '%s', got '%s'", expected, similars[1]);
+  fail_unless(strcmp(similars[1], expected) != 0,
+    "Unexpectedly got similar '%s'", similars[1]);

   mark_point();
   res = pr_str_get_similars(p, s, candidates, 0, PR_STR_FL_IGNORE_CASE);
@@ -1499,18 +1497,22 @@ START_TEST (similars_test) {
   mark_point();
   similars = (const char **) res->elts;

+  /*
+   * similars[0] and similars[1] should be "FOO" and "fools", but
+   * not necessarily in that order
+   */
   expected = "FOO";
   if (strcmp(similars[0], expected) != 0) {
-    expected = "fools";
+    expected = similars[0];
+    similars[0] = similars[1];
+    similars[1] = expected;
+    expected = "FOO";
   }

   fail_unless(strcmp(similars[0], expected) == 0,
     "Expected similar '%s', got '%s'", expected, similars[0]);

   expected = "fools";
-  if (strcmp(similars[1], expected) != 0) {
-    expected = "FOO";
-  }

   fail_unless(strcmp(similars[1], expected) == 0,
     "Expected similar '%s', got '%s'", expected, similars[1]);
