Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Added basic tests upon LFSR output.
Browse files Browse the repository at this point in the history
* NaiveLFSR is compared against test vectors from my CTAP Assignment.
* CountOnesLFSR and PopCntLFSR are tested against 32KiB of NaiveLFSR output.
  - Quicktest would work at hunting for any valid parameters where this fails.
  • Loading branch information
46bit committed Nov 26, 2016
1 parent f098509 commit c63db7a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
26 changes: 9 additions & 17 deletions src/lfsr/count_ones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lfsr::*;
pub struct CountOnesLFSR {
pub width: usize,
pub taps: Vec<usize>,
pub tapmask: usize,
tapmask: usize,
pub state: usize,
}

Expand All @@ -14,7 +14,7 @@ impl CountOnesLFSR {
CountOnesLFSR {
width: width,
taps: taps.clone(),
tapmask: CountOnesLFSR::calculate_tapmask(taps),
tapmask: Self::calculate_tapmask(taps),
state: seed[0],
}
}
Expand Down Expand Up @@ -64,23 +64,15 @@ impl LFSRTrait for CountOnesLFSR {

#[cfg(test)]
mod tests {
use quickcheck::{Gen, Arbitrary, quickcheck};
use super::*;
pub use lsfr::*;

impl Arbitrary for CountOnesLFSR {
fn arbitrary<G: Gen>(g: &mut G) -> CountOnesLFSR {
let (width, y) = Arbitrary::arbitrary(g);
return CountOnesLFSR::new(width, taps, seed);
}
}

fn clocks_correctly_prop(l: CountOnesLFSR) -> bool {
unimplemented!("UNsure how to check will tick correctly given mutates CountOnesLFSR.")
}

#[test]
fn clocks_correctly() {
quickcheck(clocks_correctly_prop as fn(CountOnesLFSR) -> bool);
fn ticks_as_expected() {
let mut naive_lfsr = NaiveLFSR::new(7, vec![0, 1], vec![44]);
let mut count_ones_lfsr = CountOnesLFSR::new(7, vec![0, 1], vec![44]);

for _ in 0..32768 {
assert!(naive_lfsr.clock() == count_ones_lfsr.clock());
}
}
}
2 changes: 2 additions & 0 deletions src/lfsr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ pub mod naive;
pub mod count_ones;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub mod pop_cnt;
pub mod galois;

pub use naive::*;
pub use count_ones::*;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use pop_cnt::*;
pub use galois::*;
35 changes: 22 additions & 13 deletions src/lfsr/naive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,32 @@ impl LFSRTrait for NaiveLFSR {

#[cfg(test)]
mod tests {
use quickcheck::{Gen, Arbitrary, quickcheck};
use super::*;
pub use lsfr::*;

impl Arbitrary for NaiveLFSR {
fn arbitrary<G: Gen>(g: &mut G) -> NaiveLFSR {
let (width, y) = Arbitrary::arbitrary(g);
return NaiveLFSR::new(width, taps, seed);
}
}
#[test]
fn ticks_as_expected() {
let l1_exps = vec![1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0];
ticks_as_expected_prop(7, vec![0, 6], vec![0b100111], l1_exps);

fn clocks_correctly_prop(l: NaiveLFSR) -> bool {
unimplemented!("UNsure how to check will tick correctly given mutates NaiveLFSR.")
let l2_exps = vec![1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0,
1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 0, 1, 0, 1];
ticks_as_expected_prop(11, vec![0, 9], vec![0b101101101], l2_exps);

let l3_exps = vec![1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0,
1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0];
ticks_as_expected_prop(13, vec![0, 9, 10, 12], vec![7413], l3_exps);
}

#[test]
fn clocks_correctly() {
quickcheck(clocks_correctly_prop as fn(NaiveLFSR) -> bool);
fn ticks_as_expected_prop(width: usize,
taps: Vec<usize>,
seed: Vec<usize>,
expectations: Vec<usize>) {
let mut lfsr = NaiveLFSR::new(width, taps, seed);
for expectation in expectations {
assert!(lfsr.clock() == expectation);
}
}
}
26 changes: 9 additions & 17 deletions src/lfsr/pop_cnt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lfsr::*;
pub struct PopCntLFSR {
pub width: usize,
pub taps: Vec<usize>,
pub tapmask: usize,
tapmask: usize,
pub state: usize,
}

Expand All @@ -14,7 +14,7 @@ impl PopCntLFSR {
PopCntLFSR {
width: width,
taps: taps.clone(),
tapmask: PopCntLFSR::calculate_tapmask(taps),
tapmask: Self::calculate_tapmask(taps),
state: seed[0],
}
}
Expand Down Expand Up @@ -77,23 +77,15 @@ impl LFSRTrait for PopCntLFSR {

#[cfg(test)]
mod tests {
use quickcheck::{Gen, Arbitrary, quickcheck};
use super::*;
pub use lsfr::*;

impl Arbitrary for PopCntLFSR {
fn arbitrary<G: Gen>(g: &mut G) -> PopCntLFSR {
let (width, y) = Arbitrary::arbitrary(g);
return PopCntLFSR::new(width, taps, seed);
}
}

fn clocks_correctly_prop(l: PopCntLFSR) -> bool {
unimplemented!("UNsure how to check will tick correctly given mutates PopCntLFSR.")
}

#[test]
fn clocks_correctly() {
quickcheck(clocks_correctly_prop as fn(PopCntLFSR) -> bool);
fn ticks_as_expected() {
let mut naive_lfsr = NaiveLFSR::new(7, vec![0, 1], vec![44]);
let mut pop_cnt_lfsr = PopCntLFSR::new(7, vec![0, 1], vec![44]);

for _ in 0..32768 {
assert!(naive_lfsr.clock() == pop_cnt_lfsr.clock());
}
}
}

0 comments on commit c63db7a

Please sign in to comment.