annotate tests/test-hash.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /*
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 * Copyright (C) 2009-2019 Free Software Foundation, Inc.
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
3 * Written by Jim Meyering
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4 *
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
7 * the Free Software Foundation; either version 3 of the License, or
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8 * (at your option) any later version.
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9 *
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 * GNU General Public License for more details.
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 *
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
16 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18 #include <config.h>
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20 #include "hash.h"
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21 #include "hash-pjw.h"
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22 #include "inttostr.h"
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
24 #include <stdio.h>
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
25 #include <stdlib.h>
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
26 #include <stdbool.h>
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27 #include <string.h>
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
28 #include <unistd.h>
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
29
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
30 #include "macros.h"
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
31
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32 #define STREQ(a, b) (strcmp (a, b) == 0)
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
33 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
34
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35 static bool
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
36 hash_compare_strings (void const *x, void const *y)
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
37 {
11636
95fd221d763c hash: minor optimization
Eric Blake <ebb9@byu.net>
parents: 11632
diff changeset
38 ASSERT (x != y);
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
39 return STREQ (x, y) ? true : false;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
40 }
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
41
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
42 static void
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
43 hash_freer (void *x)
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
44 {
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
45 free (x);
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
46 }
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
47
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
48 static void
12197
e45d9bb2233e tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents: 11644
diff changeset
49 insert_new (Hash_table *ht, const void *ent)
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 {
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
51 void *e = hash_insert (ht, ent);
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
52 ASSERT (e == ent);
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53 }
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
54
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
55 static bool
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
56 walk (void *ent, void *data)
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
57 {
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
58 char *str = ent;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
59 unsigned int *map = data;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
60 switch (*str)
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
61 {
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
62 case 'a': *map |= 1; return true;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
63 case 'b': *map |= 2; return true;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
64 case 'c': *map |= 4; return true;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 }
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
66 *map |= 8;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
67 return false;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
68 }
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69
11643
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
70 static int
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
71 get_seed (char const *str, unsigned int *seed)
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
72 {
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
73 size_t len = strlen (str);
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
74 if (len == 0 || strspn (str, "0123456789") != len || 10 < len)
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
75 return 1;
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
76
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
77 *seed = atoi (str);
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
78 return 0;
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
79 }
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
80
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
81 int
11643
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
82 main (int argc, char **argv)
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
83 {
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
84 unsigned int i;
11644
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
85 unsigned int k;
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
86 unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
87 Hash_table *ht;
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
88 Hash_tuning tuning;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
89
11644
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
90 hash_reset_tuning (&tuning);
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
91 tuning.shrink_threshold = 0.3;
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
92 tuning.shrink_factor = 0.707;
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
93 tuning.growth_threshold = 1.5;
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
94 tuning.growth_factor = 2.0;
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
95 tuning.is_n_buckets = true;
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
96
11643
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
97 if (1 < argc)
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
98 {
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
99 unsigned int seed;
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
100 if (get_seed (argv[1], &seed) != 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
101 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
102 fprintf (stderr, "invalid seed: %s\n", argv[1]);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
103 exit (EXIT_FAILURE);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
104 }
11643
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
105
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
106 srand (seed);
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
107 }
560a14733476 tests: test-hash: allow seed selection via a command line argument
Jim Meyering <meyering@redhat.com>
parents: 11637
diff changeset
108
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
109 for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
110 {
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
111 size_t sz = table_size[i];
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
112 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
113 ASSERT (ht);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
114 insert_new (ht, "a");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
115 {
14644
157bb0cdd13a hash, mgetgroups: drop xalloc dependency
Eric Blake <eblake@redhat.com>
parents: 14079
diff changeset
116 char *str1 = strdup ("a");
157bb0cdd13a hash, mgetgroups: drop xalloc dependency
Eric Blake <eblake@redhat.com>
parents: 14079
diff changeset
117 char *str2;
157bb0cdd13a hash, mgetgroups: drop xalloc dependency
Eric Blake <eblake@redhat.com>
parents: 14079
diff changeset
118 ASSERT (str1);
157bb0cdd13a hash, mgetgroups: drop xalloc dependency
Eric Blake <eblake@redhat.com>
parents: 14079
diff changeset
119 str2 = hash_insert (ht, str1);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
120 ASSERT (str1 != str2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
121 ASSERT (STREQ (str1, str2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
122 free (str1);
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
123 }
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
124 insert_new (ht, "b");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
125 insert_new (ht, "c");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
126 i = 0;
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
127 ASSERT (hash_do_for_each (ht, walk, &i) == 3);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
128 ASSERT (i == 7);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
129 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
130 void *buf[5] = { NULL };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
131 ASSERT (hash_get_entries (ht, NULL, 0) == 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
132 ASSERT (hash_get_entries (ht, buf, 5) == 3);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
133 ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
134 }
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
135 ASSERT (hash_delete (ht, "a"));
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
136 ASSERT (hash_delete (ht, "a") == NULL);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
137 ASSERT (hash_delete (ht, "b"));
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
138 ASSERT (hash_delete (ht, "c"));
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
139
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
140 ASSERT (hash_rehash (ht, 47));
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
141 ASSERT (hash_rehash (ht, 467));
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
142
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
143 /* Free an empty table. */
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
144 hash_clear (ht);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
145 hash_free (ht);
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
146
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
147 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
148 ASSERT (ht);
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
149
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
150 insert_new (ht, "z");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
151 insert_new (ht, "y");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
152 insert_new (ht, "x");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
153 insert_new (ht, "w");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
154 insert_new (ht, "v");
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
155 insert_new (ht, "u");
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
156
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
157 hash_clear (ht);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
158 ASSERT (hash_get_n_entries (ht) == 0);
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
159 hash_free (ht);
11637
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
160
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
161 /* Test pointer hashing. */
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
162 ht = hash_initialize (sz, NULL, NULL, NULL, NULL);
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
163 ASSERT (ht);
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
164 {
14644
157bb0cdd13a hash, mgetgroups: drop xalloc dependency
Eric Blake <eblake@redhat.com>
parents: 14079
diff changeset
165 char *str = strdup ("a");
157bb0cdd13a hash, mgetgroups: drop xalloc dependency
Eric Blake <eblake@redhat.com>
parents: 14079
diff changeset
166 ASSERT (str);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
167 insert_new (ht, "a");
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
168 insert_new (ht, str);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
169 ASSERT (hash_lookup (ht, str) == str);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
170 free (str);
11637
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
171 }
0b026e877c96 hash: provide default callback functions
Eric Blake <ebb9@byu.net>
parents: 11636
diff changeset
172 hash_free (ht);
11632
5439d2ea6789 hash-tests: add a loop around the small tests
Jim Meyering <meyering@redhat.com>
parents: 11630
diff changeset
173 }
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
174
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
175 hash_reset_tuning (&tuning);
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
176 tuning.shrink_threshold = 0.3;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
177 tuning.shrink_factor = 0.707;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
178 tuning.growth_threshold = 1.5;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
179 tuning.growth_factor = 2.0;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
180 tuning.is_n_buckets = true;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
181 /* Invalid tuning. */
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
182 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
183 hash_freer);
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
184 ASSERT (!ht);
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
185
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
186 /* Alternate tuning. */
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
187 tuning.growth_threshold = 0.89;
11644
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
188
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
189 /* Run with default tuning, then with custom tuning settings. */
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
190 for (k = 0; k < 2; k++)
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
191 {
11644
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
192 Hash_tuning const *tune = (k == 0 ? NULL : &tuning);
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
193 /* Now, each entry is malloc'd. */
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
194 ht = hash_initialize (4651, tune, hash_pjw,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
195 hash_compare_strings, hash_freer);
11644
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
196 ASSERT (ht);
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
197 for (i = 0; i < 10000; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
198 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
199 unsigned int op = rand () % 10;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
200 switch (op)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
201 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
202 case 0:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
203 case 1:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
204 case 2:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
205 case 3:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
206 case 4:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
207 case 5:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
208 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
209 char buf[50];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
210 char const *p = uinttostr (i, buf);
14648
745a0964c221 test-hash.c: avoid a new shadowing warning
Jim Meyering <meyering@redhat.com>
parents: 14644
diff changeset
211 char *p_dup = strdup (p);
745a0964c221 test-hash.c: avoid a new shadowing warning
Jim Meyering <meyering@redhat.com>
parents: 14644
diff changeset
212 ASSERT (p_dup);
745a0964c221 test-hash.c: avoid a new shadowing warning
Jim Meyering <meyering@redhat.com>
parents: 14644
diff changeset
213 insert_new (ht, p_dup);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
214 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
215 break;
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
216
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
217 case 6:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
218 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
219 size_t n = hash_get_n_entries (ht);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
220 ASSERT (hash_rehash (ht, n + rand () % 20));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
221 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
222 break;
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
223
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
224 case 7:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
225 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
226 size_t n = hash_get_n_entries (ht);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
227 size_t delta = rand () % 20;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
228 if (delta < n)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
229 ASSERT (hash_rehash (ht, n - delta));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
230 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
231 break;
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
232
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
233 case 8:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
234 case 9:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
235 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
236 /* Delete a random entry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
237 size_t n = hash_get_n_entries (ht);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
238 if (n)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
239 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
240 size_t kk = rand () % n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
241 void const *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
242 void *v;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
243 for (p = hash_get_first (ht); kk;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
244 --kk, p = hash_get_next (ht, p))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
245 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
246 /* empty */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
247 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
248 ASSERT (p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
249 v = hash_delete (ht, p);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
250 ASSERT (v);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
251 free (v);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
252 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
253 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
254 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
255 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
256 ASSERT (hash_table_ok (ht));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12197
diff changeset
257 }
11644
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
258
df58e63c07a8 tests: test-hash: avoid wholesale duplication
Jim Meyering <meyering@redhat.com>
parents: 11643
diff changeset
259 hash_free (ht);
11630
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
260 }
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
261
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
262 return 0;
9e35f8067719 hash-tests: new module
Jim Meyering <jim@meyering.net>
parents:
diff changeset
263 }