isaac.rs 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! The ISAAC random number generator.

13 14
#![allow(non_camel_case_types)]

A
Alex Crichton 已提交
15
use core::prelude::*;
16
use core::slice;
A
Aaron Turon 已提交
17 18
use core::iter::repeat;
use core::num::Wrapping as w;
A
Alex Crichton 已提交
19 20

use {Rng, SeedableRng, Rand};
21

22 23 24 25 26 27
type w32 = w<u32>;
type w64 = w<u64>;

const RAND_SIZE_LEN: usize = 8;
const RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
const RAND_SIZE_USIZE: usize = 1 << RAND_SIZE_LEN;
28

29
/// A random number generator that uses the ISAAC algorithm[1].
30
///
31 32
/// The ISAAC algorithm is generally accepted as suitable for
/// cryptographic purposes, but this implementation has not be
P
Piotr Jawniak 已提交
33
/// verified as such. Prefer a generator like `OsRng` that defers to
34 35 36 37
/// the operating system for cases that need high security.
///
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
38
#[derive(Copy)]
39
pub struct IsaacRng {
40
    cnt: u32,
41 42 43 44 45
    rsl: [w32; RAND_SIZE_USIZE],
    mem: [w32; RAND_SIZE_USIZE],
    a: w32,
    b: w32,
    c: w32,
46
}
N
Niko Matsakis 已提交
47

H
Huon Wilson 已提交
48 49
static EMPTY: IsaacRng = IsaacRng {
    cnt: 0,
50 51 52
    rsl: [w(0); RAND_SIZE_USIZE],
    mem: [w(0); RAND_SIZE_USIZE],
    a: w(0), b: w(0), c: w(0),
H
Huon Wilson 已提交
53
};
54 55

impl IsaacRng {
56

57 58 59
    /// Create an ISAAC random number generator using the default
    /// fixed seed.
    pub fn new_unseeded() -> IsaacRng {
H
Huon Wilson 已提交
60
        let mut rng = EMPTY;
61 62 63 64 65 66 67 68
        rng.init(false);
        rng
    }

    /// Initialises `self`. If `use_rsl` is true, then use the current value
    /// of `rsl` as a seed, otherwise construct one algorithmically (not
    /// randomly).
    fn init(&mut self, use_rsl: bool) {
69
        let mut a = w(0x9e3779b9);
70 71 72 73 74 75 76 77
        let mut b = a;
        let mut c = a;
        let mut d = a;
        let mut e = a;
        let mut f = a;
        let mut g = a;
        let mut h = a;

78
        macro_rules! mix {
79
            () => {{
80 81 82 83 84 85 86 87
                a=a^(b<<11); d=d+a; b=b+c;
                b=b^(c>>2);  e=e+b; c=c+d;
                c=c^(d<<8);  f=f+c; d=d+e;
                d=d^(e>>16); g=g+d; e=e+f;
                e=e^(f<<10); h=h+e; f=f+g;
                f=f^(g>>4);  a=a+f; g=g+h;
                g=g^(h<<8);  b=b+g; h=h+a;
                h=h^(a>>9);  c=c+h; a=a+b;
88
            }}
89
        }
90

A
Alfie John 已提交
91
        for _ in 0..4 {
92 93
            mix!();
        }
94 95

        if use_rsl {
96
            macro_rules! memloop {
97
                ($arr:expr) => {{
A
Aaron Turon 已提交
98
                    for i in (0..RAND_SIZE_USIZE).step_by(8) {
99 100 101 102
                        a=a+$arr[i  ]; b=b+$arr[i+1];
                        c=c+$arr[i+2]; d=d+$arr[i+3];
                        e=e+$arr[i+4]; f=f+$arr[i+5];
                        g=g+$arr[i+6]; h=h+$arr[i+7];
103
                        mix!();
104 105 106 107
                        self.mem[i  ]=a; self.mem[i+1]=b;
                        self.mem[i+2]=c; self.mem[i+3]=d;
                        self.mem[i+4]=e; self.mem[i+5]=f;
                        self.mem[i+6]=g; self.mem[i+7]=h;
108 109
                    }
                }}
110
            }
111 112 113 114

            memloop!(self.rsl);
            memloop!(self.mem);
        } else {
A
Aaron Turon 已提交
115
            for i in (0..RAND_SIZE_USIZE).step_by(8) {
116
                mix!();
117 118 119 120
                self.mem[i  ]=a; self.mem[i+1]=b;
                self.mem[i+2]=c; self.mem[i+3]=d;
                self.mem[i+4]=e; self.mem[i+5]=f;
                self.mem[i+6]=g; self.mem[i+7]=h;
121 122 123 124 125 126 127 128
            }
        }

        self.isaac();
    }

    /// Refills the output buffer (`self.rsl`)
    #[inline]
A
Aaron Turon 已提交
129
    #[allow(unsigned_negation)]
130
    fn isaac(&mut self) {
131
        self.c = self.c + w(1);
132 133 134 135
        // abbreviations
        let mut a = self.a;
        let mut b = self.b + self.c;

136
        const MIDPOINT: usize = RAND_SIZE_USIZE / 2;
137

138
        macro_rules! ind {
A
Aaron Turon 已提交
139
            ($x:expr) => (self.mem[($x >> 2).0 as usize & (RAND_SIZE_USIZE - 1)] )
140
        }
141 142

        let r = [(0, MIDPOINT), (MIDPOINT, 0)];
143
        for &(mr_offset, m2_offset) in r.iter() {
J
John Clements 已提交
144

145
            macro_rules! rngstepp {
J
John Clements 已提交
146
                ($j:expr, $shift:expr) => {{
147
                    let base = $j;
148
                    let mix = a << $shift;
J
John Clements 已提交
149

150
                    let x = self.mem[base  + mr_offset];
151 152 153
                    a = (a ^ mix) + self.mem[base + m2_offset];
                    let y = ind!(x) + a + b;
                    self.mem[base + mr_offset] = y;
J
John Clements 已提交
154

155
                    b = ind!(y >> RAND_SIZE_LEN) + x;
156 157 158 159 160
                    self.rsl[base + mr_offset] = b;
                }}
            }

            macro_rules! rngstepn {
J
John Clements 已提交
161
                ($j:expr, $shift:expr) => {{
162
                    let base = $j;
163
                    let mix = a >> $shift;
J
John Clements 已提交
164

165
                    let x = self.mem[base  + mr_offset];
166 167 168
                    a = (a ^ mix) + self.mem[base + m2_offset];
                    let y = ind!(x) + a + b;
                    self.mem[base + mr_offset] = y;
J
John Clements 已提交
169

170
                    b = ind!(y >> RAND_SIZE_LEN) + x;
171 172 173
                    self.rsl[base + mr_offset] = b;
                }}
            }
J
John Clements 已提交
174

A
Aaron Turon 已提交
175
            for i in (0..MIDPOINT).step_by(4) {
176 177 178 179
                rngstepp!(i + 0, 13);
                rngstepn!(i + 1, 6);
                rngstepp!(i + 2, 2);
                rngstepn!(i + 3, 16);
180 181 182 183 184 185 186 187 188
            }
        }

        self.a = a;
        self.b = b;
        self.cnt = RAND_SIZE;
    }
}

S
Simonas Kazlauskas 已提交
189 190 191 192 193 194 195
// Cannot be derived because [u32; 256] does not implement Clone
impl Clone for IsaacRng {
    fn clone(&self) -> IsaacRng {
        *self
    }
}

196 197
impl Rng for IsaacRng {
    #[inline]
198
    fn next_u32(&mut self) -> u32 {
199 200 201 202 203
        if self.cnt == 0 {
            // make some more numbers
            self.isaac();
        }
        self.cnt -= 1;
204 205 206 207 208 209 210 211 212 213 214 215

        // self.cnt is at most RAND_SIZE, but that is before the
        // subtraction above. We want to index without bounds
        // checking, but this could lead to incorrect code if someone
        // misrefactors, so we check, sometimes.
        //
        // (Changes here should be reflected in Isaac64Rng.next_u64.)
        debug_assert!(self.cnt < RAND_SIZE);

        // (the % is cheaply telling the optimiser that we're always
        // in bounds, without unsafe. NB. this is a power of two, so
        // it optimises to a bitwise mask).
216
        self.rsl[(self.cnt % RAND_SIZE) as usize].0
217 218
    }
}
219

E
Erik Price 已提交
220 221
impl<'a> SeedableRng<&'a [u32]> for IsaacRng {
    fn reseed(&mut self, seed: &'a [u32]) {
222 223
        // make the seed into [seed[0], seed[1], ..., seed[seed.len()
        // - 1], 0, 0, ...], to fill rng.rsl.
224
        let seed_iter = seed.iter().cloned().chain(repeat(0));
225

A
Aaron Turon 已提交
226
        for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) {
227
            *rsl_elem = w(seed_elem);
228 229
        }
        self.cnt = 0;
230 231 232
        self.a = w(0);
        self.b = w(0);
        self.c = w(0);
233 234 235 236 237 238 239 240 241

        self.init(true);
    }

    /// Create an ISAAC random number generator with a seed. This can
    /// be any length, although the maximum number of elements used is
    /// 256 and any more will be silently ignored. A generator
    /// constructed with a given seed will generate the same sequence
    /// of values as all other generators constructed with that seed.
E
Erik Price 已提交
242
    fn from_seed(seed: &'a [u32]) -> IsaacRng {
H
Huon Wilson 已提交
243
        let mut rng = EMPTY;
244 245 246 247 248
        rng.reseed(seed);
        rng
    }
}

A
Alex Crichton 已提交
249 250 251 252
impl Rand for IsaacRng {
    fn rand<R: Rng>(other: &mut R) -> IsaacRng {
        let mut ret = EMPTY;
        unsafe {
253
            let ptr = ret.rsl.as_mut_ptr() as *mut u8;
A
Alex Crichton 已提交
254

255
            let slice = slice::from_raw_parts_mut(ptr, RAND_SIZE_USIZE * 4);
256
            other.fill_bytes(slice);
A
Alex Crichton 已提交
257 258
        }
        ret.cnt = 0;
259 260 261
        ret.a = w(0);
        ret.b = w(0);
        ret.c = w(0);
A
Alex Crichton 已提交
262 263 264 265 266

        ret.init(true);
        return ret;
    }
}
267

268 269
const RAND_SIZE_64_LEN: usize = 8;
const RAND_SIZE_64: usize = 1 << RAND_SIZE_64_LEN;
270

271 272 273 274 275
/// A random number generator that uses ISAAC-64[1], the 64-bit
/// variant of the ISAAC algorithm.
///
/// The ISAAC algorithm is generally accepted as suitable for
/// cryptographic purposes, but this implementation has not be
P
Piotr Jawniak 已提交
276
/// verified as such. Prefer a generator like `OsRng` that defers to
277
/// the operating system for cases that need high security.
278
///
279 280
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
281
#[derive(Copy)]
282
pub struct Isaac64Rng {
283 284 285 286 287 288
    cnt: usize,
    rsl: [w64; RAND_SIZE_64],
    mem: [w64; RAND_SIZE_64],
    a: w64,
    b: w64,
    c: w64,
289 290
}

H
Huon Wilson 已提交
291 292
static EMPTY_64: Isaac64Rng = Isaac64Rng {
    cnt: 0,
293 294 295
    rsl: [w(0); RAND_SIZE_64],
    mem: [w(0); RAND_SIZE_64],
    a: w(0), b: w(0), c: w(0),
H
Huon Wilson 已提交
296 297
};

298 299 300 301
impl Isaac64Rng {
    /// Create a 64-bit ISAAC random number generator using the
    /// default fixed seed.
    pub fn new_unseeded() -> Isaac64Rng {
H
Huon Wilson 已提交
302
        let mut rng = EMPTY_64;
303 304 305 306 307 308 309 310
        rng.init(false);
        rng
    }

    /// Initialises `self`. If `use_rsl` is true, then use the current value
    /// of `rsl` as a seed, otherwise construct one algorithmically (not
    /// randomly).
    fn init(&mut self, use_rsl: bool) {
311
        macro_rules! init {
312
            ($var:ident) => (
313
                let mut $var = w(0x9e3779b97f4a7c13);
314
            )
315
        }
316 317 318
        init!(a); init!(b); init!(c); init!(d);
        init!(e); init!(f); init!(g); init!(h);

319
        macro_rules! mix {
320
            () => {{
321 322 323 324 325 326 327 328
                a=a-e; f=f^(h>>9);  h=h+a;
                b=b-f; g=g^(a<<9);  a=a+b;
                c=c-g; h=h^(b>>23); b=b+c;
                d=d-h; a=a^(c<<15); c=c+d;
                e=e-a; b=b^(d>>14); d=d+e;
                f=f-b; c=c^(e<<20); e=e+f;
                g=g-c; d=d^(f>>17); f=f+g;
                h=h-d; e=e^(g<<14); g=g+h;
329
            }}
330
        }
331

A
Alfie John 已提交
332
        for _ in 0..4 {
333 334 335
            mix!();
        }

336
        if use_rsl {
337
            macro_rules! memloop {
338
                ($arr:expr) => {{
339
                    for i in (0..RAND_SIZE_64 / 8).map(|i| i * 8) {
340 341 342 343
                        a=a+$arr[i  ]; b=b+$arr[i+1];
                        c=c+$arr[i+2]; d=d+$arr[i+3];
                        e=e+$arr[i+4]; f=f+$arr[i+5];
                        g=g+$arr[i+6]; h=h+$arr[i+7];
344
                        mix!();
345 346 347 348
                        self.mem[i  ]=a; self.mem[i+1]=b;
                        self.mem[i+2]=c; self.mem[i+3]=d;
                        self.mem[i+4]=e; self.mem[i+5]=f;
                        self.mem[i+6]=g; self.mem[i+7]=h;
349 350
                    }
                }}
351
            }
352 353 354 355

            memloop!(self.rsl);
            memloop!(self.mem);
        } else {
356
            for i in (0..RAND_SIZE_64 / 8).map(|i| i * 8) {
357
                mix!();
358 359 360 361
                self.mem[i  ]=a; self.mem[i+1]=b;
                self.mem[i+2]=c; self.mem[i+3]=d;
                self.mem[i+4]=e; self.mem[i+5]=f;
                self.mem[i+6]=g; self.mem[i+7]=h;
362 363 364 365 366 367 368 369
            }
        }

        self.isaac64();
    }

    /// Refills the output buffer (`self.rsl`)
    fn isaac64(&mut self) {
370
        self.c = self.c + w(1);
371
        // abbreviations
372 373 374 375
        let mut a = self.a;
        let mut b = self.b + self.c;
        const MIDPOINT: usize =  RAND_SIZE_64 / 2;
        const MP_VEC: [(usize, usize); 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
376
        macro_rules! ind {
377
            ($x:expr) => {
378
                *self.mem.get_unchecked((($x >> 3).0 as usize) & (RAND_SIZE_64 - 1))
379
            }
380
        }
381

382
        for &(mr_offset, m2_offset) in MP_VEC.iter() {
383
            for base in (0..MIDPOINT / 4).map(|i| i * 4) {
J
John Clements 已提交
384

385
                macro_rules! rngstepp {
J
John Clements 已提交
386
                    ($j:expr, $shift:expr) => {{
387
                        let base = base + $j;
388
                        let mix = a ^ (a << $shift);
389 390 391
                        let mix = if $j == 0 {!mix} else {mix};

                        unsafe {
392 393 394 395
                            let x = *self.mem.get_unchecked(base + mr_offset);
                            a = mix + *self.mem.get_unchecked(base + m2_offset);
                            let y = ind!(x) + a + b;
                            *self.mem.get_unchecked_mut(base + mr_offset) = y;
396

397 398
                            b = ind!(y >> RAND_SIZE_64_LEN) + x;
                            *self.rsl.get_unchecked_mut(base + mr_offset) = b;
399 400 401 402 403
                        }
                    }}
                }

                macro_rules! rngstepn {
J
John Clements 已提交
404
                    ($j:expr, $shift:expr) => {{
405
                        let base = base + $j;
406
                        let mix = a ^ (a >> $shift);
407 408 409
                        let mix = if $j == 0 {!mix} else {mix};

                        unsafe {
410 411 412 413
                            let x = *self.mem.get_unchecked(base + mr_offset);
                            a = mix + *self.mem.get_unchecked(base + m2_offset);
                            let y = ind!(x) + a + b;
                            *self.mem.get_unchecked_mut(base + mr_offset) = y;
414

415 416
                            b = ind!(y >> RAND_SIZE_64_LEN) + x;
                            *self.rsl.get_unchecked_mut(base + mr_offset) = b;
417 418 419 420
                        }
                    }}
                }

A
Alfie John 已提交
421 422 423 424
                rngstepp!(0, 21);
                rngstepn!(1, 5);
                rngstepp!(2, 12);
                rngstepn!(3, 33);
425 426 427
            }
        }

428 429
        self.a = a;
        self.b = b;
430 431 432 433
        self.cnt = RAND_SIZE_64;
    }
}

S
Simonas Kazlauskas 已提交
434 435 436 437 438 439 440
// Cannot be derived because [u32; 256] does not implement Clone
impl Clone for Isaac64Rng {
    fn clone(&self) -> Isaac64Rng {
        *self
    }
}

441
impl Rng for Isaac64Rng {
442 443 444 445 446 447
    // FIXME #7771: having next_u32 like this should be unnecessary
    #[inline]
    fn next_u32(&mut self) -> u32 {
        self.next_u64() as u32
    }

448 449 450 451 452 453 454
    #[inline]
    fn next_u64(&mut self) -> u64 {
        if self.cnt == 0 {
            // make some more numbers
            self.isaac64();
        }
        self.cnt -= 1;
455 456 457

        // See corresponding location in IsaacRng.next_u32 for
        // explanation.
458
        debug_assert!(self.cnt < RAND_SIZE_64);
459
        self.rsl[(self.cnt % RAND_SIZE_64) as usize].0
460 461 462
    }
}

E
Erik Price 已提交
463 464
impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng {
    fn reseed(&mut self, seed: &'a [u64]) {
465 466
        // make the seed into [seed[0], seed[1], ..., seed[seed.len()
        // - 1], 0, 0, ...], to fill rng.rsl.
467
        let seed_iter = seed.iter().cloned().chain(repeat(0));
468

A
Aaron Turon 已提交
469
        for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) {
470
            *rsl_elem = w(seed_elem);
471 472
        }
        self.cnt = 0;
473 474 475
        self.a = w(0);
        self.b = w(0);
        self.c = w(0);
476 477 478 479 480 481 482 483 484

        self.init(true);
    }

    /// Create an ISAAC random number generator with a seed. This can
    /// be any length, although the maximum number of elements used is
    /// 256 and any more will be silently ignored. A generator
    /// constructed with a given seed will generate the same sequence
    /// of values as all other generators constructed with that seed.
E
Erik Price 已提交
485
    fn from_seed(seed: &'a [u64]) -> Isaac64Rng {
H
Huon Wilson 已提交
486
        let mut rng = EMPTY_64;
487 488 489 490 491
        rng.reseed(seed);
        rng
    }
}

A
Alex Crichton 已提交
492 493 494 495
impl Rand for Isaac64Rng {
    fn rand<R: Rng>(other: &mut R) -> Isaac64Rng {
        let mut ret = EMPTY_64;
        unsafe {
496
            let ptr = ret.rsl.as_mut_ptr() as *mut u8;
A
Alex Crichton 已提交
497

498
            let slice = slice::from_raw_parts_mut(ptr, RAND_SIZE_64 * 8);
499
            other.fill_bytes(slice);
A
Alex Crichton 已提交
500 501
        }
        ret.cnt = 0;
502 503 504
        ret.a = w(0);
        ret.b = w(0);
        ret.c = w(0);
A
Alex Crichton 已提交
505 506 507 508 509 510

        ret.init(true);
        return ret;
    }
}

S
Simonas Kazlauskas 已提交
511

512
#[cfg(test)]
513
mod tests {
514
    use std::prelude::v1::*;
A
Alex Crichton 已提交
515 516 517

    use core::iter::order;
    use {Rng, SeedableRng};
H
Huon Wilson 已提交
518
    use super::{IsaacRng, Isaac64Rng};
519 520

    #[test]
H
Huon Wilson 已提交
521
    fn test_rng_32_rand_seeded() {
A
Alex Crichton 已提交
522
        let s = ::test::rng().gen_iter::<u32>().take(256).collect::<Vec<u32>>();
523 524
        let mut ra: IsaacRng = SeedableRng::from_seed(&s[..]);
        let mut rb: IsaacRng = SeedableRng::from_seed(&s[..]);
A
Alex Crichton 已提交
525 526
        assert!(order::equals(ra.gen_ascii_chars().take(100),
                              rb.gen_ascii_chars().take(100)));
H
Huon Wilson 已提交
527 528 529
    }
    #[test]
    fn test_rng_64_rand_seeded() {
A
Alex Crichton 已提交
530
        let s = ::test::rng().gen_iter::<u64>().take(256).collect::<Vec<u64>>();
531 532
        let mut ra: Isaac64Rng = SeedableRng::from_seed(&s[..]);
        let mut rb: Isaac64Rng = SeedableRng::from_seed(&s[..]);
A
Alex Crichton 已提交
533 534
        assert!(order::equals(ra.gen_ascii_chars().take(100),
                              rb.gen_ascii_chars().take(100)));
535 536 537
    }

    #[test]
H
Huon Wilson 已提交
538
    fn test_rng_32_seeded() {
539
        let seed: &[_] = &[1, 23, 456, 7890, 12345];
540 541
        let mut ra: IsaacRng = SeedableRng::from_seed(seed);
        let mut rb: IsaacRng = SeedableRng::from_seed(seed);
A
Alex Crichton 已提交
542 543
        assert!(order::equals(ra.gen_ascii_chars().take(100),
                              rb.gen_ascii_chars().take(100)));
H
Huon Wilson 已提交
544 545 546
    }
    #[test]
    fn test_rng_64_seeded() {
547
        let seed: &[_] = &[1, 23, 456, 7890, 12345];
548 549
        let mut ra: Isaac64Rng = SeedableRng::from_seed(seed);
        let mut rb: Isaac64Rng = SeedableRng::from_seed(seed);
A
Alex Crichton 已提交
550 551
        assert!(order::equals(ra.gen_ascii_chars().take(100),
                              rb.gen_ascii_chars().take(100)));
552 553
    }

H
Huon Wilson 已提交
554 555
    #[test]
    fn test_rng_32_reseed() {
A
Alex Crichton 已提交
556
        let s = ::test::rng().gen_iter::<u32>().take(256).collect::<Vec<u32>>();
557
        let mut r: IsaacRng = SeedableRng::from_seed(&s[..]);
A
Alex Crichton 已提交
558
        let string1: String = r.gen_ascii_chars().take(100).collect();
H
Huon Wilson 已提交
559

560
        r.reseed(&s);
H
Huon Wilson 已提交
561

A
Alex Crichton 已提交
562
        let string2: String = r.gen_ascii_chars().take(100).collect();
H
Huon Wilson 已提交
563 564 565 566
        assert_eq!(string1, string2);
    }
    #[test]
    fn test_rng_64_reseed() {
A
Alex Crichton 已提交
567
        let s = ::test::rng().gen_iter::<u64>().take(256).collect::<Vec<u64>>();
568
        let mut r: Isaac64Rng = SeedableRng::from_seed(&s[..]);
A
Alex Crichton 已提交
569
        let string1: String = r.gen_ascii_chars().take(100).collect();
H
Huon Wilson 已提交
570

571
        r.reseed(&s);
H
Huon Wilson 已提交
572

A
Alex Crichton 已提交
573
        let string2: String = r.gen_ascii_chars().take(100).collect();
H
Huon Wilson 已提交
574 575 576
        assert_eq!(string1, string2);
    }

577
    #[test]
578
    fn test_rng_32_true_values() {
579
        let seed: &[_] = &[1, 23, 456, 7890, 12345];
580
        let mut ra: IsaacRng = SeedableRng::from_seed(seed);
581
        // Regression test that isaac is actually using the above vector
582
        let v = (0..10).map(|_| ra.next_u32()).collect::<Vec<_>>();
583
        assert_eq!(v,
584 585
                   vec!(2558573138, 873787463, 263499565, 2103644246, 3595684709,
                        4203127393, 264982119, 2765226902, 2737944514, 3900253796));
586

587
        let seed: &[_] = &[12345, 67890, 54321, 9876];
588 589
        let mut rb: IsaacRng = SeedableRng::from_seed(seed);
        // skip forward to the 10000th number
A
Alfie John 已提交
590
        for _ in 0..10000 { rb.next_u32(); }
591

592
        let v = (0..10).map(|_| rb.next_u32()).collect::<Vec<_>>();
593
        assert_eq!(v,
594 595
                   vec!(3676831399, 3183332890, 2834741178, 3854698763, 2717568474,
                        1576568959, 3507990155, 179069555, 141456972, 2478885421));
596 597 598
    }
    #[test]
    fn test_rng_64_true_values() {
599
        let seed: &[_] = &[1, 23, 456, 7890, 12345];
600
        let mut ra: Isaac64Rng = SeedableRng::from_seed(seed);
601
        // Regression test that isaac is actually using the above vector
602
        let v = (0..10).map(|_| ra.next_u64()).collect::<Vec<_>>();
603
        assert_eq!(v,
604 605 606 607
                   vec!(547121783600835980, 14377643087320773276, 17351601304698403469,
                        1238879483818134882, 11952566807690396487, 13970131091560099343,
                        4469761996653280935, 15552757044682284409, 6860251611068737823,
                        13722198873481261842));
608

609
        let seed: &[_] = &[12345, 67890, 54321, 9876];
610 611
        let mut rb: Isaac64Rng = SeedableRng::from_seed(seed);
        // skip forward to the 10000th number
A
Alfie John 已提交
612
        for _ in 0..10000 { rb.next_u64(); }
613

614
        let v = (0..10).map(|_| rb.next_u64()).collect::<Vec<_>>();
615
        assert_eq!(v,
616 617 618 619
                   vec!(18143823860592706164, 8491801882678285927, 2699425367717515619,
                        17196852593171130876, 2606123525235546165, 15790932315217671084,
                        596345674630742204, 9947027391921273664, 11788097613744130851,
                        10391409374914919106));
620
    }
621 622 623 624 625 626

    #[test]
    fn test_rng_clone() {
        let seed: &[_] = &[1, 23, 456, 7890, 12345];
        let mut rng: Isaac64Rng = SeedableRng::from_seed(seed);
        let mut clone = rng.clone();
A
Alfie John 已提交
627
        for _ in 0..16 {
628 629 630
            assert_eq!(rng.next_u64(), clone.next_u64());
        }
    }
631
}