<commit_before>module Tests.SchemaQuickCheck
    (schemaCGRQuickCheck)
  where

import qualified Data.ByteString as BS

import Prelude hiding (length)
import Data.CapNProto.Untyped (length, Ptr(PtrStruct), rootPtr)
import Data.CapNProto.Blob (BlobSlice)
import Data.CapNProto.Message as M
import Schema.CapNProto.Reader.Schema as Schema hiding (Field)
import qualified Schema.CapNProto.Reader.Schema.CodeGeneratorRequest as CGReq

-- Testing framework imports
import Test.QuickCheck
import Test.Framework (Test)
import Test.Framework.Providers.QuickCheck2 (testProperty)

-- Schema generation imports
import Tests.Util
import Tests.SchemaGeneration

-- Schema validation imports
import Control.Monad.Catch as C
import Control.Monad.Quota as Q

-- Functions to generate valid CGRs

generateCGR :: Schema -> IO BS.ByteString
generateCGR schema = do
  pOut <- capnpCompile (MsgMetaData (show schema) "-o-")
  return pOut

-- Functions to validate CGRs

decodeCGR :: BS.ByteString -> IO (Quota, Int)
decodeCGR bytes = do
  msg <- M.decode bytes
  (numNodes, endQuota) <- runQuotaT (rootPtr msg >>= reader) 1024
  return (endQuota, numNodes)
  where
    reader :: Maybe (Ptr BS.ByteString) -> QuotaT IO Int
    reader (Just (PtrStruct root)) = do
        let req = Schema.CodeGeneratorRequest root
        Just nodes <- CGReq.nodes req
        Just requestedFiles <- CGReq.requestedFiles req
        numNodes <- length nodes
        return numNodes
    reader _ = error "Expected `Just (PtrStruct root)`"

-- QuickCheck properties

prop_schemaValid :: Schema -> Property
prop_schemaValid schema = ioProperty $ do
  compiled <- generateCGR schema
  decoded <- try $ decodeCGR compiled
  return $ case decoded of
    Left QuotaError -> False
    Right _ -> True

schemaCGRQuickCheck = testProperty "valid schema QuickCheck" prop_schemaValid <commit_msg>fix Untyped.Struct issues in quickcheck<commit_after>module Tests.SchemaQuickCheck
    (schemaCGRQuickCheck)
  where

import qualified Data.ByteString as BS

import qualified Data.CapNProto.Untyped as Untyped
import Data.CapNProto.Blob (BlobSlice)
import Data.CapNProto.Message as M
import Schema.CapNProto.Reader.Schema as Schema hiding (Field)
import qualified Schema.CapNProto.Reader.Schema.CodeGeneratorRequest as CGReq

-- Testing framework imports
import Test.QuickCheck
import Test.Framework (Test)
import Test.Framework.Providers.QuickCheck2 (testProperty)

-- Schema generation imports
import Tests.Util
import Tests.SchemaGeneration

-- Schema validation imports
import Control.Monad.Catch as C
import Control.Monad.Quota as Q

-- Functions to generate valid CGRs

generateCGR :: Schema -> IO BS.ByteString
generateCGR schema = do
  pOut <- capnpCompile (MsgMetaData (show schema) "-o-")
  return pOut

-- Functions to validate CGRs

decodeCGR :: BS.ByteString -> IO (Quota, Int)
decodeCGR bytes = do
  msg <- M.decode bytes
  (numNodes, endQuota) <- runQuotaT (Untyped.rootPtr msg >>= reader) 1024
  return (endQuota, numNodes)
  where
    reader :: Untyped.Struct BS.ByteString -> QuotaT IO Int
    reader struct = do
      let req = Schema.CodeGeneratorRequest struct
      Just nodes <- CGReq.nodes req
      Just requestedFiles <- CGReq.requestedFiles req
      return (Untyped.length nodes)

-- QuickCheck properties

prop_schemaValid :: Schema -> Property
prop_schemaValid schema = ioProperty $ do
  compiled <- generateCGR schema
  decoded <- try $ decodeCGR compiled
  return $ case decoded of
    Left QuotaError -> False
    Right _ -> True

schemaCGRQuickCheck = testProperty "valid schema QuickCheck" prop_schemaValid 
{'fraction_non_alphanumeric': 0.07778087538332869, 'fraction_numerical': 0.0027878449958182324, 'mean_word_length': 4.03932584269663, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, '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': '3391117', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1025, 'n_words': 343}