Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - language - hash.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.11.0 lcov report (development 22851-e834f1b2f) Lines: 142 175 81.1 %
Date: 2018-07-16 05:36:59 Functions: 26 29 89.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : /********************************************************************/
      17             : /*                                                                  */
      18             : /*                    GENERAL HASHTABLES                            */
      19             : /*                                                                  */
      20             : /********************************************************************/
      21             : /* http://planetmath.org/encyclopedia/GoodHashTablePrimes.html */
      22             : static const ulong hashprimes[] = {
      23             :   53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613,
      24             :   393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653,
      25             :   100663319, 201326611, 402653189, 805306457, 1610612741
      26             : };
      27             : static const int hashprimes_len = numberof(hashprimes);
      28             : 
      29             : INLINE void
      30       28320 : setlen(hashtable *h, ulong len) {
      31       28320 :   h->maxnb = (ulong)ceil(len * 0.65);
      32       28320 :   h->len  = len;
      33       28320 : }
      34             : 
      35             : static int
      36       26203 : get_prime_index(ulong len)
      37             : {
      38             :   int i;
      39       39196 :   for (i=0; i < hashprimes_len; i++)
      40       39196 :     if (hashprimes[i] > len) return i;
      41           0 :   pari_err_OVERFLOW("hash table [too large]");
      42             :   return -1; /* LCOV_EXCL_LINE */
      43             : }
      44             : 
      45             : /* link hashentry e to hashtable h, setting e->hash / e->next */
      46             : INLINE void
      47      874473 : hash_link2(hashtable *h, hashentry *e, ulong hash)
      48             : {
      49             :   ulong index;
      50      874473 :   e->hash = hash; index = e->hash % h->len;
      51      874473 :   e->next = h->table[index]; h->table[index] = e;
      52      874473 : }
      53             : INLINE void
      54       11862 : hash_link(hashtable *h, hashentry *e) { hash_link2(h,e,h->hash(e->key));}
      55             : 
      56             : hashtable *
      57       21632 : hash_create(ulong minsize, ulong (*hash)(void*), int (*eq)(void*,void*),
      58             :             int use_stack)
      59             : {
      60       21632 :   int i = get_prime_index(minsize);
      61       21632 :   ulong len = hashprimes[i];
      62             :   hashtable *h;
      63             : 
      64       21632 :   if (use_stack)
      65             :   {
      66       20069 :     h = (hashtable*)stack_malloc(sizeof(hashtable));
      67       20069 :     h->table = (hashentry**)stack_calloc(len * sizeof(hashentry*));
      68       20069 :     h->use_stack = 1;
      69             :   }
      70             :   else
      71             :   {
      72        1563 :     h = (hashtable*)pari_malloc(sizeof(hashtable));
      73        1563 :     h->table = (hashentry**)pari_calloc(len * sizeof(hashentry*));
      74        1563 :     h->use_stack = 0;
      75             :   }
      76       21632 :   h->pindex = i;
      77       21632 :   h->nb = 0;
      78       21632 :   h->hash = hash;
      79       21632 :   h->eq   = eq;
      80       21632 :   setlen(h, len); return h;
      81             : }
      82             : 
      83             : void
      84        4571 : hash_init_GEN(hashtable *h, ulong minsize, int (*eq)(GEN,GEN), int use_stack)
      85             : {
      86        4571 :   int i = get_prime_index(minsize);
      87        4571 :   ulong len = hashprimes[i];
      88        4571 :   if (use_stack)
      89        4571 :     h->table = (hashentry**)stack_calloc(len * sizeof(hashentry*));
      90             :   else
      91           0 :     h->table = (hashentry**)pari_calloc(len * sizeof(hashentry*));
      92        4571 :   h->use_stack = use_stack;
      93        4571 :   h->pindex = i;
      94        4571 :   h->nb = 0;
      95        4571 :   h->hash = (ulong (*)(void*)) hash_GEN;
      96        4571 :   h->eq   = (int (*)(void*,void*)) eq;
      97        4571 :   setlen(h, len);
      98        4571 : }
      99             : 
     100             : void
     101      862611 : hash_insert2(hashtable *h, void *k, void *v, ulong hash)
     102             : {
     103             :   hashentry *e;
     104             :   ulong index;
     105             : 
     106      862611 :   if (h->use_stack)
     107      854194 :     e = (hashentry*) stack_malloc(sizeof(hashentry));
     108             :   else
     109        8417 :     e = (hashentry*) pari_malloc(sizeof(hashentry));
     110             : 
     111      862611 :   if (++(h->nb) > h->maxnb && h->pindex < hashprimes_len-1)
     112             :   { /* double table size */
     113        2117 :     ulong i, newlen = hashprimes[++(h->pindex)];
     114             :     hashentry *E, **newtable;
     115        2117 :     if (h->use_stack)
     116        2117 :       newtable = (hashentry**)stack_calloc(newlen*sizeof(hashentry*));
     117             :     else
     118           0 :       newtable = (hashentry**)pari_calloc(newlen*sizeof(hashentry*));
     119      773658 :     for (i = 0; i < h->len; i++)
     120     2045638 :       while ( (E = h->table[i]) )
     121             :       {
     122      502556 :         h->table[i] = E->next;
     123      502556 :         index = E->hash % newlen;
     124      502556 :         E->next = newtable[index];
     125      502556 :         newtable[index] = E;
     126             :       }
     127        2117 :     if (!h->use_stack) pari_free(h->table);
     128        2117 :     h->table = newtable;
     129        2117 :     setlen(h, newlen);
     130             :   }
     131      862611 :   e->key = k;
     132      862611 :   e->val = v; hash_link2(h, e, hash);
     133      862611 : }
     134             : void
     135      310472 : hash_insert(hashtable *h, void *k, void *v)
     136      310472 : { hash_insert2(h,k,v,h->hash(k)); }
     137             : 
     138             : void
     139       15393 : hash_insert_long(hashtable *h, void *k, long v)
     140       15393 : { hash_insert2(h,k,(void*)v,h->hash(k)); }
     141             : 
     142             : /* the key 'k' may correspond to different values in the hash, return
     143             :  * one satisfying the selection callback */
     144             : hashentry *
     145          70 : hash_select(hashtable *h, void *k, void *E,int(*select)(void *,hashentry *))
     146             : {
     147          70 :   ulong hash = h->hash(k);
     148          70 :   hashentry *e = h->table[ hash % h->len ];
     149         210 :   while (e)
     150             :   {
     151          91 :     if (hash == e->hash && h->eq(k, e->key) && select(E,e)) return e;
     152          70 :     e = e->next;
     153             :   }
     154          49 :   return NULL;
     155             : }
     156             : 
     157             : GEN
     158        1043 : hash_keys(hashtable *h)
     159             : {
     160        1043 :   long k = 1;
     161             :   ulong i;
     162        1043 :   GEN v = cgetg(h->nb+1, t_VECSMALL);
     163      193522 :   for (i = 0; i < h->len; i++)
     164             :   {
     165      192479 :     hashentry *e = h->table[i];
     166      192479 :     while (e) { v[k++] = (long)e->key; e = e->next; }
     167             :   }
     168        1043 :   return v;
     169             : }
     170             : GEN
     171         140 : hash_values(hashtable *h)
     172             : {
     173         140 :   long k = 1;
     174             :   ulong i;
     175         140 :   GEN v = cgetg(h->nb+1, t_VECSMALL);
     176       27160 :   for (i = 0; i < h->len; i++)
     177             :   {
     178       27020 :     hashentry *e = h->table[i];
     179       27020 :     while (e) { v[k++] = (long)e->val; e = e->next; }
     180             :   }
     181         140 :   return v;
     182             : }
     183             : 
     184             : /* assume hash = h->hash(k) */
     185             : hashentry *
     186     2387361 : hash_search2(hashtable *h, void *k, ulong hash)
     187             : {
     188     2387361 :   hashentry *e = h->table[ hash % h->len ];
     189     5458851 :   while (e)
     190             :   {
     191     2322046 :     if (hash == e->hash && h->eq(k, e->key)) return e;
     192      684129 :     e = e->next;
     193             :   }
     194      749444 :   return NULL; /* not found */
     195             : }
     196             : /* returns entry attached to key k or NULL */
     197             : hashentry *
     198     1158770 : hash_search(hashtable *h, void *k)
     199             : {
     200     1158770 :   if (h->nb == 0) return NULL;
     201     1157083 :   return hash_search2(h, k, h->hash(k));
     202             : }
     203             : 
     204             : int
     205       11053 : hash_haskey_long(hashtable *h, void *k, long *v)
     206             : {
     207       11053 :   hashentry * e = hash_search(h, k);
     208       11053 :   if (e) { *v = (long) e->val; return 1; }
     209        8918 :   else return 0;
     210             : }
     211             : 
     212             : hashentry *
     213        3143 : hash_remove_select(hashtable *h, void *k, void *E,
     214             :   int (*select)(void*,hashentry*))
     215             : {
     216        3143 :   ulong hash = h->hash(k), index = hash % h->len;
     217        3143 :   hashentry **pE = &(h->table[index]), *e = *pE;
     218        6286 :   while (e)
     219             :   {
     220        3143 :     if (hash == e->hash && h->eq(k, e->key) && select(E,e)) {
     221        3143 :       *pE = e->next; h->nb--;
     222        3143 :       return e;
     223             :     }
     224           0 :     pE = &(e->next);
     225           0 :     e = e->next;
     226             :   }
     227           0 :   return NULL;
     228             : }
     229             : 
     230             : hashentry *
     231           0 : hash_remove(hashtable *h, void *k)
     232             : {
     233           0 :   ulong hash = h->hash(k), index = hash % h->len;
     234           0 :   hashentry **pE = &(h->table[index]), *e = *pE;
     235           0 :   while (e)
     236             :   {
     237           0 :     if (hash == e->hash && h->eq(k, e->key)) {
     238           0 :       *pE = e->next; h->nb--;
     239           0 :       return e;
     240             :     }
     241           0 :     pE = &(e->next);
     242           0 :     e = e->next;
     243             :   }
     244           0 :   return NULL;
     245             : }
     246             : void
     247        1595 : hash_destroy(hashtable *h)
     248             : {
     249             :   ulong i;
     250        1595 :   if (h->use_stack) return;
     251      309430 :   for (i = 0; i < h->len; i++)
     252             :   {
     253      307835 :     hashentry *e = h->table[i];
     254      307835 :     while (e) { hashentry *f = e; e = e->next; pari_free(f); }
     255             :   }
     256        1595 :   pari_free(h->table); pari_free(h);
     257             : }
     258             : static ulong
     259        5446 : hash_id(void *x) { return (ulong)x; }
     260             : static int
     261        4627 : eq_id(void *x, void *y) { return x == y; }
     262             : hashtable *
     263         854 : hash_create_ulong(ulong s, long stack)
     264         854 : { return hash_create(s, &hash_id, &eq_id, stack); }
     265             : 
     266             : static
     267       13745 : int strequal(void *a, void *b) { return !strcmp((char*)a,(char*)b); }
     268             : hashtable *
     269        1563 : hash_create_str(ulong s, long stack)
     270        1563 : { return hash_create(s, (ulong (*)(void *))&hash_str, strequal, stack); }
     271             : 
     272             : hashtable *
     273          18 : hashstr_import_static(hashentry *e, ulong size)
     274             : {
     275          18 :   hashtable *h = hash_create_str(size, 0);
     276          18 :   for ( ; e->key; e++) { hash_link(h, e); h->nb++; }
     277          18 :   return h;
     278             : }
     279             : 
     280             : void
     281           0 : hash_dbg(hashtable *h)
     282             : {
     283           0 :   ulong n, Total = 0, Max = 0;
     284           0 :   hashentry *e, **table = h->table;
     285           0 :   for (n=0; n < h->len; n++)
     286             :   {
     287           0 :     ulong m=0;
     288           0 :     for (e=table[n]; e; e=e->next) m++;
     289           0 :     Total += m; if (Max < m) Max = m;
     290           0 :     pari_printf("%4ld:%2ld ",n,m);
     291           0 :     if (n%9 == 8) pari_putc('\n');
     292             :   }
     293           0 :   pari_printf("\nTotal = %ld, Max = %ld\n", Total, Max);
     294           0 : }
     295             : 
     296             : /********************************************************************/
     297             : /*                                                                  */
     298             : /*                          HASH FUNCTIONS                          */
     299             : /*                                                                  */
     300             : /********************************************************************/
     301             : 
     302             : INLINE ulong
     303   611916944 : glue(ulong h, ulong a) { return 404936533*h + a; }
     304             : ulong
     305   191133198 : hash_GEN(GEN x)
     306             : {
     307   191133198 :   ulong h = x[0];
     308   191133198 :   long tx = typ(x), lx, i;
     309   191133198 :   switch(tx)
     310             :   { /* non recursive types */
     311             :     case t_INT:
     312    96215492 :       lx = lgefint(x);
     313    96215492 :       h &= TYPBITS;
     314    96215492 :       for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
     315    96215492 :       return h;
     316             :     case t_REAL:
     317             :     case t_STR:
     318             :     case t_VECSMALL:
     319    59123155 :       lx = lg(x);
     320    59123155 :       for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
     321    59123155 :       return h;
     322             :     /* one more special case */
     323             :     case t_LIST:
     324           0 :       x = list_data(x);
     325           0 :       if (!x) return h;
     326             :       /* fall through */
     327             :     default:
     328    35794551 :       if (lontyp[tx] == 2) { h = glue(h, x[1]); i = 2; } else i = 1;
     329    35794551 :       lx = lg(x);
     330    35794551 :       for (; i < lx; i++) h = glue(h, hash_GEN(gel(x,i)));
     331    35794551 :       return h;
     332             :   }
     333             : }
     334             : 
     335             : /* djb's hash */
     336             : ulong
     337       34003 : hash_str(const char *str)
     338             : {
     339       34003 :   ulong hash = 5381, c;
     340      250362 :   while ( (c = (ulong)*str++) )
     341      182356 :     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
     342       34003 :   return hash;
     343             : }
     344             : 
     345             : /* hashvalue's underlying hash function */
     346             : ulong
     347           0 : hash_str2(const char *s)
     348             : {
     349           0 :   ulong n = 0, c;
     350           0 :   while ( (c = (ulong)*s++) ) n = (n<<1) ^ c;
     351           0 :   return n;
     352             : }

Generated by: LCOV version 1.13