qute

A software analysis framework built around the QBE intermediate language

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

 1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>
 2--
 3-- SPDX-License-Identifier: GPL-3.0-only
 4
 5module Memory (memTests) where
 6
 7import Data.Array.IO (IOUArray)
 8import Data.Word (Word8)
 9import Language.QBE.Simulator.Memory
10import Test.Tasty
11import Test.Tasty.HUnit
12
13memTests :: TestTree
14memTests =
15  testGroup
16    "Memory tests"
17    [ testCase "Create memory and extract its size" $ do
18        mem <- mkMemory 0x0 512 :: IO (Memory IOUArray Word8)
19        memSize mem >>= assertEqual "" 512,
20      testCase "Store and read byte" $ do
21        m <- mkMemory 0 64 :: IO (Memory IOUArray Word8)
22        storeBytes m 0x0 [0xff]
23        loadBytes m 0x0 1 >>= assertEqual "" [0xff],
24      testCase "Store and read bytes" $ do
25        m <- mkMemory 0 32 :: IO (Memory IOUArray Word8)
26        storeBytes m 0x0 [0xde, 0xad, 0xbe, 0xef]
27        loadBytes m 0x0 4 >>= assertEqual "" [0xde, 0xad, 0xbe, 0xef],
28      testCase "Store and read multiple bytes" $ do
29        m <- mkMemory 0 4 :: IO (Memory IOUArray Word8)
30        storeBytes m 0x0 [0xde, 0xad]
31        storeBytes m 0x2 [0xbe, 0xef]
32        loadBytes m 0x0 2 >>= assertEqual "" [0xde, 0xad]
33        loadBytes m 0x2 2 >>= assertEqual "" [0xbe, 0xef]
34        loadBytes m 0x0 4 >>= assertEqual "" [0xde, 0xad, 0xbe, 0xef],
35      testCase "Overlapping memory address" $ do
36        addrOverlap 0x100 0x100 1 @?= True
37        addrOverlap 100 200 50 @?= False
38        addrOverlap 116 120 4 @?= False
39        addrOverlap 100 100 0 @?= False
40    ]