LCOV - code coverage report
Current view: top level - glib/girepository/tests - cmph-bdz.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 66 66 100.0 %
Date: 2024-05-07 05:15:23 Functions: 5 5 100.0 %
Branches: 6 6 100.0 %

           Branch data     Line data    Source code
       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                 :          2 : build (void)
      28                 :            : {
      29                 :            :   cmph_config_t *config;
      30                 :            :   cmph_io_adapter_t *io;
      31                 :            :   char **strings;
      32                 :            :   cmph_t *c;
      33                 :            :   uint32_t size;
      34                 :            : 
      35                 :          2 :   strings = g_strsplit ("foo,bar,baz", ",", -1);
      36                 :            : 
      37                 :          2 :   io = cmph_io_vector_adapter (strings, g_strv_length (strings));
      38                 :          2 :   config = cmph_config_new (io);
      39                 :          2 :   cmph_config_set_algo (config, CMPH_BDZ);
      40                 :            : 
      41                 :          2 :   c = cmph_new (config);
      42                 :          2 :   size = cmph_size (c);
      43                 :          2 :   g_assert_cmpuint (size, ==, g_strv_length (strings));
      44                 :            : 
      45                 :          2 :   cmph_config_destroy (config);
      46                 :          2 :   cmph_io_vector_adapter_destroy (io);
      47                 :          2 :   g_strfreev (strings);
      48                 :            : 
      49                 :          2 :   return c;
      50                 :            : }
      51                 :            : 
      52                 :            : static void
      53                 :          2 : assert_hashes_unique (size_t    n_hashes,
      54                 :            :                       uint32_t* hashes)
      55                 :            : {
      56                 :            :   size_t i;
      57                 :            : 
      58         [ +  + ]:          8 :   for (i = 0; i < n_hashes; i++)
      59                 :            :     {
      60         [ +  + ]:         24 :       for (size_t j = 0; j < n_hashes; j++)
      61                 :            :         {
      62         [ +  + ]:         18 :           if (j != i)
      63                 :         12 :             g_assert_cmpuint (hashes[i], !=, hashes[j]);
      64                 :            :         }
      65                 :            :     }
      66                 :          2 : }
      67                 :            : 
      68                 :            : static void
      69                 :          1 : test_search (void)
      70                 :            : {
      71                 :          1 :   cmph_t *c = build();
      72                 :            :   size_t i;
      73                 :            :   uint32_t hash;
      74                 :            :   uint32_t hashes[3];
      75                 :            :   uint32_t size;
      76                 :            : 
      77                 :          1 :   size = cmph_size (c);
      78                 :            : 
      79                 :          1 :   i = 0;
      80                 :          1 :   hash = cmph_search (c, "foo", 3);
      81                 :          1 :   g_assert_cmpuint (hash, >=, 0);
      82                 :          1 :   g_assert_cmpuint (hash, <, size);
      83                 :          1 :   hashes[i++] = hash;
      84                 :            : 
      85                 :          1 :   hash = cmph_search (c, "bar", 3);
      86                 :          1 :   g_assert_cmpuint (hash, >=, 0);
      87                 :          1 :   g_assert_cmpuint (hash, <, size);
      88                 :          1 :   hashes[i++] = hash;
      89                 :            : 
      90                 :          1 :   hash = cmph_search (c, "baz", 3);
      91                 :          1 :   g_assert_cmpuint (hash, >=, 0);
      92                 :          1 :   g_assert_cmpuint (hash, <, size);
      93                 :          1 :   hashes[i++] = hash;
      94                 :            : 
      95                 :          1 :   assert_hashes_unique (G_N_ELEMENTS (hashes), &hashes[0]);
      96                 :            : 
      97                 :          1 :   cmph_destroy (c);
      98                 :          1 : }
      99                 :            : 
     100                 :            : static void
     101                 :          1 : test_search_packed (void)
     102                 :            : {
     103                 :          1 :   cmph_t *c = build();
     104                 :            :   size_t i;
     105                 :            :   uint32_t bufsize;
     106                 :            :   uint32_t hash;
     107                 :            :   uint32_t hashes[3];
     108                 :            :   uint32_t size;
     109                 :            :   uint8_t *buf;
     110                 :            : 
     111                 :          1 :   bufsize = cmph_packed_size (c);
     112                 :          1 :   buf = g_malloc (bufsize);
     113                 :          1 :   cmph_pack (c, buf);
     114                 :            : 
     115                 :          1 :   size = cmph_size (c);
     116                 :            : 
     117                 :          1 :   cmph_destroy (c);
     118                 :          1 :   c = NULL;
     119                 :            : 
     120                 :          1 :   i = 0;
     121                 :          1 :   hash = cmph_search_packed (buf, "foo", 3);
     122                 :          1 :   g_assert_cmpuint (hash, >=, 0);
     123                 :          1 :   g_assert_cmpuint (hash, <, size);
     124                 :          1 :   hashes[i++] = hash;
     125                 :            : 
     126                 :          1 :   hash = cmph_search_packed (buf, "bar", 3);
     127                 :          1 :   g_assert_cmpuint (hash, >=, 0);
     128                 :          1 :   g_assert_cmpuint (hash, <, size);
     129                 :          1 :   hashes[i++] = hash;
     130                 :            : 
     131                 :          1 :   hash = cmph_search_packed (buf, "baz", 3);
     132                 :          1 :   g_assert_cmpuint (hash, >=, 0);
     133                 :          1 :   g_assert_cmpuint (hash, <, size);
     134                 :          1 :   hashes[i++] = hash;
     135                 :            : 
     136                 :          1 :   assert_hashes_unique (G_N_ELEMENTS (hashes), &hashes[0]);
     137                 :            : 
     138                 :          1 :   g_free (buf);
     139                 :          1 : }
     140                 :            : 
     141                 :            : int
     142                 :          1 : main(int argc, char **argv)
     143                 :            : {
     144                 :          1 :   g_test_init (&argc, &argv, NULL);
     145                 :            : 
     146                 :          1 :   g_test_add_func ("/cmph-bdz/search", test_search);
     147                 :          1 :   g_test_add_func ("/cmph-bdz/search-packed", test_search_packed);
     148                 :            : 
     149                 :          1 :   return g_test_run ();
     150                 :            : }
     151                 :            : 

Generated by: LCOV version 1.14