Changeset a04da26a0a26ed33041c9059b44ece39727fc2a1

Show
Ignore:
Timestamp:
04/25/08 11:12:30 (9 months ago)
Author:
dave <dave@…>
Parents:
0c11a534a1b97b7addf59c733a31bcbe1120fcc9
Children:
afa46936e67e892eb5c8429bd0991a4630cbd71f, ea6c371194730654bebe8dce00285a87789ce06a
git-committer:
dave <dave@06fd6eb0-0002-0410-a719-e5602cce40bc> / 2008-04-25T01:12:30Z+0000
Message:

hash.c test-coverage is 100%

Added a few extra tests to bring test coverage up to 100%

Also added FILE *temp_open(void) to testhelper.h which creates a read-write
file for testing that will be ignored by git.

git-svn-id: svn+ssh://davebalmain.com/home/dave/repos/ferret/trunk@1044 06fd6eb0-0002-0410-a719-e5602cce40bc

Location:
c
Files:
1 added
9 modified

Legend:

Unmodified
Added
Removed
  • c/include/global.h

    r48290f ra04da2  
    8080extern int frt_icmp(const void *p1, const void *p2); 
    8181extern int frt_icmp_risky(const void *p1, const void *p2); 
     82extern void frt_strsort(char **string_array, int size); 
    8283 
    8384extern int frt_min2(int a, int b); 
  • c/include/hash.h

    rb39568 ra04da2  
    492492                                            register const void *key); 
    493493 
    494 extern void frt_h_str_print_keys(FrtHash *ht); 
     494extern void frt_h_str_print_keys(FrtHash *ht, FILE *out); 
     495 
     496/** 
     497 * The Hash implementation actually keeps a buffer of old hash tables around 
     498 * for performance reasons. If you want all memory freed when your program 
     499 * finishes (useful if you are using valgrind) you should call this method on 
     500 * exit. 
     501 * 
     502 * One way to do this is to register it with atexit(). This is done for you 
     503 * when you call +frt_init+. 
     504 */ 
     505extern void frt_hash_finalize(); 
    495506 
    496507#endif 
  • c/include/internal.h

    r0c11a5 ra04da2  
    537537#define h_set_safe_int                          frt_h_set_safe_int 
    538538#define h_str_print_keys                        frt_h_str_print_keys 
     539#define hash_finalize                           frt_hash_finalize 
    539540#define hash_ft                                 frt_hash_ft 
    540541#define hlp_string_diff                         frt_hlp_string_diff 
     
    893894#define str_hash                                frt_str_hash 
    894895#define strfmt                                  frt_strfmt 
     896#define strsort                                 frt_strsort 
    895897#define sym_hash                                frt_sym_hash 
    896898#define sym_len                                 frt_sym_len 
  • c/src/global.c

    r950230 ra04da2  
    11#include "global.h" 
    22#include "symbol.h" 
     3#include "hash.h" 
    34#include <stdarg.h> 
    45#include <stdio.h> 
     
    4344{ 
    4445    return strcmp(*(char **) p1, *(char **) p2); 
     46} 
     47 
     48void frt_strsort(char **str_array, int size) 
     49{ 
     50    qsort(str_array, size, sizeof(char *), &scmp); 
    4551} 
    4652 
     
    448454 
    449455    symbol_init(); 
    450 } 
     456 
     457    atexit(&hash_finalize); 
     458} 
  • c/src/hash.c

    rb39568 ra04da2  
    243243        } 
    244244 
    245 #ifdef DEBUG 
    246         free(self); 
    247 #else 
    248245        if (num_free_hts < MAX_FREE_HASH_TABLES) { 
    249246            free_hts[num_free_hts++] = self; 
     
    252249            free(self); 
    253250        } 
    254 #endif 
    255251    } 
    256252} 
     
    502498} 
    503499 
    504 void h_str_print_keys(Hash *self) 
     500void h_str_print_keys(Hash *self, FILE *out) 
    505501{ 
    506502    HashEntry *he; 
    507503    int i = self->size; 
    508     printf("keys:\n"); 
     504    char **keys = ALLOC_N(char *, self->size); 
    509505    for (he = self->table; i > 0; he++) { 
    510506        if (he->key && he->key != dummy_key) {        /* active entry */ 
    511             printf("\t%s\n", (char *)he->key); 
    512507            i--; 
    513         } 
    514     } 
    515 } 
     508            keys[i] = (char *)he->key; 
     509        } 
     510    } 
     511    strsort(keys, self->size); 
     512    fprintf(out, "keys:\n"); 
     513    for (i = 0; i < self->size; i++) { 
     514        fprintf(out, "\t%s\n", keys[i]); 
     515    } 
     516    free(keys); 
     517} 
     518 
     519void hash_finalize() 
     520{ 
     521    while (num_free_hts > 0) { 
     522        free(free_hts[--num_free_hts]); 
     523    } 
     524} 
  • c/src/store.c

    r0c11a5 ra04da2  
    7070    mutex_init(&store->mutex_i, NULL); 
    7171    mutex_init(&store->mutex, NULL); 
    72     store->locks = hs_new(ptr_hash, ptr_eq, (free_ft)&close_lock_i); 
     72    store->locks = hs_new_ptr((free_ft)&close_lock_i); 
    7373    return store; 
    7474} 
  • c/test/test_hash.c

    r950230 ra04da2  
    33#include "global.h" 
    44#include <stdlib.h> 
     5#include <unistd.h> 
    56#include "test.h" 
     7#include "testhelper.h" 
    68 
    79static int *malloc_int(int val) 
     
    1214} 
    1315 
     16static void mark_free(void *p) 
     17{ 
     18    strcpy((char *)p, "freed"); 
     19} 
    1420/** 
    1521 * Basic test for string Hash. Make sure string can be retrieved 
     
    1824{ 
    1925    Hash *h = h_new_str(NULL, &free); 
     26    FILE *f; 
     27    char buf[100], *t; 
     28    memset(buf, 0, 100); 
    2029    (void)data; /* suppress unused argument warning */ 
    2130 
     
    3241    Apnull(h_get(h, "one")); 
    3342    Atrue(h_has_key(h, "one")); 
     43    h_set(h, "two", malloc_int(2)); 
     44    h_set(h, "three", malloc_int(3)); 
     45    h_set(h, "four", malloc_int(4)); 
     46 
     47    f = temp_open(); 
     48    h_str_print_keys(h, f); 
     49    fseek(f, 0, SEEK_SET); 
     50    fread(buf, 1, 100, f); 
     51    fclose(f); 
     52    Asequal("keys:\n\tfour\n\tone\n\tthree\n\ttwo\n", buf); 
     53    h_destroy(h); 
     54 
     55    /* test h_rem with allocated key */ 
     56    strcpy(buf, "key"); 
     57    h_new_str(&mark_free, (free_ft)NULL); 
     58    h_set(h, buf, "val"); 
     59    Asequal("val", h_get(h, "key")); 
     60    t = (char *)h_rem(h, "key", false); 
     61    Asequal("val", t); 
     62    Asequal("key", buf); 
     63    h_set(h, buf, "new val"); 
     64    Asequal("new val", h_get(h, "key")); 
     65    t = (char *)h_rem(h, "key", true); 
     66    Asequal("new val", t); 
     67    Asequal("freed", buf); 
    3468    h_destroy(h); 
    3569} 
     
    122156    Aiequal(1, h->size); 
    123157    Atrue(h_has_key_int(h, 0)); 
    124     h_set_int(h, 10, estrdup("ten")); 
     158    Assert(h_set_safe_int(h, 10, estrdup("ten")), "Not existing"); 
     159    Assert(!h_set_safe_int(h, 10, "10"), "Won't overwrite existing"); 
     160    Asequal("ten", h_get_int(h, 10)); 
    125161    Aiequal(2, h->size); 
    126162    Atrue(h_has_key_int(h, 10)); 
     
    170206    (void)data; /* suppress unused argument warning */ 
    171207 
     208    Aiequal(ptr_hash(word1), intern("one")); 
     209    Atrue(ptr_eq(word1, intern("one"))); 
    172210    h_set(h, word1, estrdup("1")); 
    173211    h_set(h, word2, estrdup("2")); 
  • c/test/testhelper.c

    r5a8e6f ra04da2  
    557557} 
    558558 
     559FILE *temp_open() 
     560{ 
     561    return fopen("test/testdir/tmp", "w+"); 
     562} 
  • c/test/testhelper.h

    r7134d5 ra04da2  
    2121extern bool ary_includes(int *array, int size, int val); 
    2222extern char *num_to_str(int num); 
     23extern FILE *temp_open(); 
    2324 
    2425#endif