Week2: Unit tests done and added comments where needed

This commit is contained in:
Kaloyan Stoykov
2024-03-01 19:55:06 +01:00
parent 260ab592da
commit 60d9c6e242
2 changed files with 134 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ encode offset character =
-- Char.isUpper replaces the check for the ASCII code range of uppercase letters -- Char.isUpper replaces the check for the ASCII code range of uppercase letters
if Char.isUpper character then if Char.isUpper character then
shiftChar (Char.toCode 'A') offset character shiftChar (Char.toCode 'A') offset character
-- Char.isLowe replaces the check for the ASCII code range of lowercase letters -- Char.isLower replaces the check for the ASCII code range of lowercase letters
else if Char.isLower character then else if Char.isLower character then
shiftChar (Char.toCode 'a') offset character shiftChar (Char.toCode 'a') offset character
else ' ' else ' '
@@ -24,18 +24,24 @@ decode: Int -> Char -> Char
decode offset character = decode offset character =
encode (0 - offset) character encode (0 - offset) character
{- Return a list with a filter function that keeps only uppercase and lowercase ascii chars. -}
normalize: String -> String normalize: String -> String
normalize input = normalize input =
String.fromList (List.filter Char.isAlpha (String.toList input)) String.fromList (List.filter Char.isAlpha (String.toList input))
encrypt: Int -> String -> String encrypt: Int -> String -> String
encrypt offset input = encrypt offset input =
case String.uncons input of case String.uncons input of
-- Check for empty string
Nothing -> Nothing ->
"" ""
-- When string is split into form of "a", "bc" for example, encode the head and call recursively with the same offset -> encoded char :: encrypt offset tail
Just (head, tail) -> Just (head, tail) ->
String.cons (encode offset head) (encrypt offset tail) String.cons (encode offset head) (encrypt offset tail)
{- Mirrored version of encrypt with decode and decrypt used instead -}
decrypt: Int -> String -> String decrypt: Int -> String -> String
decrypt offset input = decrypt offset input =
case String.uncons input of case String.uncons input of
@@ -43,3 +49,65 @@ decrypt offset input =
"" ""
Just (head, tail) -> Just (head, tail) ->
String.cons (decode offset head) (decrypt offset tail) String.cons (decode offset head) (decrypt offset tail)
testEncodeUpperSmallOffsetA: Bool
testEncodeUpperSmallOffsetA = (encode 5 'A' == 'F')
testEncodeUpperBigOffsetF: Bool
testEncodeUpperBigOffsetF = (encode 348 'F' == 'P')
testEncodeLowerSmallOffsetQ: Bool
testEncodeLowerSmallOffsetQ = (encode 9 'q' == 'z')
testEncodeLowerBigOffsetG: Bool
testEncodeLowerBigOffsetG = (encode 215 'g' == 'n')
testDecodeUpperSmallOffsetA: Bool
testDecodeUpperSmallOffsetA = (decode 5 'F' == 'A')
testDecodeUpperBigOffsetF: Bool
testDecodeUpperBigOffsetF = (decode 348 'P' == 'F')
testDecodeLowerSmallOffsetQ: Bool
testDecodeLowerSmallOffsetQ = (decode 9 'z' == 'q')
testDecodeLowerBigOffsetG: Bool
testDecodeLowerBigOffsetG = (decode 215 'n' == 'g')
testShiftChar: Bool
testShiftChar = (shiftChar 97 3 'a' == 'd')
testNormalize: Bool
testNormalize = (normalize "Hello, Fontys!" == "HelloFontys")
testNormalizeB: Bool
testNormalizeB = (normalize "Hello#@#!#@" == "Hello")
testEncrypt: Bool
testEncrypt = (encrypt 7 "HelloFontys" == "OlssvMvuafz")
testEncryptB: Bool
testEncryptB = (encrypt 6 "RandomText" == "XgtjusZkdz")
testDecrypt: Bool
testDecrypt = (decrypt 6 "Hsdsakw" == "Bmxmueq")
testDecryptB: Bool
testDecryptB = (decrypt 9 "HSAhsh" == "YJRyjy")
allCaesar2Tests = [testEncodeUpperSmallOffsetA, testEncodeUpperBigOffsetF, testEncodeLowerSmallOffsetQ, testEncodeLowerBigOffsetG, testDecodeUpperSmallOffsetA, testDecodeUpperBigOffsetF, testDecodeLowerSmallOffsetQ, testDecodeLowerBigOffsetG, testShiftChar, testNormalize, testNormalizeB, testEncrypt, testEncryptB, testDecrypt, testDecryptB]

View File

@@ -50,13 +50,76 @@ arePythTriplesFilter: List (Int, Int, Int) -> List (Int, Int, Int)
arePythTriplesFilter tripleList = arePythTriplesFilter tripleList =
List.filter isTripleTuple tripleList List.filter isTripleTuple tripleList
arePythTriplesRec: List (Int, Int, Int) -> List (Int, Int, Int) arePythTriplesRec: List (Int, Int, Int) -> List (Int, Int, Int)
arePythTriplesRec tripleList = arePythTriplesRec tripleList =
case tripleList of case tripleList of
-- Base Case
[] -> [] ->
[] []
x :: xs -> x :: xs ->
-- Check if first element (x) is a triple tuple and call recursively for the rest of the list -> (x + y) :: arePythTriplesRec(rest of tuples list)
if isTripleTuple x then if isTripleTuple x then
List.singleton x ++ arePythTriplesRec xs List.singleton x ++ arePythTriplesRec xs
-- Else - means x was not a triple tuple - call recursively for the rest of the list as parameter.
else else
arePythTriplesRec xs arePythTriplesRec xs
testPythTriplesMap: Bool
testPythTriplesMap = (pythTriplesMap [(5,4),(2,1),(35,7)] == [(9,40,41),(3,4,5),(1176,490,1274)])
testPythTriplesMapB: Bool
testPythTriplesMapB = (pythTriplesMap [(7,8), (9,7)] == [(-15,112,113),(32,126,130)])
testPythTriplesRec: Bool
testPythTriplesRec = (pythTriplesRec [(5,4),(2,1),(35,7)] == [(9,40,41),(3,4,5),(1176,490,1274)])
testPythTriplesRecB: Bool
testPythTriplesRecB = (pythTriplesRec [(6,4),(2,1),(20,7)] == [(20,48,52),(3,4,5),(351,280,449)])
testIsTripleZeros: Bool
testIsTripleZeros = (isTriple 0 0 0 == False)
testIsTripleNotEqual: Bool
testIsTripleNotEqual = (isTriple 6 5 4 == False)
testIsTripleNotEqual2: Bool
testIsTripleNotEqual2 = (isTriple 4 6 5 == False)
testIsTripleEqual: Bool
testIsTripleEqual =(isTriple 3 4 5 == True)
testPythTriple: Bool
testPythTriple = (pythTriple (4, 5) == (-9, 40, 41))
testPythTripleB: Bool
testPythTripleB = (pythTriple (7, 3) == (40, 42, 58))
testisTripleTuple: Bool
testisTripleTuple = (isTripleTuple (3, 4, 5) == True)
testArePythTriplesFilter: Bool
testArePythTriplesFilter = (arePythTriplesFilter [(1,2,3), (9,40,41), (3,4,5), (100,2,500)] == [(9,40,41),(3,4,5)] )
testArePythTriplesFilterB: Bool
testArePythTriplesFilterB = (arePythTriplesFilter [(1,2,3), (9,40,41), (3,4,5), (360,2,500)] == [(9,40,41),(3,4,5)] )
testArePythTriplesRec: Bool
testArePythTriplesRec = (arePythTriplesRec [(1,2,3), (9,40,41), (3,4,5), (100,2,500)] == [(9,40,41),(3,4,5)] )
testArePythTriplesRecB: Bool
testArePythTriplesRecB = (arePythTriplesRec [(1,2,3), (9,40,41), (3,4,5), (360,2,500)] == [(9,40,41),(3,4,5)] )
allTests = [testPythTriplesMap, testPythTriplesMapB, testPythTriplesRec, testPythTriplesRecB, testIsTripleZeros, testIsTripleNotEqual, testIsTripleNotEqual2, testIsTripleEqual, testPythTriple, testPythTripleB, testisTripleTuple, testArePythTriplesFilter, testArePythTriplesFilterB, testArePythTriplesRec, testArePythTriplesRecB]