1 /*
2  *  Copyright (c) 2008-2012 NFG Net Facilities Group BV support@nfg.nl
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation; either
7  *   version 2 of the License, or (at your option) any later
8  *   version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *
20  *   Basic unit-test framework for dbmail (www.dbmail.org)
21  *
22  *   See http://check.sf.net for details and docs.
23  *
24  *   Run 'make check' to see some action.
25  *
26  */
27 
28 #include <check.h>
29 #include "check_dbmail.h"
30 #include "dm_mempool.h"
31 
32 extern char configFile[PATH_MAX];
33 
34 /*
35  *
36  * the test fixtures
37  *
38  */
setup(void)39 void setup(void)
40 {
41 	config_get_file();
42 	config_read(configFile);
43 	configure_debug(NULL,255,0);
44 }
45 
teardown(void)46 void teardown(void)
47 {
48 	config_free();
49 }
50 
START_TEST(test_mempool_new)51 START_TEST(test_mempool_new)
52 {
53 	Mempool_T M = mempool_open();
54 	fail_unless(M != NULL);
55 	mempool_close(&M);
56 }
57 END_TEST
58 
START_TEST(test_mempool_pop)59 START_TEST(test_mempool_pop)
60 {
61 	int i;
62 	Mempool_T M = mempool_open();
63 	for (i = 0; i < 1024; i++) {
64 		uint64_t *i = mempool_pop(M, sizeof(uint64_t));
65 		fail_unless(i != NULL);
66 		mempool_push(M, i, sizeof(uint64_t));
67 	}
68 	mempool_close(&M);
69 }
70 END_TEST
71 
START_TEST(test_mempool_push)72 START_TEST(test_mempool_push)
73 {
74 	int i;
75 	struct test_data {
76 		void *data;
77 		char key[128];
78 		uint64_t id;
79 	};
80 
81 	Mempool_T M = mempool_open();
82 	for (i = 0; i < 1024; i++) {
83 		uint64_t *i = mempool_pop(M, sizeof(uint64_t));
84 		fail_unless(i != NULL);
85 		mempool_push(M, i, sizeof(*i));
86 	}
87 
88 	struct test_data *data;
89 	for (i = 0; i < 1024; i++) {
90 		data = mempool_pop(M, sizeof(*data));
91 		fail_unless(data != NULL);
92 		mempool_push(M, data, sizeof(*data));
93 	}
94 	mempool_close(&M);
95 }
96 END_TEST
97 
98 
99 
dbmail_mempool_suite(void)100 Suite *dbmail_mempool_suite(void)
101 {
102 	Suite *s = suite_create("Dbmail Mempool");
103 	TCase *tc_mempool = tcase_create("Mempool");
104 
105 	suite_add_tcase(s, tc_mempool);
106 
107 	tcase_add_checked_fixture(tc_mempool, setup, teardown);
108 	tcase_add_test(tc_mempool, test_mempool_new);
109 	tcase_add_test(tc_mempool, test_mempool_pop);
110 	tcase_add_test(tc_mempool, test_mempool_push);
111 
112 	return s;
113 }
114 
main(void)115 int main(void)
116 {
117 	int nf;
118 	Suite *s = dbmail_mempool_suite();
119 	SRunner *sr = srunner_create(s);
120 	srunner_run_all(sr, CK_NORMAL);
121 	nf = srunner_ntests_failed(sr);
122 	srunner_free(sr);
123 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
124 }
125 
126