Beispiel #1
0
    def test_contains(self):
        v = LinExp({"a": 123, "b": 321, None: 111})

        assert ("a" in v) is True
        assert ("b" in v) is True
        assert (None in v) is True
        assert ("xxx" in v) is False
Beispiel #2
0
    def test_hash(self):
        # Hashses should be order insensitive
        assert hash(LinExp(OrderedDict([
            ("a", 123),
            ("b", 321),
            ("c", 111),
        ]))) == hash(
            LinExp(OrderedDict([
                ("c", 111),
                ("b", 321),
                ("a", 123),
            ])))

        # Hashes of LinExp constants should match the raw constant
        assert hash(LinExp(123)) == hash(123)
        assert hash(LinExp(Fraction(1, 3))) == hash(Fraction(1, 3))

        # Hashes of symbols should match the raw symbols
        assert hash(LinExp("a")) == hash("a")

        # Hashes should produce different values for different contents... Note
        # that this test is not strictly guaranteed to pass in theory, though
        # it practice it is almost certain to do so. Should it start failing,
        # think very hard!!
        assert hash(LinExp({"a": 123, "b": 321})) != hash(LinExp({"a": 123}))
Beispiel #3
0
    def test_number_type_casts(self):
        v = LinExp(1.5)

        assert isinstance(complex(v), complex)
        assert complex(v) == 1.5 + 0j

        assert isinstance(float(v), float)
        assert float(v) == 1.5

        assert isinstance(int(v), int)
        assert int(v) == 1
def test_serialise_signal_bounds():
    before = OrderedDict([
        ((1, "LH", 2, 3), (
            LinExp("foo")/2,
            LinExp("bar")/4,
        )),
        ((2, "HL", 3, 2), (
            LinExp("qux")/8,
            LinExp("quo")/16,
        )),
    ])
    after = serialise_signal_bounds(before)
    after = json_roundtrip(after)
    
    assert after == [
        {
            "level": 1,
            "array_name": "LH",
            "phase": [2, 3],
            "lower_bound": [
                {"symbol": "foo", "numer": "1", "denom": "2"},
            ],
            "upper_bound": [
                {"symbol": "bar", "numer": "1", "denom": "4"},
            ],
        },
        {
            "level": 2,
            "array_name": "HL",
            "phase": [3, 2],
            "lower_bound": [
                {"symbol": "qux", "numer": "1", "denom": "8"},
            ],
            "upper_bound": [
                {"symbol": "quo", "numer": "1", "denom": "16"},
            ],
        },
    ]
    
    assert deserialise_signal_bounds(after) == before
def test_serialise_linexp():
    before = LinExp("foo")/2 + 1
    after = serialise_linexp(before)
    after = json_roundtrip(after)
    
    exp = [
        {"symbol": "foo", "numer": "1", "denom": "2"},
        {"symbol": None, "numer": "1", "denom": "1"},
    ]
    
    assert after == exp or after == exp[::-1]
    
    assert deserialise_linexp(after) == before