qute

A software analysis framework built around the QBE intermediate language

git clone https://git.8pit.net/qute.git

 1-- SPDX-FileCopyrightText: 2026 Sören Tempel <soeren+git@soeren-tempel.net>
 2--
 3-- SPDX-License-Identifier: GPL-3.0-only
 4
 5module State (stateTests) where
 6
 7import Control.Monad.State.Strict (evalStateT)
 8import Data.Word (Word8)
 9import Language.QBE.Simulator.Default.Expression qualified as D
10import Language.QBE.Simulator.Default.State (Env, loadObj, mkEnv)
11import Language.QBE.Types qualified as QBE
12import Test.Tasty
13import Test.Tasty.HUnit
14
15stateTests :: TestTree
16stateTests =
17  testGroup
18    "Test the default state implementation"
19    [ testCase "loadObj returns end address" $
20        do
21          let obj = QBE.OItem QBE.Byte [QBE.DString "foobar"]
22
23          env <- mkEnv [] 0x1000 128 :: IO (Env D.RegVal Word8)
24          res <- evalStateT (loadObj 0x1000 obj) env
25
26          res @?= 0x1006,
27      -- TODO: Turn this into a QuickCheck 'testProperty'.
28      testCase "loadObj return value is aligned with QBE.dataSize" $
29        do
30          let o1 = QBE.OItem QBE.Byte [QBE.DString "foobar"]
31          let o2 = QBE.OItem (QBE.Base QBE.Word) [QBE.DConst $ QBE.Number 23]
32
33          env <- mkEnv [] 0x0 128 :: IO (Env D.RegVal Word8)
34          res <- evalStateT (loadObj 0x0 o1 >>= flip loadObj o2) env
35
36          -- No padding inserted.
37          res @?= 10
38
39          -- The same calculation should be performed by QBE.dataSize.
40          let ds = QBE.DataDef [] (QBE.GlobalIdent "d") Nothing [o1, o2]
41          10 @?= QBE.dataSize ds
42    ]