<commit_before>module UnitTests.Distribution.Client.GZipUtils (
  tests
  ) where

import Codec.Compression.GZip          as GZip
import Codec.Compression.Zlib          as Zlib
import Control.Exception.Base                  (evaluate)
import Control.Exception                       (try, SomeException)
import Control.Monad                           (void)
import Data.ByteString                as BS    (null)
import Data.ByteString.Lazy           as BSL   (pack, toChunks)
import Data.ByteString.Lazy.Char8     as BSLL  (pack, init, length)
import Data.Monoid                             ((<>))
import Distribution.Client.GZipUtils           (maybeDecompress)
import Data.Word                               (Word8)

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck

tests :: [TestTree]
tests = [ testCase "maybeDecompress" maybeDecompressUnitTest
        , testProperty "decompress plain" prop_maybeDecompress_plain
        , testProperty "decompress zlib"  prop_maybeDecompress_zlib
        , testProperty "decompress gzip"  prop_maybeDecompress_gzip
        ]

maybeDecompressUnitTest :: Assertion
maybeDecompressUnitTest =
        assertBool "decompress plain"            (maybeDecompress original              == original)
     >> assertBool "decompress zlib (with show)" (show (maybeDecompress compressedZlib) == show original)
     >> assertBool "decompress gzip (with show)" (show (maybeDecompress compressedGZip) == show original)
     >> assertBool "decompress zlib"             (maybeDecompress compressedZlib        == original)
     >> assertBool "decompress gzip"             (maybeDecompress compressedGZip        == original)
     >> assertBool "have no empty chunks"        (Prelude.all (not . BS.null) . BSL.toChunks . maybeDecompress $ compressedZlib)
     >> (runBrokenStream >>= assertBool "decompress broken stream" . isLeft)
  where
    original = BSLL.pack "original uncompressed input"
    compressedZlib = Zlib.compress original
    compressedGZip = GZip.compress original

    runBrokenStream :: IO (Either SomeException ())
    runBrokenStream = try . void . evaluate . BSLL.length $ maybeDecompress (BSLL.init compressedZlib <> BSLL.pack "*")

prop_maybeDecompress_plain :: [Word8] -> Property
prop_maybeDecompress_plain ws = property $ maybeDecompress original == original
  where original = BSL.pack ws

prop_maybeDecompress_zlib :: [Word8] -> Property
prop_maybeDecompress_zlib ws = property $ maybeDecompress compressedZlib == original
  where original = BSL.pack ws
        compressedZlib = Zlib.compress original

prop_maybeDecompress_gzip :: [Word8] -> Property
prop_maybeDecompress_gzip ws = property $ maybeDecompress compressedGZip == original
  where original = BSL.pack ws
        compressedGZip = GZip.compress original

-- (Only available from "Data.Either" since 7.8.)
isLeft :: Either a b -> Bool
isLeft (Right _) = False
isLeft (Left _) = True
<commit_msg>Fix #2794: Generate ascii data for plain maybeDecompress property<commit_after>module UnitTests.Distribution.Client.GZipUtils (
  tests
  ) where

import Codec.Compression.GZip          as GZip
import Codec.Compression.Zlib          as Zlib
import Control.Exception.Base                  (evaluate)
import Control.Exception                       (try, SomeException)
import Control.Monad                           (void)
import Data.ByteString                as BS    (null)
import Data.ByteString.Lazy           as BSL   (pack, toChunks)
import Data.ByteString.Lazy.Char8     as BSLL  (pack, init, length)
import Data.Monoid                             ((<>))
import Distribution.Client.GZipUtils           (maybeDecompress)
import Data.Word                               (Word8)

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck

tests :: [TestTree]
tests = [ testCase "maybeDecompress" maybeDecompressUnitTest
        , testProperty "decompress plain" $ forAll (listOf asciiWord8) prop_maybeDecompress_plain
        , testProperty "decompress zlib"  prop_maybeDecompress_zlib
        , testProperty "decompress gzip"  prop_maybeDecompress_gzip
        ]
  where asciiWord8 = elements [32..126]

maybeDecompressUnitTest :: Assertion
maybeDecompressUnitTest =
        assertBool "decompress plain"            (maybeDecompress original              == original)
     >> assertBool "decompress zlib (with show)" (show (maybeDecompress compressedZlib) == show original)
     >> assertBool "decompress gzip (with show)" (show (maybeDecompress compressedGZip) == show original)
     >> assertBool "decompress zlib"             (maybeDecompress compressedZlib        == original)
     >> assertBool "decompress gzip"             (maybeDecompress compressedGZip        == original)
     >> assertBool "have no empty chunks"        (Prelude.all (not . BS.null) . BSL.toChunks . maybeDecompress $ compressedZlib)
     >> (runBrokenStream >>= assertBool "decompress broken stream" . isLeft)
  where
    original = BSLL.pack "original uncompressed input"
    compressedZlib = Zlib.compress original
    compressedGZip = GZip.compress original

    runBrokenStream :: IO (Either SomeException ())
    runBrokenStream = try . void . evaluate . BSLL.length $ maybeDecompress (BSLL.init compressedZlib <> BSLL.pack "*")

prop_maybeDecompress_plain :: [Word8] -> Property
prop_maybeDecompress_plain ws = property $ maybeDecompress original === original
  where original = BSL.pack ws

prop_maybeDecompress_zlib :: [Word8] -> Property
prop_maybeDecompress_zlib ws = property $ maybeDecompress compressedZlib === original
  where original = BSL.pack ws
        compressedZlib = Zlib.compress original

prop_maybeDecompress_gzip :: [Word8] -> Property
prop_maybeDecompress_gzip ws = property $ maybeDecompress compressedGZip === original
  where original = BSL.pack ws
        compressedGZip = GZip.compress original

-- (Only available from "Data.Either" since 7.8.)
isLeft :: Either a b -> Bool
isLeft (Right _) = False
isLeft (Left _) = True

{'fraction_non_alphanumeric': 0.08151991965182458, 'fraction_numerical': 0.004184800803481754, 'mean_word_length': 3.0591032608695654, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4263021', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1629, 'n_words': 449}