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 104 105 106 107 108 109 110 111 112 113 114 115
|
/*
* ProFTPD - mod_proxy testsuite
* Copyright (c) 2013-2022 TJ Saunders <tj@castaglia.org>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
/* Random API tests. */
#include "tests.h"
static pool *p = NULL;
static void set_up(void) {
if (p == NULL) {
p = make_sub_pool(NULL);
session.c = NULL;
session.notes = NULL;
}
proxy_random_init();
}
static void tear_down(void) {
if (p) {
destroy_pool(p);
p = NULL;
session.c = NULL;
session.notes = NULL;
}
}
START_TEST (random_next_range_10_test) {
register unsigned int i;
long min, max;
min = -4;
max = 5;
for (i = 0; i < 10; i++) {
long num;
num = proxy_random_next(min, max);
ck_assert_msg(num >= min, "random number %ld less than minimum %ld", num,
min);
ck_assert_msg(num <= max, "random number %ld greater than maximum %ld", num,
max);
}
}
END_TEST
START_TEST (random_next_range_1000_test) {
register int i;
long min, max;
int count = 10, seen[10];
min = 0;
max = count-1;
memset(seen, 0, sizeof(seen));
for (i = 0; i < 1000; i++) {
long num;
num = proxy_random_next(min, max);
ck_assert_msg(num >= min, "random number %ld less than minimum %ld", num,
min);
ck_assert_msg(num <= max, "random number %ld greater than maximum %ld", num,
max);
seen[num] = 1;
}
/* In 1000 rounds, the chances of seeing all 10 possible numbers is pretty
* good, right?
*/
for (i = 0; i < count; i++) {
ck_assert_msg(seen[i] == 1, "Expected to have generated number %d", i);
}
}
END_TEST
Suite *tests_get_random_suite(void) {
Suite *suite;
TCase *testcase;
suite = suite_create("random");
testcase = tcase_create("base");
tcase_add_checked_fixture(testcase, set_up, tear_down);
tcase_add_test(testcase, random_next_range_10_test);
tcase_add_test(testcase, random_next_range_1000_test);
suite_add_tcase(suite, testcase);
return suite;
}
|