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 to exceed 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.12.0 lcov report (development 23344-f0cc1b3f6) Lines: 147 169 87.0 %
Date: 2018-12-12 05:41:43 Functions: 27 28 96.4 %
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       29893 : setlen(hashtable *h, ulong len) {
      31       29893 :   h->maxnb = (ulong)ceil(len * 0.65);
      32       29893 :   h->len  = len;
      33       29893 : }
      34             : 
      35             : static int
      36       27776 : get_prime_index(ulong len)
      37             : {
      38             :   int i;
      39       40797 :   for (i=0; i < hashprimes_len; i++)
      40       40797 :     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      874566 : hash_link2(hashtable *h, hashentry *e, ulong hash)
      48             : {
      49             :   ulong index;
      50      874566 :   e->hash = hash; index = e->hash % h->len;
      51      874566 :   e->next = h->table[index]; h->table[index] = e;
      52      874566 : }
      53             : INLINE void
      54       11862 : hash_link(hashtable *h, hashentry *e) { hash_link2(h,e,h->hash(e->key));}
      55             : 
      56             : hashtable *
      57       23205 : hash_create(ulong minsize, ulong (*hash)(void*), int (*eq)(void*,void*),
      58             :             int use_stack)
      59             : {
      60       23205 :   int i = get_prime_index(minsize);
      61       23205 :   ulong len = hashprimes[i];
      62             :   hashtable *h;
      63             : 
      64       23205 :   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        3136 :     h = (hashtable*)pari_malloc(sizeof(hashtable));
      73        3136 :     h->table = (hashentry**)pari_calloc(len * sizeof(hashentry*));
      74        3136 :     h->use_stack = 0;
      75             :   }
      76       23205 :   h->pindex = i;
      77       23205 :   h->nb = 0;
      78       23205 :   h->hash = hash;
      79       23205 :   h->eq   = eq;
      80       23205 :   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      862704 : hash_insert2(hashtable *h, void *k, void *v, ulong hash)
     102             : {
     103             :   hashentry *e;
     104             :   ulong index;
     105             : 
     106      862704 :   if (h->use_stack)
     107      854187 :     e = (hashentry*) stack_malloc(sizeof(hashentry));
     108             :   else
     109        8517 :     e = (hashentry*) pari_malloc(sizeof(hashentry));
     110             : 
     111      862704 :   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      862704 :   e->key = k;
     132      862704 :   e->val = v; hash_link2(h, e, hash);
     133      862704 : }
     134             : void
     135      310572 : hash_insert(hashtable *h, void *k, void *v)
     136      310572 : { 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        1689 : hash_values(hashtable *h)
     172             : {
     173        1689 :   long k = 1;
     174             :   ulong i;
     175        1689 :   GEN v = cgetg(h->nb+1, t_VECSMALL);
     176      327666 :   for (i = 0; i < h->len; i++)
     177             :   {
     178      325977 :     hashentry *e = h->table[i];
     179      325977 :     while (e) { v[k++] = (long)e->val; e = e->next; }
     180             :   }
     181        1689 :   return v;
     182             : }
     183             : 
     184             : /* assume hash = h->hash(k) */
     185             : hashentry *
     186     2387573 : hash_search2(hashtable *h, void *k, ulong hash)
     187             : {
     188     2387573 :   hashentry *e = h->table[ hash % h->len ];
     189     5459272 :   while (e)
     190             :   {
     191     2322248 :     if (hash == e->hash && h->eq(k, e->key)) return e;
     192      684126 :     e = e->next;
     193             :   }
     194      749451 :   return NULL; /* not found */
     195             : }
     196             : /* returns entry attached to key k or NULL */
     197             : hashentry *
     198     1158981 : hash_search(hashtable *h, void *k)
     199             : {
     200     1158981 :   if (h->nb == 0) return NULL;
     201     1157292 :   return hash_search2(h, k, h->hash(k));
     202             : }
     203             : 
     204             : int
     205       11067 : hash_haskey_long(hashtable *h, void *k, long *v)
     206             : {
     207       11067 :   hashentry * e = hash_search(h, k);
     208       11067 :   if (e) { *v = (long) e->val; return 1; }
     209        8932 :   else return 0;
     210             : }
     211             : 
     212             : GEN
     213         195 : hash_haskey_GEN(hashtable *h, void *k)
     214             : {
     215         195 :   hashentry * e = hash_search(h, k);
     216         196 :   return e ? (GEN) e->val: NULL;
     217             : }
     218             : 
     219             : hashentry *
     220        3178 : hash_remove_select(hashtable *h, void *k, void *E,
     221             :   int (*select)(void*,hashentry*))
     222             : {
     223        3178 :   ulong hash = h->hash(k), index = hash % h->len;
     224        3178 :   hashentry **pE = &(h->table[index]), *e = *pE;
     225        6356 :   while (e)
     226             :   {
     227        3178 :     if (hash == e->hash && h->eq(k, e->key) && select(E,e)) {
     228        3178 :       *pE = e->next; h->nb--;
     229        3178 :       return e;
     230             :     }
     231           0 :     pE = &(e->next);
     232           0 :     e = e->next;
     233             :   }
     234           0 :   return NULL;
     235             : }
     236             : 
     237             : hashentry *
     238           2 : hash_remove(hashtable *h, void *k)
     239             : {
     240           2 :   ulong hash = h->hash(k), index = hash % h->len;
     241           2 :   hashentry **pE = &(h->table[index]), *e = *pE;
     242           4 :   while (e)
     243             :   {
     244           2 :     if (hash == e->hash && h->eq(k, e->key)) {
     245           2 :       *pE = e->next; h->nb--;
     246           2 :       return e;
     247             :     }
     248           0 :     pE = &(e->next);
     249           0 :     e = e->next;
     250             :   }
     251           0 :   return NULL;
     252             : }
     253             : void
     254        3098 : hash_destroy(hashtable *h)
     255             : {
     256             :   ulong i;
     257        3098 :   if (h->use_stack) return;
     258      384152 :   for (i = 0; i < h->len; i++)
     259             :   {
     260      381054 :     hashentry *e = h->table[i];
     261      381054 :     while (e) { hashentry *f = e; e = e->next; pari_free(f); }
     262             :   }
     263        3098 :   pari_free(h->table); pari_free(h);
     264             : }
     265             : static ulong
     266        5446 : hash_id(void *x) { return (ulong)x; }
     267             : static int
     268        4627 : eq_id(void *x, void *y) { return x == y; }
     269             : hashtable *
     270         854 : hash_create_ulong(ulong s, long stack)
     271         854 : { return hash_create(s, &hash_id, &eq_id, stack); }
     272             : 
     273             : static
     274       13978 : int strequal(void *a, void *b) { return !strcmp((char*)a,(char*)b); }
     275             : hashtable *
     276        3136 : hash_create_str(ulong s, long stack)
     277        3136 : { return hash_create(s, (ulong (*)(void *))&hash_str, strequal, stack); }
     278             : 
     279             : hashtable *
     280          18 : hashstr_import_static(hashentry *e, ulong size)
     281             : {
     282          18 :   hashtable *h = hash_create_str(size, 0);
     283          18 :   for ( ; e->key; e++) { hash_link(h, e); h->nb++; }
     284          18 :   return h;
     285             : }
     286             : 
     287             : void
     288           0 : hash_dbg(hashtable *h)
     289             : {
     290           0 :   ulong n, Total = 0, Max = 0;
     291           0 :   hashentry *e, **table = h->table;
     292           0 :   for (n=0; n < h->len; n++)
     293             :   {
     294           0 :     ulong m=0;
     295           0 :     for (e=table[n]; e; e=e->next) m++;
     296           0 :     Total += m; if (Max < m) Max = m;
     297           0 :     pari_printf("%4ld:%2ld ",n,m);
     298           0 :     if (n%9 == 8) pari_putc('\n');
     299             :   }
     300           0 :   pari_printf("\nTotal = %ld, Max = %ld\n", Total, Max);
     301           0 : }
     302             : 
     303             : /********************************************************************/
     304             : /*                                                                  */
     305             : /*                          HASH FUNCTIONS                          */
     306             : /*                                                                  */
     307             : /********************************************************************/
     308             : 
     309             : INLINE ulong
     310   606597723 : glue(ulong h, ulong a) { return 404936533*h + a; }
     311             : ulong
     312   190977783 : hash_GEN(GEN x)
     313             : {
     314   190977783 :   ulong h = x[0] & ~CLONEBIT;
     315   190977783 :   long tx = typ(x), lx, i;
     316   190977783 :   switch(tx)
     317             :   { /* non recursive types */
     318             :     case t_INT:
     319    96822059 :       lx = lgefint(x);
     320    96822059 :       h &= TYPBITS;
     321    96822059 :       for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
     322    96822059 :       return h;
     323             :     case t_REAL:
     324             :     case t_STR:
     325             :     case t_VECSMALL:
     326    58361033 :       lx = lg(x);
     327    58361033 :       for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
     328    58361033 :       return h;
     329             :     /* one more special case */
     330             :     case t_LIST:
     331           0 :       x = list_data(x);
     332           0 :       if (!x) return h;
     333             :       /* fall through */
     334             :     default:
     335    35794691 :       if (lontyp[tx] == 2) { h = glue(h, x[1]); i = 2; } else i = 1;
     336    35794691 :       lx = lg(x);
     337    35794691 :       for (; i < lx; i++) h = glue(h, hash_GEN(gel(x,i)));
     338    35794691 :       return h;
     339             :   }
     340             : }

Generated by: LCOV version 1.13