1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-27 15:31:17 +01:00

Merge pull request #2182 from phiros/tests_bloom_transform_into_unittest

tests: transformed tests/bloom into an unittest
This commit is contained in:
Martine Lenders 2014-12-12 10:02:59 +01:00
commit 55dfd3d053
9 changed files with 1177 additions and 124 deletions

View File

@ -1,25 +0,0 @@
APPLICATION = bloom
include ../Makefile.tests_common
BOARD_INSUFFICIENT_RAM := chronos msb-430 msb-430h redbee-econotag \
telosb wsn430-v1_3b wsn430-v1_4 z1 stm32f0discovery \
spark-core
BOARD_BLACKLIST := arduino-mega2560
# arduino-mega2560: Errors in assembly, e.g:
# Error: value of 105617 too large for field of 2 bytes at 20018
USEMODULE += hashes
USEMODULE += bloom
APPDEPS = $(BINDIR)projdeps/sets.h
INCLUDES += -I$(BINDIR)projdeps
DISABLE_MODULE += auto_init
include $(RIOTBASE)/Makefile.include
$(BINDIR)projdeps/sets.h: generate_sets.py words.txt.gz
mkdir -p ${@D}
./generate_sets.py words.txt.gz $@

View File

@ -1,36 +0,0 @@
#!/usr/bin/env python2
import gzip
import random
import sys
sizeOfA = 10 * 1000
sizeOfB = 20
print sys.argv
if len(sys.argv) == 5:
sizeOfA = int(sys.argv[3])
sizeOfB = int(sys.argv[4])
# read all words
lines = [line.strip() for line in gzip.open(sys.argv[1])]
# get A lines
A = random.sample(lines, sizeOfA + sizeOfB)
# get B from the first sieOfB element
B = A[:sizeOfB]
A = A[sizeOfB:]
SetsFile = open(sys.argv[2], 'w')
SetsFile.write('const int lenA = ' + str(sizeOfA) + ';\n')
SetsFile.write('const char* const A[' + str(sizeOfA) + '] = {')
SetsFile.writelines(",".join('"' + x + '"\n' for x in A))
SetsFile.write('};\n')
SetsFile.write('const int lenB = ' + str(sizeOfB) + ';\n')
SetsFile.write('const char* const B[' + str(sizeOfB) + '] = {')
SetsFile.writelines(",".join('"' + x + '"\n' for x in B))
SetsFile.write('};\n')
print("sets.h: sizeOfA = " + str(len(A) + len(B)) + " generated...")

View File

@ -1,63 +0,0 @@
/*
* 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.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Bloom filter test application
*
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include "hashes.h"
#include "bloom.h"
#include "sets.h"
int main(void)
{
bloom_t *bloom = bloom_new(1 << 7, 6, fnv_hash, sax_hash, sdbm_hash,
djb2_hash, kr_hash, dek_hash, rotating_hash, one_at_a_time_hash);
printf("Testing Bloom filter.\n\n");
printf("m: %zd\nk: %zd\n\n", bloom->m, bloom->k);
for (int i = 0; i < lenB; i++) {
bloom_add(bloom, (const uint8_t *) B[i], strlen(B[i]));
printf("Added \"%s\"\n", B[i]);
}
int in = 0;
int not_in = 0;
for (int i = 0; i < lenA; i++) {
if (bloom_check(bloom, (const uint8_t *) A[i], strlen(A[i]))) {
in++;
}
else {
not_in++;
}
}
printf("\n");
printf("%d elements probably in the filter.\n", in);
printf("%d elements not in the filter.\n", not_in);
double false_positive_rate = (double) in / (double) lenA;
printf("%f false positive rate.\n", false_positive_rate);
bloom_del(bloom);
printf("\nAll done!\n");
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,3 @@
MODULE = tests-bloom
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,3 @@
USEMODULE += bloom
USEMODULE += hashes

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,95 @@
/*
* 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 "tests-bloom-sets.h"
#define TESTS_BLOOM_BYTES (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;
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 = bloom_new(TESTS_BLOOM_BYTES, TESTS_BLOOM_HASHF, fnv_hash, sax_hash,
sdbm_hash, djb2_hash, kr_hash, dek_hash, rotating_hash,
one_at_a_time_hash);
}
static void tear_down_bloom(void)
{
bloom_del(bloom);
}
static void test_bloom_parameters_bytes_hashf(void)
{
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_BYTES, 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());
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2014 Philipp Rosenkranz
*
* 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.
*/
/**
* @addtogroup unittests
* @{
*
* @file tests-bloom.h
* @brief Unittests for the ``bloom`` module
*
* @author Philipp Rosenkranz <philipp.rosenkranz@fu-berlin.de>
*/
#ifndef __TESTS_BLOOM_H_
#define __TESTS_BLOOM_H_
#include "../unittests.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The entry point of this test suite.
*/
void tests_bloom(void);
/**
* @brief Generates tests for bloom
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_bloom_tests(void);
#ifdef __cplusplus
}
#endif
#endif /* __TESTS_BLOOM_H_ */
/** @} */