File: test_pint_eval.py

package info (click to toggle)
python-pint 0.24.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,924 kB
  • sloc: python: 19,983; makefile: 149
file content (148 lines) | stat: -rw-r--r-- 6,359 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from __future__ import annotations

import pytest

from pint.pint_eval import build_eval_tree, tokenizer
from pint.util import string_preprocessor

# This is how we enable the parsing of uncertainties
# tokenizer = pint.pint_eval.uncertainty_tokenizer


class TestPintEval:
    def _test_one(self, input_text, parsed, preprocess=False):
        if preprocess:
            input_text = string_preprocessor(input_text)
        assert build_eval_tree(tokenizer(input_text)).to_string() == parsed

    @pytest.mark.parametrize(
        ("input_text", "parsed"),
        (
            ("3", "3"),
            ("1 + 2", "(1 + 2)"),
            ("1 - 2", "(1 - 2)"),
            ("2 * 3 + 4", "((2 * 3) + 4)"),  # order of operations
            ("2 * (3 + 4)", "(2 * (3 + 4))"),  # parentheses
            (
                "1 + 2 * 3 ** (4 + 3 / 5)",
                "(1 + (2 * (3 ** (4 + (3 / 5)))))",
            ),  # more order of operations
            (
                "1 * ((3 + 4) * 5)",
                "(1 * ((3 + 4) * 5))",
            ),  # nested parentheses at beginning
            ("1 * (5 * (3 + 4))", "(1 * (5 * (3 + 4)))"),  # nested parentheses at end
            (
                "1 * (5 * (3 + 4) / 6)",
                "(1 * ((5 * (3 + 4)) / 6))",
            ),  # nested parentheses in middle
            ("-1", "(- 1)"),  # unary
            ("3 * -1", "(3 * (- 1))"),  # unary
            ("3 * --1", "(3 * (- (- 1)))"),  # double unary
            ("3 * -(2 + 4)", "(3 * (- (2 + 4)))"),  # parenthetical unary
            ("3 * -((2 + 4))", "(3 * (- (2 + 4)))"),  # parenthetical unary
            # implicit op
            ("3 4", "(3 4)"),
            # implicit op, then parentheses
            ("3 (2 + 4)", "(3 (2 + 4))"),
            # parentheses, then implicit
            ("(3 ** 4 ) 5", "((3 ** 4) 5)"),
            # implicit op, then exponentiation
            ("3 4 ** 5", "(3 (4 ** 5))"),
            # implicit op, then addition
            ("3 4 + 5", "((3 4) + 5)"),
            # power followed by implicit
            ("3 ** 4 5", "((3 ** 4) 5)"),
            # implicit with parentheses
            ("3 (4 ** 5)", "(3 (4 ** 5))"),
            # exponent with e
            ("3e-1", "3e-1"),
            # multiple units with exponents
            ("kg ** 1 * s ** 2", "((kg ** 1) * (s ** 2))"),
            # multiple units with neg exponents
            ("kg ** -1 * s ** -2", "((kg ** (- 1)) * (s ** (- 2)))"),
            # multiple units with neg exponents
            ("kg^-1 * s^-2", "((kg ^ (- 1)) * (s ^ (- 2)))"),
            # multiple units with neg exponents, implicit op
            ("kg^-1 s^-2", "((kg ^ (- 1)) (s ^ (- 2)))"),
            # nested power
            ("2 ^ 3 ^ 2", "(2 ^ (3 ^ 2))"),
            # nested power
            ("gram * second / meter ** 2", "((gram * second) / (meter ** 2))"),
            # nested power
            ("gram / meter ** 2 / second", "((gram / (meter ** 2)) / second)"),
            # units should behave like numbers, so we don't need a bunch of extra tests for them
            # implicit op, then addition
            ("3 kg + 5", "((3 kg) + 5)"),
            ("(5 % 2) m", "((5 % 2) m)"),  # mod operator
            ("(5 // 2) m", "((5 // 2) m)"),  # floordiv operator
        ),
    )
    def test_build_eval_tree(self, input_text, parsed):
        self._test_one(input_text, parsed, preprocess=False)

    @pytest.mark.parametrize(
        ("input_text", "parsed"),
        (
            ("3", "3"),
            ("1 + 2", "(1 + 2)"),
            ("1 - 2", "(1 - 2)"),
            ("2 * 3 + 4", "((2 * 3) + 4)"),  # order of operations
            ("2 * (3 + 4)", "(2 * (3 + 4))"),  # parentheses
            (
                "1 + 2 * 3 ** (4 + 3 / 5)",
                "(1 + (2 * (3 ** (4 + (3 / 5)))))",
            ),  # more order of operations
            (
                "1 * ((3 + 4) * 5)",
                "(1 * ((3 + 4) * 5))",
            ),  # nested parentheses at beginning
            ("1 * (5 * (3 + 4))", "(1 * (5 * (3 + 4)))"),  # nested parentheses at end
            (
                "1 * (5 * (3 + 4) / 6)",
                "(1 * ((5 * (3 + 4)) / 6))",
            ),  # nested parentheses in middle
            ("-1", "(- 1)"),  # unary
            ("3 * -1", "(3 * (- 1))"),  # unary
            ("3 * --1", "(3 * (- (- 1)))"),  # double unary
            ("3 * -(2 + 4)", "(3 * (- (2 + 4)))"),  # parenthetical unary
            ("3 * -((2 + 4))", "(3 * (- (2 + 4)))"),  # parenthetical unary
            # implicit op
            ("3 4", "(3 * 4)"),
            # implicit op, then parentheses
            ("3 (2 + 4)", "(3 * (2 + 4))"),
            # parentheses, then implicit
            ("(3 ** 4 ) 5", "((3 ** 4) * 5)"),
            # implicit op, then exponentiation
            ("3 4 ** 5", "(3 * (4 ** 5))"),
            # implicit op, then addition
            ("3 4 + 5", "((3 * 4) + 5)"),
            # power followed by implicit
            ("3 ** 4 5", "((3 ** 4) * 5)"),
            # implicit with parentheses
            ("3 (4 ** 5)", "(3 * (4 ** 5))"),
            # exponent with e
            ("3e-1", "3e-1"),
            # multiple units with exponents
            ("kg ** 1 * s ** 2", "((kg ** 1) * (s ** 2))"),
            # multiple units with neg exponents
            ("kg ** -1 * s ** -2", "((kg ** (- 1)) * (s ** (- 2)))"),
            # multiple units with neg exponents
            ("kg^-1 * s^-2", "((kg ** (- 1)) * (s ** (- 2)))"),
            # multiple units with neg exponents, implicit op
            ("kg^-1 s^-2", "((kg ** (- 1)) * (s ** (- 2)))"),
            # nested power
            ("2 ^ 3 ^ 2", "(2 ** (3 ** 2))"),
            # nested power
            ("gram * second / meter ** 2", "((gram * second) / (meter ** 2))"),
            # nested power
            ("gram / meter ** 2 / second", "((gram / (meter ** 2)) / second)"),
            # units should behave like numbers, so we don't need a bunch of extra tests for them
            # implicit op, then addition
            ("3 kg + 5", "((3 * kg) + 5)"),
            ("(5 % 2) m", "((5 % 2) * m)"),  # mod operator
            ("(5 // 2) m", "((5 // 2) * m)"),  # floordiv operator
        ),
    )
    def test_preprocessed_eval_tree(self, input_text, parsed):
        self._test_one(input_text, parsed, preprocess=True)