summaryrefslogtreecommitdiffstats
path: root/docs/Reference/LuaUnitAssertionFunctions/0_GeneralAssertions.md
blob: 6282755a8e05c1baea003111f1279d7dd1e7ea78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# General Assertions

* **_AFT.assertEquals(actual, expected)**

    Assert that two values are equal.

    For tables, the comparison is a deep comparison :

  * number of elements must be the same
  * tables must contain the same keys
  * each key must contain the same values. The values are also compared recursively with deep comparison.

    LuaUnit provides other table-related assertions, see [Table assertions](http://luaunit.readthedocs.io/en/luaunit_v3_2_1/#assert-table)

    ```lua
    _AFT.assertEquals("string", "string")
    ```

* **_AFT.assertNotEquals(actual, expected)**

    Assert that two values are different. The assertion fails if the two values are identical.

    It also uses table deep comparison.
    
    ```lua
    _AFT.assertNotEquals("string", "Another string")
    ```

* **_AFT.assertAlmostEquals(actual, expected, margin)**

    Assert that two floating point numbers are almost equal.

    When comparing floating point numbers, strict equality does not work.
    Computer arithmetic is so that an operation that mathematically yields
    1.00000000 might yield 0.999999999999 in lua . That’s why you need an
    almost equals comparison, where you specify the error margin.
    
    ```lua
    -- convert pi/6 radian to 30 degree
    pi_div_6_deg_calculated = math.deg(math.pi/6)
    pi_div_6_deg_expected = 30

    -- convert pi/3 radian to 60 degree
    pi_div_3_deg_calculated = math.deg(math.pi/3)
    pi_div_3_deg_expected = 60

    -- check absolute error: it is not constant
    print( (pi_div_6_deg_expected - pi_div_6_deg_calculated) / lu.EPS ) -- prints: 16
    print( (pi_div_3_deg_expected - pi_div_3_deg_calculated) / lu.EPS ) -- prints: 32

    -- Better use relative error:
    print( ( (pi_div_6_deg_expected - pi_div_6_deg_calculated) / pi_div_6_deg_expected) / lu.EPS ) -- prints: 0.53333
    print( ( (pi_div_3_deg_expected - pi_div_3_deg_calculated) / pi_div_3_deg_expected) / lu.EPS ) -- prints: 0.53333

    -- relative error is constant. Assertion can take the form of:
    assertAlmostEquals( (pi_div_6_deg_expected - pi_div_6_deg_calculated) / pi_div_6_deg_expected, lu.EPS )
    assertAlmostEquals( (pi_div_3_deg_expected - pi_div_3_deg_calculated) / pi_div_3_deg_expected, lu.EPS )
    ```

* **_AFT.assertNotAlmostEquals(actual, expected, margin)**

    Assert that two floating point numbers are not almost equal.