packages feed

wasmedge-hs-0.17.1.0: test/Test/Property/ValueProp.hs

-- | Hedgehog property tests for WasmVal encoding
module Test.Property.ValueProp (spec) where

import Data.Int (Int32, Int64)
import Hedgehog.Gen qualified as Gen
import Hedgehog.Range qualified as Range
import Test.Hspec
import Test.Hspec.Hedgehog

import WasmEdge.Types
import WasmEdge.Value

spec :: Spec
spec = describe "WasmVal property tests" $ do
  it "I32 roundtrip: encode >> decode == id" $ hedgehog prop_i32_roundtrip
  it "I64 roundtrip: encode >> decode == id" $ hedgehog prop_i64_roundtrip
  it "F64 roundtrip: encode >> decode == id" $ hedgehog prop_f64_roundtrip

-- ---------------------------------------------------------------------------
-- Generators
-- ---------------------------------------------------------------------------

genI32 :: Gen Int32
genI32 = Gen.int32 Range.constantBounded

genI64 :: Gen Int64
genI64 = Gen.int64 Range.constantBounded

genF64 :: Gen Double
genF64 = Gen.double (Range.constant (-1e15) 1e15)

-- ---------------------------------------------------------------------------
-- Properties
-- ---------------------------------------------------------------------------

prop_i32_roundtrip :: PropertyT IO ()
prop_i32_roundtrip = do
  v <- forAll genI32
  result <- evalIO $ encodeVal (WasmI32 v) decodeVal
  result === WasmI32 v

prop_i64_roundtrip :: PropertyT IO ()
prop_i64_roundtrip = do
  v <- forAll genI64
  result <- evalIO $ encodeVal (WasmI64 v) decodeVal
  result === WasmI64 v

prop_f64_roundtrip :: PropertyT IO ()
prop_f64_roundtrip = do
  v <- forAll genF64
  result <- evalIO $ encodeVal (WasmF64 v) decodeVal
  case result of
    WasmF64 v' -> v' === v
    other -> footnote ("unexpected: " ++ show other) >> failure