Add tests for part of utils

master
Simon Bruder 2020-06-26 23:48:34 +02:00
parent 4f6c971ec7
commit 7bbc416ac6
No known key found for this signature in database
GPG Key ID: 6F03E0000CC5B62F
5 changed files with 146 additions and 2 deletions

89
Cargo.lock generated
View File

@ -55,6 +55,8 @@ dependencies = [
"num-derive",
"num-traits",
"pretty_env_logger",
"quickcheck",
"quickcheck_macros",
"thiserror",
"zip",
]
@ -137,6 +139,17 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "heck"
version = "0.3.1"
@ -241,6 +254,12 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b18befed8bc2b61abc79a457295e7e838417326da1586050b919414073977f19"
[[package]]
name = "ppv-lite86"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "pretty_env_logger"
version = "0.4.0"
@ -292,6 +311,29 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quickcheck"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f"
dependencies = [
"env_logger",
"log",
"rand",
"rand_core",
]
[[package]]
name = "quickcheck_macros"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608c156fd8e97febc07dc9c2e2c80bf74cfc6ef26893eae3daf8bc2bc94a4b7f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "quote"
version = "1.0.7"
@ -301,6 +343,47 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "regex"
version = "1.3.9"
@ -424,6 +507,12 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.3.8"

View File

@ -14,3 +14,7 @@ num-traits = "0.2"
pretty_env_logger = "0.4"
thiserror = "1.0.20"
zip = { version = "0.5.5", default-features = false, features = ["deflate"] }
[dev-dependencies]
quickcheck = "0.9.2"
quickcheck_macros = "0.9.1"

View File

@ -58,7 +58,7 @@ Basic Usage:
This is my first rust project. Dont expect too much from the code in terms of
quality, robustness or idiomacity (especially regarding error handling). There
currently are no tests.
currently are no tests for the actual functionality.
Large portions of this tool would not have been possible without the following
resources:

View File

@ -1,3 +1,7 @@
#[cfg(test)]
#[macro_use(quickcheck)]
extern crate quickcheck_macros;
pub mod converter;
pub mod ddr;
mod mini_parser;

View File

@ -1,6 +1,6 @@
use std::fmt;
pub fn get_nth_bit(byte: u8, n: u8) -> bool {
fn get_nth_bit(byte: u8, n: u8) -> bool {
((byte & (0b1 << n)) >> n) != 0
}
@ -12,6 +12,16 @@ pub fn byte_to_bitarray(byte: u8) -> [bool; 8] {
bitarray
}
#[allow(dead_code)]
/// Used to test `byte_to_bitarray`
pub fn bitarray_to_byte(bitarray: [bool; 8]) -> u8 {
bitarray
.iter()
.enumerate()
.map(|(i, bit)| (*bit as u8) << i)
.sum()
}
pub fn join_display_values<T: fmt::Display>(iterable: Vec<T>, separator: &'_ str) -> String {
iterable
.iter()
@ -19,3 +29,40 @@ pub fn join_display_values<T: fmt::Display>(iterable: Vec<T>, separator: &'_ str
.collect::<Vec<_>>()
.join(&separator)
}
#[cfg(test)]
mod tests {
use super::*;
use quickcheck::TestResult;
/// Helper function to test bitarray functions
fn vec_to_arr_bool_8(vector: Vec<bool>) -> [bool; 8] {
let mut array = [false; 8];
for (arr_el, vec_el) in array.iter_mut().zip(vector.into_iter()) {
*arr_el = vec_el;
}
array
}
#[quickcheck]
fn test_vec_to_arr_bool_8(vector: Vec<bool>) -> TestResult {
if vector.len() != 8 {
return TestResult::discard();
}
TestResult::from_bool(vec_to_arr_bool_8(vector.clone()).to_vec() == vector)
}
#[quickcheck]
fn test_byte_to_bitarray(byte: u8) -> bool {
bitarray_to_byte(byte_to_bitarray(byte)) == byte
}
#[quickcheck]
fn test_bitarray_to_byte(bitvec: Vec<bool>) -> TestResult {
if bitvec.len() != 8 {
return TestResult::discard();
}
let bitarray = vec_to_arr_bool_8(bitvec);
TestResult::from_bool(byte_to_bitarray(bitarray_to_byte(bitarray)) == bitarray)
}
}