Skip to content

Commit eb19e46

Browse files
committed
Fix test bugs
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.
1 parent ad64349 commit eb19e46

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

tests/ArrayTests.hs

Lines changed: 25 additions & 28 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,8 +256,7 @@ 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+
#ifndef PRIM_MUTABLE_ARRAYS
265260
, testSwapSameIndex
266261
#endif
267262
] where
@@ -285,12 +280,14 @@ testSwap = testGroup "Swap Tests" [ caseTests, performanceTests, propertyTests ]
285280
testSwapSameIndex :: TestTree
286281
testSwapSameIndex = testCase "Test Swapping Same Index" $ do
287282
let arr = CustomArray.makeArray 5 (1 :: Int)
288-
let swappedArr = CustomOperations.swap arr 2 2
283+
swappedArr = CustomOperations.swap arr 2 2
289284
assertEqual "Array should remain unchanged" 1 (CustomArray.get swappedArr 2)
290285

291286
-- Test performance for swap.
292287
performanceTests :: TestTree
293-
performanceTests = testGroup "Swap Performance Tests" [ testLargeArraySwap ] where
288+
performanceTests = testGroup "Swap Performance Tests" [
289+
testLargeArraySwap
290+
] where
294291
largeInt = 1000000 * performanceTestMultiplier
295292

296293
-- Test performance of creating and splitting a large.
@@ -304,7 +301,7 @@ testSwap = testGroup "Swap Tests" [ caseTests, performanceTests, propertyTests ]
304301
-- The property tests for the split function.
305302
propertyTests :: TestTree
306303
propertyTests = testGroup "Swap Property Tests" [
307-
testSwapCommutative
304+
testSwapCommutative
308305
] where
309306

310307
-- Generates random arrays to test that swap is commutative.
@@ -326,25 +323,25 @@ testSwap = testGroup "Swap Tests" [ caseTests, performanceTests, propertyTests ]
326323
-- TODO: Missing performance and property tests.
327324
--------------------------------------------------------------------------------
328325
testSplit :: TestTree
329-
testSplit = testGroup "Test Split" [ caseTests ] where
326+
testSplit = testGroup "Test Split" [
327+
caseTests
328+
] where
330329

331330
-- The case tests for the split function.
332331
caseTests :: TestTree
333332
caseTests = testGroup "Split Case Tests" [
334-
-- TODO: the below should work for mutable backends too
335-
#ifndef MUTABLE_ARRAYS
336333
testSplitMid
337-
,
338-
#endif
339-
testSplitEmpty
334+
, testSplitEmpty
340335
] where
341336

342337
-- Testing after splitting an array in the middle the sizes of the left and right are correct.
343338
-- and the contents of the left and right don't change.
344339
-- Hardcoded array size and values.
345340
testSplitMid :: TestTree
346341
testSplitMid = testCase "Test Split Mid" $ do
347-
let arr = CustomArray.makeArray 6 (1 :: Int)
342+
--let arr = CustomArray.makeArray 6 (1 :: Int)
343+
let list = replicate 6 (1 :: Int)
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)

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/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)