based/test/Main.hs

108 lines
5.1 KiB
Haskell
Raw Normal View History

2024-05-19 15:22:13 +02:00
module Main where
import Test.HUnit
import Encoding.Base64 (enc64, dec64)
2024-05-20 14:06:00 +02:00
import Encoding.Base32 (enc32, dec32)
import Encoding.Base16 (enc16, dec16)
import Encoding.Base8 (enc8, dec8)
import Encoding.Base2 (enc2, dec2)
2024-05-19 15:22:13 +02:00
import System.Exit (exitFailure, exitSuccess)
-- Test cases for enc64 function
testEnc64 :: Test
testEnc64 = TestCase $ do
assertEqual "for (enc64 \"hello\")," "aGVsbG8=" (enc64 "hello")
assertEqual "for (enc64 \"Haskell\")," "SGFza2VsbA==" (enc64 "Haskell")
assertEqual "for (enc64 \"\x00\x01\x02\")," "AAEC" (enc64 "\x00\x01\x02")
assertEqual "for (enc64 \"😂\")," "8J+Ygg==" (enc64 "😂")
-- Test cases for dec64 function
testDec64 :: Test
testDec64 = TestCase $ do
assertEqual "for (dec64 \"aGVsbG8=\")," "hello" (dec64 "aGVsbG8=")
assertEqual "for (dec64 \"SGFza2VsbA==\")," "Haskell" (dec64 "SGFza2VsbA==")
assertEqual "for (dec64 \"AAEC\")," "\x00\x01\x02" (dec64 "AAEC")
2024-05-20 14:06:00 +02:00
assertEqual "for (dec64 \"8J+Ygg==\")," "😂" (dec64 "8J+Ygg==")
testDec32 :: Test
testDec32 = TestCase $ do
assertEqual "for (dec32 \"JBSWY3DPFQQFO33SNRSCC===\")," "Hello, World!" (dec32 "JBSWY3DPFQQFO33SNRSCC===")
assertEqual "for (dec32 \"JBQXG23FNRWA====\")," "Haskell" (dec32 "JBQXG23FNRWA====")
-- assertEqual "for (dec32 \"AAEC\")," "\x00\x01\x02" (dec32 "AAEC")
assertEqual "for (dec32 \"6CPZRAQ=\")," "😂" (dec32 "6CPZRAQ=")
testEnc32 :: Test
testEnc32 = TestCase $ do
assertEqual "for (enc32 \"Hello, World!\")," "JBSWY3DPFQQFO33SNRSCC===" (enc32 "Hello, World!")
assertEqual "for (enc32 \"Haskell\")," "JBQXG23FNRWA====" (enc32 "Haskell")
assertEqual "for (enc32 \"😂\")," "6CPZRAQ=" (enc32 "😂")
testEnc16 :: Test
testEnc16 = TestCase $ do
assertEqual "for (enc16 \"Hello, World!\")," "48656C6C6F2C20576F726C6421" (enc16 "Hello, World!")
assertEqual "for (enc16 \"Haskell\")," "4861736B656C6C" (enc16 "Haskell")
assertEqual "for (enc16 \"😂\")," "F09F9882" (enc16 "😂")
testDec16 :: Test
testDec16 = TestCase $ do
assertEqual "for (dec16 \"48656C6C6F2C20576F726C6421\")," "Hello, World!" (dec16 "48656C6C6F2C20576F726C6421")
assertEqual "for (dec16 \"4861736B656C6C\")," "Haskell" (dec16 "4861736B656C6C")
assertEqual "for (dec16 \"F09F9882\")," "😂" (dec16 "F09F9882")
2024-05-19 15:22:13 +02:00
testEnc8 :: Test
testEnc8 = TestCase $ do
assertEqual "for (enc8 \"Hello, World!\")," "110 145 154 154 157 54 40 127 157 162 154 144 41" (enc8 "Hello, World!")
assertEqual "for (enc8 \"Haskell\")," "110 141 163 153 145 154 154" (enc8 "Haskell")
assertEqual "for (enc8 \"😂\")," "360 237 230 202" (enc8 "😂")
testDec8 :: Test
testDec8 = TestCase $ do
assertEqual "for (dec8 \"110 145 154 154 157 54 40 127 157 162 154 144 41\")," "Hello, World!" (dec8 "110 145 154 154 157 54 40 127 157 162 154 144 41")
assertEqual "for (dec8 \"110 141 163 153 145 154 154\")," "Haskell" (dec8 "110 141 163 153 145 154 154")
assertEqual "for (dec8 \"360 237 230 202\")," "😂" (dec8 "360 237 230 202")
testEnc2 :: Test
testEnc2 = TestCase $ do
assertEqual "for (enc2 \"Hello, World!\")," "01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001" (enc2 "Hello, World!")
assertEqual "for (enc2 \"Haskell\")," "01001000 01100001 01110011 01101011 01100101 01101100 01101100" (enc2 "Haskell")
assertEqual "for (enc2 \"😂\")," "11110000 10011111 10011000 10000010" (enc2 "😂")
testDec2 :: Test
testDec2 = TestCase $ do
assertEqual "for (dec2 \"01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001\")," "Hello, World!" (dec2 "01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001")
assertEqual "for (dec2 \"01001000 01100001 01110011 01101011 01100101 01101100 01101100\")," "Haskell" (dec2 "01001000 01100001 01110011 01101011 01100101 01101100 01101100")
assertEqual "for (dec2 \"11110000 10011111 10011000 10000010\")," "😂" (dec2 "11110000 10011111 10011000 10000010")
2024-05-19 15:22:13 +02:00
tests :: Test
tests = TestList [TestLabel "Test enc64" testEnc64,
2024-05-20 14:06:00 +02:00
TestLabel "Test dec64" testDec64,
TestLabel "Test enc32" testEnc32,
TestLabel "Test dec32" testDec32,
TestLabel "Test enc16" testEnc16,
TestLabel "Test dec16" testDec16,
TestLabel "Test enc8 " testEnc8 ,
TestLabel "Test dec8 " testDec8 ,
TestLabel "Test dec2 " testDec2 ,
TestLabel "Test enc2 " testEnc2 ]
2024-05-20 14:06:00 +02:00
2024-05-19 15:22:13 +02:00
-- main :: IO Counts
-- main = runTestTT tests >>= \counts -> print counts >> runTestTTAndExit tests
-- runAndPrint :: Test -> IO Counts
-- runAndPrint t = do
-- counts <- runTestTT t
-- let label = case t of
-- TestLabel l _ -> l
-- _ -> "Unnamed test."
-- putStrLn $ label ++ ": " ++ if errors counts + failures counts == 0 then "[OK]" else "[FAIL]"
-- return counts
main :: IO ()
main = do
counts <- runTestTT tests
print counts
if errors counts + failures counts == 0
then exitSuccess
else exitFailure