Skip to content

Commit c2060ef

Browse files
authored
Merge pull request #227 from pitdicker/no_std_alloc
Implement Rng for Box with alloc feature
2 parents 8e8bb6a + 4828506 commit c2060ef

File tree

12 files changed

+259
-289
lines changed

12 files changed

+259
-289
lines changed

src/distributions/exponential.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ mod test {
105105
#[test]
106106
fn test_exp() {
107107
let mut exp = Exp::new(10.0);
108-
let mut rng = ::test::rng();
108+
let mut rng = ::test::rng(221);
109109
for _ in 0..1000 {
110110
assert!(exp.sample(&mut rng) >= 0.0);
111111
assert!(exp.ind_sample(&mut rng) >= 0.0);

src/distributions/gamma.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ mod test {
334334
#[test]
335335
fn test_chi_squared_one() {
336336
let mut chi = ChiSquared::new(1.0);
337-
let mut rng = ::test::rng();
337+
let mut rng = ::test::rng(201);
338338
for _ in 0..1000 {
339339
chi.sample(&mut rng);
340340
chi.ind_sample(&mut rng);
@@ -343,7 +343,7 @@ mod test {
343343
#[test]
344344
fn test_chi_squared_small() {
345345
let mut chi = ChiSquared::new(0.5);
346-
let mut rng = ::test::rng();
346+
let mut rng = ::test::rng(202);
347347
for _ in 0..1000 {
348348
chi.sample(&mut rng);
349349
chi.ind_sample(&mut rng);
@@ -352,7 +352,7 @@ mod test {
352352
#[test]
353353
fn test_chi_squared_large() {
354354
let mut chi = ChiSquared::new(30.0);
355-
let mut rng = ::test::rng();
355+
let mut rng = ::test::rng(203);
356356
for _ in 0..1000 {
357357
chi.sample(&mut rng);
358358
chi.ind_sample(&mut rng);
@@ -367,7 +367,7 @@ mod test {
367367
#[test]
368368
fn test_f() {
369369
let mut f = FisherF::new(2.0, 32.0);
370-
let mut rng = ::test::rng();
370+
let mut rng = ::test::rng(204);
371371
for _ in 0..1000 {
372372
f.sample(&mut rng);
373373
f.ind_sample(&mut rng);
@@ -377,7 +377,7 @@ mod test {
377377
#[test]
378378
fn test_t() {
379379
let mut t = StudentT::new(11.0);
380-
let mut rng = ::test::rng();
380+
let mut rng = ::test::rng(205);
381381
for _ in 0..1000 {
382382
t.sample(&mut rng);
383383
t.ind_sample(&mut rng);

src/distributions/mod.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ mod tests {
311311
fn test_rand_sample() {
312312
let mut rand_sample = RandSample::<ConstRand>::new();
313313

314-
assert_eq!(rand_sample.sample(&mut ::test::rng()), ConstRand(0));
315-
assert_eq!(rand_sample.ind_sample(&mut ::test::rng()), ConstRand(0));
314+
assert_eq!(rand_sample.sample(&mut ::test::rng(231)), ConstRand(0));
315+
assert_eq!(rand_sample.ind_sample(&mut ::test::rng(232)), ConstRand(0));
316316
}
317+
317318
#[test]
318319
fn test_weighted_choice() {
319320
// this makes assumptions about the internal implementation of
@@ -335,36 +336,36 @@ mod tests {
335336
}}
336337
}
337338

338-
t!(vec!(Weighted { weight: 1, item: 10}), [10]);
339+
t!([Weighted { weight: 1, item: 10}], [10]);
339340

340341
// skip some
341-
t!(vec!(Weighted { weight: 0, item: 20},
342-
Weighted { weight: 2, item: 21},
343-
Weighted { weight: 0, item: 22},
344-
Weighted { weight: 1, item: 23}),
342+
t!([Weighted { weight: 0, item: 20},
343+
Weighted { weight: 2, item: 21},
344+
Weighted { weight: 0, item: 22},
345+
Weighted { weight: 1, item: 23}],
345346
[21,21, 23]);
346347

347348
// different weights
348-
t!(vec!(Weighted { weight: 4, item: 30},
349-
Weighted { weight: 3, item: 31}),
349+
t!([Weighted { weight: 4, item: 30},
350+
Weighted { weight: 3, item: 31}],
350351
[30,30,30,30, 31,31,31]);
351352

352353
// check that we're binary searching
353354
// correctly with some vectors of odd
354355
// length.
355-
t!(vec!(Weighted { weight: 1, item: 40},
356-
Weighted { weight: 1, item: 41},
357-
Weighted { weight: 1, item: 42},
358-
Weighted { weight: 1, item: 43},
359-
Weighted { weight: 1, item: 44}),
356+
t!([Weighted { weight: 1, item: 40},
357+
Weighted { weight: 1, item: 41},
358+
Weighted { weight: 1, item: 42},
359+
Weighted { weight: 1, item: 43},
360+
Weighted { weight: 1, item: 44}],
360361
[40, 41, 42, 43, 44]);
361-
t!(vec!(Weighted { weight: 1, item: 50},
362-
Weighted { weight: 1, item: 51},
363-
Weighted { weight: 1, item: 52},
364-
Weighted { weight: 1, item: 53},
365-
Weighted { weight: 1, item: 54},
366-
Weighted { weight: 1, item: 55},
367-
Weighted { weight: 1, item: 56}),
362+
t!([Weighted { weight: 1, item: 50},
363+
Weighted { weight: 1, item: 51},
364+
Weighted { weight: 1, item: 52},
365+
Weighted { weight: 1, item: 53},
366+
Weighted { weight: 1, item: 54},
367+
Weighted { weight: 1, item: 55},
368+
Weighted { weight: 1, item: 56}],
368369
[50, 51, 52, 53, 54, 55, 56]);
369370
}
370371

@@ -404,7 +405,7 @@ mod tests {
404405
}
405406
#[test] #[should_panic]
406407
fn test_weighted_choice_weight_overflows() {
407-
let x = ::std::u32::MAX / 2; // x + x + 2 is the overflow
408+
let x = ::core::u32::MAX / 2; // x + x + 2 is the overflow
408409
WeightedChoice::new(&mut [Weighted { weight: x, item: 0 },
409410
Weighted { weight: 1, item: 1 },
410411
Weighted { weight: x, item: 2 },

src/distributions/normal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ mod tests {
171171
#[test]
172172
fn test_normal() {
173173
let mut norm = Normal::new(10.0, 10.0);
174-
let mut rng = ::test::rng();
174+
let mut rng = ::test::rng(210);
175175
for _ in 0..1000 {
176176
norm.sample(&mut rng);
177177
norm.ind_sample(&mut rng);
@@ -187,7 +187,7 @@ mod tests {
187187
#[test]
188188
fn test_log_normal() {
189189
let mut lnorm = LogNormal::new(10.0, 10.0);
190-
let mut rng = ::test::rng();
190+
let mut rng = ::test::rng(211);
191191
for _ in 0..1000 {
192192
lnorm.sample(&mut rng);
193193
lnorm.ind_sample(&mut rng);

src/distributions/range.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ mod tests {
185185

186186
#[test]
187187
fn test_integers() {
188-
let mut rng = ::test::rng();
188+
let mut rng = ::test::rng(251);
189189
macro_rules! t {
190190
($($ty:ident),*) => {{
191191
$(
@@ -214,7 +214,7 @@ mod tests {
214214

215215
#[test]
216216
fn test_floats() {
217-
let mut rng = ::test::rng();
217+
let mut rng = ::test::rng(252);
218218
macro_rules! t {
219219
($($ty:ty),*) => {{
220220
$(
@@ -237,5 +237,4 @@ mod tests {
237237

238238
t!(f32, f64)
239239
}
240-
241240
}

src/lib.rs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ use core::marker;
254254
use core::mem;
255255
#[cfg(feature="std")] use std::cell::RefCell;
256256
#[cfg(feature="std")] use std::rc::Rc;
257+
#[cfg(all(feature="alloc", not(feature="std")))] use alloc::boxed::Box;
257258

258259
// external rngs
259260
pub use jitter::JitterRng;
@@ -654,7 +655,7 @@ impl<'a, R: ?Sized> Rng for &'a mut R where R: Rng {
654655
}
655656
}
656657

657-
#[cfg(feature="std")]
658+
#[cfg(any(feature="std", feature="alloc"))]
658659
impl<R: ?Sized> Rng for Box<R> where R: Rng {
659660
#[inline]
660661
fn next_u32(&mut self) -> u32 {
@@ -1034,12 +1035,15 @@ pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>
10341035
#[cfg(test)]
10351036
mod test {
10361037
use impls;
1037-
use super::{Rng, thread_rng, random, SeedableRng, StdRng, weak_rng};
1038-
use std::iter::repeat;
1038+
#[cfg(feature="std")]
1039+
use super::{random, thread_rng, weak_rng};
1040+
use super::{Rng, SeedableRng, StdRng};
1041+
#[cfg(feature="alloc")]
1042+
use alloc::boxed::Box;
10391043

1040-
pub struct MyRng<R> { inner: R }
1044+
pub struct TestRng<R> { inner: R }
10411045

1042-
impl<R: Rng> Rng for MyRng<R> {
1046+
impl<R: Rng> Rng for TestRng<R> {
10431047
fn next_u32(&mut self) -> u32 {
10441048
self.inner.next_u32()
10451049
}
@@ -1051,8 +1055,9 @@ mod test {
10511055
}
10521056
}
10531057

1054-
pub fn rng() -> MyRng<::ThreadRng> {
1055-
MyRng { inner: ::thread_rng() }
1058+
pub fn rng(seed: u64) -> TestRng<StdRng> {
1059+
let seed = [seed as usize];
1060+
TestRng { inner: StdRng::from_seed(&seed) }
10561061
}
10571062

10581063
struct ConstRng { i: u64 }
@@ -1090,8 +1095,9 @@ mod test {
10901095
let lengths = [0, 1, 2, 3, 4, 5, 6, 7,
10911096
80, 81, 82, 83, 84, 85, 86, 87];
10921097
for &n in lengths.iter() {
1093-
let mut v = repeat(0u8).take(n).collect::<Vec<_>>();
1094-
r.fill_bytes(&mut v);
1098+
let mut buffer = [0u8; 87];
1099+
let mut v = &mut buffer[0..n];
1100+
r.fill_bytes(v);
10951101

10961102
// use this to get nicer error messages.
10971103
for (i, &byte) in v.iter().enumerate() {
@@ -1104,7 +1110,7 @@ mod test {
11041110

11051111
#[test]
11061112
fn test_gen_range() {
1107-
let mut r = thread_rng();
1113+
let mut r = rng(101);
11081114
for _ in 0..1000 {
11091115
let a = r.gen_range(-3, 42);
11101116
assert!(a >= -3 && a < 42);
@@ -1124,43 +1130,43 @@ mod test {
11241130
#[test]
11251131
#[should_panic]
11261132
fn test_gen_range_panic_int() {
1127-
let mut r = thread_rng();
1133+
let mut r = rng(102);
11281134
r.gen_range(5, -2);
11291135
}
11301136

11311137
#[test]
11321138
#[should_panic]
11331139
fn test_gen_range_panic_usize() {
1134-
let mut r = thread_rng();
1140+
let mut r = rng(103);
11351141
r.gen_range(5, 2);
11361142
}
11371143

11381144
#[test]
11391145
fn test_gen_weighted_bool() {
1140-
let mut r = thread_rng();
1146+
let mut r = rng(104);
11411147
assert_eq!(r.gen_weighted_bool(0), true);
11421148
assert_eq!(r.gen_weighted_bool(1), true);
11431149
}
11441150

11451151
#[test]
11461152
fn test_gen_ascii_str() {
1147-
let mut r = thread_rng();
1153+
let mut r = rng(105);
11481154
assert_eq!(r.gen_ascii_chars().take(0).count(), 0);
11491155
assert_eq!(r.gen_ascii_chars().take(10).count(), 10);
11501156
assert_eq!(r.gen_ascii_chars().take(16).count(), 16);
11511157
}
11521158

11531159
#[test]
11541160
fn test_gen_vec() {
1155-
let mut r = thread_rng();
1161+
let mut r = rng(106);
11561162
assert_eq!(r.gen_iter::<u8>().take(0).count(), 0);
11571163
assert_eq!(r.gen_iter::<u8>().take(10).count(), 10);
11581164
assert_eq!(r.gen_iter::<f64>().take(16).count(), 16);
11591165
}
11601166

11611167
#[test]
11621168
fn test_choose() {
1163-
let mut r = thread_rng();
1169+
let mut r = rng(107);
11641170
assert_eq!(r.choose(&[1, 1, 1]).map(|&x|x), Some(1));
11651171

11661172
let v: &[isize] = &[];
@@ -1169,7 +1175,7 @@ mod test {
11691175

11701176
#[test]
11711177
fn test_shuffle() {
1172-
let mut r = thread_rng();
1178+
let mut r = rng(108);
11731179
let empty: &mut [isize] = &mut [];
11741180
r.shuffle(empty);
11751181
let mut one = [1];
@@ -1188,6 +1194,7 @@ mod test {
11881194
}
11891195

11901196
#[test]
1197+
#[cfg(feature="std")]
11911198
fn test_thread_rng() {
11921199
let mut r = thread_rng();
11931200
r.gen::<i32>();
@@ -1199,8 +1206,9 @@ mod test {
11991206
}
12001207

12011208
#[test]
1209+
#[cfg(any(feature="std", feature="alloc"))]
12021210
fn test_rng_trait_object() {
1203-
let mut rng = thread_rng();
1211+
let mut rng = rng(109);
12041212
{
12051213
let mut r = &mut rng as &mut Rng;
12061214
r.next_u32();
@@ -1224,6 +1232,7 @@ mod test {
12241232
}
12251233

12261234
#[test]
1235+
#[cfg(feature="std")]
12271236
fn test_random() {
12281237
// not sure how to test this aside from just getting some values
12291238
let _n : usize = random();
@@ -1238,27 +1247,32 @@ mod test {
12381247
}
12391248

12401249
#[test]
1241-
fn test_std_rng_seeded() {
1242-
let s = thread_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
1243-
let mut ra: StdRng = SeedableRng::from_seed(&s[..]);
1244-
let mut rb: StdRng = SeedableRng::from_seed(&s[..]);
1245-
assert!(iter_eq(ra.gen_ascii_chars().take(100),
1246-
rb.gen_ascii_chars().take(100)));
1250+
#[cfg(target_pointer_width = "32")]
1251+
fn test_stdrng_construction() {
1252+
let seed = [1, 23, 456, 7890, 0, 0, 0, 0];
1253+
let mut rng1 = StdRng::from_seed(&seed);
1254+
assert_eq!(rng1.next_u32(), 2869442790);
1255+
1256+
/* FIXME: enable once `from_rng` has landed
1257+
let mut rng2 = StdRng::from_rng(&mut rng1).unwrap();
1258+
assert_eq!(rng1.next_u32(), 3094074039);
1259+
*/
12471260
}
1248-
12491261
#[test]
1250-
fn test_std_rng_reseed() {
1251-
let s = thread_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
1252-
let mut r: StdRng = SeedableRng::from_seed(&s[..]);
1253-
let string1 = r.gen_ascii_chars().take(100).collect::<String>();
1254-
1255-
r.reseed(&s);
1256-
1257-
let string2 = r.gen_ascii_chars().take(100).collect::<String>();
1258-
assert_eq!(string1, string2);
1262+
#[cfg(target_pointer_width = "64")]
1263+
fn test_stdrng_construction() {
1264+
let seed = [1, 23, 456, 7890, 0, 0, 0, 0];
1265+
let mut rng1 = StdRng::from_seed(&seed);
1266+
assert_eq!(rng1.next_u32(), 3477963620);
1267+
1268+
/* FIXME: enable once `from_rng` has landed
1269+
let mut rng2 = StdRng::from_rng(&mut rng1).unwrap();
1270+
assert_eq!(rng1.next_u32(), 3094074039);
1271+
*/
12591272
}
12601273

12611274
#[test]
1275+
#[cfg(feature="std")]
12621276
fn test_weak_rng() {
12631277
let s = weak_rng().gen_iter::<usize>().take(256).collect::<Vec<usize>>();
12641278
let mut ra: StdRng = SeedableRng::from_seed(&s[..]);

0 commit comments

Comments
 (0)