Loading…

Property Based Test Generator

A handwritten unit test checks the inputs you already suspected were interesting. Property-based testing checks a general claim - "doubling a positive number stays positive", "FloorLots never returns more than what was requested" - against a few hundred random inputs, and when the claim breaks, does the part a person doing this by hand skips out of fatigue: it narrows the failing input down to the smallest one that still fails. That narrowing is the entire value proposition. A random failure at n = 1,847,203,551 tells you almost nothing actionable. The same failure shrunk to n = 1,073,741,824 tells you exactly where 32-bit integer overflow starts, which is a fact you can act on. The demo in this submission is built around exactly that example - a property broken purely by int32 overflow - and shows the engine converge on the precise boundary. Neither MQL4 nor MQL5 has first-class function pointers, so this cannot use the classic Check(property_function) signature every other language's QuickCheck-style library uses. It follows the same ask-tell shape this catalogue uses for its other generic algorithms (see NelderMeadOptimizer): NextCase() hands back a generated PbtCase - an int, a double and a string, generated together - your own code evaluates whatever property it wants however it wants, and ReportResult(passed) tells the engine whether it held. The engine never sees the property function itself, only pass or fail, which is also what makes it self-contained. Shrinking works one field at a time, by bisection toward zero (int, double) or toward the empty string (string), holding the other two fields at their last confirmed-failing values. A field the property never reads collapses to its simplest value automatically, because nothing about it can ever change the outcome - watch the double and string fields in the demo's counter- example fall to 0 and "" while the int field lands exactly on the bug. The header is self-contained. No dependencies, no DLLs. Randomness comes from its own xorshift32 stream, not MathRand/MathSrand, so a chosen seed reproduces the exact same trial sequence regardless of what else in the same terminal process is reseeding the global generator. Notes and limitations - Shrinking assumes the zero value (or empty string) satisfies the property, and does not verify that assumption by testing it directly. For the vast majority of numeric and string properties this holds; if a property somehow fails even at zero, the reported "minimal" counterexample may not be the true minimum. Documented, not hidden. - Bisection shrinking assumes the failure is roughly monotonic in magnitude - true for overflow, range and boundary bugs, which are most of what this is for. A bug that fires at one isolated, non-monotonic value (rather than "everything past a threshold") may not be found by shrinking even though the original random trial found it; GetCounterexample() would then return a value that still fails but is not the smallest possible one. - Only three field types are generated: int, double, string. There is no generic/templated case type - this cannot cleanly cover generics, and three primitives cover the overwhelming majority of numeric-library and parsing-style properties this catalogue needs to check. - One CPropertyTest instance runs one property at a time. Use a separate instance (or call Start() again) for the next property, exactly as the demo does for its two runs.

Script · MT4 · v1.0 · Free

About Property Based Test Generator

Property Based Test Generator is a script for MetaTrader 4. It has 2 settings, such as Trials and Seed. MQL source code included.

Files in the download: PropertyBasedTesting.mqh, PropertyBasedTesting.txt, PropertyBasedTestingDemo.mq4.

Settings

SettingDefaultWhat it does
Trials300Random trial budget before giving up on finding a failure
Seed7Seed - same seed reproduces the same trial sequence

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria