Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: randof() strikes again #1730

Merged
merged 4 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/czmq_prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,12 @@ typedef struct {
// RAND_MAX, random() returns a 32-bit value all filled with random bits.
#if (defined (__WINDOWS__)) || (defined (__UTYPE_IBMAIX)) \
|| (defined (__UTYPE_HPUX)) || (defined (__UTYPE_SUNOS)) || (defined (__UTYPE_SOLARIS))
# define randof(num) (int) ((float) (num) * rand () / (RAND_MAX + 1.0))
# define randof(num) (int) ( floorf( (float) ( (float)(num) * (float)(rand ()) / ((float)RAND_MAX + 1.0)) ) )
#else
# if defined(RAND_MAX)
# define randof(num) (int) ((float) (num) * (random () % RAND_MAX) / (RAND_MAX + 1.0))
# define randof(num) (int) ( floorf( (float) ( (float)(num) * (float)(random () % RAND_MAX) / ((float)RAND_MAX + 1.0)) ) )
# else
# define randof(num) (int) ((float) (num) * (uint32_t)random () / (UINT32_MAX + 1.0))
# define randof(num) (int) ( floorf( (float) ( (float)(num) * (float)(uint32_t)random () / ((float)UINT32_MAX + 1.0)) ) )
# endif
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/test_zgossip.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ main (int argn, char *argv [])
int item_nbr;
for (item_nbr = 0; item_nbr < set_size; item_nbr++) {
node_nbr = randof (swarm_size);
assert (node_nbr != swarm_size);
assert (node_nbr < swarm_size);
zstr_sendm (nodes [node_nbr], "PUBLISH");
zstr_sendfm (nodes [node_nbr], "key-%d", item_nbr);
zstr_send (nodes [node_nbr], "value");
Expand Down
12 changes: 12 additions & 0 deletions src/zhashx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,8 @@ zhashx_test (bool verbose)
srandom ((unsigned) time (NULL));
for (iteration = 0; iteration < 25000; iteration++) {
testnbr = randof (testmax);
assert (testnbr != testmax);
assert (testnbr < testmax);
if (testset [testnbr].exists) {
item = (char *) zhashx_lookup (hash, testset [testnbr].name);
assert (item);
Expand All @@ -1290,6 +1292,16 @@ zhashx_test (bool verbose)
zhashx_destroy (&hash);
assert (hash == NULL);

// Test randof() limits - should be within (0..testmax)
// Note: This test can take a while on systems with weak floating point HW
testmax = 999;
for (iteration = 0; iteration < 10000000; iteration++) {
testnbr = randof (testmax);
assert (testnbr != testmax);
assert (testnbr < testmax);
assert (testnbr >= 0);
}

// Test destructor; automatically copies and frees string values
hash = zhashx_new ();
assert (hash);
Expand Down