<commit_before>{- | Implements several tests to control the validy of the program
-}
module Test.Grammar where
import PGF
import PGF.Internal
import Muste.Grammar.Internal
import Test.HUnit.Text
import Test.HUnit.Base
import Data.Maybe
import Data.Map
import Control.Monad
-- HUnit tests
hunit_Eq_Grammar_eq_test =
  let
    grammar1 = Grammar (mkCId "S")
      [
        (Function (mkCId "a") (Fun (mkCId "A") [])),
        (Function (mkCId "b") (Fun (mkCId "B") [])),
        (Function (mkCId "c") (Fun (mkCId "C") [])),
        (Function (mkCId "f") (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])),
        (Function (mkCId "g") (Fun (mkCId "B") [(mkCId "B"),(mkCId "C")])),
        (Function (mkCId "s") (Fun (mkCId "S") [(mkCId "A")]))
      ]
      emptyPGF
    grammar2 = Grammar (mkCId "A") [] emptyPGF
    pgf = readPGF "gf/ABCAbs.pgf"
    grammar3 = pgf >>= (return . pgfToGrammar)
  in
    TestList [
    TestLabel "Empty grammar" $ ( emptyGrammar == emptyGrammar ~?= True ),
    TestLabel "Simple Grammar reflexivity" $ ( grammar1 == grammar1 ~?= True ),
    TestLabel "Inequality 1" $ ( grammar1 == grammar2 ~?= False ),
    TestLabel "Inequality 2" $ ( grammar2 == grammar1 ~?= False ),
    TestLabel "Complex grammar" $ TestCase $ join $ liftM (\g -> grammar1 == g @?= True) grammar3
    ]
    
hunit_Show_FunType_show_test =
  let
    type1 = NoType
    type2 = (Fun (mkCId "A") [])
    type3 = (Fun (mkCId "A") [(mkCId "A")])
    type4 = (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])
  in
    TestList [
    TestLabel "No Type" $ ( show type1 ~?= "()" ),
    TestLabel "Simple Type" $ ( show type2 ~?= "(A)" ),
    TestLabel "Function Type" $ ( show type3 ~?= "(A -> A)" ),
    TestLabel "Function Type" $ ( show type4 ~?= "(A -> B -> A)" )
    ]

hunit_Show_Rule_show_test =
  let
    fun1 = Function (mkCId "f1") NoType
    fun2 = Function (mkCId "f2") (Fun (mkCId "A") [])
    fun3 = Function (mkCId "f3") (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])
  in
    TestList [
    TestLabel "No Type" $ ( show fun1 ~?= "f1 : ()" ),
    TestLabel "Simple Type" $ ( show fun2 ~?= "f2 : (A)" ),
    TestLabel "Function Type" $ ( show fun3 ~?= "f3 : (A -> B -> A)" )
    ]

hunit_Show_Grammar_show_test =
  let
    grammar1 = Grammar (mkCId "S") [] emptyPGF
    grammar2 = Grammar (mkCId "A")
      [
        (Function (mkCId "f1") (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])),
        (Function (mkCId "f2") (Fun (mkCId "B") [(mkCId "B"),(mkCId "B")]))
      ]
      emptyPGF
  in
    TestList [
    TestLabel "Empty Grammar" $ ( show grammar1 ~?= "Startcat: S\nRules: \n" ),
    TestLabel "Simple Grammar" $ ( show grammar2 ~?= "Startcat: A\nRules: \n\tf1 : (A -> B -> A)\n \tf2 : (B -> B -> B)\n" )
    ]

hunit_Read_FunType_readsPrec_test =
  let
    str1 = "A"
    str2 = "(A)"
    type1 = (Fun (mkCId "A") [])
    str3 = "A->B"
    str4 = "A -> B"
    str5 = "(A->B)"
    str6 = "(A -> B)"
    str7 = "( A -> B )"
    type2 = (Fun (mkCId "B") [(mkCId "A")])
  in
    TestList [
    TestLabel "Simple Type 1" $ ( readsPrec 0 str1 ~?= [(type1, "")] ),
    TestLabel "Simple Type 2" $ ( readsPrec 0 str2 ~?= [(type1, "")] ),
    TestLabel "Simple Type with rest 1" $ ( readsPrec 0 ( str2 ++ "###" ) ~?= [(type1, "###")] ),
    TestLabel "Simple Type with rest 2" $ ( readsPrec 0 ( str2 ++ "   ###" ) ~?= [(type1, "   ###")] ),
    TestLabel "Function Type 1" $ ( readsPrec 0 str3 ~?= [(type2, "")] ),
    TestLabel "Function Type 2" $ ( readsPrec 0 str4 ~?= [(type2, "")] ),
    TestLabel "Function Type 3" $ ( readsPrec 0 str5 ~?= [(type2, "")] ),
    TestLabel "Function Type 4" $ ( readsPrec 0 str6 ~?= [(type2, "")] ),
    TestLabel "Function Type 5" $ ( readsPrec 0 str7 ~?= [(type2, "")] ),
    TestLabel "Function Type with rest 1" $ ( readsPrec 0 ( str6 ++ "###" ) ~?= [(type2, "###")] ),
    TestLabel "Function Type with rest 2" $ ( readsPrec 0 ( str6 ++ "   ###" ) ~?= [(type2, "   ###")] )
    ]

hunit_isEmptyPGF_test =
  let
    pgf = PGF empty (mkCId "Abs") (Abstr empty empty empty) empty
    pgf2 = readPGF "gf/ABCAbs.pgf"
  in
  TestList [
  TestLabel "Empty PGF" $ isEmptyPGF emptyPGF ~?= True,
  TestLabel "Almost empty PGF with a name" $ isEmptyPGF pgf ~?= False,
  TestLabel "Non-empty PGF" $ TestCase $ pgf2 >>= (\g -> isEmptyPGF g @?= False)
  ]

hunit_isEmptyGrammar_test =
  let
    grammar1 = Grammar (mkCId "S") [(Function (mkCId "f") (Fun (mkCId "S") [(mkCId "A")])),(Function (mkCId "a") (Fun (mkCId "A") []))] emptyPGF
    pgf = readPGF "gf/ABCAbs.pgf"
    grammar2 = pgf >>= (\p -> return $ Grammar wildCId [] p)
  in
    TestList [
    TestLabel "Empty Grammar" $ isEmptyGrammar emptyGrammar ~?= True,
    TestLabel "Almost empty Grammar with a name" $ isEmptyGrammar grammar1 ~?= False,
    TestLabel "Grammar without a name" $ TestCase $ grammar2 >>= (\g -> isEmptyGrammar g @?= False),
    TestLabel "Complete grammar from PGF" $ TestCase $ pgf >>= (\g -> isEmptyGrammar (pgfToGrammar g) @?= False)
    ]
  <commit_msg>added missing test cases for Read FunType<commit_after>{- | Implements several tests to control the validy of the program
-}
module Test.Grammar where
import PGF
import PGF.Internal
import Muste.Grammar.Internal
import Test.HUnit.Text
import Test.HUnit.Base
import Data.Maybe
import Data.Map
import Control.Monad
-- HUnit tests
hunit_Eq_Grammar_eq_test =
  let
    grammar1 = Grammar (mkCId "S")
      [
        (Function (mkCId "a") (Fun (mkCId "A") [])),
        (Function (mkCId "b") (Fun (mkCId "B") [])),
        (Function (mkCId "c") (Fun (mkCId "C") [])),
        (Function (mkCId "f") (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])),
        (Function (mkCId "g") (Fun (mkCId "B") [(mkCId "B"),(mkCId "C")])),
        (Function (mkCId "s") (Fun (mkCId "S") [(mkCId "A")]))
      ]
      emptyPGF
    grammar2 = Grammar (mkCId "A") [] emptyPGF
    pgf = readPGF "gf/ABCAbs.pgf"
    grammar3 = pgf >>= (return . pgfToGrammar)
  in
    TestList [
    TestLabel "Empty grammar" $ ( emptyGrammar == emptyGrammar ~?= True ),
    TestLabel "Simple Grammar reflexivity" $ ( grammar1 == grammar1 ~?= True ),
    TestLabel "Inequality 1" $ ( grammar1 == grammar2 ~?= False ),
    TestLabel "Inequality 2" $ ( grammar2 == grammar1 ~?= False ),
    TestLabel "Complex grammar" $ TestCase $ join $ liftM (\g -> grammar1 == g @?= True) grammar3
    ]
    
hunit_Show_FunType_show_test =
  let
    type1 = NoType
    type2 = (Fun (mkCId "A") [])
    type3 = (Fun (mkCId "A") [(mkCId "A")])
    type4 = (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])
  in
    TestList [
    TestLabel "No Type" $ ( show type1 ~?= "()" ),
    TestLabel "Simple Type" $ ( show type2 ~?= "(A)" ),
    TestLabel "Function Type" $ ( show type3 ~?= "(A -> A)" ),
    TestLabel "Function Type" $ ( show type4 ~?= "(A -> B -> A)" )
    ]

hunit_Show_Rule_show_test =
  let
    fun1 = Function (mkCId "f1") NoType
    fun2 = Function (mkCId "f2") (Fun (mkCId "A") [])
    fun3 = Function (mkCId "f3") (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])
  in
    TestList [
    TestLabel "No Type" $ ( show fun1 ~?= "f1 : ()" ),
    TestLabel "Simple Type" $ ( show fun2 ~?= "f2 : (A)" ),
    TestLabel "Function Type" $ ( show fun3 ~?= "f3 : (A -> B -> A)" )
    ]

hunit_Show_Grammar_show_test =
  let
    grammar1 = Grammar (mkCId "S") [] emptyPGF
    grammar2 = Grammar (mkCId "A")
      [
        (Function (mkCId "f1") (Fun (mkCId "A") [(mkCId "A"),(mkCId "B")])),
        (Function (mkCId "f2") (Fun (mkCId "B") [(mkCId "B"),(mkCId "B")]))
      ]
      emptyPGF
  in
    TestList [
    TestLabel "Empty Grammar" $ ( show grammar1 ~?= "Startcat: S\nRules: \n" ),
    TestLabel "Simple Grammar" $ ( show grammar2 ~?= "Startcat: A\nRules: \n\tf1 : (A -> B -> A)\n \tf2 : (B -> B -> B)\n" )
    ]

hunit_Read_FunType_readsPrec_test =
  let
    str1 = "A"
    str2 = "(A)"
    type1 = (Fun (mkCId "A") [])
    str3 = "A->B"
    str4 = "A -> B"
    str5 = "(A->B)"
    str6 = "(A -> B)"
    str7 = "( A -> B )"
    type2 = (Fun (mkCId "B") [(mkCId "A")])
    str8 = ""
    str9 = "_"
    str10 = "###"
    str11 = "->"
    str12 = "-> A"
    str13 = "A ->"
  in
    TestList [
    TestLabel "Simple Type 1" $ ( readsPrec 0 str1 ~?= [(type1, "")] ),
    TestLabel "Simple Type 2" $ ( readsPrec 0 str2 ~?= [(type1, "")] ),
    TestLabel "Simple Type with rest 1" $ ( readsPrec 0 ( str2 ++ "###" ) ~?= [(type1, "###")] ),
    TestLabel "Simple Type with rest 2" $ ( readsPrec 0 ( str2 ++ "   ###" ) ~?= [(type1, "   ###")] ),
    TestLabel "Function Type 1" $ ( readsPrec 0 str3 ~?= [(type2, "")] ),
    TestLabel "Function Type 2" $ ( readsPrec 0 str4 ~?= [(type2, "")] ),
    TestLabel "Function Type 3" $ ( readsPrec 0 str5 ~?= [(type2, "")] ),
    TestLabel "Function Type 4" $ ( readsPrec 0 str6 ~?= [(type2, "")] ),
    TestLabel "Function Type 5" $ ( readsPrec 0 str7 ~?= [(type2, "")] ),
    TestLabel "Function Type with rest 1" $ ( readsPrec 0 ( str6 ++ "###" ) ~?= [(type2, "###")] ),
    TestLabel "Function Type with rest 2" $ ( readsPrec 0 ( str6 ++ "   ###" ) ~?= [(type2, "   ###")] ),
    TestLabel "Empty String" $ ( ( readsPrec 0 ( str8 ) :: [(FunType,String)] ) ~?= [(NoType,"")] ),
    TestLabel "Wildcard" $ ( ( readsPrec 0 ( str9 ) :: [(FunType,String)] ) ~?= [(NoType,"_")] ),
    TestLabel "Three Hashes" $ ( ( readsPrec 0 ( str10 ) :: [(FunType,String)] ) ~?= [(NoType,"###")] ),
    -- Nor really intended, but does it any harm?
    TestLabel "Arrow" $ ( ( readsPrec 0 ( str11 ) :: [(FunType,String)] ) ~?= [(NoType,"")] ),
    TestLabel "Arrow A" $ ( ( readsPrec 0 ( str12 ) :: [(FunType,String)] ) ~?= [(type1,"")] ),
    TestLabel "A Arrow" $ ( ( readsPrec 0 ( str13 ) :: [(FunType,String)] ) ~?= [(type1,"")] )
    ]

hunit_isEmptyPGF_test =
  let
    pgf = PGF empty (mkCId "Abs") (Abstr empty empty empty) empty
    pgf2 = readPGF "gf/ABCAbs.pgf"
  in
  TestList [
  TestLabel "Empty PGF" $ isEmptyPGF emptyPGF ~?= True,
  TestLabel "Almost empty PGF with a name" $ isEmptyPGF pgf ~?= False,
  TestLabel "Non-empty PGF" $ TestCase $ pgf2 >>= (\g -> isEmptyPGF g @?= False)
  ]

hunit_isEmptyGrammar_test =
  let
    grammar1 = Grammar (mkCId "S") [(Function (mkCId "f") (Fun (mkCId "S") [(mkCId "A")])),(Function (mkCId "a") (Fun (mkCId "A") []))] emptyPGF
    pgf = readPGF "gf/ABCAbs.pgf"
    grammar2 = pgf >>= (\p -> return $ Grammar wildCId [] p)
  in
    TestList [
    TestLabel "Empty Grammar" $ isEmptyGrammar emptyGrammar ~?= True,
    TestLabel "Almost empty Grammar with a name" $ isEmptyGrammar grammar1 ~?= False,
    TestLabel "Grammar without a name" $ TestCase $ grammar2 >>= (\g -> isEmptyGrammar g @?= False),
    TestLabel "Complete grammar from PGF" $ TestCase $ pgf >>= (\g -> isEmptyGrammar (pgfToGrammar g) @?= False)
    ]
  
{'fraction_non_alphanumeric': 0.19766245909303412, 'fraction_numerical': 0.021318373071528753, 'mean_word_length': 2.9395948434622468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 64, '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': '5473823', 'n_tokens_mistral': 4384, 'n_tokens_neox': 4193, 'n_words': 1118}