Skip to content

Commit df58114

Browse files
authored
Fix failing array tests (#11)
The test cases incorrectly assumed that makeArray initializes elements on the mutable backends and that fromList is defined for size 0. This commit updates the test suite to account for these issues by: - Removing tests that makeArray initializes elements for mutable backends. - Using replicate and fromList in splitMid tests to initialize elements. - Copying arrays when testing the commutativity of swaps. - Removing 0 from the range of fromList calls. There is still one failing test. This is the test for swapping the same index, which only fails on the prim-mutable-arrays backend. Other changes: - Replace type annotations with type applications to improve readability. - Set the default-language of the test suite to GHC2021.
1 parent 6b820ea commit df58114

File tree

5 files changed

+37
-39
lines changed

5 files changed

+37
-39
lines changed

lh-array-sort.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ test-suite test
144144
cpp-options: -DRUNTIME_CHECKS
145145

146146
default-language:
147-
Haskell2010
147+
GHC2021
148148
type:
149149
exitcode-stdio-1.0
150150
hs-source-dirs:

tests/ArrayTests.hs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,13 @@ arrayTests = testGroup "Array Tests"
4747
--------------------------------------------------------------------------------
4848
testMakeArray :: TestTree
4949
testMakeArray = testGroup "MakeArray Tests"
50-
[ caseTests
51-
-- TODO: property tests should work for mutable backends too (?)
52-
#ifndef MUTABLE_ARRAYS
53-
, propertyTests
54-
#endif
55-
, invalidOperationTests
56-
] where
50+
[ caseTests, propertyTests , invalidOperationTests ] where
5751

5852
-- The case tests for the MakeArray function.
5953
-- Tests that arrays of size 0 and 1 have the proper size and elements.
6054
caseTests :: TestTree
6155
caseTests = testGroup "MakeArray Case Tests"
62-
[ testEmptyArray
63-
-- TODO: the below should work for mutable backends too (?)
64-
#ifndef MUTABLE_ARRAYS
65-
, testSizeOneArray
66-
#endif
67-
]
56+
[ testEmptyArray, testSizeOneArray ]
6857
where
6958

7059
-- Testing the output of an empty array.
@@ -79,8 +68,11 @@ testMakeArray = testGroup "MakeArray Tests"
7968
QC.forAll QC.arbitrary $ \(randomElement :: Int) ->
8069
let customArr = CustomArray.makeArray 1 randomElement
8170
standardArr = makeStandardArray 1 randomElement
82-
in CustomArray.size customArr QC.=== 1 QC..&. CustomArray.toList customArr QC.=== StandardArray.elems standardArr
83-
71+
in CustomArray.size customArr QC.=== 1
72+
-- The mutable backend doesn't initialize array elements, StandardArray(Data.Array) does
73+
#ifndef MUTABLE_ARRAYS
74+
QC..&. CustomArray.toList customArr QC.=== StandardArray.elems standardArr
75+
#endif
8476

8577
-- The property tests for the makeArray function.
8678
-- Tests that random arrays have proper size and elements.
@@ -94,7 +86,11 @@ testMakeArray = testGroup "MakeArray Tests"
9486
QC.forAll ((,) <$> QC.chooseInt (0, mediumNumber) <*> QC.arbitrary) $ \(randomSize :: Int, randomElement :: Int) ->
9587
let customArr = CustomArray.makeArray randomSize randomElement
9688
standardArr = makeStandardArray randomSize randomElement
97-
in CustomArray.size customArr QC.=== randomSize QC..&. CustomArray.toList customArr QC.=== StandardArray.elems standardArr
89+
in CustomArray.size customArr QC.=== randomSize
90+
-- The mutable backend doesn't initialize array elements, StandardArray(Data.Array) does
91+
#ifndef MUTABLE_ARRAYS
92+
QC..&. CustomArray.toList customArr QC.=== StandardArray.elems standardArr
93+
#endif
9894

9995
-- The tests for exception cases of the MakeArray functions.
10096
-- Tests that negative size arrays can't be made.
@@ -260,51 +256,53 @@ testSwap = testGroup "Swap Tests" [ caseTests, performanceTests, propertyTests ]
260256
, testValuesSwapped2
261257
, testValuesSwapped3
262258
, testValuesSwapped4
263-
-- TODO: the below should work for mutable backends too
264-
#ifndef MUTABLE_ARRAYS
259+
-- TODO: This should work for the primitive mutable backend
260+
#ifndef PRIM_MUTABLE_ARRAYS
265261
, testSwapSameIndex
266262
#endif
267263
] where
268264

269265
-- Testing if swapping two elements works correctly.
270266
testValuesSwapped1 :: TestTree
271267
testValuesSwapped1 = testCase "Test Values Swapped Manual" $ do
272-
let arr = CustomArray.makeArray 5 (0 :: Int)
268+
let arr = CustomArray.makeArray @Int 5 0
273269
let arr1 = CustomArray.set arr 0 1
274270
let arr2 = CustomArray.set arr1 1 2
275271
let swappedArr = CustomOperations.swap arr2 0 1
276272
assertEqual "First element should be 2" 2 (CustomArray.get swappedArr 0)
277273
assertEqual "Second element should be 1" 1 (CustomArray.get swappedArr 1)
278274

279275
-- Testing if swapping two elements works correctly.
280-
testValuesSwapped2 = valueSwapTest 5 0 0 1 1 (2 :: Int)
281-
testValuesSwapped3 = valueSwapTest 10 1 1 2 2 (4 :: Int)
282-
testValuesSwapped4 = valueSwapTest 20 1 2 3 4 (5 :: Int)
276+
testValuesSwapped2 = valueSwapTest @Int 5 0 0 1 1 2
277+
testValuesSwapped3 = valueSwapTest @Int 10 1 1 2 2 4
278+
testValuesSwapped4 = valueSwapTest @Int 20 1 2 3 4 5
283279

284280
-- Testing that swapping an element with itself doesn't change anything.
285281
testSwapSameIndex :: TestTree
286282
testSwapSameIndex = testCase "Test Swapping Same Index" $ do
287-
let arr = CustomArray.makeArray 5 (1 :: Int)
288-
let swappedArr = CustomOperations.swap arr 2 2
283+
let arr = CustomArray.makeArray @Int 5 1
284+
swappedArr = CustomOperations.swap arr 2 2
289285
assertEqual "Array should remain unchanged" 1 (CustomArray.get swappedArr 2)
290286

291287
-- Test performance for swap.
292288
performanceTests :: TestTree
293-
performanceTests = testGroup "Swap Performance Tests" [ testLargeArraySwap ] where
289+
performanceTests = testGroup "Swap Performance Tests" [
290+
testLargeArraySwap
291+
] where
294292
largeInt = 1000000 * performanceTestMultiplier
295293

296294
-- Test performance of creating and splitting a large.
297295
testLargeArraySwap :: TestTree
298296
testLargeArraySwap = testCase "Test Large Array Swap" $ do
299-
let largeArr = CustomArray.makeArray largeInt (0 :: Int)
297+
let largeArr = CustomArray.makeArray @Int largeInt 0
300298
let updatedArr = CustomArray.set largeArr (largeInt-1) 1
301299
let swappedArr = CustomOperations.swap updatedArr (largeInt-1) 0
302300
assertEqual "Last element should be 0" 0 (CustomArray.get swappedArr (largeInt-1))
303301

304302
-- The property tests for the split function.
305303
propertyTests :: TestTree
306304
propertyTests = testGroup "Swap Property Tests" [
307-
testSwapCommutative
305+
testSwapCommutative
308306
] where
309307

310308
-- Generates random arrays to test that swap is commutative.
@@ -326,25 +324,24 @@ testSwap = testGroup "Swap Tests" [ caseTests, performanceTests, propertyTests ]
326324
-- TODO: Missing performance and property tests.
327325
--------------------------------------------------------------------------------
328326
testSplit :: TestTree
329-
testSplit = testGroup "Test Split" [ caseTests ] where
327+
testSplit = testGroup "Test Split" [
328+
caseTests
329+
] where
330330

331331
-- The case tests for the split function.
332332
caseTests :: TestTree
333333
caseTests = testGroup "Split Case Tests" [
334-
-- TODO: the below should work for mutable backends too
335-
#ifndef MUTABLE_ARRAYS
336334
testSplitMid
337-
,
338-
#endif
339-
testSplitEmpty
335+
, testSplitEmpty
340336
] where
341337

342338
-- Testing after splitting an array in the middle the sizes of the left and right are correct.
343339
-- and the contents of the left and right don't change.
344340
-- Hardcoded array size and values.
345341
testSplitMid :: TestTree
346342
testSplitMid = testCase "Test Split Mid" $ do
347-
let arr = CustomArray.makeArray 6 (1 :: Int)
343+
let list = replicate @Int 6 1
344+
arr = CustomArray.fromList list
348345
let (leftArr, rightArr) = CustomOperations.splitMid arr
349346
assertEqual "Left array size should be 3" 3 (CustomArray.size leftArr)
350347
assertEqual "Right array size should be 3" 3 (CustomArray.size rightArr)
@@ -356,7 +353,7 @@ testSplit = testGroup "Test Split" [ caseTests ] where
356353
-- Tests that splitting an empty array leads to two arrays of size 0.
357354
testSplitEmpty :: TestTree
358355
testSplitEmpty = testCase "Test Splitting Empty Array" $ do
359-
let arr = CustomArray.makeArray 0 (0 :: Int)
356+
let arr = CustomArray.makeArray @Int 0 0
360357
let (leftArr, rightArr) = CustomOperations.splitMid arr
361358
assertEqual "Left array size should be 0" 0 (CustomArray.size leftArr)
362359
assertEqual "Right array size should be 0" 0 (CustomArray.size rightArr)

tests/Infra.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Data.List (sort)
2525

2626
instance (QC.Arbitrary a, CustomArray.HasPrim a, Random a) => QC.Arbitrary (CustomArray.Array a) where
2727
arbitrary = do
28-
size <- QC.chooseInt (0, 1000) -- hardcode as needed
28+
size <- QC.chooseInt (1, 1000) -- hardcode as needed. fromList is not defined for size 0.
2929
elements <- QC.vectorOf size QC.arbitrary
3030
return $ CustomArray.fromList elements
3131

tests/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ tests :: TestTree
2020
tests = testGroup "Tests"
2121
[ arrayTests
2222
, sortTests
23-
] where
23+
] where

tests/Properties.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type LinearSort = CustomArray.Array Int -. CustomArray.Array Int
1818
-- Define the commutative property for swap.
1919
swapCommutativeProperty :: (QC.Arbitrary a, CustomArray.HasPrim a, Show a, Eq a) => CustomArray.Array a -> Int -> Int -> QC.Property
2020
swapCommutativeProperty arr i j =
21-
i /= j QC.==> CustomArray.toList (CustomOperations.swap arr i j) QC.=== CustomArray.toList (CustomOperations.swap arr j i)
21+
let arrCopy = CustomArray.fromList (CustomArray.toList arr) in
22+
i /= j QC.==> CustomArray.toList (CustomOperations.swap arrCopy i j) QC.=== CustomArray.toList (CustomOperations.swap arr j i)
2223

2324
-- Property: Function correctly sorts all elements.
2425
sortsCorrectlyProperty :: ([Int] -> [Int]) -> [Int] -> Bool

0 commit comments

Comments
 (0)