on master help sign in
Merge branch 'master' into webgl

Conflicts: server/elm-server.cabal

commit2d75c3920d5eeaf56176527618d848e36b10f5ac authorJohn P Mayer Jr <john.p.mayer.jr@gmail.com> dateSat 19 Apr 2014, 23:56 (13 years ago) parent819bf6bfa1ea89
16 files changed+227−280
Elm-Test+1−1 IO+1−1 automaton+1−1 libraries/Array.elm+30−17 libraries/Dict.elm+76−18 libraries/List.elm+7−1 libraries/Native/Array.js+2−2 libraries/Set.elm+11−1 server/LICENSE−30 server/Server.hs−127 server/Setup.hs−2 server/elm-server.cabal−41 tests/elm/Test.elm+2−1 tests/elm/Test/Array.elm+44−37 tests/elm/Test/Dict.elm+26 tests/elm/Test/Set.elm+26
Elm-Test+1−1

Submodule changed.

IO+1−1

Submodule changed.

automaton+1−1

Submodule changed.

libraries/Array.elm+30−17
@@ -11,7 +11,7 @@ reads, updates, and appends.
1111@docs length, push, append
1212
1313# Get and Set
14−@docs get, getMaybe, getWithDefault, set
14+@docs get, getSafe, getUnsafe, set
1515
1616# Taking Arrays Apart
1717@docs toList, toIndexedList, slice
@@ -82,20 +82,30 @@ map = Native.Array.map
8282indexedMap : (Int -> a -> b) -> Array a -> Array b
8383indexedMap = Native.Array.indexedMap
8484
85−{-| Reduce an array from the left.
85+{-| Reduce an array from the left. Read `foldl` as &ldquo;fold from the left&rdquo;.
8686
8787 foldl (::) [] (fromList [1,2,3]) == [3,2,1]
8888-}
8989foldl : (a -> b -> b) -> b -> Array a -> b
9090foldl = Native.Array.foldl
9191
92−{-| Reduce an array from the right.
92+{-| Reduce an array from the right. Read `foldr` as &ldquo;fold from the right&rdquo;.
9393
9494 foldr (+) 0 (repeat 3 5) == 15
9595-}
9696foldr : (a -> b -> b) -> b -> Array a -> b
9797foldr = Native.Array.foldr
9898
99+{-| Keep only elements that satisfy the predicate:
100+
101+ filter isEven (fromList [1..6]) == (fromList [2,4,6])
102+-}
103+filter : (a -> Bool) -> Array a -> Array a
104+filter isOkay arr =
105+ let update x xs = if isOkay x then Native.Array.push x xs else xs
106+ in
107+ Native.Array.foldl update Native.Array.empty arr
108+
99109{-| Return an empty array.
100110
101111 length empty == 0
@@ -110,22 +120,25 @@ empty = Native.Array.empty
110120push : a -> Array a -> Array a
111121push = Native.Array.push
112122
113−{-| Return the element at the index. Breaks, if index is out of range. If the
114−array length is unkown, use withDefaultGet oder getWithDefault.
123+{-| Get the element at a particular index.
124+
125+ getUnsafe 2 (A.fromList [3,2,1]) == 1
115126
116− get 2 (A.fromList [3,2,1]) == 1
127+Warning: this function will result in a runtime error if the index is not found,
128+so it is best to use `get` or `getSafe` unless you are very confident the index
129+will be found.
117130-}
118−get : Int -> Array a -> a
119−get = Native.Array.get
131+getUnsafe : Int -> Array a -> a
132+getUnsafe = Native.Array.get
120133
121134{-| Return Just the element at the index or Nothing if the index is out of range.
122135
123− getMaybe 2 (fromList [3,2,1]) == Just 2
124− getMaybe 5 (fromList [3,2,1]) == Nothing
125− getMaybe -1 (fromList [3,2,1]) == Nothing
136+ get 2 (fromList [3,2,1]) == Just 2
137+ get 5 (fromList [3,2,1]) == Nothing
138+ get -1 (fromList [3,2,1]) == Nothing
126139-}
127−getMaybe : Int -> Array a -> Maybe a
128−getMaybe i array =
140+get : Int -> Array a -> Maybe a
141+get i array =
129142 if 0 <= i && i < Native.Array.length array
130143 then Just (Native.Array.get i array)
131144 else Nothing
@@ -133,11 +146,11 @@ getMaybe i array =
133146{-| Get the element at the index. If the index is out of range, the given default
134147element is returned.
135148
136− getWithDefault 0 2 (fromList [3,2,1]) == 1
137− getWithDefault 0 5 (fromList [3,2,1]) == 0
149+ getSafe 0 2 (fromList [3,2,1]) == 1
150+ getSafe 0 5 (fromList [3,2,1]) == 0
138151-}
139−getWithDefault : a -> Int -> Array a -> a
140−getWithDefault default i array =
152+getSafe : a -> Int -> Array a -> a
153+getSafe default i array =
141154 if 0 <= i && i < Native.Array.length array
142155 then Native.Array.get i array
143156 else default
libraries/Dict.elm+76−18
@@ -1,6 +1,8 @@
11module Dict (empty,singleton,insert,update
2− ,lookup,findWithDefault
2+ ,get,getSafe,getUnsafe
33 ,remove,member
4+ ,filter
5+ ,partition
46 ,foldl,foldr,map
57 ,union,intersect,diff
68 ,keys,values
@@ -17,7 +19,7 @@ Insert, remove, and query operations all take *O(log n)* time.
1719@docs empty, singleton, insert, update, remove
1820
1921# Query
20−@docs member, lookup, findWithDefault
22+@docs member, get, getMaybe, getWithDefault
2123
2224# Combine
2325@docs union, intersect, diff
@@ -26,7 +28,7 @@ Insert, remove, and query operations all take *O(log n)* time.
2628@docs keys, values, toList, fromList
2729
2830# Transform
29−@docs map, foldl, foldr
31+@docs map, foldl, foldr, filter, partition
3032
3133-}
3234
@@ -84,33 +86,74 @@ max t =
8486 RBNode _ _ _ _ r -> max r
8587 RBEmpty _ -> Native.Error.raise "(max Empty) is not defined"
8688
87−{-| Lookup the value associated with a key. -}
88−lookup : comparable -> Dict comparable v -> Maybe v
89−lookup k t =
89+{-| Get the value associated with a key. If the key is not found, return
90+`Nothing`. This is useful when you are not sure if a key will be in the
91+dictionary.
92+
93+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
94+
95+ get "Tom" animals == Just Cat
96+ get "Mouse" animals == Just Mouse
97+ get "Spike" animals == Nothing
98+
99+The `getSafe` and `getUnsafe` are built-in ways to handle common ways of
100+using the resulting `Maybe`.
101+-}
102+get : comparable -> Dict comparable v -> Maybe v
103+get k t =
90104 case t of
91105 RBEmpty LBlack -> Nothing
92106 RBNode _ k' v l r ->
93107 case Native.Utils.compare k k' of
94− LT -> lookup k l
108+ LT -> get k l
95109 EQ -> Just v
96− GT -> lookup k r
110+ GT -> get k r
111+
112+{-| Get the value associated with a key. If the key is not found,
113+return a default value.
97114
98−{-| Find the value associated with a key. If the key is not found,
99−return the default value. -}
100−findWithDefault : v -> comparable -> Dict comparable v -> v
101−findWithDefault base k t =
115+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
116+
117+ getSafe Dog "Tom" animals == Cat
118+ getSafe Dog "Mouse" animals == Mouse
119+ getSafe Dog "Spike" animals == Dog
120+-}
121+getSafe : v -> comparable -> Dict comparable v -> v
122+getSafe base k t =
102123 case t of
103124 RBEmpty LBlack -> base
104125 RBNode _ k' v l r ->
105126 case Native.Utils.compare k k' of
106− LT -> findWithDefault base k l
127+ LT -> getSafe base k l
107128 EQ -> v
108− GT -> findWithDefault base k r
129+ GT -> getSafe base k r
130+
131+{-| Get the value associated with a key.
132+
133+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
134+
135+ get "Tom" animals == Cat
136+ get "Mouse" animals == Mouse
137+ get "Spike" animals == -- Runtime Error!
138+
139+Warning: this function will result in a runtime error if the key is not found,
140+so it is best to use `get` or `getSafe` unless you are very confident the key
141+will be found.
142+-}
143+getUnsafe : comparable -> Dict comparable v -> v
144+getUnsafe k t =
145+ case t of
146+ RBEmpty LBlack -> Native.Error.raise "key not found when using 'getUnsafe'"
147+ RBNode _ k' v l r ->
148+ case Native.Utils.compare k k' of
149+ LT -> getUnsafe k l
150+ EQ -> v
151+ GT -> getUnsafe k r
109152
110153{-| Determine if a key is in a dictionary. -}
111154member : comparable -> Dict comparable v -> Bool
112155-- Does t contain k?
113−member k t = isJust <| lookup k t
156+member k t = isJust <| get k t
114157
115158ensureBlackRoot : Dict k v -> Dict k v
116159ensureBlackRoot t =
@@ -327,9 +370,7 @@ union t1 t2 = foldl insert t2 t1
327370{-| Keep a key-value pair when its key appears in the second dictionary.
328371Preference is given to values in the first dictionary. -}
329372intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
330−intersect t1 t2 =
331− let combine k v t = if k `member` t2 then insert k v t else t
332− in foldl combine empty t1
373+intersect t1 t2 = filter (\k _ -> k `member` t2) t1
333374
334375{-| Keep a key-value pair when its key does not appear in the second dictionary.
335376Preference is given to the first dictionary. -}
@@ -351,3 +392,20 @@ toList t = foldr (\k v acc -> (k,v) :: acc) [] t
351392{-| Convert an association list into a dictionary. -}
352393fromList : [(comparable,v)] -> Dict comparable v
353394fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
395+
396+{-| Keep a key-value pair when it satisfies a predicate. -}
397+filter : (comparable -> v -> Bool) -> Dict comparable v -> Dict comparable v
398+filter p dict =
399+ let add k v t = if p k v then insert k v t else t
400+ in foldl add empty dict
401+
402+{-| Partition a dictionary according to a predicate. The first dictionary
403+contains all key-value pairs which satisfy the predicate, and the second
404+contains the rest.
405+-}
406+partition : (comparable -> v -> Bool) -> Dict comparable v -> (Dict comparable v, Dict comparable v)
407+partition p dict =
408+ let add k v (t1, t2) = if p k v
409+ then (insert k v t1, t2)
410+ else (t1, insert k v t2)
411+ in foldl add (empty, empty) dict
libraries/List.elm+7−1
@@ -163,7 +163,13 @@ maximum = foldl1 max
163163minimum : [comparable] -> comparable
164164minimum = foldl1 min
165165
166−{-| Split a list based on the predicate. -}
166+{-| Partition a list based on a predicate. The first list contains all values
167+that satisfy the predicate, and the second list contains all the value that do
168+not.
169+
170+ partition (\x -> x < 3) [0..5] == ([0,1,2], [3,4,5])
171+ partition isEven [0..5] == ([0,2,4], [1,3,5])
172+-}
167173partition : (a -> Bool) -> [a] -> ([a],[a])
168174partition pred lst =
169175 case lst of
libraries/Native/Array.js+2−2
@@ -242,14 +242,14 @@ Elm.Native.Array.make = function(elm) {
242242 }
243243
244244 function foldl(f, b, a) {
245− for (var i = a.table.length - 1; i >= 0; i--) {
245+ for (var i = 0; i < a.table.length; i++) {
246246 b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
247247 }
248248 return b;
249249 }
250250
251251 function foldr(f, b, a) {
252− for (var i = 0; i < a.table.length; i++) {
252+ for (var i = a.table.length; i--; ) {
253253 b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
254254 }
255255 return b;
libraries/Set.elm+11−1
@@ -2,6 +2,7 @@
22module Set (empty,singleton,insert,remove
33 ,member
44 ,foldl,foldr,map
5+ ,filter,partition
56 ,union,intersect,diff
67 ,toList,fromList
78 ) where
@@ -25,7 +26,7 @@ Insert, remove, and query operations all take *O(log n)* time.
2526@docs toList, fromList
2627
2728# Transform
28−@docs map, foldl, foldr
29+@docs map, foldl, foldr, filter, partition
2930
3031-}
3132
@@ -88,3 +89,12 @@ foldr f b s = Dict.foldr (\k _ b -> f k b) b s
8889{-| Map a function onto a set, creating a new set with no duplicates. -}
8990map : (comparable -> comparable') -> Set comparable -> Set comparable'
9091map f s = fromList (List.map f (toList s))
92+
93+{-| Create a new set consisting only of elements which satisfy a predicate. -}
94+filter : (comparable -> Bool) -> Set comparable -> Set comparable
95+filter p set = Dict.filter (\k _ -> p k) set
96+
97+{-| Create two new sets; the first consisting of elements which satisfy a
98+predicate, the second consisting of elements which do not. -}
99+partition : (comparable -> Bool) -> Set comparable -> (Set comparable, Set comparable)
100+partition p set = Dict.partition (\k _ -> p k) set
server/LICENSEdeleted−30
@@ -1,30 +0,0 @@
1−Copyright (c)2012-2014, Evan Czaplicki
2−
3−All rights reserved.
4−
5−Redistribution and use in source and binary forms, with or without
6−modification, are permitted provided that the following conditions are met:
7−
8− * Redistributions of source code must retain the above copyright
9− notice, this list of conditions and the following disclaimer.
10−
11− * Redistributions in binary form must reproduce the above
12− copyright notice, this list of conditions and the following
13− disclaimer in the documentation and/or other materials provided
14− with the distribution.
15−
16− * Neither the name of Evan Czaplicki nor the names of other
17− contributors may be used to endorse or promote products derived
18− from this software without specific prior written permission.
19−
20−THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21−"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22−LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23−A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24−OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25−SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26−LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27−DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28−THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29−(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30−OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
server/Server.hsdeleted−127
@@ -1,127 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
3−module Main where
4−
5−import Control.Applicative ((<$>),(<|>))
6−import Control.Monad (guard)
7−import Control.Monad.Trans (MonadIO(liftIO))
8−import qualified Data.ByteString as BS
9−import qualified Data.ByteString.Char8 as BSC
10−import qualified Data.HashMap.Strict as Map
11−import qualified Data.Version as Version
12−import System.Console.CmdArgs
13−import System.Directory
14−import System.Exit
15−import System.FilePath
16−import System.Process
17−import System.IO (hGetContents, Handle)
18−
19−import Paths_elm_server (version)
20−import qualified Elm.Internal.Paths as Elm
21−import Snap.Core
22−import Snap.Http.Server
23−import Snap.Util.FileServe
24−
25−data Flags = Flags
26− { port :: Int
27− , runtime :: Maybe FilePath
28− } deriving (Data,Typeable,Show,Eq)
29−
30−flags :: Flags
31−flags = Flags
32− { port = 8000 &= help "set the port of the server"
33− , runtime = Nothing &= typFile
34− &= help "Specify a custom location for Elm's runtime system."
35− } &= help "Quickly reload Elm projects in your browser. Just refresh to recompile.\n\
36− \It serves static files and freshly recompiled Elm files."
37− &= helpArg [explicit, name "help", name "h"]
38− &= versionArg [ explicit, name "version", name "v"
39− , summary (Version.showVersion version)
40− ]
41− &= summary ("Elm Server " ++ Version.showVersion version ++
42− ", (c) Evan Czaplicki 2011-2014")
43−
44−
45−config :: Config Snap a
46−config = setAccessLog ConfigNoLog (setErrorLog ConfigNoLog defaultConfig)
47−
48−-- | Set up the server.
49−main :: IO ()
50−main = do
51− cargs <- cmdArgs flags
52− putStrLn $ "Elm Server " ++ Version.showVersion version ++
53− ": Just refresh a page to recompile it!"
54− httpServe (setPort (port cargs) config) $
55− serveRuntime (maybe Elm.runtime id (runtime cargs))
56− <|> serveElm
57− <|> serveDirectoryWith directoryConfig "."
58−
59−directoryConfig :: MonadSnap m => DirectoryConfig m
60−directoryConfig =
61− fancyDirectoryConfig
62− { indexGenerator = defaultIndexGenerator defaultMimeTypes indexStyle
63− , mimeTypes = Map.insert ".elm" "text/html" defaultMimeTypes
64− }
65−
66−indexStyle :: BS.ByteString
67−indexStyle =
68− "body { margin:0; font-family:sans-serif; background:rgb(245,245,245);\
69− \ font-family: calibri, verdana, helvetica, arial; }\
70− \div.header { padding: 40px 50px; font-size: 24px; }\
71− \div.content { padding: 0 40px }\
72− \div.footer { display:none; }\
73− \table { width:100%; border-collapse:collapse; }\
74− \td { padding: 6px 10px; }\
75− \tr:nth-child(odd) { background:rgb(216,221,225); }\
76− \td { font-family:monospace }\
77− \th { background:rgb(90,99,120); color:white; text-align:left;\
78− \ padding:10px; font-weight:normal; }"
79−
80−runtimeName :: String
81−runtimeName = "elm-runtime.js"
82−
83−serveRuntime :: FilePath -> Snap ()
84−serveRuntime runtimePath =
85− do file <- BSC.unpack . rqPathInfo <$> getRequest
86− guard (file == runtimeName)
87− serveFileAs "application/javascript" runtimePath
88−
89−serveElm :: Snap ()
90−serveElm =
91− do file <- BSC.unpack . rqPathInfo <$> getRequest
92− exists <- liftIO $ doesFileExist file
93− guard (exists && takeExtension file == ".elm")
94− onSuccess (compile file) (serve file)
95− where
96− compile file =
97− let elmArgs = [ "--make", "--runtime=" ++ runtimeName, file ]
98− in createProcess $ (proc "elm" elmArgs) { std_out = CreatePipe }
99−
100− serve file =
101− serveFileAs "text/html; charset=UTF-8" ("build" </> replaceExtension file "html")
102−
103−failure :: String -> Snap ()
104−failure msg =
105− do modifyResponse $ setResponseStatus 404 "Not found"
106− writeBS $ BSC.pack msg
107−
108−onSuccess :: IO (t, Maybe Handle, t1, ProcessHandle) -> Snap () -> Snap ()
109−onSuccess action success =
110− do (_, stdout, _, handle) <- liftIO action
111− exitCode <- liftIO $ waitForProcess handle
112− case (exitCode, stdout) of
113− (ExitFailure 127, _) ->
114− failure "Error: elm compiler not found in your path."
115−
116− (ExitFailure _, Just out) ->
117− failure =<< liftIO (hGetContents out)
118−
119− (ExitFailure _, Nothing) ->
120− failure "See command line for error message."
121−
122− (ExitSuccess, _) -> success
123−
124−{--
125−pageTitle :: String -> String
126−pageTitle = dropExtension . takeBaseName
127−--}
server/Setup.hsdeleted−2
@@ -1,2 +0,0 @@
1−import Distribution.Simple
2−main = defaultMain
server/elm-server.cabaldeleted−41
@@ -1,41 +0,0 @@
1−Name: elm-server
2−Version: 0.12
3−Synopsis: Server for developing Elm projects
4−Description: Provides a standalone Snap server that serves static files.
5− For Elm files, it recompiles them and serves them as HTML,
6− so you can just refresh to see the new version.
7−
8−Homepage: http://elm-lang.org
9−
10−License: BSD3
11−License-file: LICENSE
12−
13−Author: Evan Czaplicki
14−Maintainer: info@elm-lang.org
15−Copyright: Copyright: (c) 2011-2014 Evan Czaplicki
16−
17−Category: Compiler, Language
18−
19−Build-type: Simple
20−
21−--Extra-source-files: README.md
22−Cabal-version: >=1.8
23−
24−source-repository head
25− type: git
26− location: git://github.com/evancz/Elm.git
27−
28−Executable elm-server
29− Main-is: Server.hs
30− Build-depends: base >=4.2 && <5,
31− bytestring,
32− cmdargs,
33− containers >= 0.3,
34− directory,
35− filepath,
36− Elm >= 0.12,
37− snap-core,
38− snap-server,
39− mtl,
40− process,
41− unordered-containers
tests/elm/Test.elm+2−1
@@ -12,9 +12,10 @@ import IO.Runner as Run
1212import Test.String as String
1313import Test.Trampoline as Trampoline
1414import Test.Array as Array
15+import Test.Dict as Dict
1516
1617tests : [Test]
17−tests = String.tests ++ Trampoline.tests ++ Array.tests
18+tests = String.tests ++ Trampoline.tests ++ Array.tests ++ Dict.tests
1819
1920console : IO ()
2021console = runDisplay tests
tests/elm/Test/Array.elm+44−37
@@ -8,40 +8,47 @@ import ElmTest.Test (..)
88
99tests : [Test]
1010tests =
11− let creationTests = [ test "empty" <| assertEqual Array.empty (Array.fromList [])
12− , test "initialize" <| assertEqual (Array.initialize 4 id) (Array.fromList [0,1,2,3])
13− , test "initialize 2" <| assertEqual (Array.initialize 4 (\n -> n*n)) (Array.fromList [0,1,4,9])
14− , test "initialize 3" <| assertEqual (Array.initialize 4 (always 0)) (Array.fromList [0,0,0,0])
15− , test "initialize Empty" <| assertEqual (Array.initialize 0 id) Array.empty
16− , test "initialize 4" <| assertEqual (Array.initialize 2 (always 0)) (Array.fromList [0,0])
17− , test "repeat" <| assertEqual (Array.repeat 5 40) (Array.fromList [40,40,40,40,40])
18− , test "repeat 2" <| assertEqual (Array.repeat 5 0) (Array.fromList [0,0,0,0,0])
19− , test "repeat 3" <| assertEqual (Array.repeat 3 "cat") (Array.fromList ["cat","cat","cat"])
20− , test "fromList" <| assertEqual (Array.fromList []) Array.empty
21− ]
22− basicsTests = [ test "length" <| assertEqual (Array.length (Array.fromList [1,2,3])) 3
23− , test "length - Long" <| assertEqual (Array.length (Array.repeat 10000 0)) 10000
24− , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
25− , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
26− ]
27− getAndSetTests = [ test "get" <| assertEqual (Array.get 2 (Array.fromList [3,2,1])) 1
28− , test "getMaybe" <| assertEqual (Array.getMaybe 1 (Array.fromList [3,2,1])) (Just 2)
29− , test "getMaybe 2" <| assertEqual (Array.getMaybe 5 (Array.fromList [3,2,1])) Nothing
30− , test "getMaybe 3" <| assertEqual (Array.getMaybe -1 (Array.fromList [3,2,1])) Nothing
31− , test "getWithDefault" <| assertEqual (Array.getWithDefault 0 2 (Array.fromList [3,2,1])) 1
32− , test "getWithDefault 2" <| assertEqual (Array.getWithDefault 0 5 (Array.fromList [3,2,1])) 0
33− , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
34− ]
35− takingArraysApartTests = [ test "toList" <| assertEqual (Array.toList (Array.fromList [3,5,8])) [3,5,8]
36− , test "toIndexedList" <| assertEqual (Array.toIndexedList (Array.fromList ["cat","dog"])) [(0,"cat"), (1,"dog")]
37− , test "slice" <| assertEqual (Array.slice 1 2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2])
38− , test "slice 2" <| assertEqual (Array.slice 1 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2,3])
39− , test "slice 3" <| assertEqual (Array.slice -3 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [2,3])
40− ]
41− mappingAndFoldingTests = [ test "map" <| assertEqual (Array.map sqrt (Array.fromList [1,4,9])) (Array.fromList [1,2,3])
42− , test "indexedMap" <| assertEqual (Array.indexedMap (*) (Array.fromList [5,5,5])) (Array.fromList [0,5,10])
43− , test "foldl" <| assertEqual (Array.foldl (::) [] (Array.fromList [1,2,3])) [1,2,3]
44− , test "foldr" <| assertEqual (Array.foldr (+) 0 (Array.repeat 3 5)) 15
45− , test "foldl 2" <| assertEqual (Array.foldr (::) [] (Array.fromList [1,2,3])) [3,2,1]
46− ]
47− in List.concat [creationTests, basicsTests, getAndSetTests, takingArraysApartTests, mappingAndFoldingTests]
11+ let creationTests =
12+ [ test "empty" <| assertEqual Array.empty (Array.fromList [])
13+ , test "initialize" <| assertEqual (Array.initialize 4 id) (Array.fromList [0,1,2,3])
14+ , test "initialize 2" <| assertEqual (Array.initialize 4 (\n -> n*n)) (Array.fromList [0,1,4,9])
15+ , test "initialize 3" <| assertEqual (Array.initialize 4 (always 0)) (Array.fromList [0,0,0,0])
16+ , test "initialize Empty" <| assertEqual (Array.initialize 0 id) Array.empty
17+ , test "initialize 4" <| assertEqual (Array.initialize 2 (always 0)) (Array.fromList [0,0])
18+ , test "repeat" <| assertEqual (Array.repeat 5 40) (Array.fromList [40,40,40,40,40])
19+ , test "repeat 2" <| assertEqual (Array.repeat 5 0) (Array.fromList [0,0,0,0,0])
20+ , test "repeat 3" <| assertEqual (Array.repeat 3 "cat") (Array.fromList ["cat","cat","cat"])
21+ , test "fromList" <| assertEqual (Array.fromList []) Array.empty
22+ ]
23+ basicsTests =
24+ [ test "length" <| assertEqual (Array.length (Array.fromList [1,2,3])) 3
25+ , test "length - Long" <| assertEqual (Array.length (Array.repeat 10000 0)) 10000
26+ , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
27+ , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
28+ ]
29+ getAndSetTests =
30+ [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
31+ , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
32+ , test "get 3" <| assertEqual (Array.get -1 (Array.fromList [3,2,1])) Nothing
33+ , test "getSafe 1" <| assertEqual (Array.getSafe 0 2 (Array.fromList [3,2,1])) 1
34+ , test "getSafe 2" <| assertEqual (Array.getSafe 0 5 (Array.fromList [3,2,1])) 0
35+ , test "getUnsafe" <| assertEqual (Array.getUnsafe 2 (Array.fromList [3,2,1])) 1
36+ , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
37+ ]
38+ takingArraysApartTests =
39+ [ test "toList" <| assertEqual (Array.toList (Array.fromList [3,5,8])) [3,5,8]
40+ , test "toIndexedList" <| assertEqual (Array.toIndexedList (Array.fromList ["cat","dog"])) [(0,"cat"), (1,"dog")]
41+ , test "slice" <| assertEqual (Array.slice 1 2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2])
42+ , test "slice 2" <| assertEqual (Array.slice 1 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2,3])
43+ , test "slice 3" <| assertEqual (Array.slice -3 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [2,3])
44+ ]
45+ mappingAndFoldingTests =
46+ [ test "map" <| assertEqual (Array.map sqrt (Array.fromList [1,4,9])) (Array.fromList [1,2,3])
47+ , test "indexedMap" <| assertEqual (Array.indexedMap (*) (Array.fromList [5,5,5])) (Array.fromList [0,5,10])
48+ , test "foldl" <| assertEqual (Array.foldl (::) [] (Array.fromList [1,2,3])) [3,2,1]
49+ , test "foldr" <| assertEqual (Array.foldr (+) 0 (Array.repeat 3 5)) 15
50+ , test "foldr 2" <| assertEqual (Array.foldr (::) [] (Array.fromList [1,2,3])) [1,2,3]
51+ , test "filter" <| assertEqual (Array.filter (\x -> x `mod` 2 == 0) (Array.fromList [1..6])) (Array.fromList [2,4,6])
52+ ]
53+ in
54+ List.concat [creationTests, basicsTests, getAndSetTests, takingArraysApartTests, mappingAndFoldingTests]
tests/elm/Test/Dict.elmnew file+26
@@ -0,0 +1,26 @@
1+module Test.Dict (tests) where
2+
3+import Dict
4+import List
5+
6+import ElmTest.Assertion (..)
7+import ElmTest.Test (..)
8+
9+animals : Dict.Dict String String
10+animals = Dict.fromList [ ("Tom", "cat"), ("Jerry", "mouse") ]
11+
12+tests : [Test]
13+tests =
14+ let getTests =
15+ [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
16+ , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
17+ , test "getSafe 1" <| assertEqual "mouse" (Dict.getSafe "dog" "Jerry" animals)
18+ , test "getSafe 2" <| assertEqual "dog" (Dict.getSafe "dog" "Spike" animals)
19+ , test "getUnsafe" <| assertEqual "cat" (Dict.getUnsafe "Tom" animals)
20+ ]
21+ filterTests =
22+ [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)
23+ , test "partition" <| assertEqual (Dict.singleton "Tom" "cat", Dict.singleton "Jerry" "mouse") (Dict.partition (\k v -> k == "Tom") animals)
24+ ]
25+ in
26+ List.concat [ getTests, filterTests ]
tests/elm/Test/Set.elmnew file+26
@@ -0,0 +1,26 @@
1+module Test.Set (tests) where
2+
3+import Set
4+import List
5+
6+import ElmTest.Assertion (..)
7+import ElmTest.Test (..)
8+
9+set : Set
10+set = Set.fromList [1..100]
11+
12+setPart1 : Set
13+setPart1 = Set.fromList [1..50]
14+
15+setPart2 : Set
16+setPart2 = Set.fromList [51..100]
17+
18+pred : Int -> Bool
19+pred x = x <= 50
20+
21+tests : [Test]
22+tests =
23+ let
24+ filterTests = [assertEqual setPart1 <| filter pred set]
25+ partitionTests = [assertEqual (setPart1, setPart2) <| partition pred set]
26+ in List.concat [partitionTests, filterTests]