summaryrefslogtreecommitdiff
path: root/tests/test-bloom.c
blob: ea522ce07b8155bc826d20db2927386ff75f5571 (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
/*
 * Copyright © 2017 Endless Mobile, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors:
 *  - Philip Withnall <withnall@endlessm.com>
 */

#include "config.h"

#include <gio/gio.h>
#include <glib.h>

#include "ostree-bloom-private.h"

/* Test the two different constructors work at a basic level. */
static void
test_bloom_init (void)
{
  g_autoptr(OstreeBloom) bloom = NULL;
  g_autoptr(GBytes) bytes = NULL;

  bloom = ostree_bloom_new (1, 1, ostree_str_bloom_hash);
  g_assert_cmpuint (ostree_bloom_get_size (bloom), ==, 1);
  g_assert_cmpuint (ostree_bloom_get_k (bloom), ==, 1);
  g_assert (ostree_bloom_get_hash_func (bloom) == ostree_str_bloom_hash);
  g_clear_pointer (&bloom, ostree_bloom_unref);

  bytes = g_bytes_new_take (g_malloc0 (4), 4);
  bloom = ostree_bloom_new_from_bytes (bytes, 1, ostree_str_bloom_hash);
  g_assert_cmpuint (ostree_bloom_get_size (bloom), ==, 4);
  g_assert_cmpuint (ostree_bloom_get_k (bloom), ==, 1);
  g_assert (ostree_bloom_get_hash_func (bloom) == ostree_str_bloom_hash);
  g_clear_pointer (&bloom, ostree_bloom_unref);
}

/* Test that building a bloom filter, marshalling it through GBytes, and loading
 * it again, gives the same element membership. */
static void
test_bloom_construction (void)
{
  g_autoptr(OstreeBloom) bloom = NULL;
  g_autoptr(OstreeBloom) immutable_bloom = NULL;
  g_autoptr(GBytes) bytes = NULL;
  gsize i;
  const gchar *members[] =
    {
      "hello", "there", "these", "are", "test", "strings"
    };
  const gchar *non_members[] =
    {
      "not", "an", "element"
    };
  const gsize n_bytes = 256;
  const guint8 k = 8;
  const OstreeBloomHashFunc hash = ostree_str_bloom_hash;

  /* Build a bloom filter. */
  bloom = ostree_bloom_new (n_bytes, k, hash);

  for (i = 0; i < G_N_ELEMENTS (members); i++)
    ostree_bloom_add_element (bloom, members[i]);

  bytes = ostree_bloom_seal (bloom);

  /* Read it back from the GBytes. */
  immutable_bloom = ostree_bloom_new_from_bytes (bytes, k, hash);

  for (i = 0; i < G_N_ELEMENTS (members); i++)
    g_assert_true (ostree_bloom_maybe_contains (bloom, members[i]));

  /* This should never fail in future, as we guarantee the hash function will
   * never change. But given the definition of a bloom filter, it would also
   * be valid for these calls to return %TRUE. */
  for (i = 0; i < G_N_ELEMENTS (non_members); i++)
    g_assert_false (ostree_bloom_maybe_contains (bloom, non_members[i]));
}

/* Test that an empty bloom filter definitely contains no elements. */
static void
test_bloom_empty (void)
{
  g_autoptr(OstreeBloom) bloom = NULL;
  const gsize n_bytes = 256;
  const guint8 k = 8;
  const OstreeBloomHashFunc hash = ostree_str_bloom_hash;

  /* Build an empty bloom filter. */
  bloom = ostree_bloom_new (n_bytes, k, hash);

  g_assert_false (ostree_bloom_maybe_contains (bloom, "hello"));
  g_assert_false (ostree_bloom_maybe_contains (bloom, "there"));
}

/* Build a bloom filter, and check the membership of the members as they are
 * added. */
static void
test_bloom_membership_during_construction (void)
{
  g_autoptr(OstreeBloom) bloom = NULL;
  gsize i, j;
  const gchar *members[] =
    {
      "hello", "there", "these", "are", "test", "strings"
    };
  const gsize n_bytes = 256;
  const guint8 k = 8;
  const OstreeBloomHashFunc hash = ostree_str_bloom_hash;

  /* These membership checks should never fail in future, as we guarantee
   * the hash function will never change. But given the definition of a bloom
   * filter, it would also be valid for these checks to fail. */
  bloom = ostree_bloom_new (n_bytes, k, hash);

  for (i = 0; i < G_N_ELEMENTS (members); i++)
    {
      ostree_bloom_add_element (bloom, members[i]);

      for (j = 0; j < G_N_ELEMENTS (members); j++)
        {
          if (j <= i)
            g_assert_true (ostree_bloom_maybe_contains (bloom, members[j]));
          else
            g_assert_false (ostree_bloom_maybe_contains (bloom, members[j]));
        }
    }
}

int main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/bloom/init", test_bloom_init);
  g_test_add_func ("/bloom/construction", test_bloom_construction);
  g_test_add_func ("/bloom/empty", test_bloom_empty);
  g_test_add_func ("/bloom/membership-during-construction", test_bloom_membership_during_construction);

  return g_test_run();
}