aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-02-01 16:49:00 -0800
committerAlon Zakai <alonzakai@gmail.com>2014-02-01 16:49:00 -0800
commit737de35442186143b8801e4755f69b63c59fab0b (patch)
treee3155341d71703e80b00c4b55f1019ad8cf30d9b
parentf5c957b2893744f061adbcf2a911cb693025a613 (diff)
make sure RAND_MAX is a proper value
-rw-r--r--src/library.js2
-rw-r--r--tests/test_core.py5
2 files changed, 6 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js
index d9e1942f..901c9474 100644
--- a/src/library.js
+++ b/src/library.js
@@ -3458,7 +3458,7 @@ LibraryManager.library = {
rand_r: function(seedp) {
seedp = seedp|0;
var val = 0;
- val = ((Math_imul({{{ makeGetValueAsm('seedp', 0, 'i32') }}}, 31010991)|0) + 0x676e6177 ) & {{{ cDefine('RAND_MAX') }}};
+ val = ((Math_imul({{{ makeGetValueAsm('seedp', 0, 'i32') }}}, 31010991)|0) + 0x676e6177 ) & {{{ cDefine('RAND_MAX') }}}; // assumes RAND_MAX is in bit mask form (power of 2 minus 1)
{{{ makeSetValueAsm('seedp', 0, 'val', 'i32') }}};
return val|0;
},
diff --git a/tests/test_core.py b/tests/test_core.py
index 32b3f143..4141606b 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -3552,8 +3552,13 @@ ok
def test_rand(self):
src = r'''#include <stdlib.h>
#include <stdio.h>
+#include <assert.h>
int main()
{
+ // we need RAND_MAX to be a bitmask (power of 2 minus 1). this assertions guarantees
+ // if RAND_MAX changes the test failure will focus attention on that issue here.
+ assert(RAND_MAX == 0x7fffffff);
+
srand(0xdeadbeef);
for(int i = 0; i < 10; ++i)
printf("%d\n", rand());