Encode.hs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. {-# LANGUAGE TemplateHaskell #-}
  2. module Internal.Compiler.MAlonzo.Encode ( tests ) where
  3. import Agda.Compiler.MAlonzo.Encode
  4. import Agda.Compiler.MAlonzo.Misc
  5. import Data.Char
  6. import Data.List ( intercalate, isPrefixOf )
  7. import Internal.Helpers
  8. import qualified Agda.Utils.Haskell.Syntax as HS
  9. ------------------------------------------------------------------------------
  10. -- Note: This injectivity test is quite weak. A better, dedicated
  11. -- generator could strengthen it.
  12. prop_encodeModuleName_injective :: M -> M -> Bool
  13. prop_encodeModuleName_injective (M s1) (M s2) =
  14. if encodeModuleName (HS.ModuleName s1) ==
  15. encodeModuleName (HS.ModuleName s2) then
  16. s1 == s2
  17. else
  18. True
  19. prop_encodeModuleName_OK :: M -> Bool
  20. prop_encodeModuleName_OK (M s') =
  21. s ~= unM (encodeModuleName (HS.ModuleName s))
  22. where
  23. s = mazstr ++ "." ++ s'
  24. "" ~= "" = True
  25. ('.' : s) ~= ('.' : s') = s ~= s'
  26. s ~= (c : s') = isUpper c && all isModChar s1' &&
  27. dropWhile (/= '.') s ~= s2'
  28. where (s1', s2') = span (/= '.') s'
  29. _ ~= _ = False
  30. unM (HS.ModuleName s) = s
  31. prop_encodeModuleName_preserved :: M -> Property
  32. prop_encodeModuleName_preserved (M m) =
  33. shouldBePreserved m ==>
  34. encodeModuleName (HS.ModuleName m) == HS.ModuleName m
  35. where
  36. shouldBePreserved m =
  37. not (m == mazstr || (mazstr ++ ".") `isPrefixOf` m)
  38. -- | Agda module names. Used to test 'encodeModuleName'.
  39. newtype M = M String deriving (Show)
  40. instance Arbitrary M where
  41. arbitrary = do
  42. ms <- choose (0, 2)
  43. m <- vectorOf ms namePart
  44. return $ M (intercalate "." m)
  45. where
  46. namePart =
  47. oneof [ return mazstr
  48. , do cs <- choose (1, 2)
  49. vectorOf cs (elements "a_AQZ0'-∀")
  50. ]
  51. ------------------------------------------------------------------------
  52. -- * All tests
  53. ------------------------------------------------------------------------
  54. -- Template Haskell hack to make the following $allProperties work
  55. -- under ghc-7.8.
  56. return [] -- KEEP!
  57. -- | All tests as collected by 'allProperties'.
  58. --
  59. -- Using 'allProperties' is convenient and superior to the manual
  60. -- enumeration of tests, since the name of the property is added
  61. -- automatically.
  62. tests :: TestTree
  63. tests = testProperties "Internal.Compiler.MAlonzo.Encode" $allProperties