def test_pflipflop():
    a = iso.PFlipFlop(0, iso.PConstant(0.5), iso.PConstant(0.5))
    expected = [0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1]
    a.seed(0)
    assert a.nextn(20) == expected
    a.reset()
    assert a.nextn(20) == expected
def test_pwalk():
    a = iso.PRandomWalk([0, 2, 4, 5, 12],
                        min=iso.PConstant(1),
                        max=iso.PConstant(2))
    expected = [4, 0, 5, 0, 4, 5, 2, 4, 12, 5, 12, 4, 2, 5, 12, 4, 0, 12, 2, 4]
    a.seed(0)
    assert a.nextn(20) == expected
    a.reset()
    assert a.nextn(20) == expected
def test_pwhite():
    a = iso.PWhite(iso.PConstant(5), iso.PConstant(10))
    a.seed(0)
    assert a.nextn(10) == [9, 8, 7, 6, 7, 7, 8, 6, 7, 7]

    a = iso.PWhite(iso.PConstant(-1.0), iso.PConstant(1.0))
    a.seed(0)
    assert a.nextn(10) == [
        0.6888437030500962, 0.515908805880605, -0.15885683833831,
        -0.4821664994140733, 0.02254944273721704, -0.19013172509917142,
        0.5675971780695452, -0.3933745478421451, -0.04680609169528838,
        0.1667640789100624
    ]
def test_pbrown():
    a = iso.PBrown(0, iso.PConstant(5), iso.PConstant(-5), iso.PConstant(5))
    a.seed(0)
    expected = [
        0, 1, 2, -3, -4, -1, 1, 2, 1, 3, 3, 5, 3, 5, 2, 1, -2, -5, -1, -2
    ]
    assert a.nextn(20) == expected
    a.reset()
    assert a.nextn(20) == expected

    a = iso.PBrown(0.0, 1.0, -1.0, 1.0)
    a.seed(0)
    expected = [
        0.0, 0.6888437030500962, 1.0, 0.84114316166169, 0.3589766622476167,
        0.38152610498483375, 0.19139437988566232, 0.7589915579552076,
        0.36561701011306247, 0.3188109184177741
    ]
    assert a.nextn(10) == expected
    a.reset()
    assert a.nextn(10) == expected
Exemple #5
0
    def pattern(v):
        """
        Patternify a value, turning it into an object with a next() method
        to obtain its next value.

        Pattern subclasses remain untouched.
        Scalars and other objects are turned into PConst objects. """

        if isinstance(v, Pattern):
            return v
        elif isinstance(v, dict):
            return isobar.PDict(v)
        else:
            return isobar.PConstant(v)
Exemple #6
0
def test_prange():
    a = iso.PRange(0, iso.PConstant(10), iso.PSequence([1, 2]))
    assert list(a) == [0, 1, 3, 4, 6, 7, 9]
Exemple #7
0
def test_pseries():
    a = iso.PSeries(2, iso.PSequence([1, 2]), iso.PConstant(5))
    assert list(a) == [2, 3, 5, 6, 8]
Exemple #8
0
def test_parrayindex_pattern():
    a = iso.PConstant(5)
    b = iso.PConstant(9)
    c = iso.PArrayIndex([a, b], iso.PSequence([0, 1], 2))
    assert list(c) == [5, 9, 5, 9]