Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Product rule
- If one choice has a options and an independent choice has b, together they have a × b combinations.
- Configuration Also job
- One combination of values, one from each dimension, such as Linux, Python 3.12, Postgres, x64.
- Exclude rule
- A pattern that fixes some dimensions and leaves the rest free; it removes every configuration that matches it.
- Inclusion–exclusion
- Count a union by adding the sets, subtracting every pairwise overlap, adding back every triple overlap, and so on with alternating signs.
- Pairwise coverage
- A smaller suite in which every pair of values from two dimensions appears in some job. It can miss a fault that needs three specific values together.
Why this matters
Test matrices, feature-flag combinations, component states and permission sets grow by multiplication. Counting them without listing them tells you whether a plan fits a budget and how many cases a rule really removes. Getting an overlap wrong either drops coverage you could afford or schedules jobs you cannot run.
This lesson assumes multiplication and reading a table. Whether the dimensions capture every real difference between environments is a judgment the count cannot make.
Count choices and the sets rules remove
Combinatorics counts discrete possibilities. A configuration chooses one value from each dimension. When every combination is permitted, multiply the number of choices in each dimension to count the full set; this is the product rule.
An exclusion pattern describes a subset of those configurations. Fixed dimensions restrict the subset, while unspecified dimensions still contribute all their choices. Two patterns can describe some of the same configurations. Counting the removed set therefore requires tracking overlaps so each excluded configuration is removed once.
01Define the dimensions
List the dimensions and the rules
A library wants every pull-request matrix job to start in one parallel wave on its 60-runner pool. Its planner counts 72 combinations and subtracts one job for each of two exclude rules: 70 jobs. A proposed fix drops a Python version. Do those rule entries really exclude only two jobs?
| Dimension | Values | Count |
|---|---|---|
| OS | Linux, macOS, Windows | 3 |
| Python | 3.10, 3.11, 3.12, 3.13 | 4 |
| Database | SQLite, Postgres, MySQL | 3 |
| Architecture | x64, arm64 | 2 |
| Exclude A | Windows on arm64 | |
| Exclude B | MySQL on arm64 |
02Multiply, then exclude
Multiply, then size each rule
Every job picks one value per dimension, so the full matrix has 3 × 4 × 3 × 2 = 72 jobs. An exclude rule fixes some dimensions and leaves the rest free.
03Add back the overlap
Add back what two rules both remove
“MySQL on arm64” also removes 3 × 4 = 12 jobs. The first repair subtracts those matched-job counts from 72 and gets 48, but some jobs match both rules: Windows, arm64 and MySQL, with any Python version. They were subtracted twice.
const sizes = { os: 3, python: 4, db: 3, arch: 2 };
const all = sizes.os * sizes.python * sizes.db * sizes.arch;
const windowsArm = sizes.python * sizes.db; // os and arch fixed
const armMysql = sizes.os * sizes.python; // arch and db fixed
const both = sizes.python; // os, arch and db fixed
console.log(`jobs ${all}`);
console.log(`excluded ${windowsArm} + ${armMysql} - ${both}`);
console.log(`valid ${all - windowsArm - armMysql + both}`);
Each version prints jobs 72, the excluded counts and valid 52.
Two rules that fix the same dimension to different values, such as “Windows on arm64” and
“macOS with MySQL”, cannot match the same job, so their overlap is zero.
04More than two rules
More than two rules
With three or more rules, a job matched by three of them is subtracted three times, added back three times by the pairwise overlaps, and so is not removed at all. Inclusion–exclusion continues: subtract each single rule, add each pair’s overlap, subtract each triple’s, add each quadruple’s, with alternating signs over every subset of rules.
Counting this way does not list jobs, which matters when the matrix is enormous: five dimensions of 1,000 values is 10¹⁵ jobs, far too many to enumerate, but inclusion–exclusion over five rules takes 32 small products.
number represents every integer exactly only up to 2⁵³ ≈ 9.0 × 10¹⁵. Beyond that range some integers are still representable, but arbitrary exclusion counts need not be. Use BigInt for exact counts outside the lab’s 10¹⁵-job bound.
05Vary inputs, then decide
Vary one input at a time, then decide
| Change | Jobs | Fits 60? |
|---|---|---|
| Both rules, as listed | 52 | yes |
| Original planner: one job per exclude entry | 70 | no, but wrong |
| First repair (overlap subtracted twice) | 48 | yes, but wrong |
| No exclude rules | 72 | no |
| Drop Python 3.10, both rules | 39 | yes |
| Add Python 3.14, both rules | 65 | no |
A pairwise suite covers every pair of values from any two dimensions in far fewer jobs. It can still miss a bug that needs three particular values together, such as one Python version, MySQL and arm64. Choosing it trades that risk for runner time.
06Count the matrix
Build one matrix counter
Replace the planner’s one-job-per-rule assumption with the number of combinations a pattern actually matches. Then account for jobs matched by two rules. When teams add more exclusions, the same accounting must handle every overlap without enumerating a huge matrix. The program supplies the other working helpers for each task.
A CI matrix
Size a rule, add back an overlap, then count with many rules at scale.
In the frontend A component state matrix in Next.js and Svelte Optional
Visual regression pages render every combination of a component’s props. Build the list with flatMap and a filter per design rule, and let the heading print the list’s length instead of a number worked out by hand. Here 3 sizes × 2 tones × 3 states is 18, and the rule against small danger buttons removes 3, leaving 15.
import { Button } from "@/components/button";
const sizes = ["sm", "md", "lg"] as const;
const tones = ["neutral", "danger"] as const;
const states = ["idle", "loading", "disabled"] as const;
// Design-system rule: no small danger buttons.
const cases = sizes
.flatMap((size) =>
tones.flatMap((tone) => states.map((state) => ({ size, tone, state })))
)
.filter((c) => !(c.size === "sm" && c.tone === "danger"));
export default function ButtonMatrix() {
return (
<section>
<h2>{cases.length} button states</h2>
{cases.map((c) => (
<Button
key={`${c.size}-${c.tone}-${c.state}`}
size={c.size}
tone={c.tone}
state={c.state}
>
Save
</Button>
))}
</section>
);
}
Check this idea later
Sources
- GitHub Actions: matrix include and exclude
- NIST: combinatorial testing
- ECMAScript: Number.MAX_SAFE_INTEGER
The matrix and runner pool are a worked model, not a measured CI system.
Sign in to keep your read marks on every device. Sign in
Was this helpful?