|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +"""Tests for arithmetic terms""" |
| 4 | + |
| 5 | +from unittest import TestCase |
| 6 | +from lambda_calculus.visitors.normalisation import BetaNormalisingVisitor |
| 7 | +from lambda_calculus.terms import Application, arithmetic |
| 8 | + |
| 9 | + |
| 10 | +class OrderingTest(TestCase): |
| 11 | + """Test for ordering functions""" |
| 12 | + |
| 13 | + visitor: BetaNormalisingVisitor |
| 14 | + |
| 15 | + def setUp(self) -> None: |
| 16 | + """create a visitor""" |
| 17 | + self.visitor = BetaNormalisingVisitor() |
| 18 | + |
| 19 | + def test_successor(self) -> None: |
| 20 | + """test successor term""" |
| 21 | + self.assertEqual( |
| 22 | + self.visitor.skip_intermediate( |
| 23 | + Application(arithmetic.SUCCESSOR, arithmetic.number(8)) |
| 24 | + ), |
| 25 | + arithmetic.number(9) |
| 26 | + ) |
| 27 | + |
| 28 | + def test_predecessor(self) -> None: |
| 29 | + """test predecessor term""" |
| 30 | + self.assertEqual( |
| 31 | + self.visitor.skip_intermediate( |
| 32 | + Application(arithmetic.PREDECESSOR, arithmetic.number(8)) |
| 33 | + ), |
| 34 | + arithmetic.number(7) |
| 35 | + ) |
| 36 | + |
| 37 | + def test_predecessor_zero(self) -> None: |
| 38 | + """test predecessor of zero""" |
| 39 | + self.assertEqual( |
| 40 | + self.visitor.skip_intermediate( |
| 41 | + Application(arithmetic.PREDECESSOR, arithmetic.number(0)) |
| 42 | + ), |
| 43 | + arithmetic.number(0) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class CalculationsTest(TestCase): |
| 48 | + """Test for calculations with natural numbers""" |
| 49 | + |
| 50 | + visitor: BetaNormalisingVisitor |
| 51 | + |
| 52 | + def setUp(self) -> None: |
| 53 | + """create a visitor""" |
| 54 | + self.visitor = BetaNormalisingVisitor() |
| 55 | + |
| 56 | + def test_add(self) -> None: |
| 57 | + """test addition term""" |
| 58 | + self.assertEqual( |
| 59 | + self.visitor.skip_intermediate( |
| 60 | + Application.with_arguments( |
| 61 | + arithmetic.ADD, |
| 62 | + (arithmetic.number(3), arithmetic.number(5)) |
| 63 | + ) |
| 64 | + ), |
| 65 | + arithmetic.number(8) |
| 66 | + ) |
| 67 | + |
| 68 | + def test_subtract(self) -> None: |
| 69 | + """test subtraction term""" |
| 70 | + self.assertEqual( |
| 71 | + self.visitor.skip_intermediate( |
| 72 | + Application.with_arguments( |
| 73 | + arithmetic.SUBTRACT, |
| 74 | + (arithmetic.number(10), arithmetic.number(3)) |
| 75 | + ) |
| 76 | + ), |
| 77 | + arithmetic.number(7) |
| 78 | + ) |
| 79 | + |
| 80 | + def test_subtract_greater(self) -> None: |
| 81 | + """test subtraction below zero""" |
| 82 | + self.assertEqual( |
| 83 | + self.visitor.skip_intermediate( |
| 84 | + Application.with_arguments( |
| 85 | + arithmetic.SUBTRACT, |
| 86 | + (arithmetic.number(10), arithmetic.number(13)) |
| 87 | + ) |
| 88 | + ), |
| 89 | + arithmetic.number(0) |
| 90 | + ) |
| 91 | + |
| 92 | + def test_multiply(self) -> None: |
| 93 | + """test multiplication term""" |
| 94 | + self.assertEqual( |
| 95 | + self.visitor.skip_intermediate( |
| 96 | + Application.with_arguments( |
| 97 | + arithmetic.MULTIPLY, |
| 98 | + (arithmetic.number(5), arithmetic.number(2)) |
| 99 | + ) |
| 100 | + ), |
| 101 | + arithmetic.number(10) |
| 102 | + ) |
| 103 | + |
| 104 | + def test_power(self) -> None: |
| 105 | + """test power term""" |
| 106 | + # alpha conversion needed |
| 107 | + nine = arithmetic.number(9) |
| 108 | + nine = nine.replace(body=nine.body.alpha_conversion("x1")) |
| 109 | + nine = nine.alpha_conversion("x") |
| 110 | + self.assertEqual( |
| 111 | + self.visitor.skip_intermediate( |
| 112 | + Application.with_arguments( |
| 113 | + arithmetic.POWER, |
| 114 | + (arithmetic.number(3), arithmetic.number(2)) |
| 115 | + ) |
| 116 | + ), |
| 117 | + nine |
| 118 | + ) |
| 119 | + |
| 120 | + def test_power_zero(self) -> None: |
| 121 | + """test power zero""" |
| 122 | + one = self.visitor.skip_intermediate( |
| 123 | + Application.with_arguments( |
| 124 | + arithmetic.POWER, |
| 125 | + (arithmetic.number(5), arithmetic.number(0)) |
| 126 | + ) |
| 127 | + ) |
| 128 | + # eta conversion needed |
| 129 | + self.assertEqual( |
| 130 | + self.visitor.skip_intermediate( |
| 131 | + Application.with_arguments( |
| 132 | + arithmetic.ADD, |
| 133 | + (one, arithmetic.number(3)) |
| 134 | + ) |
| 135 | + ), |
| 136 | + arithmetic.number(4) |
| 137 | + ) |
| 138 | + # alpha conversion needed |
| 139 | + zero = arithmetic.number(0) |
| 140 | + zero = zero.replace(body=zero.body.alpha_conversion("x1")) |
| 141 | + zero = zero.alpha_conversion("x") |
| 142 | + self.assertEqual( |
| 143 | + self.visitor.skip_intermediate( |
| 144 | + Application.with_arguments( |
| 145 | + arithmetic.POWER, |
| 146 | + (arithmetic.number(0), arithmetic.number(5)) |
| 147 | + ) |
| 148 | + ), |
| 149 | + zero |
| 150 | + ) |
0 commit comments