packages feed

fragr-0.1.0.0: test/Spec/Alias.hs

-- | Which entries a backend may legally place in shared memory.
module Spec.Alias (tests) where

import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase)

import Fragr qualified as FG
import Utils

tests :: TestTree
tests =
  testGroup
    "alias validation"
    [ acceptsWriteFirst
    , rejectsReadFirst
    , renamingToucherReads
    , fatalBeforeCompileAndUnknown
    ]

acceptsWriteFirst :: TestTree
acceptsWriteFirst = testCase "accepts members written before any read" do
  g <- FG.newFrameGraph @Env @Env
  a <- FG.addPass g "GenA" (FG.create @Tex "a" (tex "a") >>= FG.write) \_data -> pure ()
  b <-
    FG.addPass
      g
      "GenB"
      (FG.read a >> (FG.create @Tex "b" (tex "b") >>= FG.write))
      \_data -> pure ()
  FG.addPass_ g "Use" (FG.read b >> FG.setSideEffect) (pure ())
  FG.compile g
  FG.validateAliasGroups g [[0, 1]]

rejectsReadFirst :: TestTree
rejectsReadFirst = testCase "rejects a member whose first access is a read" do
  g <- FG.newFrameGraph @Env @Env
  -- "b" is created bare, so its first read/write access is Use's read.
  a <- FG.addPass g "GenA" (FG.create @Tex "a" (tex "a") >>= FG.write) \_data -> pure ()
  b <- FG.addPass g "Bare" (FG.create @Tex "b" (tex "b")) \_data -> pure ()
  FG.addPass_ g "Use" (FG.read a >> FG.read b >> FG.setSideEffect) (pure ())
  FG.compile g
  FG.validateAliasGroups g [[0]]
  assertFatal $ FG.validateAliasGroups g [[0], [1]]

renamingToucherReads :: TestTree
renamingToucherReads = testCase "a renaming first toucher counts as a read" do
  g <- FG.newFrameGraph @Env @Env
  imp <- FG.importResource g "imp" (tex "imp") (Tex 7)
  FG.addPass_ g "Blit" (FG.write_ imp) (pure ())
  FG.compile g
  -- The rename implicitly reads the prior version.
  assertFatal $ FG.validateAliasGroups g [[0]]

fatalBeforeCompileAndUnknown :: TestTree
fatalBeforeCompileAndUnknown = testCase "fatal before compile and on unknown entries" do
  g <- FG.newFrameGraph @Env @Env
  FG.addPass_ g "Gen" (FG.create @Tex "a" (tex "a") >>= FG.write_ >> FG.setSideEffect) (pure ())
  assertFatal $ FG.validateAliasGroups g [[0]]
  FG.compile g
  assertFatal $ FG.validateAliasGroups g [[42]]