Back to index

indicator-appmenu  12.10.0
Functions | Variables
test-distance.c File Reference
#include <glib.h>
#include <glib-object.h>
#include "hudsettings.h"
#include "hudtoken.h"

Go to the source code of this file.

Functions

static guint calculate_distance (const gchar *search, GStrv teststrings, gchar ***matches)
static void test_distance_base (void)
static void test_set (GStrv *teststrings, int num_tests, const gchar *search, int right)
static void test_distance_subfunction (void)
static void test_distance_missspelll (void)
static void test_distance_print_issues (void)
static void test_distance_variety (void)
static void test_distance_french_pref (void)
static void test_distance_dups (void)
static void test_distance_extra_terms (void)
static void test_distance_suite (void)
gint main (gint argc, gchar *argv[])

Variables

HudSettings hud_settings
 SECTION:hudsettings : HudSettings : tunable parameters.

Function Documentation

static guint calculate_distance ( const gchar *  search,
GStrv  teststrings,
gchar ***  matches 
) [static]

Definition at line 41 of file test-distance.c.

{
       HudStringList *list = NULL;
       HudTokenList *haystack;
       HudTokenList *needle;
       guint distance;
       gint i;

       if (search == NULL || teststrings == NULL) {
              return G_MAXINT;
       }

       for (i = 0; teststrings[i]; i++) {
              HudStringList *tmp;

              tmp = hud_string_list_cons (teststrings[i], list);
              hud_string_list_unref (list);
              list = tmp;
       }

       haystack = hud_token_list_new_from_string_list (list);
       hud_string_list_unref (list);

       needle = hud_token_list_new_from_string (search);
       distance = hud_token_list_distance (haystack, needle, (const gchar ***) matches);

       if (matches) {
              /* These are owned by the tokenlists, so make copies
               * before freeing.
               */
              for (i = 0; (*matches)[i]; i++) {
                     (*matches)[i] = g_strdup ((*matches)[i]);
              }
       }

       hud_token_list_free (haystack);
       hud_token_list_free (needle);

       return distance;
}

Here is the call graph for this function:

Here is the caller graph for this function:

gint main ( gint  argc,
gchar *  argv[] 
)

Definition at line 255 of file test-distance.c.

{
       //gtk_init(&argc, &argv);
       g_type_init();

       g_test_init(&argc, &argv, NULL);

       /* Test suites */
       test_distance_suite();

       return g_test_run ();
}

Here is the call graph for this function:

static void test_distance_base ( void  ) [static]

Definition at line 84 of file test-distance.c.

{
       gchar * testdata1[] = {"foo", NULL};
       g_assert(calculate_distance("foo", testdata1, NULL) == 0);

       gchar * testdata2[] = {"bar", NULL};
       g_assert(calculate_distance("foo", testdata2, NULL) != 0);

       g_assert(calculate_distance("foo", NULL, NULL) != 0);

       g_assert(calculate_distance(NULL, testdata1, NULL) != 0);

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_dups ( void  ) [static]

Definition at line 213 of file test-distance.c.

{
       GStrv hits = NULL;
       gchar * teststrings[] = {"Inflated", "Confluated", "Sublimated", "Sadated", "Situated", "Infatuated", NULL};

       g_assert(calculate_distance("ted inf", teststrings, &hits) != 0);
       g_assert(g_strv_length(hits) == 2);
       g_assert(g_strcmp0(hits[0], hits[1]) != 0);

       g_strfreev(hits);

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_extra_terms ( void  ) [static]

Definition at line 229 of file test-distance.c.

{
  const gchar *save_as[] = { "File", "Save", "As...", NULL };
  const gchar *save[] = { "File", "Save", NULL };

  g_assert_cmpint (calculate_distance ("save", (GStrv) save, NULL),
                   <,
                   calculate_distance ("save", (GStrv) save_as, NULL));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_french_pref ( void  ) [static]

Definition at line 194 of file test-distance.c.

{
       GStrv teststrings[3];
       gchar * teststrings0[] = {"Fichier", "aperçu avant impression", NULL}; teststrings[0] = teststrings0;
       gchar * teststrings1[] = {"Connexion au réseau...", NULL}; teststrings[1] = teststrings1;
       gchar * teststrings2[] = {"Edition", "préférences", NULL}; teststrings[2] = teststrings2;

       test_set(teststrings, 3, "préférences", 2);
       test_set(teststrings, 3, "pré", 2);
       test_set(teststrings, 3, "préf", 2);
       test_set(teststrings, 3, "préfé", 2);
       test_set(teststrings, 3, "pref", 2);

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_missspelll ( void  ) [static]

Definition at line 139 of file test-distance.c.

{
       GStrv teststrings[4];
       gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
       gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
       gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
       gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;

       test_set(teststrings, 4, "Prnt Pr", 3);
       test_set(teststrings, 4, "Print Preiw", 3);
       test_set(teststrings, 4, "Prnt Pr", 3);

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_print_issues ( void  ) [static]

Definition at line 156 of file test-distance.c.

{
       GStrv teststrings[6];
       gchar * teststrings0[] = {"File", "New", NULL}; teststrings[0] = teststrings0;
       gchar * teststrings1[] = {"File", "Open", NULL}; teststrings[1] = teststrings1;
       gchar * teststrings2[] = {"Edit", "Undo", NULL}; teststrings[2] = teststrings2;
       gchar * teststrings3[] = {"Help", "About", NULL}; teststrings[3] = teststrings3;
       gchar * teststrings4[] = {"Help", "Empty", NULL}; teststrings[4] = teststrings4;
       gchar * teststrings5[] = {"File", "Print...", NULL}; teststrings[5] = teststrings5;

       test_set(teststrings, 6, "Pr", 5);
       test_set(teststrings, 6, "Print", 5);
       test_set(teststrings, 6, "Print...", 5);

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_subfunction ( void  ) [static]

Definition at line 125 of file test-distance.c.

{
       GStrv teststrings[4];
       gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
       gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
       gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
       gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;

       test_set(teststrings, 4, "Print Pre", 3);
       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_suite ( void  ) [static]

Definition at line 241 of file test-distance.c.

{
       g_test_add_func ("/hud/distance/base",          test_distance_base);
       g_test_add_func ("/hud/distance/subfunction",   test_distance_subfunction);
       g_test_add_func ("/hud/distance/missspelll",    test_distance_missspelll);
       g_test_add_func ("/hud/distance/print_issues",  test_distance_print_issues);
       g_test_add_func ("/hud/distance/duplicates",    test_distance_dups);
       g_test_add_func ("/hud/distance/variety",       test_distance_variety);
       g_test_add_func ("/hud/distance/french_pref",   test_distance_french_pref);
       g_test_add_func ("/hud/distance/extra_terms",   test_distance_extra_terms);
       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_distance_variety ( void  ) [static]

Definition at line 175 of file test-distance.c.

{
       GStrv teststrings[4];
       gchar * teststrings0[] = {"Date", "House Cleaning", NULL}; teststrings[0] = teststrings0;
       gchar * teststrings1[] = {"File", "Close Window", NULL}; teststrings[1] = teststrings1;
       gchar * teststrings2[] = {"Edit", "Keyboard Shortcuts...", NULL}; teststrings[2] = teststrings2;
       gchar * teststrings3[] = {"Network", "VPN Configuration", "Configure VPN...", NULL}; teststrings[3] = teststrings3;

       test_set(teststrings, 4, "House", 0);
       test_set(teststrings, 4, "House C", 0);
       test_set(teststrings, 4, "House Cle", 0);
       test_set(teststrings, 4, "House Clean", 0);
       test_set(teststrings, 4, "Clean House", 0);

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void test_set ( GStrv *  teststrings,
int  num_tests,
const gchar *  search,
int  right 
) [static]

Definition at line 101 of file test-distance.c.

{
       int i;

       for (i = 0; i < num_tests; i++) {
              if (i == right)
                     continue;

              if (calculate_distance(search, teststrings[i], NULL) < calculate_distance(search, teststrings[right], NULL)) {
                     gchar * teststr = g_strjoinv(" > ", teststrings[i]);
                     gchar * rightstr = g_strjoinv(" > ", teststrings[right]);

                     g_error("Found '%s' with search string '%s' instead of '%s'", teststr, search, rightstr);

                     g_free(teststr);
                     g_free(rightstr);
              }
       }

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

HudSettings hud_settings
Initial value:
 {
       .indicator_penalty = 50,
       .add_penalty = 10,
       .drop_penalty = 10,
       .end_drop_penalty = 1,
       .swap_penalty = 15,
       .max_distance = 30
}

SECTION:hudsettings : HudSettings : tunable parameters.

HudSettings is a structure that contains the value of several

tunable parameters that affect the behaviour of various components of the HUD.

This structure exists for two reasons.

The first reason is that caching these values in local variables eliminates the need to look them up from #GSettings on each use. This vastly improves the performance of the matching algorithms (as many of these values are used quite a lot from within them).

The second reason is to improve testability. The testcases are able to hardcode sane values for the settings without worrying about changes that the user may have made to their local configuration (which could otherwise cause spurious test failures). HudSettings: : if usage tracking should be performed : the percentage by which to increase the distance of indicators when sorting the results list : the maximum distance value we consider as being a matching result : the penalty incurred by a character in the search term that does not exist in the item being matched : the penalty incurred by a character missing from the search string as compared to the item being matched : the penalty incurred by a character missing from the search string as compared to the item being matched when the character is at the end of the item (ie: the search term is a prefix of the item) : the penalty incurred for the substitution of one character for another

This structure contains the value of several tunable parameters that affect the behaviour of various components of the HUD. hud_settings:

The #HudSettings in effect.

hud_settings_init() can be used to keep these values in sync with

GSettings. For testing, it may make sense to set these values

directly.

Definition at line 31 of file test-distance.c.