예제 #1
0
def testPipes():
    val = Elm.pipe(5, [
            double,
            F(add)(7),
            double,
            F(add)(3),
            ])
    assertEqual(val, 37)
예제 #2
0
def testListOfLists():
    lol = toElm([
                [5],
                [1],
                [1, 2],
                [2],
                [1, 2, 3],
                [1, 2, 4],
                [1, 2, -1],
                [3],
                ])

    h = lambda lst: List.head(lst).val

    assertEqual(h(h(lol)), 5)

    assertList(List.sort(lol), [
        [ 1 ],
        [ 1, 2],
        [ 1, 2, -1],
        [ 1, 2, 3],
        [ 1, 2, 4],
        [ 2 ],
        [ 3 ],
        [ 5 ],
    ])

    assertEqual(
            List.member(List.singleton(2), lol),
            True)

    assertEqual(
            List.member(List.singleton(9), lol),
            False)
예제 #3
0
def testPartialApply():
    assertList(
            F(List.map)(double)(lst3),
            [0, 2, 4])

    accum = lambda x, acc: acc + x
    assertEqual(
            F(List.foldr)(accum)("R")(s123),
            "R321")

    assertEqual(
            F(List.foldr)(accum, "R")(s123),
            "R321")
    assertEqual(
            F(List.foldr)(accum)("R", s123),
            "R321")
예제 #4
0
def testBasics():
    assertEqual(Basics.toFloat(5), 5.0)
    assertEqual(Basics.round(8.2), 8)
    assertEqual(Basics.round(8.8), 9)
    assertEqual(Basics.round(-8.8), -9)
    assertEqual(Basics.floor(8.8), 8)
    assertEqual(Basics.floor(-8.8), -9)
    assertEqual(Basics.ceiling(8.8), 9)
    assertEqual(Basics.ceiling(-8.8), -8)
    assertEqual(Basics.truncate(8.8), 8)
    assertEqual(Basics.truncate(-8.8), -8)
    assertEqual(Basics.min(5, 3), 3)
    assertEqual(Basics.max(5, 3), 5)

    assertEqual(
            Basics.compare(4, 3),
            Order.GT
            )

    assertEqual(
            Basics.compare(4, 4),
            Order.EQ
            )

    assertEqual(
            Basics.compare(4, 8),
            Order.LT
            )

    assertEqual(Basics.xor(True, False), True)
    assertEqual(Basics.xor(False, True), True)
    assertEqual(Basics.xor(True, True), False)
    assertEqual(Basics.xor(False, False), False)

    assertEqual(Basics.modBy(3, 0), 0)
    assertEqual(Basics.modBy(3, 6), 0)
    assertEqual(Basics.modBy(3, 7), 1)

    assertEqual(Basics.modBy(3, -8), 1)
    assertEqual(Basics.modBy(3, -7), 2)
    assertEqual(Basics.modBy(3, -6), 0)

    assertEqual(Basics.modBy(-3, 7), -2)

    assertEqual(Basics.remainderBy(3, 0), 0)
    assertEqual(Basics.remainderBy(3, 3), 0)
    assertEqual(Basics.remainderBy(3, 6), 0)
    assertEqual(Basics.remainderBy(3, 7), 1)

    assertEqual(Basics.remainderBy(3, -6), 0)
    assertEqual(Basics.remainderBy(3, -7), -1)
    assertEqual(Basics.remainderBy(3, -8), -2)

    assertEqual(Basics.remainderBy(-3, -7), -1)
    assertEqual(Basics.remainderBy(-3, 7), 1)

    assertEqual(Basics.negate(5), -5)
    assertEqual(Basics.negate(-5), 5)

    assertEqual(Basics.abs(5), 5)
    assertEqual(Basics.abs(-5), 5)

    assertEqual(Basics.clamp(100, 200, 80), 100)
    assertEqual(Basics.clamp(100, 200, 150), 150)
    assertEqual(Basics.clamp(100, 200, 220), 200)

    assertEqual(Basics.sqrt(81), 9)

    assertFloat(Basics.logBase(10, 100), 2)
    assertFloat(Basics.logBase(10, 1000), 3)
    assertFloat(Basics.logBase(2, 256), 8)

    assertFloat(Basics.e, 2.718281828459045)
    assertFloat(Basics.pi, 3.141592653589793)

    pi = Basics.pi

    assertFloat(Basics.degrees(180), pi)
    assertFloat(Basics.radians(2), 2)
    assertFloat(Basics.turns(1/2), pi)

    assertFloat(Basics.cos(pi/3), 0.5)
    assertFloat(Basics.sin(pi/6), 0.5)
    assertFloat(Basics.tan(pi/4), 1)

    assertFloat(Basics.acos(0.5), pi/3)
    assertFloat(Basics.asin(0.5), pi/6)

    assertFloat(Basics.atan(1), pi/4)
    assertFloat(Basics.atan(-1), -1 * pi/4)

    assertFloat(Basics.atan2(1, 1), pi/4)
    assertFloat(Basics.atan2(1, -1), 3 * pi/4)
    assertFloat(Basics.atan2(-1, -1), -3 * pi/4)
    assertFloat(Basics.atan2(-1, 1), -1 * pi/4)

    assertFloat(Basics.toPolar((3, 4)), ( 5, 0.9272952180016122))
    assertFloat(Basics.toPolar((5,12)), (13, 1.1760052070951352))

    assertFloat(Basics.fromPolar(Basics.toPolar((-3, -4))), (-3, -4))


    assertFloat(Basics.div(7, 2), 3.5)

    assertTrue(Basics.isNaN(Basics.sqrt(-1)))
    assertTrue(Basics.isNaN(Basics.div(0, 0)))
    assertFalse(Basics.isNaN(Basics.div(1, 0)))
    assertFalse(Basics.isNaN(42))

    assertFalse(Basics.isInfinite(Basics.div(0, 0)))
    assertFalse(Basics.isInfinite(Basics.sqrt(-1)))
    assertTrue(Basics.isInfinite(Basics.div(1, 0)))
    assertFalse(Basics.isInfinite(42))

    assertEqual(Basics.identity(42), 42)
    assertEqual(Basics.always(42, 99), 42)

    assertTrue(hasattr(Basics, 'never'))

    # The following are << and >> in Basics.elm, but we put them
    # in Elm.py.
    assertEqual(Elm.lcompose(Basics.sqrt, double)(50), 10)
    assertEqual(Elm.rcompose(Basics.sqrt, double)(49), 14)
예제 #5
0
def testMaybe():
    assertEqual(Maybe.withDefault(Nothing, 5), 5)
    assertEqual(Maybe.withDefault(Just(42), 99), 42)

    assertEqual(Maybe.map(double, Nothing), Nothing)
    assertEqual(Maybe.map(double, Just(42)), Just(84))

    assertEqual(
            Maybe.map2(f2, Just(1), Nothing),
            Nothing)
    assertEqual(
            Maybe.map2(f2, Nothing, Just(2)),
            Nothing)
    assertEqual(
            Maybe.map2(f2, Just(1), Just(2)),
            Just((1, 2)))

    assertEqual(
            Maybe.map3(f3, Just(1), Nothing, Nothing),
            Nothing)
    assertEqual(
            Maybe.map3(f3, Nothing, Just(2), Nothing),
            Nothing)
    assertEqual(
            Maybe.map3(f3, Nothing, Nothing, Just(2)),
            Nothing)
    assertEqual(
            Maybe.map3(f3, Just(1), Just(2), Just(3)),
            Just((1, 2, 3)))

    # cheat for map4/map5
    assertEqual(Maybe.map4, Maybe.mapN)
    assertEqual(Maybe.map5, Maybe.mapN)

    assertEqual(Maybe.andThen(toMaybe, Just(2)), Just(20))
    assertEqual(Maybe.andThen(toMaybe, Just(99)), Nothing)
    assertEqual(Maybe.andThen(toMaybe, Nothing), Nothing)
예제 #6
0
def testTuples():
    assertEqual(Tuple.pair(1,2), Tuple.pair(1,2))
    assertTrue(Kernel.eq(Tuple.pair(1,2), Tuple.pair(1,2)))
    assertFalse(Kernel.eq(Tuple.pair(1,2), Tuple.pair(3,4)))

    t = Tuple.pair(5, 6)
    assertEqual(Tuple.first(t), 5)
    assertEqual(Tuple.second(t), 6)

    assertEqual(
            Tuple.mapFirst(double, t),
            (10, 6))

    assertEqual(
            Tuple.mapSecond(double, t),
            (5, 12))

    assertEqual(
            Tuple.mapBoth(triple, double, t),
            (15, 12))

    assertEqual(
            toPy(Tuple.mapFirst(F(List.cons)(0), toElm(([1], [2])))),
            ([0, 1], [2]))
예제 #7
0
def testCustomTypes():
    Number = CustomType('Number', 'Zero', OneDigit=1, TwoDigit=2)

    Zero = Number.Zero
    OneDigit = Number.OneDigit
    TwoDigit = Number.TwoDigit

    zero = Number.Zero
    one = Number.OneDigit(1)
    twelve = Number.TwoDigit(1, 2)

    v1 = (Val, 1)
    v2 = (Val, 2)
    v3 = (Val, 3)
    v4 = (Val, 4)

    x = (Var, 'x')
    y = (Var, 'y')

    # exact, precise matches
    assertEqual(True, patternMatch(zero, (Variant, Zero)))
    assertEqual(True, patternMatch(one, (Variant, OneDigit), v1))
    assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), v1, v2))

    # bad vtypes
    assertNone(patternMatch(one, (Variant, Zero)))
    assertNone(patternMatch(twelve, (Variant, OneDigit), v1))
    assertNone(patternMatch(zero, (Variant, TwoDigit), v1, v2))

    # good vtypes but wrong values
    assertNone(patternMatch(one, (Variant, OneDigit), v4))
    assertNone(patternMatch(twelve, (Variant, TwoDigit), v1, v3))

    # easy matches with Any
    assertEqual(True, patternMatch(one, (Variant, OneDigit), Any))
    assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), Any, Any))

    # partial matches with Any
    assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), v1, Any))
    assertEqual(True, patternMatch(twelve, (Variant, TwoDigit), Any, v2))

    # mismatches with Any
    assertNone(patternMatch(twelve, (Variant, TwoDigit), v4, Any))
    assertNone(patternMatch(twelve, (Variant, TwoDigit), Any, v4))

    # capture matches
    assertEqual(patternMatch(one, (Variant, OneDigit), x), dict(x=1))

    assertEqual(patternMatch(twelve, (Variant, TwoDigit), x, y), dict(x=1,
                                                                      y=2))

    # partial capture matches
    assertEqual(patternMatch(twelve, (Variant, TwoDigit), x, Any), dict(x=1))

    assertEqual(patternMatch(twelve, (Variant, TwoDigit), x, v2), dict(x=1))

    assertEqual(patternMatch(twelve, (Variant, TwoDigit), Any, y), dict(y=2))

    assertEqual(patternMatch(twelve, (Variant, TwoDigit), v1, y), dict(y=2))

    # more failures
    assertNone(patternMatch(twelve, (Variant, TwoDigit), x, v4))
    assertNone(patternMatch(twelve, (Variant, TwoDigit), v4, x))
    assertNone(patternMatch(zero, (Variant, TwoDigit), x, y))

    Tree = CustomType('Tree', 'Empty', Node=3)
    rrNode = Tree.Node(3, Tree.Empty, Tree.Empty)
    rNode = Tree.Node(2, Tree.Empty, rrNode)
    tNode = Tree.Node(1, Tree.Empty, rNode)

    res = patternMatch(tNode, (Variant, Tree.Node), (Var, 'n'), Any, (
        AsVar,
        'rChild',
        ((Variant, Tree.Node), (Var, 'rn'), Any, Any),
    ))
    assertEqual(res['n'], 1)
    assertEqual(res['rChild'], rNode)
    assertEqual(res['rn'], 2)
예제 #8
0
def testLists():
    lst0 = List.empty
    lst1 = List.toElm([1])
    lst2 = List.toElm([1, 2, 3])
    tupList = List.toElm([(1, 10), (2, 20)])

    assertEqual(True, patternMatch(lst0, PList))
    assertEqual(None, patternMatch(lst1, PList))

    assertEqual(None, patternMatch(lst0, PCons, Any, Any))
    assertEqual(True, patternMatch(lst1, PCons, Any, Any))
    assertEqual(True, patternMatch(lst2, PCons, Any, Any))

    # good vals
    assertEqual(True, patternMatch(lst1, PCons, (Val, 1), Any))
    assertEqual(True, patternMatch(lst2, PCons, (Val, 1), Any))

    # bad vals
    assertEqual(None, patternMatch(lst1, PCons, (Val, 5), Any))
    assertEqual(None, patternMatch(lst2, PCons, (Val, 5), Any))

    # capture
    res = patternMatch(lst2, PCons, (Var, 'h'), (Var, 'r'))
    assertEqual(res['h'], 1)
    assertEqual(list(res['r']), [2, 3])

    # tups
    res = patternMatch(tupList, PCons, ((Var, 't1'), (Var, 't2')), (Var, 'r'))
    assertEqual(res['t1'], 1)
    assertEqual(res['t2'], 10)
    assertEqual(list(res['r']), [(2, 20)])

    res = patternMatch(tupList, PCons, ((Val, 5), (Var, 't2')), (Var, 'r'))
    assertEqual(None, res)

    res = patternMatch(
        tupList,
        PCons,
        (Any, Any),
        (Var, 'r'),
    )
    assertEqual(res, dict(r=[(2, 20)]))
예제 #9
0
def checkPerformance():
    # Make sure we don't crash on large lists.  (We can't use
    # recursion carelessly in Python.)  These tests only exercise
    # performance.  Correctness tests are above.
    bigList = List.range(1, 100000)
    assertEqual(List.foldl(add, 0, bigList), 5000050000)
    assertEqual(List.length(List.foldr(List.cons, empty, bigList)),
                List.length(bigList))
    List.all(lambda x: True, bigList)
    List.any(lambda x: False, bigList)
    List.filter(lambda x: True, bigList)
    List.filterMap(toMaybe, bigList)
    List.reverse(bigList)
    assertEqual(List.maximum(bigList), Just(100000))
    assertEqual(List.minimum(bigList), Just(1))
    List.sum(bigList)

    # product is expensive with big numbers!
    List.product(List.repeat(100000, 0))

    assertEqual(List.length(List.append(bigList, bigList)), 200000)

    assertEqual(List.length(List.intersperse(0, bigList)), 199999)

    List.map2(f2, bigList, bigList)

    List.map3(f3, bigList, bigList, bigList)

    List.map4(f4, bigList, bigList, bigList, bigList)

    List.map5(f5, bigList, bigList, bigList, bigList, bigList)

    List.tail(bigList)
    List.take(99999, bigList)
    List.drop(99999, bigList)

    (evens, odds) = List.partition(even, bigList)
    assertEqual(List.length(evens), 50000)
    assertEqual(List.length(odds), 50000)

    bigListOfTups = List.indexedMap(lambda i, n: Tuple.pair(i, n * 42),
                                    bigList)

    tupOfBigLists = List.unzip(bigListOfTups)

    assertEqual(List.head(Tuple.first(tupOfBigLists)), Just(0))
    assertEqual(List.head(Tuple.second(tupOfBigLists)), Just(42))