Debian Codegraph @

tests/api/random.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2017 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 /* Random API tests. */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = make_sub_pool(NULL);
34 session.c = NULL;
35 session.notes = NULL;
36 }
37
38 pr_random_init();
39 }
40
41 static void tear_down(void) {
42 if (p) {
43 destroy_pool(p);
44 p = NULL;
45 session.c = NULL;
46 session.notes = NULL;
47 }
48 }
49
50 START_TEST (random_next_range_10_test) {
51 register unsigned int i;
52 long min, max;
53
54 min = -4;
55 max = 5;
56
57 for (i = 0; i < 10; i++) {
58 long num;
59
60 num = pr_random_next(min, max);
61 ck_assert_msg(num >= min, "random number %ld less than minimum %ld", num, min);
62 ck_assert_msg(num <= max, "random number %ld greater than maximum %ld", num, max);
63 }
64 }
65 END_TEST
66
67 START_TEST (random_next_range_1000_test) {
68 register int i;
69 long min, max;
70 int count = 10, seen[10];
71
72 min = 0;
73 max = count-1;
74
75 memset(seen, 0, sizeof(seen));
76
77 for (i = 0; i < 1000; i++) {
78 long num;
79
80 num = pr_random_next(min, max);
81 ck_assert_msg(num >= min, "random number %ld less than minimum %ld", num, min);
82 ck_assert_msg(num <= max, "random number %ld greater than maximum %ld", num, max);
83
84 seen[num] = 1;
85 }
86
87 /* In 1000 rounds, the chances of seeing all 10 possible numbers is pretty
88 * good, right?
89 */
90 for (i = 0; i < count; i++) {
91 ck_assert_msg(seen[i] == 1, "Expected to have generated number %d", i);
92 }
93 }
94 END_TEST
95
96 Suite *tests_get_random_suite(void) {
97 Suite *suite;
98 TCase *testcase;
99
100 suite = suite_create("random");
101
102 testcase = tcase_create("base");
103
104 tcase_add_checked_fixture(testcase, set_up, tear_down);
105
106 tcase_add_test(testcase, random_next_range_10_test);
107 tcase_add_test(testcase, random_next_range_1000_test);
108
109 suite_add_tcase(suite, testcase);
110 return suite;
111 }