(root)/
glib-2.79.0/
girepository/
cmph-bdz-test.c
       1  /* GObject introspection: Test cmph hashing
       2   *
       3   * Copyright (C) 2010 Red Hat, Inc.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       7   * This library is free software; you can redistribute it and/or
       8   * modify it under the terms of the GNU Lesser General Public
       9   * License as published by the Free Software Foundation; either
      10   * version 2 of the License, or (at your option) any later version.
      11   *
      12   * This library is distributed in the hope that it will be useful,
      13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15   * Lesser General Public License for more details.
      16   *
      17   * You should have received a copy of the GNU Lesser General Public
      18   * License along with this library; if not, write to the
      19   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      20   * Boston, MA 02111-1307, USA.
      21   */
      22  
      23  #include <glib-object.h>
      24  #include "cmph.h"
      25  
      26  static cmph_t *
      27  build (void)
      28  {
      29    cmph_config_t *config;
      30    cmph_io_adapter_t *io;
      31    char **strings;
      32    cmph_t *c;
      33    guint32 size;
      34  
      35    strings = g_strsplit ("foo,bar,baz", ",", -1);
      36  
      37    io = cmph_io_vector_adapter (strings, g_strv_length (strings));
      38    config = cmph_config_new (io);
      39    cmph_config_set_algo (config, CMPH_BDZ);
      40  
      41    c = cmph_new (config);
      42    size = cmph_size (c);
      43    g_assert_cmpuint (size, ==, g_strv_length (strings));
      44  
      45    cmph_config_destroy (config);
      46    cmph_io_vector_adapter_destroy (io);
      47    g_strfreev (strings);
      48  
      49    return c;
      50  }
      51  
      52  static void
      53  assert_hashes_unique (guint    n_hashes,
      54  		      guint32* hashes)
      55  {
      56    guint i;
      57  
      58    for (i = 0; i < n_hashes; i++)
      59      {
      60        guint j = 0;
      61        for (j = 0; j < n_hashes; j++)
      62  	{
      63  	  if (j != i)
      64  	    g_assert_cmpuint (hashes[i], !=, hashes[j]);
      65  	}
      66      }
      67  }
      68  
      69  static void
      70  test_search (void)
      71  {
      72    cmph_t *c = build();
      73    guint i;
      74    guint32 hash;
      75    guint32 hashes[3];
      76    guint32 size;
      77  
      78    size = cmph_size (c);
      79  
      80    i = 0;
      81    hash = cmph_search (c, "foo", 3);
      82    g_assert_cmpuint (hash, >=, 0);
      83    g_assert_cmpuint (hash, <, size);
      84    hashes[i++] = hash;
      85  
      86    hash = cmph_search (c, "bar", 3);
      87    g_assert_cmpuint (hash, >=, 0);
      88    g_assert_cmpuint (hash, <, size);
      89    hashes[i++] = hash;
      90  
      91    hash = cmph_search (c, "baz", 3);
      92    g_assert_cmpuint (hash, >=, 0);
      93    g_assert_cmpuint (hash, <, size);
      94    hashes[i++] = hash;
      95  
      96    assert_hashes_unique (G_N_ELEMENTS (hashes), &hashes[0]);
      97  
      98    cmph_destroy (c);
      99  }
     100  
     101  static void
     102  test_search_packed (void)
     103  {
     104    cmph_t *c = build();
     105    guint32 bufsize;
     106    guint i;
     107    guint32 hash;
     108    guint32 hashes[3];
     109    guint32 size;
     110    guint8 *buf;
     111  
     112    bufsize = cmph_packed_size (c);
     113    buf = g_malloc (bufsize);
     114    cmph_pack (c, buf);
     115  
     116    size = cmph_size (c);
     117  
     118    cmph_destroy (c);
     119    c = NULL;
     120  
     121    i = 0;
     122    hash = cmph_search_packed (buf, "foo", 3);
     123    g_assert_cmpuint (hash, >=, 0);
     124    g_assert_cmpuint (hash, <, size);
     125    hashes[i++] = hash;
     126  
     127    hash = cmph_search_packed (buf, "bar", 3);
     128    g_assert_cmpuint (hash, >=, 0);
     129    g_assert_cmpuint (hash, <, size);
     130    hashes[i++] = hash;
     131  
     132    hash = cmph_search_packed (buf, "baz", 3);
     133    g_assert_cmpuint (hash, >=, 0);
     134    g_assert_cmpuint (hash, <, size);
     135    hashes[i++] = hash;
     136  
     137    assert_hashes_unique (G_N_ELEMENTS (hashes), &hashes[0]);
     138  
     139    g_free (buf);
     140  }
     141  
     142  int
     143  main(int argc, char **argv)
     144  {
     145    gint ret;
     146  
     147    g_test_init (&argc, &argv, NULL);
     148  
     149    g_test_add_func ("/cmph-bdz/search", test_search);
     150    g_test_add_func ("/cmph-bdz/search-packed", test_search_packed);
     151  
     152    ret = g_test_run ();
     153  
     154    return ret;
     155  }
     156