Skip to content

Commit 0ef4409

Browse files
bashtageSheppard, Kevin
authored andcommitted
TST: Add test to ensure all critical functions are available
Change imports for distributions.h Add test for dir() on numpy against randomstate
1 parent 33ccbfe commit 0ef4409

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

randomstate/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
from randomstate.prng.mt19937 import *
44
from randomstate.entropy import random_entropy
55
import randomstate.prng
6+
7+
sample = ranf = random = random_sample

randomstate/distributions.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ double random_standard_uniform(aug_state* state)
3636
}
3737

3838

39-
void random_uniform_fill(aug_state* state, intptr_t count, double *out)
39+
void random_uniform_fill(aug_state* state, npy_intp count, double *out)
4040
{
4141
int i;
4242
for (i=0; i < count; i++) {
@@ -50,7 +50,7 @@ double random_standard_exponential(aug_state* state)
5050
return -log(1.0 - random_double(state));
5151
}
5252

53-
void random_standard_exponential_fill(aug_state* state, intptr_t count, double *out)
53+
void random_standard_exponential_fill(aug_state* state, npy_intp count, double *out)
5454
{
5555
int i;
5656
for (i=0; i < count; i++) {
@@ -87,7 +87,7 @@ double random_gauss(aug_state* state) {
8787
}
8888
}
8989

90-
void random_gauss_fill(aug_state* state, intptr_t count, double *out) {
90+
void random_gauss_fill(aug_state* state, npy_intp count, double *out) {
9191
int i;
9292
double f, x1, x2, r2;
9393
for (i = 0; i< count; i++) {
@@ -1354,7 +1354,7 @@ double random_gauss_zig_julia(aug_state *state) {
13541354
}
13551355

13561356

1357-
void random_gauss_zig_julia_fill(aug_state *state, intptr_t count, double *out) {
1357+
void random_gauss_zig_julia_fill(aug_state *state, npy_intp count, double *out) {
13581358
uint64_t r;
13591359
int64_t rabs;
13601360
int idx, i;
@@ -1444,7 +1444,7 @@ static inline uint64_t gen_mask(uint64_t max)
14441444
* Fills an array with cnt random npy_uint64 between off and off + rng
14451445
* inclusive. The numbers wrap if rng is sufficiently large.
14461446
*/
1447-
void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, intptr_t cnt, uint64_t *out)
1447+
void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, npy_intp cnt, uint64_t *out)
14481448
{
14491449
uint64_t val, mask;
14501450
int i;
@@ -1478,7 +1478,7 @@ void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, in
14781478
* Fills an array with cnt random npy_uint32 between off and off + rng
14791479
* inclusive. The numbers wrap if rng is sufficiently large.
14801480
*/
1481-
void random_bounded_uint32_fill(aug_state *state, uint32_t off, uint32_t rng, intptr_t cnt, uint32_t *out)
1481+
void random_bounded_uint32_fill(aug_state *state, uint32_t off, uint32_t rng, npy_intp cnt, uint32_t *out)
14821482
{
14831483
uint32_t val, mask = rng;
14841484
int i;
@@ -1504,7 +1504,7 @@ void random_bounded_uint32_fill(aug_state *state, uint32_t off, uint32_t rng, in
15041504
* Fills an array with cnt random npy_uint16 between off and off + rng
15051505
* inclusive. The numbers wrap if rng is sufficiently large.
15061506
*/
1507-
void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, intptr_t cnt, uint16_t *out)
1507+
void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, npy_intp cnt, uint16_t *out)
15081508
{
15091509
uint16_t val, mask;
15101510
int i;
@@ -1541,7 +1541,7 @@ void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, in
15411541
* Fills an array with cnt random npy_uint8 between off and off + rng
15421542
* inclusive. The numbers wrap if rng is sufficiently large.
15431543
*/
1544-
void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, intptr_t cnt, uint8_t *out)
1544+
void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, npy_intp cnt, uint8_t *out)
15451545
{
15461546
uint8_t val, mask = rng;
15471547
int i;
@@ -1580,7 +1580,7 @@ void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, intpt
15801580
* inclusive.
15811581
*/
15821582
/* TODO: This needs to use bools. See original */
1583-
void random_bool_fill(aug_state *state, int8_t off, int8_t rng, intptr_t cnt, int8_t *out)
1583+
void random_bool_fill(aug_state *state, int8_t off, int8_t rng, npy_intp cnt, int8_t *out)
15841584
{
15851585
int i;
15861586
uint32_t buf = 0;

randomstate/distributions.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <math.h>
22
#include <stdlib.h>
33

4+
#include "Python.h"
5+
#include "numpy/npy_common.h"
6+
47
#ifdef _WIN32
58
#include "src/common/stdint.h"
69
typedef int bool;
@@ -123,20 +126,20 @@ extern unsigned long random_uint(aug_state* state);
123126

124127
extern unsigned long random_interval(aug_state* state, unsigned long max);
125128

126-
extern void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, intptr_t cnt, uint64_t *out);
129+
extern void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, npy_intp cnt, uint64_t *out);
127130

128-
extern void random_bounded_uint32_fill(aug_state *state, uint32_t off, uint32_t rng, intptr_t cnt, uint32_t *out);
131+
extern void random_bounded_uint32_fill(aug_state *state, uint32_t off, uint32_t rng, npy_intp cnt, uint32_t *out);
129132

130-
extern void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, intptr_t cnt, uint16_t *out);
133+
extern void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, npy_intp cnt, uint16_t *out);
131134

132-
extern void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, intptr_t cnt, uint8_t *out);
135+
extern void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, npy_intp cnt, uint8_t *out);
133136

134-
extern void random_bool_fill(aug_state *state, int8_t off, int8_t rng, intptr_t cnt, int8_t *out);
137+
extern void random_bool_fill(aug_state *state, int8_t off, int8_t rng, npy_intp cnt, int8_t *out);
135138

136-
extern void random_uniform_fill(aug_state* state, intptr_t count, double *out);
139+
extern void random_uniform_fill(aug_state* state, npy_intp count, double *out);
137140

138-
extern void random_standard_exponential_fill(aug_state* state, intptr_t count, double *out);
141+
extern void random_standard_exponential_fill(aug_state* state, npy_intp count, double *out);
139142

140-
extern void random_gauss_fill(aug_state* state, intptr_t count, double *out);
143+
extern void random_gauss_fill(aug_state* state, npy_intp count, double *out);
141144

142-
extern void random_gauss_zig_julia_fill(aug_state *state, intptr_t count, double *out);
145+
extern void random_gauss_zig_julia_fill(aug_state *state, npy_intp count, double *out);

randomstate/tests/test_against_numpy.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import numpy as np
33
import numpy.random
44
from numpy.testing import assert_allclose, assert_array_equal, assert_equal
5+
import randomstate
56
from randomstate.prng.mt19937 import mt19937
67

78

@@ -506,3 +507,19 @@ def test_array(self):
506507
s = mt19937.RandomState([4294967295])
507508
assert_equal(s.randint(1000), 265)
508509

510+
def test_dir(self):
511+
nprs_d = dir(self.nprs)
512+
rs_d = dir(self.rs)
513+
print(set(nprs_d).difference(rs_d))
514+
assert(len(set(nprs_d).difference(rs_d)) == 0)
515+
516+
npmod = dir(numpy.random)
517+
mod = dir(randomstate)
518+
known_exlcuded = ['__all__', 'Tester', 'info', 'bench',
519+
'__RandomState_ctor', 'mtrand', 'test',
520+
'__warningregistry__']
521+
mod += known_exlcuded
522+
print(set(npmod).difference(mod))
523+
assert(len(set(npmod).difference(mod)) == 0)
524+
525+

0 commit comments

Comments
 (0)