From 0ccf7085c58679177bcac489602eeeda469fdcf3 Mon Sep 17 00:00:00 2001 From: Masahiro Sakai Date: Tue, 3 Dec 2024 23:02:30 +0900 Subject: [PATCH] add test cases for ToJSON/FromJSON instances of converters (cont'd) --- test/Test/Converter.hs | 16 ++++++++++++++++ test/Test/QUBO.hs | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/test/Test/Converter.hs b/test/Test/Converter.hs index 58c87d15..51df0037 100644 --- a/test/Test/Converter.hs +++ b/test/Test/Converter.hs @@ -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 -> @@ -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 diff --git a/test/Test/QUBO.hs b/test/Test/QUBO.hs index b6081ede..92123ed8 100644 --- a/test/Test/QUBO.hs +++ b/test/Test/QUBO.hs @@ -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 @@ -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 @@ -115,6 +123,13 @@ 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 @@ -122,6 +137,13 @@ prop_qubo2ising = forAll arbitrary $ \(qubo :: QUBO.Problem Rational) -> 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 @@ -129,6 +151,13 @@ prop_ising2qubo = forAll arbitrary $ \(ising :: QUBO.IsingModel Integer) -> 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