Skip to content
Gavin Fielder edited this page May 7, 2019 · 25 revisions

Usage

  • ./test help shows some help text and usage examples

The executable accepts the following as queries:

  • ./test moul runs all the enabled tests whose name starts with a string, in this case 'moul'
  • ./test "d_*prec" is a wildcard search; this one runs all the enabled tests that have start with 'd_' and have 'prec' in the name.
  • ./test 42 84 runs (enabled) test #42 through test #84
  • ./test 42 runs test #42 (also turns on debugger compatibility mode.)
  • ./test runs all the enabled tests
  • ./test -d [any of the above queries] runs the selected test(s) in debugger compatibility mode.

Wildcard-based searches have an implict '*' at the end. For example, ./test "*zeropad" runs all the tests that have 'zeropad' anywhere in the name.

Workflow with PFT

unit_tests.c shows you all the tests that are available. Failing a test means that your output and/or return value was not the same as the libc printf. When this happens, there will be a new file, 'results.txt', that holds information about the failed test, the first line of code for the test (most of them are one line anyway), what printf printed, and what ft_printf printed.

How it works, in Brief

The Makefile creates two versions of each unit test function, one that uses ft_printf, and one that uses printf. For each test, it redirects stdout to a file, then compares their return value. If the return value is identical, it opens both files and reads each one byte by byte until both reach EOF. If any single byte differs, the test fails.

Options in the PFT Config File

These options are defined in options-config.ini:

Option Description
DEFAULT_RUN_OPTIONS (string) Determines default program behavior. These are the options that are selected by default at the start of the program; they are overridden by any command line options given at execution time.
TIMEOUT_SECONDS (float) time in seconds for a test to time out, when timeouts are enabled
TEST_OUTDATED_TIME (long) time in seconds for a test result history to become outdated. The default is 900 (15 minutes). You can set this value to 0 to always consider tests outdated, or to 9999999 to never consider test results outdated, or anything in between.
MAKE_RE_ALSO_REBUILDS_LIBFTPRINTF (1 or 0) if 1, make re will also recompile your project
IGNORE_RETURN_VALUE (1 or 0) When this option is 0, tests fail if ft_printf and printf have different return values. If 1, the return value is ignored.
ENABLE_DISABLE_REMAKES_PFT (1 or 0) When this option is 1, the enable-test and disable-test scripts also re-make PFT when finished.
ENABLE_DISABLE_TOUCHES_TEST_HISTORY (1 or 0) When this option is 1, the enable=test and disable-test scripts touch history.csv when finished so as to prevent a Makefile removal of the test history.
REMOVE_HISTORY_WHEN_TESTS_NEW (1 or 0) If this option is 1, the makefile will remove history.csv whenever unit_tests.c is strictly newer.
LEAKS_TEST_CMD (C macro) The command to run at the end of the test run when the leaks test (-k) is selected. The default is to invoke leaks(1) with system(3).
TEST_FAIL_LOGGING_MAXBYTES (int) The maximum size of output strings to show in results.txt
SINGLE_NUMBER_SINGLE_TEST (1 or 0) When this option is 1, single numeric arguments run a single test. If 0, single numeric arguments runs all tests starting at the given number. The latter is a legacy feature, but could still be useful if you are using only your own tests and are writing them as you develop
SINGLE_TEST_TURNS_ON_LLDB_COMPAT_MODE (1 or 0) When this option is 1 (and SINGLE_NUMBER_SINGLE_TEST is also 1), single numeric arguments run the requested test in debugger compatibility mode unless explicitly overridden with command line options.
LIBFTPRINTF_DIR (path) If you do not wish to have PFT inside your local repo, you can specify a different path for your project.

Troubleshooting

I tried making and got a bunch of "undefined symbol" errors
Are these your libft functions? If so, you probably need to follow the installation directions under 'Installation' in order to include your libft.a separately from libftprintf.a

Help! Wildcard search isn't working!
For almost all shell terminals, the * needs to be escaped--usually, putting a string in double quotes is sufficient, but some terminals still treat it as a shell * even then. You can either escape it manually '\*', or, to make this feature compatible with all shells, I've made any character not valid for a C function name (alphanumeric + underscore) is now considered a wildcard. This means instead of \*, you can also use @, or anything else your terminal doesn't recognize as a special character. The same is true for the enable-test and disable-test scripts.

Other Issues

If something goes wrong--slack me @gfielder. I like testing, like people using good testing, and want to make this easier to use, so don't hesitate to contact me. And if you think it's a bug, let me know so I can fix it.

Contributing

I encourage everyone to contribute to this, even if it's just adding tests to the library. To do this, fork and make pull requests.

Before making pull requests, please:

./enable-test && ./disable-test argnum && ./disable-test moul_notmandatory \
&& ./disable-test nocrash && ./disable-test moul_D && ./disable-test moul_F \
&& ./disable-test f_reserved_values_ && ./disable-test f_L_reserved_values_

and if you add non-mandatory test cases or tests that can segfault, modify this block in the readme

Possible Future Features

I have a few ideas how to improve this:

  • The unit test library could stand to be a little more consistent and concise with naming conventions.
  • There are very few tests for the * bonus.
  • Could add tests for the thousands separator optional format flag.
  • Could add tests for the n specifier.

Feel free to give me suggestions, or code them yourself and make a pull request.

Credits

The test method itself was adapted from outdated moulinette test files a buddy gave me, from which the author was [email protected]. The vast majority of code was written by me. The tests prefixed moul_ were adapted from the moulinette test files, the tests with _ftfc_ were adapted from 42FileChecker, and all other tests (so far) were written by me.

Also thanks to: