Skip to content

Commit

Permalink
apply rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
torao committed Feb 11, 2020
1 parent 3ac0419 commit d0a25dc
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
- run:
name: rustup component add
command: rustup component add clippy rustfmt
# - run:
# name: fmt
# command: cargo fmt -- --check
- run:
name: fmt
command: cargo fmt -- --check
- restore_cache:
keys:
- v1-cargo-lock-{{ checksum "Cargo.lock" }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.idea/
3 changes: 3 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md
tab_spaces = 2
use_small_heuristics = "Max"
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::cmp::min;

use rand::{Error, RngCore, SeedableRng};

pub mod tinymt64;
pub mod tinymt32;
pub mod tinymt64;

pub struct TinyMT64Seed(pub [u8; 8]);

Expand Down Expand Up @@ -78,7 +78,6 @@ impl RngCore for TinyMT64 {
}
}


pub struct TinyMT32Seed(pub [u8; 4]);

impl From<u32> for TinyMT32Seed {
Expand Down Expand Up @@ -160,7 +159,6 @@ mod test {

#[test]
fn tinymt_usage() {

// from nondeterministic seed
let mut random = TinyMT64::from_entropy();
let rn = random.gen_range(0.0, 1.0);
Expand All @@ -171,4 +169,4 @@ mod test {
let rn = random.gen_range(0.0, 1.0);
assert!(rn >= 0.0 && rn < 1.0);
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("Hello, world!");
println!("Hello, world!");
}
19 changes: 11 additions & 8 deletions src/tinymt32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ fn ini_func2(x: u32) -> u32 {
/// This function certificate the period of 2^127-1.
/// @param random tinymt state vector.
fn period_certification(random: &mut TinyMT32) {
if random.status[0] & TINYMT32_MASK == 0 && random.status[1] == 0 && random.status[2] == 0 && random.status[3] == 0 {
if random.status[0] & TINYMT32_MASK == 0
&& random.status[1] == 0
&& random.status[2] == 0
&& random.status[3] == 0
{
random.status[0] = 'T' as u32;
random.status[1] = 'I' as u32;
random.status[2] = 'N' as u32;
Expand All @@ -48,7 +52,8 @@ pub fn tinymt32_init(random: &mut TinyMT32, seed: u32) {
random.status[3] = random.tmat;
for i in 1..MIN_LOOP {
random.status[i & 3] ^= (i as u32).wrapping_add(
1_812_433_253_u32.wrapping_mul(random.status[(i - 1) & 3] ^ (random.status[(i - 1) & 3] >> 30))
1_812_433_253_u32
.wrapping_mul(random.status[(i - 1) & 3] ^ (random.status[(i - 1) & 3] >> 30)),
);
}
period_certification(random);
Expand All @@ -71,11 +76,7 @@ pub fn tinymt32_init_by_array(random: &mut TinyMT32, init_key: &[u32]) {
st[1] = random.mat1;
st[2] = random.mat2;
st[3] = random.tmat;
let mut count: usize = if key_length + 1 > MIN_LOOP {
key_length + 1
} else {
MIN_LOOP
};
let mut count: usize = if key_length + 1 > MIN_LOOP { key_length + 1 } else { MIN_LOOP };
let mut r: u32 = ini_func1(st[0] ^ st[mid % size] ^ st[(size - 1) % size]);
st[mid % size] = st[mid % size].wrapping_add(r);
r += key_length as u32;
Expand All @@ -101,7 +102,9 @@ pub fn tinymt32_init_by_array(random: &mut TinyMT32, init_key: &[u32]) {
i = (i + 1) % size;
}
for _ in 0..size {
r = ini_func2(st[i % size].wrapping_add(st[(i + mid) % size]).wrapping_add(st[(i + size - 1) % size]));
r = ini_func2(
st[i % size].wrapping_add(st[(i + mid) % size]).wrapping_add(st[(i + size - 1) % size]),
);
st[(i + mid) % size] ^= r;
r -= i as u32;
st[(i + mid + lag) % size] ^= r;
Expand Down
11 changes: 3 additions & 8 deletions src/tinymt64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ pub fn tinymt64_init(random: &mut TinyMT64, seed: u64) {
random.status[1] = (random.mat2 as u64) ^ (random.tmat as u64);
for i in 1..MIN_LOOP {
random.status[i & 1] ^= (i as u64).wrapping_add(
6_364_136_223_846_793_005_u64.wrapping_mul(
random.status[(i - 1) & 1] ^ (random.status[(i - 1) & 1] >> 62)
)
6_364_136_223_846_793_005_u64
.wrapping_mul(random.status[(i - 1) & 1] ^ (random.status[(i - 1) & 1] >> 62)),
);
}
period_certification(random);
Expand All @@ -62,11 +61,7 @@ pub fn tinymt64_init_by_array(random: &mut TinyMT64, init_key: &[u64]) {
let key_length: usize = init_key.len();

let mut st: [u64; 4] = [0, random.mat1 as u64, random.mat2 as u64, random.tmat];
let mut count: usize = if key_length + 1 > MIN_LOOP {
key_length + 1
} else {
MIN_LOOP
};
let mut count: usize = if key_length + 1 > MIN_LOOP { key_length + 1 } else { MIN_LOOP };
let mut r: u64 = ini_func1(st[0] ^ st[mid % size] ^ st[(size - 1) % size]);
st[mid % size] += r;
r += key_length as u64;
Expand Down
13 changes: 9 additions & 4 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use rand::{Rng, RngCore, SeedableRng};

use tinymt::{TinyMT32, TinyMT32Seed, TinyMT64, TinyMT64Seed};

pub mod tinymt64;
pub mod tinymt32;
pub mod tinymt64;

#[test]
fn test_tinymt64_seed() {
assert_eq!(0u64, u64::from_le_bytes(TinyMT64Seed::default().0));
assert_eq!([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01], TinyMT64Seed::from(0x0102030405060708u64).as_mut());
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
TinyMT64Seed::from(0x0102030405060708u64).as_mut()
);
}

#[test]
Expand Down Expand Up @@ -47,7 +50,7 @@ fn test_try_fill_bytes_tinymt32() {
/// by the specified PRING is 95% or higher.
fn test_chi_squared(random: &mut dyn RngCore) {
const DEGREE_OF_FREEDOM: usize = 9;
const THRESHOLD: f64 = 16.92; // 5% for 9 degrees of freedom
const THRESHOLD: f64 = 16.92; // 5% for 9 degrees of freedom
const SAMPLING_COUNT: usize = 1000000;
let mut histogram = [0; DEGREE_OF_FREEDOM + 1];
let length = histogram.len();
Expand All @@ -66,7 +69,9 @@ fn test_try_fill_bytes(random: &mut dyn RngCore) {
let mut histogram = [0u32; 8];
let mut buffer = Vec::<u8>::with_capacity(size);
let mut total = 0;
for _ in 0..size { buffer.push(0) }
for _ in 0..size {
buffer.push(0)
}
for _ in 0..SAMPLING_COUNT / size {
random.try_fill_bytes(&mut buffer).unwrap();
for i in 0..buffer.len() {
Expand Down
2 changes: 1 addition & 1 deletion tests/tinymt32.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tinymt::TinyMT32;
use tinymt::tinymt32::*;
use tinymt::TinyMT32;

/// https://github.com/MersenneTwister-Lab/TinyMT/blob/master/tinymt/check32.c
#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/tinymt64.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate tinymt;

use tinymt::TinyMT64;
use tinymt::tinymt64::*;
use tinymt::TinyMT64;

/// https://github.com/MersenneTwister-Lab/TinyMT/blob/master/tinymt/check64.c
#[test]
Expand Down

0 comments on commit d0a25dc

Please sign in to comment.