<commit_before>module Main where

import Codec.Compression.Zlib.Internal

import Test.Codec.Compression.Zlib.Internal
import Test.Codec.Compression.Zlib.Stream

import Control.Monad
import Data.Word
import Test.QuickCheck
import qualified Data.ByteString.Lazy as BL

import Test.Framework
import Test.Framework.Providers.QuickCheck2

instance Arbitrary Word8 where
  arbitrary = fmap fromInteger arbitrary
  shrink = map fromInteger . shrink . toInteger

maxStrSize = 10000

-- convert a QC size parameter into one for generating long lists,
-- growing inverse exponentially up to maxStrSize
strSize n = floor (maxStrSize * (1 - 2 ** (-fromIntegral n/100)))

instance Arbitrary BL.ByteString where
  arbitrary = sized $ \sz -> resize (strSize sz) $ fmap BL.pack $ listOf $ arbitrary
  shrink = map BL.pack . shrink . BL.unpack

prop_decompress_after_compress w cp dp =
   (w /= zlibFormat || decompressWindowBits dp >= compressWindowBits cp) &&
   -- Zlib decompression has been observed to fail with both compress and decompress
   -- window bits = 8. This seems to be contrary to the docs and to a quick reading
   -- of the zlib source code.
   (decompressWindowBits dp > compressWindowBits cp || decompressWindowBits dp > WindowBits 8) &&
   decompressBufferSize dp > 0 && compressBufferSize cp > 0 ==>
   liftM2 (==) (decompress w dp . compress w cp) id

prop_gziporzlib1 cp dp =
   decompressWindowBits dp > compressWindowBits cp &&
   decompressBufferSize dp > 0 && compressBufferSize cp > 0 ==>
   liftM2 (==) (decompress gzipOrZlibFormat dp . compress zlibFormat cp) id

prop_gziporzlib2 cp dp =
   decompressBufferSize dp > 0 && compressBufferSize cp > 0 ==>
   liftM2 (==) (decompress gzipOrZlibFormat dp . compress gzipFormat cp) id

main = defaultMain [
         testProperty "decompress . compress = id (standard)" prop_decompress_after_compress,
         testProperty "decompress . compress = id (Zlib -> GZipOrZLib)" prop_gziporzlib1,
         testProperty "decompress . compress = id (GZip -> GZipOrZlib)" prop_gziporzlib2
       ]<commit_msg>Rearrange code in the main test module<commit_after>module Main where

import Codec.Compression.Zlib.Internal

import Test.Codec.Compression.Zlib.Internal
import Test.Codec.Compression.Zlib.Stream

import Test.QuickCheck
import Test.Framework
import Test.Framework.Providers.QuickCheck2

import Control.Monad
import Data.Word
import qualified Data.ByteString.Lazy as BL


main :: IO ()
main = defaultMain [
    testProperty "decompress . compress = id (standard)"           prop_decompress_after_compress,
    testProperty "decompress . compress = id (Zlib -> GZipOrZLib)" prop_gziporzlib1,
    testProperty "decompress . compress = id (GZip -> GZipOrZlib)" prop_gziporzlib2
  ]


prop_decompress_after_compress :: Format
                               -> CompressParams
                               -> DecompressParams
                               -> Property
prop_decompress_after_compress w cp dp =
   (w /= zlibFormat || decompressWindowBits dp >= compressWindowBits cp) &&
   -- Zlib decompression has been observed to fail with both compress and decompress
   -- window bits = 8. This seems to be contrary to the docs and to a quick reading
   -- of the zlib source code.
   (decompressWindowBits dp > compressWindowBits cp || decompressWindowBits dp > WindowBits 8) &&
   decompressBufferSize dp > 0 && compressBufferSize cp > 0 ==>
   liftM2 (==) (decompress w dp . compress w cp) id


prop_gziporzlib1 :: CompressParams
                 -> DecompressParams
                 -> Property
prop_gziporzlib1 cp dp =
   decompressWindowBits dp > compressWindowBits cp &&
   decompressBufferSize dp > 0 && compressBufferSize cp > 0 ==>
   liftM2 (==) (decompress gzipOrZlibFormat dp . compress zlibFormat cp) id


prop_gziporzlib2 :: CompressParams
                 -> DecompressParams
                 -> Property
prop_gziporzlib2 cp dp =
   decompressBufferSize dp > 0 && compressBufferSize cp > 0 ==>
   liftM2 (==) (decompress gzipOrZlibFormat dp . compress gzipFormat cp) id


-------------------
-- QuickCheck Utils

instance Arbitrary Word8 where
  arbitrary = fmap fromInteger arbitrary
  shrink = map fromInteger . shrink . toInteger

maxStrSize :: Double
maxStrSize = 10000

-- convert a QC size parameter into one for generating long lists,
-- growing inverse exponentially up to maxStrSize
strSize :: Int -> Int
strSize n = floor (maxStrSize * (1 - 2 ** (-fromIntegral n/100)))

instance Arbitrary BL.ByteString where
  arbitrary = sized $ \sz -> resize (strSize sz) $ fmap BL.pack $ listOf $ arbitrary
  shrink = map BL.pack . shrink . BL.unpack
{'fraction_non_alphanumeric': 0.0823961937716263, 'fraction_numerical': 0.012110726643598616, 'mean_word_length': 3.3386491557223263, 'pattern_counts': {'":': 0, '<': 3, '<?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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4349506', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1323, 'n_words': 444}