計算機科学のブログ

コードの整理とプロジェクトのビルド Monad型クラス QuickCheckを使ったプロパティテスト QuickCheckによるプロパティテスト QuickCheck

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT6(コードの整理とプロジェクトのビルド)、LESSON36(QuickCheckを使ったプロパティテスト)、36.3(QuickCheckによるプロパティテスト)、QuickCheck、クイックチェック 37-5の解答を求めてみる。

package.yaml

name:                palindrome-testing
version:             0.1.0.0
github:              "githubuser/palindrome-testing"
license:             BSD3
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2022 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/palindrome-testing#readme>

dependencies:
- base >= 4.7 && < 5

library:
  source-dirs: src

executables:
  palindrome-testing-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-testing

tests:
  palindrome-testing-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-testing
    - QuickCheck

palindrome-testing.cabal

cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack

name:           palindrome-testing
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/githubuser/palindrome-testing#readme>
homepage:       https://github.com/githubuser/palindrome-testing#readme
bug-reports:    https://github.com/githubuser/palindrome-testing/issues
author:         Author name here
maintainer:     example@example.com
copyright:      2022 Author name here
license:        BSD3
license-file:   LICENSE
build-type:     Simple
extra-source-files:
    README.md
    ChangeLog.md

source-repository head
  type: git
  location: https://github.com/githubuser/palindrome-testing

library
  exposed-modules:
      Lib
  other-modules:
      Paths_palindrome_testing
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

executable palindrome-testing-exe
  main-is: Main.hs
  other-modules:
      Paths_palindrome_testing
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-testing
  default-language: Haskell2010

test-suite palindrome-testing-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_palindrome_testing
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      QuickCheck
    , base >=4.7 && <5
    , palindrome-testing
  default-language: Haskell2010

コード

test/Spec.hs

import Lib (isPalindrome)
import Test.QuickCheck

assert :: Bool -> String -> String -> IO ()
assert test passStatement failStatement =
  putStrLn $
    if test
      then passStatement
      else failStatement

prop_reverseInvariant text =
  isPalindrome text == isPalindrome (reverse text)

main :: IO ()
main = do
  quickCheck prop_reverseInvariant
  putStrLn "done!"

入出力結果(Terminal, Zsh)

% stack test
splitmix          > configure
splitmix          > Configuring splitmix-0.1.0.4...
splitmix          > build
splitmix          > Preprocessing library for splitmix-0.1.0.4..
splitmix          > Building library for splitmix-0.1.0.4..
splitmix          > [1 of 4] Compiling Data.Bits.Compat
splitmix          > [2 of 4] Compiling System.Random.SplitMix.Init
splitmix          > [3 of 4] Compiling System.Random.SplitMix
splitmix          > [4 of 4] Compiling System.Random.SplitMix32
splitmix          > copy/register
splitmix          > Installing library in /Users/…/.stack/snapshots/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/lib/x86_64-osx-ghc-8.10.7/splitmix-0.1.0.4-Udb1gWOWPT4ftIqVxSbTF
splitmix          > Registering library for splitmix-0.1.0.4..
random            > configure
random            > Configuring random-1.2.0...
random            > build
random            > Preprocessing library for random-1.2.0..
random            > Building library for random-1.2.0..
random            > [1 of 3] Compiling System.Random.Internal
random            > [2 of 3] Compiling System.Random
random            > [3 of 3] Compiling System.Random.Stateful
random            > copy/register
random            > Installing library in /Users/…/.stack/snapshots/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/lib/x86_64-osx-ghc-8.10.7/random-1.2.0-GAV679NMk7iDELNoxF99p4
random            > Registering library for random-1.2.0..
QuickCheck        > configure
QuickCheck        > Configuring QuickCheck-2.14.2...
QuickCheck        > build
QuickCheck        > Preprocessing library for QuickCheck-2.14.2..
QuickCheck        > Building library for QuickCheck-2.14.2..
QuickCheck        > [ 1 of 16] Compiling Test.QuickCheck.Exception
QuickCheck        > [ 2 of 16] Compiling Test.QuickCheck.Random
QuickCheck        > [ 3 of 16] Compiling Test.QuickCheck.Gen
QuickCheck        > [ 4 of 16] Compiling Test.QuickCheck.Gen.Unsafe
QuickCheck        > [ 5 of 16] Compiling Test.QuickCheck.Arbitrary
QuickCheck        > [ 6 of 16] Compiling Test.QuickCheck.Poly
QuickCheck        > [ 7 of 16] Compiling Test.QuickCheck.Modifiers
QuickCheck        > [ 8 of 16] Compiling Test.QuickCheck.Function
QuickCheck        > [ 9 of 16] Compiling Test.QuickCheck.Text
QuickCheck        > [10 of 16] Compiling Test.QuickCheck.State
QuickCheck        > [11 of 16] Compiling Test.QuickCheck.Property
QuickCheck        > [12 of 16] Compiling Test.QuickCheck.Test
QuickCheck        > [13 of 16] Compiling Test.QuickCheck.Monadic
QuickCheck        > [14 of 16] Compiling Test.QuickCheck.All
QuickCheck        > [15 of 16] Compiling Test.QuickCheck.Features
QuickCheck        > [16 of 16] Compiling Test.QuickCheck
QuickCheck        > copy/register
QuickCheck        > Installing library in /Users/…/.stack/snapshots/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/lib/x86_64-osx-ghc-8.10.7/QuickCheck-2.14.2-9znbJPXXIAU5zq9aq8lWOx
QuickCheck        > Registering library for QuickCheck-2.14.2..
palindrome-testing> configure (lib + exe + test)
Configuring palindrome-testing-0.1.0.0...
palindrome-testing> build (lib + exe + test)
Preprocessing library for palindrome-testing-0.1.0.0..
Building library for palindrome-testing-0.1.0.0..
[1 of 2] Compiling Lib          
[2 of 2] Compiling Paths_palindrome_testing
Preprocessing executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Building executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
[1 of 2] Compiling Main [Lib changed]
[2 of 2] Compiling Paths_palindrome_testing
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-exe/palindrome-testing-exe ...
Preprocessing test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
Building test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
[1 of 2] Compiling Main         
[2 of 2] Compiling Paths_palindrome_testing
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-test/palindrome-testing-test ...
palindrome-testing> copy/register
Installing library in /Users/…/palindrome-testing/.stack-work/install/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/lib/x86_64-osx-ghc-8.10.7/palindrome-testing-0.1.0.0-Lo0twh14VmV8lGyY2zGauK
Installing executable palindrome-testing-exe in /Users/…/palindrome-testing/.stack-work/install/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/bin
Registering library for palindrome-testing-0.1.0.0..
palindrome-testing> test (suite: palindrome-testing-test)
                                
+++ OK, passed 100 tests.
done!

palindrome-testing> Test suite palindrome-testing-test passed
Completed 5 action(s).
% stack test
palindrome-testing-0.1.0.0: unregistering (local file changes: .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/autogen/Paths_palindrome_testing.hs .stack-work/d...)
palindrome-testing> configure (lib + exe + test)
Configuring palindrome-testing-0.1.0.0...
palindrome-testing> build (lib + exe + test)
Preprocessing library for palindrome-testing-0.1.0.0..
Building library for palindrome-testing-0.1.0.0..
[2 of 2] Compiling Paths_palindrome_testing [.stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/autogen/cabal_macros.h changed]
Preprocessing executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Building executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
[2 of 2] Compiling Paths_palindrome_testing [.stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-exe/autogen/cabal_macros.h changed]
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-exe/palindrome-testing-exe ...
Preprocessing test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
Building test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-test/palindrome-testing-test ...
palindrome-testing> copy/register
Installing library in /Users/…/palindrome-testing/.stack-work/install/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/lib/x86_64-osx-ghc-8.10.7/palindrome-testing-0.1.0.0-Lo0twh14VmV8lGyY2zGauK
Installing executable palindrome-testing-exe in /Users/…/palindrome-testing/.stack-work/install/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/bin
Registering library for palindrome-testing-0.1.0.0..
palindrome-testing> test (suite: palindrome-testing-test)
                                
+++ OK, passed 100 tests.
done!

palindrome-testing> Test suite palindrome-testing-test passed
Completed 2 action(s).
%