import pytest import numpy as np from ee.formatting import eng_str from ee import EeVal @pytest.mark.parametrize("input,expected", [ (0, "0"), (5.5, "5.5"), (55, "55"), (550, "550"), (5500, "5.5 k"), (55000, "55 k"), (550000, "550 k"), (5500000, "5.5 M"), (0.5, "500 m"), (0.055, "55 m"), (0.0055, "5.5 m"), (0.00055, "550 u"), (0.000055, "55 u"), (0.0000055, "5.5 u"), (0.00000055, "550 n"), ]) def test_eng_str(input, expected): assert expected == eng_str(input) npinput = np.float64(input) assert expected == eng_str(npinput) @pytest.mark.parametrize("input,expected", [ (0, "0 A"), (5.5, "5.5 A"), (1100, "1.1 kA"), (0.05, "50 mA"), ]) def test_eng_str_with_unit(input, expected): assert expected == eng_str(input, unit='A') @pytest.mark.parametrize("input,expected", [ (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, ''), ]) def xx_test_eng_str2(input, expected): assert expected == eng_str(input) @pytest.mark.parametrize("input,expected", [ (10, '10'), (11, '11'), (12, '12'), (13, '13'), (14, '14'), (15, '15'), (16, '16'), (17, '17'), (18, '18'), (19, '19'), ]) def xx_test_eng_str3(input, expected): assert expected == eng_str(input) @pytest.mark.parametrize("s, num_value, str_value, unit", [ ("1", 1, "1", None), ("10", 10, "10", None), ("10k", 10 * 1000, "10 k", None), ("10 k", 10 * 1000, "10 k", None), ("10 k", 10 * 1000, "10 k", None), ("1000 m", 1, "1", None), ("100 m", 0.1, "100 m", None), ("100 n", 0.0000001, "100 n", None), ("1 m", 0.001, "1 m", None), ("1 u", 0.000001, "1 u", None), ("1 ยต", 0.000001, "1 u", None), ("1 n", 0.000000001, "1 n", None), ("10 n", 0.00000001, "10 n", None), ("1p", 1e-12, "1 p", None), ]) def test_EeVal(s, num_value, str_value, unit): num_value = float(num_value) v = EeVal(s) assert float(v) == v.value epsilon = num_value * 0.01 assert (num_value + epsilon) >= float(v) and (num_value - epsilon) <= float(v) assert str_value == str(v) assert unit == v.unit