added test for more functions, solved bugs in accordance to that functions, mainly Unicode support
This commit is contained in:
parent
b27ca89424
commit
bdbb3bed3e
43
app/Main.hs
43
app/Main.hs
|
@ -5,52 +5,9 @@
|
||||||
module Main where
|
module Main where
|
||||||
import System.Console.CmdArgs
|
import System.Console.CmdArgs
|
||||||
import Control.Arrow
|
import Control.Arrow
|
||||||
-- import Data.Text (pack, unpack, Text)
|
|
||||||
-- import Data.Bits
|
|
||||||
-- import Data.Char
|
|
||||||
-- import Text.Read (readMaybe)
|
|
||||||
-- import qualified Data.Text as T
|
|
||||||
-- import qualified Data.Text.Encoding as T
|
|
||||||
-- import qualified Data.Text.IO as T
|
|
||||||
-- import Data.Text (Text)
|
|
||||||
-- import qualified Data.Text.Lazy as TL
|
|
||||||
-- import Data.Text.Lazy.Builder (toLazyText)
|
|
||||||
-- import Data.Text.Encoding (decodeUtf8With, decodeUtf8, encodeUtf8)
|
|
||||||
-- import Data.Maybe (fromJust, fromMaybe)
|
|
||||||
-- import Data.Text.Encoding.Error (lenientDecode)
|
|
||||||
-- import TextShow (toText)
|
|
||||||
-- import Text.XML.HXT.DOM.Util (hexStringToInt, intToHexString, decimalStringToInt)
|
|
||||||
-- import TextShow.Data.Integral (showbBin, showbOct)
|
|
||||||
-- import Text.Ascii (fromBinDigit, fromOctDigit)
|
|
||||||
-- import Codec.CBOR.Magic (intToWord64)
|
|
||||||
-- import qualified Data.Either.Unwrap as U
|
|
||||||
-- import Data.Bytes.Text.Latin1 as Latin1
|
|
||||||
-- import qualified Codec.Binary.Base91 as B91
|
|
||||||
-- import qualified Codec.Binary.Base85 as B85
|
|
||||||
-- import qualified Codec.Binary.Base64 as B64
|
|
||||||
-- import qualified Data.ByteString.Base64 as B64L
|
|
||||||
-- import qualified Codec.Binary.Base64Url as B64U
|
|
||||||
-- import qualified Network.HTTP.Base as HB
|
|
||||||
-- import qualified Data.Word.Base62 as B62
|
|
||||||
-- import qualified Haskoin.Address.Base58 as B58
|
|
||||||
-- import qualified Codec.Binary.Base32 as B32
|
|
||||||
-- import qualified Codec.Binary.Base16 as B16
|
|
||||||
-- import qualified Codec.Binary.QuotedPrintable as QP
|
|
||||||
-- import qualified Codec.Binary.Uu as UU
|
|
||||||
-- import qualified Codec.Binary.Xx as XX
|
|
||||||
-- import qualified Codec.Binary.Yenc as Y
|
|
||||||
-- import qualified Data.Bytes as Bytes
|
|
||||||
-- import qualified Data.Bytes.Text.Ascii as ASCII
|
|
||||||
-- import Data.Bytes.Get (getWord64host)
|
|
||||||
-- import Data.ByteString (singleton)
|
|
||||||
-- import GHC.Word (Word8)
|
|
||||||
-- import Data.Word (Word8)
|
|
||||||
-- import Data.Char (ord, chr, intToDigit, digitToInt)
|
|
||||||
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
||||||
import qualified Data.ByteString.Char8 as C
|
import qualified Data.ByteString.Char8 as C
|
||||||
-- Regex imports
|
|
||||||
import Text.Regex.TDFA
|
import Text.Regex.TDFA
|
||||||
|
|
||||||
import Encoding.Base2 (enc2, dec2)
|
import Encoding.Base2 (enc2, dec2)
|
||||||
import Encoding.Base8 (enc8, dec8)
|
import Encoding.Base8 (enc8, dec8)
|
||||||
import Encoding.Base10 (enc10, dec10)
|
import Encoding.Base10 (enc10, dec10)
|
||||||
|
|
|
@ -6,11 +6,14 @@ module Encoding.Base16
|
||||||
import qualified Data.ByteString.Char8 as C
|
import qualified Data.ByteString.Char8 as C
|
||||||
import Data.ByteString.UTF8 as BSU
|
import Data.ByteString.UTF8 as BSU
|
||||||
import qualified Codec.Binary.Base16 as B16
|
import qualified Codec.Binary.Base16 as B16
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Text.Encoding as T
|
||||||
|
import qualified Data.Text.IO as T
|
||||||
|
|
||||||
dec16 :: String -> String
|
dec16 :: String -> String
|
||||||
dec16 input =
|
dec16 input =
|
||||||
case B16.decode (BSU.fromString input) of
|
case B16.decode (BSU.fromString input) of
|
||||||
Right decoded -> C.unpack decoded
|
Right decoded -> T.unpack (T.decodeUtf8 decoded)
|
||||||
Left _ -> "Error decoding Base16.\n"
|
Left _ -> "Error decoding Base16.\n"
|
||||||
|
|
||||||
enc16 :: String -> String
|
enc16 :: String -> String
|
||||||
|
|
|
@ -7,11 +7,14 @@ module Encoding.Base32
|
||||||
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
||||||
import qualified Data.ByteString.Char8 as C
|
import qualified Data.ByteString.Char8 as C
|
||||||
import qualified Codec.Binary.Base32 as B32
|
import qualified Codec.Binary.Base32 as B32
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Text.Encoding as T
|
||||||
|
import qualified Data.Text.IO as T
|
||||||
|
|
||||||
dec32 :: String -> String
|
dec32 :: String -> String
|
||||||
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 -> T.unpack (T.decodeUtf8 decoded)
|
||||||
Left _ -> "Error decoding Base32.\n"
|
Left _ -> "Error decoding Base32.\n"
|
||||||
|
|
||||||
enc32 :: String -> String
|
enc32 :: String -> String
|
||||||
|
|
|
@ -11,7 +11,6 @@ import qualified Data.Text.Encoding as T
|
||||||
import qualified Data.Text.IO as T
|
import qualified Data.Text.IO as T
|
||||||
|
|
||||||
dec58 :: String -> String
|
dec58 :: String -> String
|
||||||
-- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
|
|
||||||
dec58 input =
|
dec58 input =
|
||||||
maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input))
|
maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input))
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,16 @@ module Encoding.Base64
|
||||||
, dec64url
|
, dec64url
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
|
||||||
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
||||||
import qualified Data.ByteString.Char8 as C
|
import qualified Data.ByteString.Char8 as C
|
||||||
import qualified Codec.Binary.Base64 as B64
|
import qualified Codec.Binary.Base64 as B64
|
||||||
import qualified Data.ByteString.Base64 as B64L
|
import qualified Data.ByteString.Base64 as B64L
|
||||||
import qualified Codec.Binary.Base64Url as B64U
|
import qualified Codec.Binary.Base64Url as B64U
|
||||||
|
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Text.Encoding as T
|
||||||
|
import qualified Data.Text.IO as T
|
||||||
|
|
||||||
encodeToBase64 :: C.ByteString -> Either String C.ByteString
|
encodeToBase64 :: C.ByteString -> Either String C.ByteString
|
||||||
encodeToBase64 bs =
|
encodeToBase64 bs =
|
||||||
case B64.encode bs of
|
case B64.encode bs of
|
||||||
|
@ -30,7 +33,7 @@ decodeFromBase64 bs =
|
||||||
dec64 :: String -> String
|
dec64 :: String -> String
|
||||||
dec64 input =
|
dec64 input =
|
||||||
case decodeFromBase64 (BSU.fromString input) of
|
case decodeFromBase64 (BSU.fromString input) of
|
||||||
Right byteString -> C.unpack byteString
|
Right byteString -> T.unpack (T.decodeUtf8 byteString)
|
||||||
Left errMsg -> "Error: " ++ errMsg
|
Left errMsg -> "Error: " ++ errMsg
|
||||||
|
|
||||||
enc64 :: String -> String
|
enc64 :: String -> String
|
||||||
|
@ -42,7 +45,7 @@ enc64 input =
|
||||||
dec64url :: String -> String
|
dec64url :: String -> String
|
||||||
dec64url input =
|
dec64url input =
|
||||||
case B64.decode (BSU.fromString input) of
|
case B64.decode (BSU.fromString input) of
|
||||||
Right decoded -> C.unpack decoded
|
Right decoded -> T.unpack (T.decodeUtf8 decoded)
|
||||||
Left _ -> "Error decoding Base64 for URLs.\n"
|
Left _ -> "Error decoding Base64 for URLs.\n"
|
||||||
|
|
||||||
enc64url :: String -> String
|
enc64url :: String -> String
|
||||||
|
|
32
test/Main.hs
32
test/Main.hs
|
@ -4,6 +4,8 @@ import Test.HUnit
|
||||||
import Encoding.Base64 (enc64, dec64)
|
import Encoding.Base64 (enc64, dec64)
|
||||||
import Encoding.Base32 (enc32, dec32)
|
import Encoding.Base32 (enc32, dec32)
|
||||||
import Encoding.Base16 (enc16, dec16)
|
import Encoding.Base16 (enc16, dec16)
|
||||||
|
import Encoding.Base8 (enc8, dec8)
|
||||||
|
import Encoding.Base2 (enc2, dec2)
|
||||||
import System.Exit (exitFailure, exitSuccess)
|
import System.Exit (exitFailure, exitSuccess)
|
||||||
|
|
||||||
-- Test cases for enc64 function
|
-- Test cases for enc64 function
|
||||||
|
@ -47,13 +49,41 @@ testDec16 = TestCase $ do
|
||||||
assertEqual "for (dec16 \"4861736B656C6C\")," "Haskell" (dec16 "4861736B656C6C")
|
assertEqual "for (dec16 \"4861736B656C6C\")," "Haskell" (dec16 "4861736B656C6C")
|
||||||
assertEqual "for (dec16 \"F09F9882\")," "😂" (dec16 "F09F9882")
|
assertEqual "for (dec16 \"F09F9882\")," "😂" (dec16 "F09F9882")
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
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 enc32" testEnc32,
|
||||||
TestLabel "Test dec32" testDec32,
|
TestLabel "Test dec32" testDec32,
|
||||||
TestLabel "Test enc16" testEnc16,
|
TestLabel "Test enc16" testEnc16,
|
||||||
TestLabel "Test dec16" testDec16]
|
TestLabel "Test dec16" testDec16,
|
||||||
|
TestLabel "Test enc8 " testEnc8 ,
|
||||||
|
TestLabel "Test dec8 " testDec8 ,
|
||||||
|
TestLabel "Test dec2 " testDec2 ,
|
||||||
|
TestLabel "Test enc2 " testEnc2 ]
|
||||||
|
|
||||||
|
|
||||||
-- main :: IO Counts
|
-- main :: IO Counts
|
||||||
|
|
Loading…
Reference in New Issue