Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_ghash_test.cc « blenlib « gtests « tests - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fcc0512cb9e42484eaf50eb575e50bac90e793cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Apache License, Version 2.0 */

#include "testing/testing.h"

#define GHASH_INTERNAL_API

#include "BLI_ghash.h"
#include "BLI_rand.h"
#include "BLI_utildefines.h"

#define TESTCASE_SIZE 10000

/* Only keeping this in case here, for now. */
#define PRINTF_GHASH_STATS(_gh) \
  { \
    double q, lf, var, pempty, poverloaded; \
    int bigb; \
    q = BLI_ghash_calc_quality_ex((_gh), &lf, &var, &pempty, &poverloaded, &bigb); \
    printf( \
        "GHash stats (%d entries):\n\t" \
        "Quality (the lower the better): %f\n\tVariance (the lower the better): %f\n\tLoad: " \
        "%f\n\t" \
        "Empty buckets: %.2f%%\n\tOverloaded buckets: %.2f%% (biggest bucket: %d)\n", \
        BLI_ghash_len(_gh), \
        q, \
        var, \
        lf, \
        pempty * 100.0, \
        poverloaded * 100.0, \
        bigb); \
  } \
  void(0)

/* Note: for pure-ghash testing, nature of the keys and data have absolutely no importance! So here
 * we just use mere random integers stored in pointers. */

static void init_keys(unsigned int keys[TESTCASE_SIZE], const int seed)
{
  RNG *rng = BLI_rng_new(seed);
  unsigned int *k;
  int i;

  for (i = 0, k = keys; i < TESTCASE_SIZE;) {
    /* Risks of collision are low, but they do exist.
     * And we cannot use a GSet, since we test that here! */
    int j, t = BLI_rng_get_uint(rng);
    for (j = i; j--;) {
      if (keys[j] == t) {
        continue;
      }
    }
    *k = t;
    i++;
    k++;
  }
  BLI_rng_free(rng);
}

/* Here we simply insert and then lookup all keys, ensuring we do get back the expected stored
 * 'data'. */
TEST(ghash, InsertLookup)
{
  GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  unsigned int keys[TESTCASE_SIZE], *k;
  int i;

  init_keys(keys, 0);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    BLI_ghash_insert(ghash, POINTER_FROM_UINT(*k), POINTER_FROM_UINT(*k));
  }

  EXPECT_EQ(BLI_ghash_len(ghash), TESTCASE_SIZE);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    void *v = BLI_ghash_lookup(ghash, POINTER_FROM_UINT(*k));
    EXPECT_EQ(POINTER_AS_UINT(v), *k);
  }

  BLI_ghash_free(ghash, NULL, NULL);
}

/* Here we simply insert and then remove all keys, ensuring we do get an empty, unshrinked ghash.
 */
TEST(ghash, InsertRemove)
{
  GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  unsigned int keys[TESTCASE_SIZE], *k;
  int i, bkt_size;

  init_keys(keys, 10);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    BLI_ghash_insert(ghash, POINTER_FROM_UINT(*k), POINTER_FROM_UINT(*k));
  }

  EXPECT_EQ(BLI_ghash_len(ghash), TESTCASE_SIZE);
  bkt_size = BLI_ghash_buckets_len(ghash);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    void *v = BLI_ghash_popkey(ghash, POINTER_FROM_UINT(*k), NULL);
    EXPECT_EQ(POINTER_AS_UINT(v), *k);
  }

  EXPECT_EQ(BLI_ghash_len(ghash), 0);
  EXPECT_EQ(BLI_ghash_buckets_len(ghash), bkt_size);

  BLI_ghash_free(ghash, NULL, NULL);
}

/* Same as above, but this time we allow ghash to shrink. */
TEST(ghash, InsertRemoveShrink)
{
  GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  unsigned int keys[TESTCASE_SIZE], *k;
  int i, bkt_size;

  BLI_ghash_flag_set(ghash, GHASH_FLAG_ALLOW_SHRINK);
  init_keys(keys, 20);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    BLI_ghash_insert(ghash, POINTER_FROM_UINT(*k), POINTER_FROM_UINT(*k));
  }

  EXPECT_EQ(BLI_ghash_len(ghash), TESTCASE_SIZE);
  bkt_size = BLI_ghash_buckets_len(ghash);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    void *v = BLI_ghash_popkey(ghash, POINTER_FROM_UINT(*k), NULL);
    EXPECT_EQ(POINTER_AS_UINT(v), *k);
  }

  EXPECT_EQ(BLI_ghash_len(ghash), 0);
  EXPECT_LT(BLI_ghash_buckets_len(ghash), bkt_size);

  BLI_ghash_free(ghash, NULL, NULL);
}

/* Check copy. */
TEST(ghash, Copy)
{
  GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  GHash *ghash_copy;
  unsigned int keys[TESTCASE_SIZE], *k;
  int i;

  init_keys(keys, 30);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    BLI_ghash_insert(ghash, POINTER_FROM_UINT(*k), POINTER_FROM_UINT(*k));
  }

  EXPECT_EQ(BLI_ghash_len(ghash), TESTCASE_SIZE);

  ghash_copy = BLI_ghash_copy(ghash, NULL, NULL);

  EXPECT_EQ(BLI_ghash_len(ghash_copy), TESTCASE_SIZE);
  EXPECT_EQ(BLI_ghash_buckets_len(ghash_copy), BLI_ghash_buckets_len(ghash));

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    void *v = BLI_ghash_lookup(ghash_copy, POINTER_FROM_UINT(*k));
    EXPECT_EQ(POINTER_AS_UINT(v), *k);
  }

  BLI_ghash_free(ghash, NULL, NULL);
  BLI_ghash_free(ghash_copy, NULL, NULL);
}

/* Check pop. */
TEST(ghash, Pop)
{
  GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  unsigned int keys[TESTCASE_SIZE], *k;
  int i;

  BLI_ghash_flag_set(ghash, GHASH_FLAG_ALLOW_SHRINK);
  init_keys(keys, 30);

  for (i = TESTCASE_SIZE, k = keys; i--; k++) {
    BLI_ghash_insert(ghash, POINTER_FROM_UINT(*k), POINTER_FROM_UINT(*k));
  }

  EXPECT_EQ(BLI_ghash_len(ghash), TESTCASE_SIZE);

  GHashIterState pop_state = {0};

  for (i = TESTCASE_SIZE / 2; i--;) {
    void *k, *v;
    bool success = BLI_ghash_pop(ghash, &pop_state, &k, &v);
    EXPECT_EQ(k, v);
    EXPECT_TRUE(success);

    if (i % 2) {
      BLI_ghash_insert(ghash, POINTER_FROM_UINT(i * 4), POINTER_FROM_UINT(i * 4));
    }
  }

  EXPECT_EQ(BLI_ghash_len(ghash), (TESTCASE_SIZE - TESTCASE_SIZE / 2 + TESTCASE_SIZE / 4));

  {
    void *k, *v;
    while (BLI_ghash_pop(ghash, &pop_state, &k, &v)) {
      EXPECT_EQ(k, v);
    }
  }
  EXPECT_EQ(BLI_ghash_len(ghash), 0);

  BLI_ghash_free(ghash, NULL, NULL);
}