Simple testing utility for C programs.
- Compile given source code file with GCC compiler and print out compiler output in case of errors or warnings.
- If the compilation is successful, print out
OK
message. The executable is located in thebuild
directory. - For each test scenario located in the
tests
directory:- Create working directory in the
runs
directory with the templateruns\<test_run>\<test_name>\
where<test_run>
is name of the source code file. - Copy test files to the working directory, except
expected.txt
. - Execute the program in the test working directory with input read from a
input.txt
file and its output redirected tooutput.txt
file. - Compare program output in
output.txt
with the expected output inexpected.txt
in the test directory. - If outputs match, print out
PASSED
message. - If outputs do not match, print out
CHECK
message (manual check required) and actual output and expected output together with comparison table. - If program had no output but it was expected, print out
FAILED
message.
- Create working directory in the
- Each test scenario must be completed within the 1000 millisecond timeout (default, can be changed).
Clone the repository, or download it as a ZIP file.
Put your test scenarios into the tests
directory, each in separate directory with name of the scenario. Each scenario must include these files:
input.txt
which will be redirected to standard input, andexpected.txt
which contains expected output of the program.
Template:
.\tests\<test_name>\<test_files>
For example:
tests\
├── basic_test\
│ ├── input.txt
│ └── expected.txt
└── hard_test\
├── input.txt
└── expected.txt
- Windows PowerShell 5.1 - recommended, lower versions have not been tested
- GCC (compiler)
- Environment variable
PATH
set to the GCCbin
directory
Open Windows PowerShell console and run the script with -SourcePath
parameter set to the source code file to test. The script will build the source file with GCC compiler and run all test scenarios in the tests
directory.
You can put source code file into the src
directory.
& .\Test.ps1 -SourcePath .\src\source.c
To run a specific test, use the -TestsFilter
parameter with name of the test to run.
& .\Test.ps1 -SourcePath .\src\source.c -TestsFilter "basic_test"
To redirect output of the test run into a file, first redirect the Information stream (no. 6) to the Success stream (no. 1), then redirect to a file. If the Information stream was not redirected, the Write-Host
messages would not be written to the file. Write-Host
is used instead of Write-Output
because of support for coloring messages in console window.
& .\Test.ps1 -SourcePath .\src\source.c -TestsFilter "basic_test" 6>&1 | Out-File output.txt
This feature is currently available in the PowerShell version only
Use the -LogMemAlloc
switch parameter to enable logging of calls to standard *alloc
and free
functions in each test run. These functions are wrapped in the memlog.c where they log their call parameters and return values to a log file (default name: memlog.csv
). The log file is evaluated after the test run - invalid calls to free
are listed and a report with the total size of unfreed memory, count of blocks left and size of each block is given.
& .\Test.ps1 -SourcePath .\src\source.c -LogMemAlloc
List of related parameters:
-LogMemAlloc
(switch) - enables logging of dynamic memory management.-MemAllocLogFile [FILE]
(string) - specifies custom name for the log file, use in case thememlog.csv
filename could clash with program files. Default ismemlog.csv
.-MemAllocAcceptedLimit [SIZE]
(int) - specifies accepted limit of unfreed memory in Bytes. Default value is8
.
- GCC
- (optional) rsync
# To run all tests
./Test.sh ./src/source.c
# Display help
./Test.sh --help
# Run tests with filter
./Test.sh ./src/source.c -f "your filter"
Please if you find an error or bug, create issue, so it can be fixed. Instructions:
- Go here
- Click
New issue
in top right corner - Fill out title and description
- Do not assign any tags!
Change the execution policy for current user to either RemoteSigned
or Unrestricted
.
Check the current execution policy settings with:
Get-ExecutionPolicy -List
Run the PowerShell as an Administrator and change the execution policy for CurrentUser
to Unrestricted
(prompts before running scripts) or RemoteSigned
(requires remote scripts to be signed).
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
Run Test.ps1
again, it should work.
If you are done with testing, revert the execution policy to the original setting with Set-ExecutionPolicy
.
More info:
You have set path to the GCC bin
directory as an entry to the PATH
evnironment variable. Doing this allows you to call gcc.exe
(and other executables from the GCC directory) in any directory without using the absolute path.
- Download MinGW distribution of the GCC compiler. By default, it is installed in
C:\MinGW\
directory. - Open
Advanced System Settings
using the Windows search. - Go to the
Advanced
tab. - Press the
Evironment Variables...
button. - Select the
PATH
variable in the list of the user variables and pressEdit...
or create a new one. - Add new entry with path to the GCC bin directory to the list of entries for PATH, for example
C:\MinGW\bin\
. - Save all changes, close all the windows open in these steps.
- Test the new setting by opening a new command line window and running
gcc.exe
. You should see the output of the GCC compiler. - Reopen PowerShell console to update environment variables.
It can! Mac is POSIX-compliant (Unix-like), so try following Linux instructions. If that is not working, please report it. Instructions here. Last resort solution is trying PowerShell (it is not tested though). Although it is native to Microsoft Windows, it is available for MacOS too, see PowerShell repository for instructions. You can also run the tests manually :-)