Skip to content

Commit 2236319

Browse files
committed
refactor: Use hspec and remove stack
+ Convert current manual IO test to hspec test + Remove stack, use cabal instead
1 parent 72d536a commit 2236319

File tree

6 files changed

+104
-201
lines changed

6 files changed

+104
-201
lines changed

package.yaml

Lines changed: 0 additions & 56 deletions
This file was deleted.

simplex-method.cabal

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
cabal-version: 1.12
2-
3-
-- This file has been generated from package.yaml by hpack version 0.36.0.
4-
--
5-
-- see: https://github.com/sol/hpack
6-
71
name: simplex-method
82
version: 0.2.0.0
93
synopsis: Implementation of the two-phase simplex method in exact rational arithmetic
@@ -16,6 +10,7 @@ maintainer: [email protected]
1610
copyright: BSD-3
1711
license: BSD3
1812
license-file: LICENSE
13+
cabal-version: 1.12
1914
build-type: Simple
2015
extra-source-files:
2116
README.md
@@ -36,34 +31,75 @@ library
3631
hs-source-dirs:
3732
src
3833
default-extensions:
39-
DataKinds DeriveFunctor DeriveGeneric DisambiguateRecordFields DuplicateRecordFields FlexibleContexts LambdaCase OverloadedLabels OverloadedRecordDot OverloadedStrings RecordWildCards TemplateHaskell TupleSections TypeApplications NamedFieldPuns
34+
DataKinds
35+
DeriveFunctor
36+
DeriveGeneric
37+
DerivingStrategies
38+
DisambiguateRecordFields
39+
DuplicateRecordFields
40+
ExtendedDefaultRules
41+
FlexibleContexts
42+
GeneralizedNewtypeDeriving
43+
LambdaCase
44+
NamedFieldPuns
45+
OverloadedLabels
46+
OverloadedRecordDot
47+
OverloadedStrings
48+
RecordWildCards
49+
TemplateHaskell
50+
TupleSections
51+
TypeApplications
52+
QuasiQuotes
4053
build-depends:
4154
base >=4.14 && <5
42-
, containers >=0.6.5.1 && <0.7
43-
, generic-lens >=2.2.0 && <2.3
44-
, lens >=5.2.2 && <5.3
45-
, monad-logger >=0.3.40 && <0.4
46-
, text >=2.0.2 && <2.1
47-
, time >=1.12.2 && <1.13
55+
, containers >= 0.6.7 && < 0.8
56+
, generic-lens >= 2.2.2 && < 2.3
57+
, text >= 2.0.2 && < 2.2
58+
, lens >= 5.3.5 && < 5.4
59+
, time >= 1.12.2 && < 1.15
60+
, monad-logger >= 0.3.42 && < 0.4
61+
, QuickCheck >= 2.16.0 && < 2.17
4862
default-language: Haskell2010
4963

5064
test-suite simplex-haskell-test
5165
type: exitcode-stdio-1.0
5266
main-is: Spec.hs
5367
other-modules:
54-
TestFunctions
68+
Linear.Simplex.Solver.TwoPhaseSpec
5569
Paths_simplex_method
5670
hs-source-dirs:
5771
test
5872
default-extensions:
59-
DataKinds DeriveFunctor DeriveGeneric DisambiguateRecordFields DuplicateRecordFields FlexibleContexts LambdaCase OverloadedLabels OverloadedRecordDot OverloadedStrings RecordWildCards TemplateHaskell TupleSections TypeApplications NamedFieldPuns
73+
DataKinds
74+
DeriveFunctor
75+
DeriveGeneric
76+
DerivingStrategies
77+
DisambiguateRecordFields
78+
DuplicateRecordFields
79+
ExtendedDefaultRules
80+
FlexibleContexts
81+
GeneralizedNewtypeDeriving
82+
LambdaCase
83+
NamedFieldPuns
84+
OverloadedLabels
85+
OverloadedRecordDot
86+
OverloadedStrings
87+
RecordWildCards
88+
TemplateHaskell
89+
TupleSections
90+
TypeApplications
91+
QuasiQuotes
6092
build-depends:
6193
base >=4.14 && <5
62-
, containers >=0.6.5.1 && <0.7
63-
, generic-lens >=2.2.0 && <2.3
64-
, lens >=5.2.2 && <5.3
65-
, monad-logger >=0.3.40 && <0.4
6694
, simplex-method
67-
, text >=2.0.2 && <2.1
68-
, time >=1.12.2 && <1.13
95+
, containers >= 0.6.7 && < 0.8
96+
, generic-lens >= 2.2.2 && < 2.3
97+
, text >= 2.0.2 && < 2.2
98+
, lens >= 5.3.5 && < 5.4
99+
, time >= 1.12.2 && < 1.15
100+
, monad-logger >= 0.3.42 && < 0.4
101+
, QuickCheck >= 2.16.0 && < 2.17
102+
, hspec >= 2.11.12 && < 2.12
103+
, hspec-expectations >= 0.8.3 && < 0.9
104+
, interpolatedstring-perl6 >= 1.0.2 && < 1.1
69105
default-language: Haskell2010

stack.yaml

Lines changed: 0 additions & 68 deletions
This file was deleted.

stack.yaml.lock

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/TestFunctions.hs renamed to test/Linear/Simplex/Solver/TwoPhaseSpec.hs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
module TestFunctions where
1+
module Linear.Simplex.Solver.TwoPhaseSpec where
22

3+
import Prelude hiding (EQ)
4+
5+
import Control.Monad
6+
import Control.Monad.IO.Class
7+
import Control.Monad.Logger
38
import qualified Data.Map as M
49
import Data.Ratio
10+
import Text.InterpolatedString.Perl6
11+
12+
import Test.Hspec
13+
import Test.Hspec.Expectations.Contrib (annotate)
14+
15+
import Linear.Simplex.Prettify
16+
import Linear.Simplex.Solver.TwoPhase
517
import Linear.Simplex.Types
6-
import Prelude hiding (EQ)
18+
import Linear.Simplex.Util
719

820
testsList :: [((ObjectiveFunction, [PolyConstraint]), Maybe Result)]
921
testsList =
@@ -1046,3 +1058,35 @@ testQuickCheck3 =
10461058
, GEQ (M.fromList [(1, -5), (2, -1), (2, 1)]) (-5)
10471059
]
10481060
)
1061+
1062+
spec :: Spec
1063+
spec = describe "twoPhaseSimplex" $ do
1064+
it "Check golden tests" $ do
1065+
forM_ testsList $
1066+
\((obj, constraints), expectedResult) -> do
1067+
actualResult <-
1068+
runStdoutLoggingT $
1069+
filterLogger (\_logSource logLevel -> logLevel > LevelInfo) $
1070+
twoPhaseSimplex obj constraints
1071+
let prettyObj = prettyShowObjectiveFunction obj
1072+
prettyConstraints = map prettyShowPolyConstraint constraints
1073+
1074+
expectedObjVal = extractObjectiveValue expectedResult
1075+
actualObjVal = extractObjectiveValue actualResult
1076+
annotate
1077+
[qc|
1078+
1079+
Objective Function (Non-prettified): {obj}
1080+
Constraints (Non-prettified): {constraints}
1081+
====================================
1082+
Objective Function (Prettified): {prettyObj}
1083+
Constraints (Prettified): {prettyConstraints}
1084+
====================================
1085+
Expected Solution (Full): {expectedResult}
1086+
Actual Solution (Full): {actualResult}
1087+
Expected Solution (Objective): {expectedObjVal}
1088+
Actual Solution (Objective): {actualObjVal}
1089+
1090+
|]
1091+
$ do
1092+
actualResult `shouldBe` expectedResult

test/Spec.hs

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1 @@
1-
module Main where
2-
3-
import Control.Monad
4-
import Control.Monad.IO.Class
5-
import Control.Monad.Logger
6-
7-
import Linear.Simplex.Prettify
8-
import Linear.Simplex.Solver.TwoPhase
9-
import Linear.Simplex.Types
10-
import Linear.Simplex.Util
11-
12-
import TestFunctions
13-
14-
main :: IO ()
15-
main = runStdoutLoggingT $ filterLogger (\_logSource logLevel -> logLevel > LevelInfo) $ runTests testsList
16-
17-
runTests :: (MonadLogger m, MonadFail m, MonadIO m) => [((ObjectiveFunction, [PolyConstraint]), Maybe Result)] -> m ()
18-
runTests [] = do
19-
liftIO $ putStrLn "All tests passed"
20-
pure ()
21-
runTests (((testObjective, testConstraints), expectedResult) : tests) =
22-
do
23-
testResult <- twoPhaseSimplex testObjective testConstraints
24-
if testResult == expectedResult
25-
then runTests tests
26-
else do
27-
let msg =
28-
"\nThe following test failed: "
29-
<> ("\nObjective Function (Non-prettified): " ++ show testObjective)
30-
<> ("\nConstraints (Non-prettified): " ++ show testConstraints)
31-
<> "\n===================================="
32-
<> ("\nObjective Function (Prettified): " ++ prettyShowObjectiveFunction testObjective)
33-
<> "\nConstraints (Prettified): "
34-
<> "\n"
35-
<> concatMap (\c -> "\t" ++ prettyShowPolyConstraint c ++ "\n") testConstraints
36-
<> "\n===================================="
37-
<> ("\nExpected Solution (Full): " ++ show expectedResult)
38-
<> ("\nActual Solution (Full): " ++ show testResult)
39-
<> ("\nExpected Solution (Objective): " ++ show (extractObjectiveValue expectedResult))
40-
<> ("\nActual Solution (Objective): " ++ show (extractObjectiveValue testResult))
41-
<> "\n"
42-
fail msg
1+
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 commit comments

Comments
 (0)