packages feed

futhark-0.28.1: src-testing/Language/Futhark/TypeChecker/ConsumptionTests.hs

module Language.Futhark.TypeChecker.ConsumptionTests
  ( tests,
  )
where

import Data.Bifunctor
import Data.Set qualified as S
import Language.Futhark
import Language.Futhark.SyntaxTests ()
import Language.Futhark.TypeChecker.Consumption
import Test.Tasty
import Test.Tasty.HUnit

tests :: TestTree
tests =
  testGroup
    "ConsumptionTests"
    [ testGroup
        "inferReturnFreshness"
        [ testCase "*[]i32" $
            inferReturnFreshness
              [Id "x_1" (Info "[2]i32") mempty]
              "[2]i32"
              (second (const mempty) ("[2]i32" :: StructType))
              @?= "*[2]i32",
          --
          testCase "[]i32" $
            inferReturnFreshness
              [Id "x_1" (Info "[2]i32") mempty]
              "[2]i32"
              ( second
                  (const (S.singleton (AliasBound ("x_1", []))))
                  ("[2]i32" :: StructType)
              )
              @?= "[2]i32",
          --
          testCase "([]i32,[]i32)" $
            inferReturnFreshness
              [Id "x_1" (Info "[2]i32") mempty]
              "([2]i32, [2]i32)"
              ( second
                  (const (S.singleton (AliasFree ("y_2", []))))
                  ("([2]i32,[2]i32)" :: StructType)
              )
              @?= "([2]i32, [2]i32)",
          --
          -- An opaque value that may have internal aliasing cannot be given
          -- a fresh type.  See Note [Parametric results] in
          -- Language.Futhark.TypeChecker.Consumption.
          testCase "opaque" $
            let t = Scalar (TypeVar Nonfresh (qualName "t_2") [])
             in inferReturnFreshness
                  [Id "n_1" (Info "i64") mempty]
                  t
                  (second (const (S.singleton AliasSelf)) t)
                  @?= (t `setMode` Nonfresh),
          --
          testCase "*opaque" $
            let t = Scalar (TypeVar Nonfresh (qualName "t_2") [])
             in inferReturnFreshness
                  [Id "n_1" (Info "i64") mempty]
                  t
                  (second (const mempty) t)
                  @?= (t `setMode` Fresh)
        ]
    ]