File: Listtest.hs

package info (click to toggle)
missingh 1.6.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 688 kB
  • sloc: haskell: 5,472; makefile: 2
file content (243 lines) | stat: -rw-r--r-- 8,330 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{- arch-tag: List tests main file
Copyright (C) 2004-2011 John Goerzen <jgoerzen@complete.org>

All rights reserved.

For license and copyright information, see the file LICENSE
-}

module Listtest(tests) where
import           Data.List
import           Data.List.Utils
import           Test.HUnit
import           Test.HUnit
-- import           Test.QuickCheck as QC

test_delFromAL =
    let f :: [(String, Int)] -> [(String, Int)] -> Test
        f inp exp = TestCase $ exp @=? (delFromAL inp "testkey") in
        [
                 f [] []
                 ,f [("one", 1)] [("one", 1)]
                 ,f [("1", 1), ("2", 2), ("testkey", 3)] [("1", 1), ("2", 2)]
                 ,f [("testkey", 1)] []
                 ,f [("testkey", 1), ("testkey", 2)] []
                 ,f [("testkey", 1), ("2", 2), ("3", 3)] [("2", 2), ("3", 3)]
                 ,f [("testkey", 1), ("2", 2), ("testkey", 3), ("4", 4)]
                    [("2", 2), ("4", 4)]
        ]

test_addToAL =
    let f :: [(String, Int)] -> [(String, Int)] -> Test
        f inp exp = TestCase $ exp @=? (addToAL inp "testkey" 101) in
        [
         f [] [("testkey", 101)]
        ,f [("testkey", 5)] [("testkey", 101)]
        ,f [("testkey", 5), ("testkey", 6)] [("testkey", 101)]
        ]

test_split =
    let f delim inp exp = TestCase $ exp @=? split delim inp in
        [
         f "," "foo,bar,,baz," ["foo", "bar", "", "baz", ""]
        ,f "ba" ",foo,bar,,baz," [",foo,","r,,","z,"]
        ,f "," "" []
        ,f "," "," ["", ""]
        ]

test_join =
    let f :: (Eq a, Show a) => [a] -> [[a]] -> [a] -> Test
        f delim inp exp = TestCase $ exp @=? join delim inp in
        [
         f "|" ["foo", "bar", "baz"] "foo|bar|baz"
        ,f "|" [] ""
        ,f "|" ["foo"] "foo"
         -- f 5 [[1, 2], [3, 4]] [1, 2, 5, 3, 4]
        ]

test_replace =
    let f old new inp exp = TestCase $ exp @=? replace old new inp in
        [
         f "" "" "" ""
        ,f "foo" "bar" "" ""
        ,f "foo" "bar" "foo" "bar"
        ,f "foo" "bar" "footestfoothisisabarfoo" "bartestbarthisisabarbar"
        ,f "," ", " "1,2,3,4" "1, 2, 3, 4"
        ,f "," "." "1,2,3,4" "1.2.3.4"
        ]

test_genericJoin =
    let f delim inp exp = TestCase $ exp @=? genericJoin delim inp in
        [
         f ", " [1, 2, 3, 4] "1, 2, 3, 4"
        ,f ", " ([] :: [Int]) ""
        ,f "|" ["foo", "bar", "baz"] "\"foo\"|\"bar\"|\"baz\""
        ,f ", " [5] "5"
        ]

test_flipAL =
    let f inp exp = TestCase $ exp @=? flipAL inp in
        [
         f ([]::[(Int,Int)]) ([]::[(Int,[Int])])
        ,f [("a", "b")] [("b", ["a"])]
        ,f [("a", "b"),
            ("c", "b"),
            ("d", "e"),
            ("b", "b")] [("b", ["b", "c", "a"]),
                         ("e", ["d"])]
        ]

test_uniq =
    let f inp exp = TestCase $ exp @=? uniq inp in
    [f ([]::[Int]) [],
     f "asdf" "asdf",
     f "aabbcc" "abc",
     f "abcabc" "abc",
     f "aaaaaa" "a",
     f "aaaaaab" "ab",
     f "111111111111111" "1",
     f "baaaaaaaaa" "ba",
     f "baaaaaaaaab" "ba",
     f "aaacccdbbbefff" "acdbef",
     f "foo" "fo",
     f "15553344409" "153409",
     f "Mississippi" "Misp"]

test_trunc =
    let f len inp exp = TestCase $ exp @=? take len inp in
        [
         f 2 "Hello" "He"
        ,f 1 "Hello" "H"
        ,f 0 "Hello" ""
        ,f 2 "H" "H"
        ,f 2 "" ""
        ,f 2 [1, 2, 3, 4, 5] [1, 2]
        ,f 10 "Hello" "Hello"
        ,f 0 "" ""
        ]

test_contains =
    let f msg sub testlist exp = TestCase $ assertEqual msg exp (contains sub testlist) in
        [
         f "t1" "Haskell" "I really like Haskell." True
        ,f "t2" "" "Foo" True
        ,f "t3" "" "" True
        ,f "t4" "Hello" "" False
        ,f "t5" "Haskell" "Haskell" True
        ,f "t6" "Haskell" "1Haskell" True
        ,f "t7" "Haskell" "Haskell1" True
        ,f "t8" "Haskell" "Ocaml" False
        ,f "t9" "Haskell" "OCamlasfasfasdfasfd" False
        ,f "t10" "a" "Hello" False
        ,f "t11" "e" "Hello" True
        ]

test_elemRIndex =
    let f item inp exp = TestCase $ exp @=? elemRIndex item inp in
        [
         f "foo" [] Nothing
        ,f "foo" ["bar", "baz"] Nothing
        ,f "foo" ["foo"] (Just 0)
        ,f "foo" ["foo", "bar"] (Just 0)
        ,f "foo" ["bar", "foo"] (Just 1)
        ,f "foo" ["foo", "bar", "foo", "bar", "foo"] (Just 4)
        ,f 'f' ['f', 'b', 'f', 'f', 'b'] (Just 3)
        ,f 'f' ['b', 'b', 'f'] (Just 2)
        ]

test_alwaysElemRIndex =
    let f item inp exp = TestCase $ exp @=? alwaysElemRIndex item inp in
        [
         f "foo" [] (-1)
        ,f 'f' ['b', 'q'] (-1)
        ,f 'f' ['f', 'b', 'f', 'f', 'b'] 3
        ]

test_subIndex =
    let f item inp exp = TestCase $ exp @=? subIndex item inp in
        [f "foo" "asdfoobar" (Just 3)
        ,f "foo" [] (Nothing)
        ,f "" [] (Just 0)
        ,f "" "asdf" (Just 0)
        ,f "test" "asdftestbartest" (Just 4)
        ,f [(1::Int), 2] [0, 5, 3, 2, 1, 2, 4] (Just 4)
        ]

test_fixedWidth =
    let f inplen inplist exp = TestLabel ((show inplen) ++ ", " ++
                                          (show inplist)) $ TestCase $
                               wholeMap (fixedWidth inplen) inplist @=? exp in
        [
         f [] ([]::[Int]) ([]::[[Int]])
        ,f [1] [5] [[5]]
        ,f [1] [3, 4, 5, 6] [[3], [4, 5, 6]]
        ,f [1] ([]::[Int]) ([]::[[Int]])
        ,f [2] [3] [[3]]
        ,f [2] [3, 4, 5, 6] [[3, 4], [5, 6]]
        ,f [2] [3, 4, 5] [[3, 4], [5]]
        ,f [1, 2, 3] "1234567890"  ["1","23","456","7890"]
        ,f (repeat 2) "123456789" ["12","34","56","78","9"]
        ,f [] "123456789" ["123456789"]
        ,f [5, 3, 6, 1] "Hello, This is a test."
               ["Hello",", T","his is"," ","a test."]
        ]

test_strToAL =
    let f inp exp = TestLabel (show inp) $ TestCase $ do let r = strFromAL inp
                                                         exp @=? r
                                                         inp @=? strToAL r
        in
        [
         f ([]::[(String, String)]) ""
        ,f [("foo", "bar")] "\"foo\",\"bar\"\n"
        ,f [("foo", "bar"), ("baz", "quux")] "\"foo\",\"bar\"\n\"baz\",\"quux\"\n"
        ,f [(1::Int, 2::Int), (3, 4)] "1,2\n3,4\n"
        ,f [(1::Int, "one"), (2, "two")] "1,\"one\"\n2,\"two\"\n"
        ,f [("one", 1::Double), ("n\nl", 2::Double)]
           "\"one\",1.0\n\"n\\nl\",2.0\n"
        ]

test_spanList =
    let f func inp exp = TestLabel (show inp) $ TestCase $ exp @=? spanList func inp
        in
          [f (contains "foo") "Testfoobar" ("Testf", "oobar"),
           f (\_ -> True) "Testasdf" ("Testasdf", ""),
           f (\_ -> False) "Testasdf" ("", "Testasdf"),
           f (contains "foo") "" ("", ""),
           f (contains "foo") "foo" ("f", "oo")]


-- test_merge =
--     qctest "prop_merge" prop_merge

prop_merge xs ys =
    merge (sort xs) (sort ys) == sort (xs ++ ys)
          where types = xs :: [Int]

-- test_mergeBy =
--     qctest "test_mergeBy" prop_mergeBy

prop_mergeBy xs ys =
    mergeBy cmp (sortBy cmp xs) (sortBy cmp ys) == sortBy cmp (xs ++ ys)
          where types = xs :: [Int]
                cmp = compare

tests = TestList [-- test_merge,
                  -- test_mergeBy,
                  TestLabel "delFromAL" (TestList test_delFromAL),
                  TestLabel "uniq" (TestList test_uniq),
                  TestLabel "addToAL" (TestList test_addToAL),
                  TestLabel "split" (TestList test_split),
                  TestLabel "join" (TestList test_join),
                  TestLabel "genericJoin" (TestList test_genericJoin),
                  TestLabel "trunc" (TestList test_trunc),
                  TestLabel "flipAL" (TestList test_flipAL),
                  TestLabel "elemRIndex" (TestList test_elemRIndex),
                  TestLabel "alwaysElemRIndex" (TestList test_alwaysElemRIndex),
                  TestLabel "replace" (TestList test_replace),
                  TestLabel "contains" (TestList test_contains),
                  TestLabel "strFromAL & strToAL" (TestList test_strToAL),
                  TestLabel "fixedWidth" (TestList test_fixedWidth),
                  TestLabel "subIndex" (TestList test_subIndex),
                  TestLabel "spanList" (TestList test_spanList)]