1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>2--3-- SPDX-License-Identifier: GPL-3.0-only45module BV (bvTests) where67import SimpleBV qualified as SMT8import Test.Tasty9import Test.Tasty.HUnit1011foldingTests :: TestTree12foldingTests =13 testGroup14 "foldingTests"15 [ testCase "Folding of continous concat expressions" $16 do17 let val = SMT.const "foo" 321819 let b1 = SMT.extract val 0 820 let b2 = SMT.extract val 8 82122 SMT.concat b2 b1 @?= SMT.extract val 0 16,23 testCase "Concat with zeros" $24 do25 let lhs = SMT.bvLit 8 0x026 let rhs = SMT.const "foo" 82728 SMT.concat lhs rhs @?= SMT.zeroExtend 8 rhs,29 testCase "Folding of ite-based equalities" $30 do31 let cond = SMT.const "foo" 32 `SMT.eq` SMT.const "bar" 323233 let ifT = SMT.bvLit 32 0xdeadbeef34 let ifF = SMT.bvLit 32 0xbeefdead35 let val = SMT.ite cond ifT ifF3637 SMT.eq val ifT @?= cond38 SMT.eq val ifF @?= SMT.not cond,39 testCase "Folding of same-width extraction" $40 do41 let val = SMT.const "byte" 842 SMT.extract val 0 8 @?= val,43 testCase "Folding of constant extractions" $44 do45 let val = SMT.bvLit 32 0xdeadbeef46 SMT.extract val 16 16 @?= SMT.bvLit 16 0xdead,47 testCase "Folding of identical nested extracts" $48 do49 let val = SMT.const "foobar" 645051 let ex1 = SMT.extract val 0 1652 let ex2 = SMT.extract ex1 0 165354 ex2 @?= ex1,55 testCase "Folding of ITE expressions" $56 do57 let cond = SMT.const "foo" 32 `SMT.eq` SMT.const "bar" 3258 let val = SMT.ite cond (SMT.bvLit 32 0xdeadbeef) (SMT.bvLit 32 0xbeefdead)5960 SMT.extract val 0 16 @?= SMT.ite cond (SMT.bvLit 16 0xbeef) (SMT.bvLit 16 0xdead),61 testCase "Extract reduces size" $62 do63 let val = SMT.bvLit 32 0xdeadbeef64 SMT.extract val 8 8 @?= SMT.bvLit 8 0xbe,65 testCase "Removal of non-extracted zero-extensions" $66 do67 let val = SMT.zeroExtend 24 $ SMT.bvLit 8 0xff68 SMT.extract val 0 8 @?= SMT.bvLit 8 0xff69 SMT.extract val 4 4 @?= SMT.bvLit 4 0xf,70 testCase "Extraction of zero bits" $71 do72 let val = SMT.zeroExtend 24 $ SMT.bvLit 8 0xff73 SMT.extract val 8 24 @?= SMT.bvLit 24 0x074 SMT.extract val 0 8 @?= SMT.bvLit 8 0xff75 SMT.extract val 24 8 @?= SMT.bvLit 8 0x0,76 -- TODO: constant fold extractions of zeros.77 -- SMT.extract val 8 8 @?= SMT.bvLit 8 0x078 testCase "Extraction including zero-extended bits" $79 do80 let lit = SMT.bvLit 8 0xab81 let val = SMT.zeroExtend 56 lit82 SMT.extract val 0 32 @?= SMT.zeroExtend 24 lit83 SMT.extract val 0 64 @?= val84 ]8586bvTests :: TestTree87bvTests = testGroup "SimpleBV" [foldingTests]