summaryrefslogtreecommitdiff
path: root/ctdb/tests/src/db_hash_test.c
blob: e05d1168776bc722451c4ba30f4e075e2c8ee991 (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
/*
   db_hash tests

   Copyright (C) Amitay Isaacs  2015

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <assert.h>

#include "common/db_hash.c"

static void do_test(enum db_hash_type type)
{
	struct db_hash_context *dh;
	TALLOC_CTX *mem_ctx = talloc_new(NULL);
	TALLOC_CTX *tmp_ctx = talloc_new(NULL);
	uint8_t *key = (uint8_t *)talloc_strdup(tmp_ctx, "This is a long key");
	uint8_t *value = (uint8_t *)talloc_strdup(tmp_ctx, "This is a long value");
	int ret;

	ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
	assert(ret == 0);

	ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
	assert(ret == 0);

	ret = db_hash_exists(dh, key, sizeof(key));
	assert(ret == 0);

	ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
	assert(ret == EEXIST);

	ret = db_hash_delete(dh, key, sizeof(key));
	assert(ret == 0);

	ret = db_hash_exists(dh, key, sizeof(key));
	assert(ret == ENOENT);

	ret = db_hash_delete(dh, key, sizeof(key));
	assert(ret == ENOENT);

	ret = db_hash_add(dh, key, sizeof(key), key, sizeof(key));
	assert(ret == 0);

	ret = db_hash_add(dh, key, sizeof(key), value, sizeof(value));
	assert(ret == 0);

	talloc_free(dh);
	ret = talloc_get_size(mem_ctx);
	assert(ret == 0);

	talloc_free(mem_ctx);
}

static void do_traverse_test(enum db_hash_type type)
{
	struct db_hash_context *dh;
	TALLOC_CTX *mem_ctx = talloc_new(NULL);
	char key[] = "keyXXXX";
	char value[] = "This is some test value";
	int count, ret, i;

	ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
	assert(ret == 0);

	for (i=0; i<2000; i++) {
		sprintf(key, "key%04d", i);
		ret = db_hash_insert(dh, (uint8_t *)key, sizeof(key),
				     (uint8_t *)value, sizeof(value));
		assert(ret == 0);
	}

	ret = db_hash_traverse(dh, NULL, NULL, &count);
	assert(ret == 0);
	assert(count == 2000);

	talloc_free(dh);
	talloc_free(mem_ctx);
}

int main(void)
{
	do_test(DB_HASH_SIMPLE);
	do_test(DB_HASH_COMPLEX);
	do_traverse_test(DB_HASH_SIMPLE);
	do_traverse_test(DB_HASH_COMPLEX);
	return 0;
}