Merge pull request #56 from tgecho/run-length-encoding-property-tests

Add additional test cases (including a property based test!) to run-length-encoding
This commit is contained in:
Lew Parker 2016-04-07 13:12:47 -06:00
commit 03be35553d
5 changed files with 84 additions and 3 deletions

View file

@ -29,6 +29,7 @@
],
"exposed-modules": [],
"dependencies": {
"NoRedInk/elm-check": "3.0.0 <= v < 4.0.0",
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
"elm-lang/core": "2.0.0 <= v < 4.0.0",
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"

View file

@ -1,4 +1,4 @@
module RunLengthEncoding (encode, decode) where
module RunLengthEncoding (version, encode, decode) where
import String exposing (fromChar)
import List exposing (head, tail)
@ -12,6 +12,10 @@ it an idiomatic exemplar to emulate.
-}
version =
2
encode : String -> String
encode string =
String.toList string

View file

@ -0,0 +1,64 @@
module RunLengthEncodingPropertyChecks (propertyTests) where
import ElmTest
import Check exposing (suite, claim, that, is, for, quickCheck)
import Check.Producer as P
import Check.Test
import RunLengthEncoding exposing (decode, encode)
import String as S
import Char
{-
Welcome! This is a property based test which will generate a bunch of random
test cases in an attempt to find edge cases in your solution. If all goes well,
any code that passes the regular tests should be fine here as well. If it goes
less well, this should hopefully narrow the failure down to a useful test case.
Good luck!
-}
claims : Check.Claim
claims =
suite
"List Reverse"
[ claim
"Encoding and decoding yields the original string"
`that` (\input -> decode (encode (S.concat input)))
`is` S.concat
`for` inputProducer
]
inputProducer : P.Producer (List String)
inputProducer =
P.tuple ( P.rangeInt 0 1001, upperCaseLetter )
|> P.convert
(\( n, c ) -> S.repeat n (S.fromChar c))
(\s ->
( S.length s
, S.toList s |> List.head |> crashIfNothing
)
)
|> P.list
upperCaseLetter : P.Producer Char
upperCaseLetter =
P.filter Char.isUpper P.upperCaseChar
crashIfNothing : Maybe a -> a
crashIfNothing a =
case a of
Just a ->
a
Nothing ->
Debug.crash "Nothing!"
propertyTests : ElmTest.Test
propertyTests =
Check.Test.evidenceToTest (quickCheck claims)

View file

@ -3,7 +3,8 @@ module Main (..) where
import Task
import Console
import ElmTest exposing (..)
import RunLengthEncoding exposing (decode, encode)
import RunLengthEncoding exposing (version, decode, encode)
import RunLengthEncodingPropertyChecks exposing (propertyTests)
tests : Test
@ -11,6 +12,9 @@ tests =
suite
"RunLengthEncoding"
[ test
"the solution is for the correct version of the test"
(assertEqual 2 version)
, test
"encode simple"
(assertEqual "2A3B4C" (encode "AABBBCCCC"))
, test
@ -29,17 +33,24 @@ tests =
(decode "12WB12W3B24WB")
)
, test
"decode(encode(...)) combination"
"(decode (encode (...)) combination"
(assertEqual
"zzz ZZ zZ"
(decode (encode "zzz ZZ zZ"))
)
, test
"decode with a x10 value"
(assertEqual
"WWWWWWWWWW"
(decode "10W")
)
, test
"encode unicode"
(assertEqual "32" (encode ""))
, test
"decode unicode"
(assertEqual "" (decode "32"))
, propertyTests
]

View file

@ -8,6 +8,7 @@
],
"exposed-modules": [],
"dependencies": {
"NoRedInk/elm-check": "3.0.0 <= v < 4.0.0",
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
"elm-lang/core": "2.0.0 <= v < 4.0.0",
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"