[Core] Add new switch_rand() a compliant random number generator API. Add a unit-test.

* [Core] Add new switch_rand() a compliant random number generator API. Add a unit-test.

* Fall back to rand() on unsupported platforms compile time.
This commit is contained in:
Andrey Volk
2024-06-06 21:05:33 +03:00
parent b84b7cbf71
commit c7e793c345
3 changed files with 83 additions and 0 deletions

View File

@@ -53,6 +53,27 @@ FST_CORE_BEGIN("./conf")
}
FST_TEARDOWN_END()
FST_TEST_BEGIN(test_switch_rand)
{
int i, c = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "\nLet's generate a few random numbers.\n");
for (i = 0; i < 10; i++) {
uint32_t rnd = switch_rand();
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Random number %d\n", rnd);
if (rnd == 1) {
c++;
}
}
/* We do not expect all random numbers to be 1 all 10 times. That would mean we have an error OR we are lucky to have 10 random ones! */
fst_check(c < 10);
}
FST_TEST_END()
FST_TEST_BEGIN(test_switch_uint31_t_overflow)
{
switch_uint31_t x;