Blame view

RIOT/tests/unittests/tests-bloom/tests-bloom.c 2.43 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2014 Philipp Rosenkranz
   * Copyright (C) 2013 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  #include <string.h>
  #include <stdio.h>
  
  #include "tests-bloom.h"
  
  #include "hashes.h"
  #include "bloom.h"
  #include "bitfield.h"
  
  #include "tests-bloom-sets.h"
  
  #define TESTS_BLOOM_BITS (128)
  #define TESTS_BLOOM_HASHF (6)
  #define TESTS_BLOOM_PROB_IN_FILTER (4)
  #define TESTS_BLOOM_NOT_IN_FILTER (996)
  #define TESTS_BLOOM_FALSE_POS_RATE_THR (0.005)
  
  static bloom_t bloom;
  BITFIELD(bf, TESTS_BLOOM_BITS);
  hashfp_t hashes[TESTS_BLOOM_HASHF] = {
                       (hashfp_t) fnv_hash,
                       (hashfp_t) sax_hash,
                       (hashfp_t) sdbm_hash,
                       (hashfp_t) djb2_hash,
                       (hashfp_t) kr_hash,
                       (hashfp_t) dek_hash,
                      };
  
  static void load_dictionary_fixture(void)
  {
      for (int i = 0; i < lenB; i++)
      {
          bloom_add(&bloom, (const uint8_t *) B[i], strlen(B[i]));
      }
  
  }
  
  static void set_up_bloom(void)
  {
      bloom_init(&bloom, TESTS_BLOOM_BITS, bf, hashes, TESTS_BLOOM_HASHF);
  }
  
  static void tear_down_bloom(void)
  {
      bloom_del(&bloom);
  }
  
  static void test_bloom_parameters_bytes_hashf(void)
  {
      TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_BITS, bloom.m);
      TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_HASHF, bloom.k);
  }
  
  static void test_bloom_based_on_dictionary_fixture(void)
  {
      int in = 0;
      int not_in = 0;
      double false_positive_rate = 0;
  
      load_dictionary_fixture();
  
      for (int i = 0; i < lenA; i++)
      {
          if (bloom_check(&bloom, (const uint8_t *) A[i], strlen(A[i])))
          {
              in++;
          }
          else
          {
              not_in++;
          }
      }
      false_positive_rate = (double) in / (double) lenA;
  
      TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_PROB_IN_FILTER, in);
      TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_NOT_IN_FILTER, not_in);
      TEST_ASSERT(false_positive_rate < TESTS_BLOOM_FALSE_POS_RATE_THR);
  }
  
  Test *tests_bloom_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_bloom_parameters_bytes_hashf),
          new_TestFixture(test_bloom_based_on_dictionary_fixture),
      };
  
      EMB_UNIT_TESTCALLER(bloom_tests, set_up_bloom, tear_down_bloom, fixtures);
  
      return (Test *)&bloom_tests;
  }
  
  void tests_bloom(void)
  {
      TESTS_RUN(tests_bloom_tests());
  }