Ejemplo n.º 1
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))
Ejemplo n.º 2
0
def testListBasics():
    assertEqual(
            lst3 == lst3Clone,
            True
            )

    assertEqual(
            List.empty == empty,
            True
            )

    assertEqual(
            lst3 == numLst,
            False
            )

    assertEqual(
            Kernel.eq(lst3, lst3Clone),
            True
            )

    assertEqual(
            Kernel.eq(List.empty, empty),
            True
            )

    assertEqual(
            Kernel.eq(lst3, numLst),
            False
            )

    assertTrue(List.isEmpty(empty))
    assertFalse(List.isEmpty(numLst))

    assertEqual(isList(empty), True)
    assertEqual(isList(lst3), True)
    assertEqual(isList(99), False)

    assertList(List.singleton(5), [5])
    assertList(List.repeat(3, 'x'), ['x', 'x', 'x'])
    assertList(List.range(3, 6), [3, 4, 5, 6])
    assertList(List.cons(42, lst3), [42, 0, 1, 2])
    assertList(List.map(double, lst3), [0, 2, 4])

    tup = lambda i, x: (i, 2*x)
    assertList(
            List.indexedMap(tup, lst3),
            [ (0, 0), (1, 2), (2, 4)])

    assertEqual(
            List.foldl(lambda x, acc: acc + x, "L", s123),
            "L123")

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

    assertList(
            List.filter(even, numLst),
            [2, 4, 6, 8, 10])

    assertList(
            List.filterMap(toMaybe, numLst),
            [ 20, 40 ])

    assertEqual(List.length(lst3), 3)

    assertTrue(List.any(even, numLst))
    assertFalse(List.any(negative, numLst))

    assertList(
            List.reverse(lst3),
            [2, 1, 0])

    assertTrue(List.member(7, numLst))
    assertFalse(List.member(987, numLst))

    assertTrue(List.all(positive, numLst))
    assertFalse(List.all(even, numLst))

    assertTrue(List.any(even, numLst))
    assertFalse(List.any(negative, numLst))

    assertEqual(List.maximum(empty), Nothing)
    assertEqual(List.maximum(numLst), Just(10))

    assertEqual(List.minimum(empty), Nothing)
    assertEqual(List.minimum(numLst), Just(1))

    assertEqual(List.sum(numLst), 55)
    assertEqual(List.product(numLst), 3628800)

    assertEqual(List.append(empty, lst3), lst3)
    assertEqual(List.append(lst3, empty), lst3)
    assertList(
            List.append(lst3, numLst),
            [0, 1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

    lsts = List.toElm([empty, lst3, empty, numLst])
    assertList(List.concat(lsts),
            [0, 1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

    mapper = lambda x: toElm([x, 5*x])
    assertList(List.concatMap(mapper, lst3),
            [0, 0, 1, 5, 2, 10])

    assertList(List.intersperse(999, lst3), [0, 999, 1, 999, 2])

    assertList(
            toPy(
                List.map2(
                    f2,
                    toElm([5, 7]),
                    toElm([6, 99, 3, 88888, 77777]),
                )
            ),
            [ (5, 6),
              (7, 99),
            ]
            )

    assertList(
            List.map3(
                f3,
                toElm([5, 7]),
                toElm([6, 99, 3]),
                toElm([8, 101]),
            ),
            [ (5, 6, 8),
              (7, 99, 101),
            ]
            )

    assertList(
            List.map4(
                f4,
                toElm([5, 7]),
                toElm([6, 99, 3]),
                toElm([8, 101]),
                toElm([1, 2]),
            ),
            [ (5, 6, 8, 1),
              (7, 99, 101, 2),
            ]
            )

    assertList(
            List.map5(
                f5,
                toElm([1, 2, 3, 4]),
                toElm([2, 4, 6, 8]),
                toElm([3, 6, 9, 12]),
                toElm([10, 20, 30, 40]),
                toElm([33, 66]),
            ),
            [ (1, 2, 3, 10, 33),
              (2, 4, 6, 20, 66),
            ]
            )

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

    assertList(
            List.sortBy(mod10, toElm([34, 15, 71, 83, 92])),
            [ 71
            , 92
            , 83
            , 34
            , 15
            ]
            )

    assertList(
            List.sortWith(toOrder, toElm([4, 5, 1, 3, 2])),
            [1, 2, 3, 4, 5]
            )

    assertEqual(
            List.tail(empty),
            Nothing)

    assertEqual(
            List.tail(toElm([1, 2, 3])),
            Just(toElm([2, 3]))
            )

    assertList(
            List.take(0, lst3),
            []
            )

    assertList(
            List.take(2, lst3),
            [0, 1]
            )

    assertList(
            List.take(3, lst3),
            [0, 1, 2]
            )

    assertList(
            List.take(4, lst3),
            [0, 1, 2]
            )

    assertList(
            List.drop(0, lst3),
            [0, 1, 2]
            )

    assertList(
            List.drop(2, lst3),
            [2]
            )

    assertList(
            List.drop(3, lst3),
            []
            )

    assertList(
            List.drop(4, lst3),
            []
            )

    assertEqual(
            toPy(List.partition(even, numLst)),
            ([2, 4, 6, 8, 10], [1, 3, 5, 7, 9])
            )

    assertEqual(
            toPy(List.unzip(toElm([(1, 11), (2, 22), (3, 33)]))),
            ([1, 2, 3], [11, 22, 33])
            )