Apache 2.0 licensed

Test generator

The test-generator is a test-function-generator.

Use this macro if you need to test a single feature with various independent input files or if you want to parallelize the test over all elements of a large array.

This macro obsoletes copy-paste of test-functions.

Test example: Generate for each file matching the pattern

Generate a test-function call for each file matching the pattern ``` extern crate test_generator;

[cfg(test)]

mod tests { testgenerator::testexpandpaths! { testexists; "data/*" }

fn test_exists(dir_name: &str) { assert!(std::path::Path::new(dir_name).exists()); }

} Assuming `"data/*"` expands to "data/set1", and "data/set2" the macro will expand to mod tests { #[test] fn testexistsdataset1() { testexists("data/set1"); }

#[test]
fn test_exists_data_set2() {
    test_exists("data/set2");
}

} ```

Test example: Generate for each list-element

Generate a test-function call for each list-element ``` extern crate testgenerator; #[cfg(test)] mod tests { testgenerator::testexpandlist! { test_size; [ 10, 100, 1000 ]}

fn test_size(value: &usize) { assert!( *value > 0 ); }

const VEC1 = [ 1, 2, 3, 4 ]; /* speaking array names */ const VEC2 = [ 5, 6, 7, 8 ]; testgenerator::testexpandlist! { testarraysize; [ &VEC1, &VEC2 ]} testgenerator::testexpandlist! { testarraysize; [ [1, 2, 3, 4], [ 5, 6, 7, 8 ]}

fn testarraysize(ar: &[T]) { assert!(ar.len() > 0); } } Will expand to test-functions incorporating the array-elements mod tests { #[test] fn testsize0000000010() { testsize(&10); } #[test] fn testsize0000000100() { testsize(&100); } #[test] fn testsize0000001000() { test_size(&1000); }

#[test]
fn test_array_size_VEC1() { test_array_size( &VEC1 ); }
#[test]
fn test_array_size_VEC2() { test_array_size( &VEC2 ); }

#[test]
fn test_array_size_01020304() { test_array_size( &[ 1, 2, 3, 4 ] ); }
fn test_array_size_05060708() { test_array_size( &[ 5, 6, 7, 8 ] ); }

} ```

Benchmarking example: Generate for each each file matching the pattern

Generate a benchmark-function call for each file matching the pattern ``` extern crate testgenerator; #[cfg(test)] mod tests { testgenerator::benchexpandpaths! { bench_exists; "data/*" }

fn benchexists(bencher: &mut test::Bencher, filename: &str) { let path = std::path::Path::new(filename); b.iter(|| { path.exists() }); } } Assuming `"data/*"` expands to "data/set1", and "data/set2" the macro will expand to mod tests { #[bench] fn benchexistsdataset1(bencher: & mut test::Bencher) { bench_exists(bencher, "data/set1"); }

#[bench]
fn bench_exists_data_set2(bencher: & mut test::Bencher) {
    bench_exists(bencher, "data/set2");
}

} ```

Benchmark example: Generate for each list-element

Generate a benchmark-function call for each list-element ``` extern crate testgenerator; #[cfg(test)] mod tests { testgenerator::benchexpandlist! { bench_size; [ 10, 100, 1000 ]}

fn benchsize(b: &mut test::Bencher, val: &usize) { let input = val; b.iter(|| { *input > 0 }); } } Will expand to bench-functions incorporating the array-elements mod tests { #[bench] fn benchsize0000000010(bencher: & mut test::Bencher) { benchexists(bencher, &10); } #[bench] fn benchsize0000000100(bencher: & mut test::Bencher) { benchexists(bencher, &100); } #[bench] fn benchsize0000001000(bencher: & mut test::Bencher) { benchexists(bencher, &1000); } } ```

Please note, the generated function names are unique, formed by the user defined test-function and the input-data. So, changing the list of files or the test-input, this will be reflected in the generated function-name, too.

Every time the macro is executed, for each entry in file-system or the array-listing, a corresponding test-function is generated.

The signature of the user's test-function or bench-function must declare an additional reference-parameter, please see above of example.

The generated tests are regular test-functions of the Rust test-framework, and will be executed in parallel.

Adding to your project:

Add the following line to the project file Cargo.toml

Cargo.toml ... edition = "2018" ... [dev-dependencies] test-generator = "^0.2"

Limitations/Behavior

Test output

This crate is shipped with an example. Invoking the following cargo command in the folder ./example, the following results are print to console. console $ cargo test will produce a test-output for both sub-folders according to the pattern "data/*": * "data/set1" and * "data/set2" * "data/set3"

``` running 24 tests test tests::benchexistsdataset1 ... ok test tests::benchexistsdataset2 ... ok test tests::benchexistsdataset3 ... ok test tests::benchsize0000000010 ... ok test tests::benchsize0000000100 ... ok test tests::benchsize0000001000 ... ok test tests::testarraysize01020304 ... ok test tests::testarraysize0102030405060708090a0b0c ... ok test tests::testarraysize05060708 ... ok test tests::testarraysize2a2b2c2d2e2f3031323334 ... ok test tests::testarraysizeVEC1 ... ok test tests::testarraysizeVEC2 ... ok test tests::testsize0000000001 ... ok test tests::testsize0000000002 ... ok test tests::testexistsdataset1 ... ok test tests::testexistsdataset2 ... ok test tests::testexistsdataset3 ... ok test tests::teststringsizewelt ... ok test tests::teststringsizehallo ... ok test tests::testarraysize0000000000000156000000000000015700000000000001580000000000000159000000000000015a000000000000015b0000 ... ok test tests::testarraysize000000000000012d000000000000012e000000000000012f0000000000000130000000000000013100000000000001320000 ... ok

test result: ok. 24 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ```