Changeset a04da26a0a26ed33041c9059b44ece39727fc2a1
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r48290f
|
ra04da2
|
|
| 80 | 80 | extern int frt_icmp(const void *p1, const void *p2); |
| 81 | 81 | extern int frt_icmp_risky(const void *p1, const void *p2); |
| | 82 | extern void frt_strsort(char **string_array, int size); |
| 82 | 83 | |
| 83 | 84 | extern int frt_min2(int a, int b); |
-
|
rb39568
|
ra04da2
|
|
| 492 | 492 | register const void *key); |
| 493 | 493 | |
| 494 | | extern void frt_h_str_print_keys(FrtHash *ht); |
| | 494 | extern 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 | */ |
| | 505 | extern void frt_hash_finalize(); |
| 495 | 506 | |
| 496 | 507 | #endif |
-
|
r0c11a5
|
ra04da2
|
|
| 537 | 537 | #define h_set_safe_int frt_h_set_safe_int |
| 538 | 538 | #define h_str_print_keys frt_h_str_print_keys |
| | 539 | #define hash_finalize frt_hash_finalize |
| 539 | 540 | #define hash_ft frt_hash_ft |
| 540 | 541 | #define hlp_string_diff frt_hlp_string_diff |
| … |
… |
|
| 893 | 894 | #define str_hash frt_str_hash |
| 894 | 895 | #define strfmt frt_strfmt |
| | 896 | #define strsort frt_strsort |
| 895 | 897 | #define sym_hash frt_sym_hash |
| 896 | 898 | #define sym_len frt_sym_len |
-
|
r950230
|
ra04da2
|
|
| 1 | 1 | #include "global.h" |
| 2 | 2 | #include "symbol.h" |
| | 3 | #include "hash.h" |
| 3 | 4 | #include <stdarg.h> |
| 4 | 5 | #include <stdio.h> |
| … |
… |
|
| 43 | 44 | { |
| 44 | 45 | return strcmp(*(char **) p1, *(char **) p2); |
| | 46 | } |
| | 47 | |
| | 48 | void frt_strsort(char **str_array, int size) |
| | 49 | { |
| | 50 | qsort(str_array, size, sizeof(char *), &scmp); |
| 45 | 51 | } |
| 46 | 52 | |
| … |
… |
|
| 448 | 454 | |
| 449 | 455 | symbol_init(); |
| 450 | | } |
| | 456 | |
| | 457 | atexit(&hash_finalize); |
| | 458 | } |
-
|
rb39568
|
ra04da2
|
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | | #ifdef DEBUG |
| 246 | | free(self); |
| 247 | | #else |
| 248 | 245 | if (num_free_hts < MAX_FREE_HASH_TABLES) { |
| 249 | 246 | free_hts[num_free_hts++] = self; |
| … |
… |
|
| 252 | 249 | free(self); |
| 253 | 250 | } |
| 254 | | #endif |
| 255 | 251 | } |
| 256 | 252 | } |
| … |
… |
|
| 502 | 498 | } |
| 503 | 499 | |
| 504 | | void h_str_print_keys(Hash *self) |
| | 500 | void h_str_print_keys(Hash *self, FILE *out) |
| 505 | 501 | { |
| 506 | 502 | HashEntry *he; |
| 507 | 503 | int i = self->size; |
| 508 | | printf("keys:\n"); |
| | 504 | char **keys = ALLOC_N(char *, self->size); |
| 509 | 505 | for (he = self->table; i > 0; he++) { |
| 510 | 506 | if (he->key && he->key != dummy_key) { /* active entry */ |
| 511 | | printf("\t%s\n", (char *)he->key); |
| 512 | 507 | 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 | |
| | 519 | void hash_finalize() |
| | 520 | { |
| | 521 | while (num_free_hts > 0) { |
| | 522 | free(free_hts[--num_free_hts]); |
| | 523 | } |
| | 524 | } |
-
|
r0c11a5
|
ra04da2
|
|
| 70 | 70 | mutex_init(&store->mutex_i, NULL); |
| 71 | 71 | 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); |
| 73 | 73 | return store; |
| 74 | 74 | } |
-
|
r950230
|
ra04da2
|
|
| 3 | 3 | #include "global.h" |
| 4 | 4 | #include <stdlib.h> |
| | 5 | #include <unistd.h> |
| 5 | 6 | #include "test.h" |
| | 7 | #include "testhelper.h" |
| 6 | 8 | |
| 7 | 9 | static int *malloc_int(int val) |
| … |
… |
|
| 12 | 14 | } |
| 13 | 15 | |
| | 16 | static void mark_free(void *p) |
| | 17 | { |
| | 18 | strcpy((char *)p, "freed"); |
| | 19 | } |
| 14 | 20 | /** |
| 15 | 21 | * Basic test for string Hash. Make sure string can be retrieved |
| … |
… |
|
| 18 | 24 | { |
| 19 | 25 | Hash *h = h_new_str(NULL, &free); |
| | 26 | FILE *f; |
| | 27 | char buf[100], *t; |
| | 28 | memset(buf, 0, 100); |
| 20 | 29 | (void)data; /* suppress unused argument warning */ |
| 21 | 30 | |
| … |
… |
|
| 32 | 41 | Apnull(h_get(h, "one")); |
| 33 | 42 | 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); |
| 34 | 68 | h_destroy(h); |
| 35 | 69 | } |
| … |
… |
|
| 122 | 156 | Aiequal(1, h->size); |
| 123 | 157 | 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)); |
| 125 | 161 | Aiequal(2, h->size); |
| 126 | 162 | Atrue(h_has_key_int(h, 10)); |
| … |
… |
|
| 170 | 206 | (void)data; /* suppress unused argument warning */ |
| 171 | 207 | |
| | 208 | Aiequal(ptr_hash(word1), intern("one")); |
| | 209 | Atrue(ptr_eq(word1, intern("one"))); |
| 172 | 210 | h_set(h, word1, estrdup("1")); |
| 173 | 211 | h_set(h, word2, estrdup("2")); |
-
|
r5a8e6f
|
ra04da2
|
|
| 557 | 557 | } |
| 558 | 558 | |
| | 559 | FILE *temp_open() |
| | 560 | { |
| | 561 | return fopen("test/testdir/tmp", "w+"); |
| | 562 | } |
-
|
r7134d5
|
ra04da2
|
|
| 21 | 21 | extern bool ary_includes(int *array, int size, int val); |
| 22 | 22 | extern char *num_to_str(int num); |
| | 23 | extern FILE *temp_open(); |
| 23 | 24 | |
| 24 | 25 | #endif |