|
valFuzz 1.2.0
|
Welcome to the official valFuzz documentation. This guide provides comprehensive information and practical examples to help you use valFuzz for your testing needs.
valFuzz is a lightweight testing, fuzzing and benchmarking framework for C++17 onwards. It uses modern standards and techniques to provide a robust and efficient framework for testing and fuzzing for small-medium size projects. The simpliciry of the library allows for easy extension and customization for your own need.
You can compile the library as a shared library or a static library using cmake. valfuzz has no dependencies other than the standard library.
The library provides various macros for assertions to be used in your tests:
ASSERT - Asserts that the given expression is true.ASSERT_EQ - Asserts that the two given values are equal.ASSERT_NE - Asserts that the two given values are not equal.ASSERT_LT - Asserts that the first value is less than the second value.ASSERT_LE - Asserts that the first value is less than or equal to the second value.ASSERT_GT - Asserts that the first value is greater than the second value.ASSERT_GE - Asserts that the first value is greater than or equal to the second value.ASSERT_THROW - Asserts that the given expression throws an exception of the given type.ASSERT_NO_THROW - Asserts that the given expression does not throw any exception.For example:
When an assertion fails, the program will output the test name, the line number and the assertion arguments that caused the failure. The program will then continue to run the other tests.
To run your first test, you need to define a test function using the TEST macro. You will have to specify an unique name for the test and a string name for display purposes.
Tests are automatically registered and run when the program starts. You can run a particular test by specifying the test name after --test as a command line argument:
It is often necessary to perform some setup and cleanup operations before tests are run. You can define those functions using the BEFORE and AFTER macros respectively. The setup function will be run before all tests and the cleanup function will be run after all tests.
valFuzz provides a simple interface for fuzzing your code. You can define a fuzz function using the FUZZME macro, this function will be called repedetly on multiple threads until you stop the program. You can disable multithreading by passing --no-multithread as a command line argument.
You can get random values using the T get_random<T>() function, where T is the type of the value you want to get. The function will return a random value of the given type, allowed types are:
intfloatdoublecharstd::stringAfter defining the fuzz function, you can run it by passing --fuzz as a command line argument:
You can define a benchmark function with the macro BENCHMARK. Inside the benchmark, you can do any setup / cleanup and run an expression to be benchmarked with BENCHMARK_RUN.
The first argument to BENCHMARK_RUN is the input size, and the second argument is the expression to be benchmarked. The input size is specified for logging purposes only and can be set to any value that can be printed with std::cout.
currently, the cache will be reset between benchmarks only on linux
For example:
Compile and run with --benchmark flag:
The benchmark will be run 100000 times to get the average time. You can set the number of iterations using the --num-iterations flag.
You can save the results of your benchmark in a CSV file by passing --report <file> as a command line argument. The output will be saved using the following format:
After you have collected the benchmark data, you can generate plots and graphs using python templates and examples provided in the plotting directory. The templates use seaborn and matplotlib libraries to generate the graphs.
All tests and fuzz functions are run on multiple threads by default. You can disable multithreading by passing --no-multithread as a command line argument.
You can pass the following command line arguments to the program:
--test <test_name> - Run a specific test by name.--fuzz - Run all fuzz functions.--fuzz <fuzz_name> - Run a specific fuzz function by name.--benchmark - Run benchmarks.--num-iterations <num> - Set the number of iterations for benchmarks.--run-one-benchmark <name> - run a specific benchmark--no-multithread - Disable multithreading.--verbose - Enable verbose output.--max-threads <n> - Set the maximum number of threads to use.--no-header - Disable the header print at the start.--seed <seed> - Set the seed for the random number generator.--report <file> - Save benchmark report to a file.--help - Display help information.