File: test_pint_eval.py

package info (click to toggle)
python-pint 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,940 kB
  • sloc: python: 20,478; makefile: 149
file content (173 lines) | stat: -rw-r--r-- 6,756 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from __future__ import annotations

import pytest

from pint.pint_eval import build_eval_tree, plain_tokenizer, uncertainty_tokenizer
from pint.util import string_preprocessor

TOKENIZERS = (plain_tokenizer, uncertainty_tokenizer)


def _pre(tokenizer, input_text: str, preprocess: bool = False) -> str:
    if preprocess:
        input_text = string_preprocessor(input_text)
    return build_eval_tree(tokenizer(input_text)).to_string()


@pytest.mark.parametrize("tokenizer", TOKENIZERS)
@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(tokenizer, input_text: str, parsed: str):
    assert _pre(tokenizer, input_text) == parsed


@pytest.mark.parametrize("tokenizer", TOKENIZERS)
@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(tokenizer, input_text: str, parsed: str):
    assert _pre(tokenizer, input_text, True) == parsed


@pytest.mark.parametrize(
    ("input_text", "parsed"),
    (
        ("( 8.0 + / - 4.0 ) e6 m", "((8.0e6 +/- 4.0e6) m)"),
        ("( 8.0 ± 4.0 ) e6 m", "((8.0e6 +/- 4.0e6) m)"),
        ("( 8.0 + / - 4.0 ) e-6 m", "((8.0e-6 +/- 4.0e-6) m)"),
        ("( nan + / - 0 ) e6 m", "((nan +/- 0) m)"),
        ("( nan ± 4.0 ) m", "((nan +/- 4.0) m)"),
        ("8.0 + / - 4.0 m", "((8.0 +/- 4.0) m)"),
        ("8.0 ± 4.0 m", "((8.0 +/- 4.0) m)"),
        ("8.0(4)m", "((8.0 +/- 0.4) m)"),
        ("8.0(.4)m", "((8.0 +/- .4) m)"),
        # ("8.0(-4)m", None), # TODO: this should raise an exception
    ),
)
def test_uncertainty(input_text: str, parsed: str):
    if parsed is None:
        with pytest.raises():
            assert _pre(uncertainty_tokenizer, input_text)
    else:
        assert _pre(uncertainty_tokenizer, input_text) == parsed