Skip to content

Commit

Permalink
add test cases for ToJSON/FromJSON instances of converters (cont'd)
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Dec 3, 2024
1 parent a0390f9 commit 0ccf708
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/Test/Converter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ prop_naesat2maxcut_forward =
forAllAssignments (fst nae) $ \m ->
evalNAESAT m nae === (MaxCut.eval (transformForward info m) maxcut >= threshold)

prop_naesat2maxcut_json :: Property
prop_naesat2maxcut_json =
forAll arbitraryNAESAT $ \nae ->
let ret@(_, info) = naesat2maxcut nae
json = J.encode info
in counterexample (show ret) $ counterexample (show json) $
J.eitherDecode json === Right info

prop_naesat2max2sat_forward :: Property
prop_naesat2max2sat_forward =
forAll arbitraryNAESAT $ \nae ->
Expand Down Expand Up @@ -190,6 +198,14 @@ prop_maxSAT2ToSimpleMaxCut_forward =
b2 = o2 >= th2
]

prop_maxSAT2ToSimpleMaxCut_json :: Property
prop_maxSAT2ToSimpleMaxCut_json =
forAll arbitraryMaxSAT2 $ \(wcnf, th1) ->
let ret@(_, info) = maxSAT2ToSimpleMaxCut (wcnf, th1)
json = J.encode info
in counterexample (show ret) $ counterexample (show json) $
J.eitherDecode json === Right info

------------------------------------------------------------------------

prop_satToIS_forward :: Property
Expand Down
29 changes: 29 additions & 0 deletions test/Test/QUBO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Test.QUBO (quboTestGroup) where

import Control.Monad
import qualified Data.Aeson as J
import Data.Array.IArray
import Data.ByteString.Builder
import qualified Data.IntMap.Strict as IntMap
Expand Down Expand Up @@ -90,6 +91,13 @@ prop_qubo2pb = forAll arbitrary $ \(qubo :: QUBO.Problem Integer) ->
let (pbo,_) = qubo2pb qubo
in Just qubo === fmap fst (pbAsQUBO pbo)

prop_qubo2pb_json :: Property
prop_qubo2pb_json = forAll arbitrary $ \(qubo :: QUBO.Problem Integer) ->
let ret@(pbo, info) = qubo2pb qubo
json = J.encode info
in counterexample (show ret) $ counterexample (show json) $
J.eitherDecode json === Right info

prop_pb2qubo :: Property
prop_pb2qubo = forAll arbitraryPBFormula $ \formula ->
let ((qubo :: QUBO.Problem Integer, th), info) = pb2qubo formula
Expand All @@ -115,20 +123,41 @@ prop_pb2qubo = forAll arbitraryPBFormula $ \formula ->
property True
]

prop_pb2qubo_json :: Property
prop_pb2qubo_json = forAll arbitraryPBFormula $ \formula ->
let ret@(_, info) = pb2qubo formula
json = J.encode info
in counterexample (show ret) $ counterexample (show json) $
J.eitherDecode json === Right info

prop_qubo2ising :: Property
prop_qubo2ising = forAll arbitrary $ \(qubo :: QUBO.Problem Rational) ->
let (ising, info) = qubo2ising qubo
in counterexample (show ising) $
forAll (arbitrarySolution (QUBO.quboNumVars qubo)) $ \sol ->
transformObjValueForward info (QUBO.eval sol qubo) === QUBO.evalIsingModel sol ising

prop_qubo2ising_json :: Property
prop_qubo2ising_json = forAll arbitrary $ \(qubo :: QUBO.Problem Rational) ->
let ret@(_, info) = qubo2ising qubo
json = J.encode info
in counterexample (show ret) $ counterexample (show json) $
J.eitherDecode json === Right info

prop_ising2qubo :: Property
prop_ising2qubo = forAll arbitrary $ \(ising :: QUBO.IsingModel Integer) ->
let (qubo, info) = ising2qubo ising
in counterexample (show qubo) $
forAll (arbitrarySolution (QUBO.isingNumVars ising)) $ \sol ->
transformObjValueForward info (QUBO.evalIsingModel sol ising) === QUBO.eval sol qubo

prop_ising2qubo_json :: Property
prop_ising2qubo_json = forAll arbitrary $ \(ising :: QUBO.IsingModel Integer) ->
let ret@(_, info) = ising2qubo ising
json = J.encode info
in counterexample (show ret) $ counterexample (show json) $
J.eitherDecode json === Right info

------------------------------------------------------------------------
-- Test harness

Expand Down

0 comments on commit 0ccf708

Please sign in to comment.