#-- # $Id: tc_list.rb 1 2006-08-24 20:34:21Z prelude $ # # # This file is part of the Prelude library that provides tools to # enable Haskell style functional programming in Ruby. # # http://prelude.rubyforge.org # # Copyright (C) 2006 APP Design, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #++ class TestList < Test::Unit::TestCase def setup @a = List.new([1, 2, 3, 4, 5]) @true_list = List.new([true, true, true]) @false_list = List.new([false, false, false]) @mixed_list = List.new([false, true, false]) end # setup def teardown # Nothing end # teardown def test_head result = @a.head expect = 1 assert_equal(expect, result) end # Compatibility with the Array class def test_array_plus b = [6, 7] result = @a+b expect = [1, 2, 3, 4, 5, 6, 7] assert_instance_of(List, result) assert_equal(expect, result) end def test_array_assignment @a[1] = 10 result = @a expect = [1, 10, 3, 4, 5] assert_instance_of(List, result) assert_equal(expect, result) end def test_array_shift result = @a << 100 << 101 expect = [1, 2, 3, 4, 5, 100, 101] assert_instance_of(List, result) assert_equal(expect, result) end # , last -- :: [a] -> a def test_last result = @a.last expect = 5 assert_equal(expect, result) end # , tail -- :: [a] -> [a] def test_tail result = @a.tail expect = [2, 3, 4, 5] assert_equal(expect, result) end # , init -- :: [a] -> [a] def test_init result = @a.init expect = [1, 2, 3, 4] assert_equal(expect, result) end # , null -- :: [a] -> Bool def test_null result = @a.null expect = false assert_equal(expect, result) end # , length -- :: [a] -> Int def test_length result = @a.length expect = 5 assert_equal(expect, result) end # -- * List transformations # , map -- :: (a -> b) -> [a] -> [b] # already defined by the Array # , reverse -- :: [a] -> [a] # already defined by the Array # , intersperse -- :: a -> [a] -> [a] def test_intersperse result = @a.intersperse expect = [] assert_equal(expect, result) end # , transpose -- :: [[a]] -> [[a]] def test_transpose result = List.new([@a, @a, @a]).transpose expect = [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5] ] assert_equal(expect, result) end # -- * Reducing lists (folds) # , foldl -- :: (a -> b -> a) -> a -> [b] -> a # Classic recursive functional definition causes stack overflow for # arrays of any usable size... So don't use it, it's here for completeness def test_f_foldl result = @a.f_foldl(0){ |x, y| x+y} expect = 15 assert_equal(expect, result) end # This is a more pedestrian iterative version. def test_foldl result = @a.foldl(0){ |x, y| x+y} expect = 15 assert_equal(expect, result) end # , foldl' -- :: (a -> b -> a) -> a -> [b] -> a def test_foldl_ result = @a.foldl_ expect = [] assert_equal(expect, result) end # , foldl1 -- :: (a -> a -> a) -> [a] -> a def test_foldl1 result = @a.foldl1{ |x, y| x*y} expect = 120 assert_equal(expect, result) end # , foldl1' -- :: (a -> a -> a) -> [a] -> a def test_foldl1_ result = @a.foldl1_ expect = [] assert_equal(expect, result) end # , foldr -- :: (a -> b -> b) -> b -> [a] -> b def test_foldr result = @a.foldr(0){ |x, y| x+y} expect = 15 assert_equal(expect, result) end # , foldr1 -- :: (a -> a -> a) -> [a] -> a def test_foldr1 result = @a.foldr1{ |x, y| x*y} expect = 120 assert_equal(expect, result) end # -- ** Special folds # , concat -- :: [[a]] -> [a] def test_concat result = List.new([@a, [@a]]).concat expect = @a+@a assert_equal(expect, result) end # , concatMap -- :: (a -> [b]) -> [a] -> [b] def test_concatMap result = @a.concatMap expect = [] assert_equal(expect, result) end # , and -- :: [Bool] -> Bool def test_and result = @true_list.and expect = true assert_equal(expect, result) result = @mixed_list.and expect = false assert_equal(expect, result) result = @false_list.and expect = false assert_equal(expect, result) end # , or -- :: [Bool] -> Bool def test_or result = @true_list.or expect = true assert_equal(expect, result) result = @mixed_list.or expect = true assert_equal(expect, result) result = @false_list.or expect = false assert_equal(expect, result) end # , any -- :: (a -> Bool) -> [a] -> Bool def test_any result = @a.any{|x| x>5} expect = false assert_equal(expect, result) result = @a.any{|x| x<5} expect = true assert_equal(expect, result) end # , all -- :: (a -> Bool) -> [a] -> Bool def test_all result = @a.all{|x| x>3} expect = false assert_equal(expect, result) result = @a.all{|x| x>0} expect = true assert_equal(expect, result) end # , sum -- :: (Num a) => [a] -> a def test_sum result = @a.sum expect = [] assert_equal(expect, result) end # , product -- :: (Num a) => [a] -> a def test_product result = @a.product expect = [] assert_equal(expect, result) end # , maximum -- :: (Ord a) => [a] -> a def test_maximum result = @a.maximum expect = [] assert_equal(expect, result) end # , minimum -- :: (Ord a) => [a] -> a def test_minimum result = @a.minimum expect = [] assert_equal(expect, result) end # -- * Building lists # -- ** Scans # , scanl -- :: (a -> b -> a) -> a -> [b] -> [a] def test_scanl result = @a.scanl(0){ |x, y| x+y} expect = [0, 1, 3, 6, 10, 15] assert_equal(expect, result) end # , scanl1 -- :: (a -> a -> a) -> [a] -> [a] def test_scanl1 result = @a.scanl1{ |x, y| x+y} expect = [1, 3, 6, 10, 15] assert_equal(expect, result) end # , scanr -- :: (a -> b -> b) -> b -> [a] -> [b] def test_scanr result = @a.scanr(0){ |x, y| x+y} expect = [0, 1, 3, 6, 10, 15] assert_equal(expect, result) end # , scanr1 -- :: (a -> a -> a) -> [a] -> [a] def test_scanr1 result = @a.scanr1{ |x, y| x+y} expect = [1, 3, 6, 10, 15] assert_equal(expect, result) end # -- ** Accumulating maps # , mapAccumL -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c]) def test_mapAccumL result = @a.mapAccumL expect = [] assert_equal(expect, result) end # , mapAccumR -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c]) def test_mapAccumR result = @a.mapAccumR expect = [] assert_equal(expect, result) end # -- ** Infinite lists # , iterate -- :: (a -> a) -> a -> [a] def test_iterate result = @a.iterate expect = [] assert_equal(expect, result) end # , repeat -- :: a -> [a] def test_repeat result = @a.repeat expect = [] assert_equal(expect, result) end # , replicate -- :: Int -> a -> [a] def test_replicate result = @a.replicate expect = [] assert_equal(expect, result) end # , cycle -- :: [a] -> [a] def test_cycle result = @a.cycle expect = [] assert_equal(expect, result) end # -- ** Unfolding # , unfoldr -- :: (b -> Maybe (a, b)) -> b -> [a] def test_unfoldr result = @a.unfoldr expect = [] assert_equal(expect, result) end # -- * Sublists # -- ** Extracting sublists # , take -- :: Int -> [a] -> [a] def test_take result = @a.take(2) expect = [1, 2] assert_equal(expect, result) end # , drop -- :: Int -> [a] -> [a] def test_drop result = @a.drop(2) expect = [3, 4, 5] assert_equal(expect, result) end # , splitAt -- :: Int -> [a] -> ([a], [a]) def test_split_at result = @a.split_at(2) expect = [[1, 2],[3, 4, 5]] assert_equal(expect, result) end # , takeWhile -- :: (a -> Bool) -> [a] -> [a] def test_takeWhile result = @a.takeWhile{|x| x<4} expect = [1, 2, 3] assert_equal(expect, result) end # , dropWhile -- :: (a -> Bool) -> [a] -> [a] def test_dropWhile result = @a.dropWhile{|x| x<4} expect = [4, 5] assert_equal(expect, result) end # , span -- :: (a -> Bool) -> [a] -> ([a], [a]) def test_span result = @a.span{|x| x<3} expect = [[1, 2], [3, 4, 5]] assert_equal(expect, result) end # , break -- :: (a -> Bool) -> [a] -> ([a], [a]) def test_break result = @a.break expect = [] assert_equal(expect, result) end # , group -- :: Eq a => [a] -> [[a]] def test_group result = @a.group expect = [] assert_equal(expect, result) end # , inits -- :: [a] -> [[a]] def test_inits result = @a.inits expect = [] assert_equal(expect, result) end # , tails -- :: [a] -> [[a]] def test_tails result = @a.tails expect = [] assert_equal(expect, result) end # -- ** Predicates # , isPrefixOf -- :: (Eq a) => [a] -> [a] -> Bool def test_isPrefixOf result = @a.isPrefixOf expect = [] assert_equal(expect, result) end # , isSuffixOf -- :: (Eq a) => [a] -> [a] -> Bool def test_isSuffixOf result = @a.isSuffixOf expect = [] assert_equal(expect, result) end # -- * Searching lists # -- ** Searching by equality # , elem -- :: a -> [a] -> Bool def test_elem result = @a.elem expect = [] assert_equal(expect, result) end # , notElem -- :: a -> [a] -> Bool def test_notElem result = @a.notElem expect = [] assert_equal(expect, result) end # , lookup -- :: (Eq a) => a -> [(a,b)] -> Maybe b def test_lookup result = @a.lookup expect = [] assert_equal(expect, result) end # -- ** Searching with a predicate # , find -- :: (a -> Bool) -> [a] -> Maybe a def test_find result = @a.find{|x| x==3} expect = 3 assert_equal(expect, result) end # , filter -- :: (a -> Bool) -> [a] -> [a] def test_filter result = @a.filter expect = [] assert_equal(expect, result) end # , partition -- :: (a -> Bool) -> [a] -> ([a], [a]) def test_partition result = @a.partition{|x| x>3} expect = [[4, 5], [1, 2, 3]] assert_equal(expect, result) end # -- * Indexing lists # -- | These functions treat a list @xs@ as a indexed collection, # -- with indices ranging from 0 to @'length' xs - 1@. # , (!!) -- :: [a] -> Int -> a # Don't know how to implement it in Ruby # , elemIndex -- :: (Eq a) => a -> [a] -> Maybe Int def test_elemIndex result = @a.elemIndex expect = [] assert_equal(expect, result) end # , elemIndices -- :: (Eq a) => a -> [a] -> [Int] def test_elemIndices result = @a.elemIndices expect = [] assert_equal(expect, result) end # , findIndex -- :: (a -> Bool) -> [a] -> Maybe Int def test_findIndex result = @a.findIndex expect = [] assert_equal(expect, result) end # , findIndices -- :: (a -> Bool) -> [a] -> [Int] def test_findIndices result = @a.findIndices expect = [] assert_equal(expect, result) end # -- * Zipping and unzipping lists # , zip -- :: [a] -> [b] -> [(a,b)] def test_zip result = @a.zip(@a) expect = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5] ] assert_equal(expect, result) end # , zip3 def test_zip3 result = @a.zip3 expect = [] assert_equal(expect, result) end # , zip4, zip5, zip6, zip7 def test_zip4 result = @a.zip4 expect = [] assert_equal(expect, result) end # , zipWith -- :: (a -> b -> c) -> [a] -> [b] -> [c] def test_zipWith result = @a.zipWith expect = [] assert_equal(expect, result) end # , zipWith3 def test_zipWith3 result = @a.zipWith3 expect = [] assert_equal(expect, result) end # , zipWith4, zipWith5, zipWith6, zipWith7 def test_zipWith4 result = @a.zipWith4 expect = [] assert_equal(expect, result) end # , unzip -- :: [(a,b)] -> ([a],[b]) def test_unzip result = @a.unzip expect = [] assert_equal(expect, result) end # , unzip3 def test_unzip3 result = @a.unzip3 expect = [] assert_equal(expect, result) end # , unzip4, unzip5, unzip6, unzip7 def test_unzip4 result = @a.unzip4 expect = [] assert_equal(expect, result) end # -- * Special lists # -- ** Functions on strings # , lines -- :: String -> [String] def test_lines result = @a.lines expect = [] assert_equal(expect, result) end # , words -- :: String -> [String] def test_words result = @a.words expect = [] assert_equal(expect, result) end # , unlines -- :: [String] -> String def test_unlines result = @a.unlines expect = [] assert_equal(expect, result) end # , unwords -- :: [String] -> String def test_unwords result = @a.unwords expect = [] assert_equal(expect, result) end # -- ** \"Set\" operations # , nub -- :: (Eq a) => [a] -> [a] def test_nub result = @a.nub expect = [] assert_equal(expect, result) end # , delete -- :: (Eq a) => a -> [a] -> [a] def test_delete result = @a.delete(3) expect = [] assert_equal(expect, result) end # , (\\) -- :: (Eq a) => [a] -> [a] -> [a] # Don't know how to implement it in Ruby # , union -- :: (Eq a) => [a] -> [a] -> [a] def test_union result = @a.union expect = [] assert_equal(expect, result) end # , intersect -- :: (Eq a) => [a] -> [a] -> [a] def test_intersect result = @a.intersect expect = [] assert_equal(expect, result) end # -- ** Ordered lists # , sort -- :: (Ord a) => [a] -> [a] # , insert -- :: (Ord a) => a -> [a] -> [a] def test_insert result = @a.insert(3.5) expect = [] assert_equal(expect, result) end end # TestList