Meaning
Defines a test function that asserts the commutative property of addition for two operands.
Primary Function
Verify that swapping the operands of an addition operation does not change the result.
Communicative Purpose
Assert that an operation is commutative, documenting an algebraic property in executable form.
Pattern
def test_<name>(<arg1>, <arg2>): assert <arg1> <op> <arg2> == <arg2> <op> <arg1>
Core Structure
def test_<name>(<params>): assert <expr>
Função primária
Verify that swapping the operands of an addition operation does not change the result.
Propósito comunicativo
Assert that an operation is commutative, documenting an algebraic property in executable form.
Situações de gatilho
When writing unit tests for numeric types, custom classes with __add__, or any domain where commutativity is expected.
Contextos
Unit test suites, property‑based testing suites, educational examples of algebraic properties.
Padrão
def test_<name>(<arg1>, <arg2>): assert <arg1> <op> <arg2> == <arg2> <op> <arg1>
Estrutura central
def test_<name>(<params>): assert <expr>
Slots de substituição
a: any object supporting the + operator, b: any object supporting the + operator, function_name: identifier (e.g., test_add_commutes)
Colocados típicos
- assertEqual
- pytest
- unittest
- hypothesis
- property‑based testing
Substituições comuns
- Using assertEqual for clearer failure messages
- using != to test non‑commutativity
- substituting * for + to test multiplication commutativity.
Erros comuns
Using assignment (=) instead of equality (==): cause: confusing assignment with comparison; consequence: SyntaxError. Omitting the assert statement: cause: forgetting to assert; consequence: expression is evaluated but the test passes regardless of the result. Using a non‑commutative operator (e.g., subtraction): cause: misunderstanding which operators commute; consequence: test fails incorrectly. Floating‑point equality without tolerance: cause: assuming exact equality for floats; consequence: false failures due to rounding errors. Incorrect test function naming (not starting with test_): cause: test runner ignores the function; consequence: test never executed.
Similar / contraste
test_associative: checks (a + b) + c == a + (b + c) – tests associativity rather than commutativity. test_distributive: checks a * (b + c) == a * b + a * c – tests distribution of multiplication over addition. test_identity: checks a + 0 == a – tests the identity element for addition.
Interferências
Coming from C/Fortran: may assume floating‑point addition is exactly commutative; recall that rounding can break exact equality, so consider using a tolerance. Coming from Java: may prefer assertEquals(a+b, b+a) over plain assert; both work but assert gives less diagnostic detail. Coming from Haskell: may rely on type‑class constraints and forget to constrain the arguments to a Num‑like type, leading to type errors.
Família do chunk
- test_associative
- test_distributive
- test_identity
- test_inverse
Nuance
Do not use this test for non‑commutative operations such as subtraction, matrix multiplication, or string concatenation. The test has negligible runtime cost; the dominant cost is the operation being tested. For floating‑point numbers, exact equality can fail due to rounding; consider using math.isclose or a tolerance‑based assert instead of plain ==
Efeito pragmático
Guards against subtle bugs in algebraic code and gives confidence that refactorings that rely on commutativity (e.g., reordering terms) will not change program behavior.
Dica de memória
Think of addition as a dance where partners can swap places and still end up in the same spot – the result doesn’t change who they’re dancing with.
Nota
In property‑based testing frameworks like Hypothesis, this property can be expressed as a rule that holds for all generated inputs, increasing confidence beyond hand‑picked examples.
Upgrade path
Move to property‑based testing with libraries like Hypothesis to verify the commutative property across thousands of random inputs.
Log in to save chunks.