1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
module Futhark.Internalise.TypesValuesTests (tests) where
import Control.Monad.Free (Free (..))
import Data.Map qualified as M
import Data.String (fromString)
import Futhark.IR.Syntax hiding (Free)
import Futhark.IR.SyntaxTests ()
import Futhark.Internalise.TypesValues
import Language.Futhark.SyntaxTests ()
import Test.Tasty
import Test.Tasty.HUnit
internaliseTypeTests :: TestTree
internaliseTypeTests =
testGroup
"internaliseType"
[ mkTest
"[0]()"
[Free [Pure "[0i64]unit"]],
mkTest
"{a: [t_7447][n_7448](f32, f32), b: i64, c: i64}"
[Free [Pure "[t_7447][n_7448]f32", Pure "[t_7447][n_7448]f32"], Pure "i64", Pure "i64"],
mkTest
"([0]i32, {a: f32, b: f32, c: f32, d: [0]((f32, f32), (f32, f32))})"
[ Free [Pure "[0i64]i32"],
Pure "f32",
Pure "f32",
Pure "f32",
Free [Pure "[0i64]f32", Pure "[0i64]f32", Pure "[0i64]f32", Pure "[0i64]f32"]
],
mkTest
"[0]([1]i32, f32)"
[Free [Free [Pure "[0i64][1i64]i32"], Pure "[0i64]f32"]]
]
where
mkTest x y =
testCase (prettyString x) $ internaliseType x @?= y
sumTypeTests :: TestTree
sumTypeTests =
testGroup
"internaliseConstructors"
[ testCase "Dedup of primitives" $
internaliseConstructors
( M.fromList
[ ("foo", [Pure "i64"]),
("bar", [Pure "i64"])
]
)
@?= ( [Pure "i64"],
[ ("bar", [0]),
("foo", [0])
]
),
testCase "Dedup of array" $
internaliseConstructors
( M.fromList
[ ("foo", [Pure "[?0]i64"]),
("bar", [Pure "[?0]i64"])
]
)
@?= ( [Pure "[?0]i64", Pure "[?0]i64"],
[ ("bar", [0]),
("foo", [1])
]
),
testCase
"Dedup of array of tuple"
$ internaliseConstructors
( M.fromList
[ ("foo", [Free [Pure "[?0]i64", Pure "[?0]i64"]]),
("bar", [Pure "[?0]i64"])
]
)
@?= ( [Pure "[?0]i64", Free [Pure "[?0]i64", Pure "[?0]i64"]],
[ ("bar", [0]),
("foo", [1, 2])
]
)
]
-- Be aware that some of these tests simply reinforce current
-- behaviour - it may be that we want to restrict aliasing even
-- further in the future; these tests would have to be updated in such
-- cases.
inferAliasesTests :: TestTree
inferAliasesTests =
testGroup
"inferAliases"
[ mkTest
[Free [Pure "[0i64]i32"]]
[Free [Pure "[?0]i32"]]
[[("[?0]i32", RetAls [0] [0])]],
mkTest
[Free [Pure "[0i64]i32", Pure "[0i64]i32"]]
[Free [Pure "[0i64]i32", Pure "[0i64]i32"]]
[ [ ("[0i64]i32", RetAls [0] [0]),
("[0i64]i32", RetAls [1] [1])
]
],
-- Basically zip.
mkTest
[Free [Pure "[n_0]i32"], Free [Pure "[n_0]i32"]]
[Free [Pure "[n_0]i32", Pure "[n_0]i32"]]
[ [ ("[n_0]i32", RetAls [] [0]),
("[n_0]i32", RetAls [] [1])
]
],
mkTest
[Free [Pure "[0i64]i32"], Free [Pure "[0i64]i32", Pure "[0i64]i32"]]
[Free [Pure "[?0]i32", Pure "[?0]i32"]]
[ [ ("[?0]i32", RetAls [1] [0]),
("[?0]i32", RetAls [2] [1])
]
],
mkTest
[Free [Pure "[0i64][1i64]i32", Pure "[0i64][1i64]i32"]]
[Free [Pure "[?0]i32", Pure "[?0]i32"]]
[ [ ("[?0]i32", RetAls [0] [0]),
("[?0]i32", RetAls [1] [1])
]
],
-- Basically unzip.
mkTest
[Free [Pure "[n_0][n_1]i32", Pure "[n_0][n_1]i32"]]
[Free [Pure "[?0]i32"], Free [Pure "[?0]i32"]]
[ [("[?0]i32", RetAls [] [0, 1])],
[("[?0]i32", RetAls [] [0, 1])]
],
mkTest
[ Free [Pure "*[n_0][n_1]i32"],
Free [Pure "[n_2]i64"],
Free [Pure "[n_3]i64"]
]
[Free [Pure "*[n_0][n_1]i32"]]
[[("*[n_0][n_1]i32", RetAls [] [])]],
mkTest
[Free [Pure "[n_0]i32", Free [Pure "[n_0][n_1]i32"]]]
[Free [Pure "[n_0]i32"]]
[[("[n_0]i32", RetAls [1] [0])]],
mkTest
[]
[ Free [Pure "[n_0]i32", Free [Pure "[n_0][n_1]i32"]],
Free [Pure "[n_0]i32"]
]
[ [("[n_0]i32", RetAls [] [0]), ("[n_0][n_1]i32", RetAls [] [1])],
[("[n_0]i32", RetAls [] [1, 2])]
],
mkTest
[Free [Pure "[n_0]i32"]]
[Free [Pure "[m_1][m_1]i32"]]
[ [("[m_1][m_1]i32", RetAls [0] [0])]
]
]
where
mkTest all_param_ts all_res_ts expected =
testCase (show all_param_ts <> " " <> show all_res_ts) $
inferAliases
(map (fmap fromString) all_param_ts)
(map (fmap fromString) all_res_ts)
@?= expected
tests :: TestTree
tests =
testGroup
"Futhark.Internalise.TypesValuesTests"
[ internaliseTypeTests,
sumTypeTests,
inferAliasesTests
]
|