added tests for b32 and b16

This commit is contained in:
Stefan Friese 2024-05-20 14:06:00 +02:00
parent d44cef3060
commit b27ca89424
2 changed files with 34 additions and 3 deletions

View File

@ -9,7 +9,6 @@ import qualified Data.ByteString.Char8 as C
import qualified Codec.Binary.Base32 as B32 import qualified Codec.Binary.Base32 as B32
dec32 :: String -> String dec32 :: String -> String
-- dec32 = C.unpack . U.fromRight . B32.decode . BSU.fromString
dec32 input = dec32 input =
case B32.decode (BSU.fromString input) of case B32.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded Right decoded -> C.unpack decoded

View File

@ -2,6 +2,8 @@ module Main where
import Test.HUnit import Test.HUnit
import Encoding.Base64 (enc64, dec64) import Encoding.Base64 (enc64, dec64)
import Encoding.Base32 (enc32, dec32)
import Encoding.Base16 (enc16, dec16)
import System.Exit (exitFailure, exitSuccess) import System.Exit (exitFailure, exitSuccess)
-- Test cases for enc64 function -- Test cases for enc64 function
@ -18,11 +20,41 @@ testDec64 = TestCase $ do
assertEqual "for (dec64 \"aGVsbG8=\")," "hello" (dec64 "aGVsbG8=") assertEqual "for (dec64 \"aGVsbG8=\")," "hello" (dec64 "aGVsbG8=")
assertEqual "for (dec64 \"SGFza2VsbA==\")," "Haskell" (dec64 "SGFza2VsbA==") assertEqual "for (dec64 \"SGFza2VsbA==\")," "Haskell" (dec64 "SGFza2VsbA==")
assertEqual "for (dec64 \"AAEC\")," "\x00\x01\x02" (dec64 "AAEC") assertEqual "for (dec64 \"AAEC\")," "\x00\x01\x02" (dec64 "AAEC")
assertEqual "for (dec64 \"8J+Ygg==\")," "😂" (dec64 "😂") 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")
tests :: Test tests :: Test
tests = TestList [TestLabel "Test enc64" testEnc64, tests = TestList [TestLabel "Test enc64" testEnc64,
TestLabel "Test dec64" testDec64] TestLabel "Test dec64" testDec64,
TestLabel "Test enc32" testEnc32,
TestLabel "Test dec32" testDec32,
TestLabel "Test enc16" testEnc16,
TestLabel "Test dec16" testDec16]
-- main :: IO Counts -- main :: IO Counts
-- main = runTestTT tests >>= \counts -> print counts >> runTestTTAndExit tests -- main = runTestTT tests >>= \counts -> print counts >> runTestTTAndExit tests