From df8578fbde3f99d1c556f87d0ccd7b38fa49b3e8 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Mon, 10 Dec 2012 17:23:54 +0100 Subject: [PATCH] Generalise the test helper 'genUniquesList' Instead of always using "arbitrary", make it require a generator (that can then use more complex rules for building the list elements), and also convert it to use a temporary set instead of list membership. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Guido Trotter <ultrotter@google.com> --- htest/Test/Ganeti/HTools/Container.hs | 2 +- htest/Test/Ganeti/HTools/Types.hs | 2 +- htest/Test/Ganeti/OpCodes.hs | 2 +- htest/Test/Ganeti/TestCommon.hs | 11 ++++++----- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/htest/Test/Ganeti/HTools/Container.hs b/htest/Test/Ganeti/HTools/Container.hs index 4df06a735..5ea85f6ba 100644 --- a/htest/Test/Ganeti/HTools/Container.hs +++ b/htest/Test/Ganeti/HTools/Container.hs @@ -63,7 +63,7 @@ prop_findByName = forAll (genNode (Just 1) Nothing) $ \node -> forAll (choose (1, 20)) $ \ cnt -> forAll (choose (0, cnt - 1)) $ \ fidx -> - forAll (genUniquesList (cnt * 2)) $ \ allnames -> + forAll (genUniquesList (cnt * 2) arbitrary) $ \ allnames -> forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername -> let names = zip (take cnt allnames) (drop cnt allnames) nl = makeSmallCluster node cnt diff --git a/htest/Test/Ganeti/HTools/Types.hs b/htest/Test/Ganeti/HTools/Types.hs index ea53d6932..5dbcbe715 100644 --- a/htest/Test/Ganeti/HTools/Types.hs +++ b/htest/Test/Ganeti/HTools/Types.hs @@ -106,7 +106,7 @@ instance Arbitrary Types.IPolicy where istd <- genBiggerISpec imin imax <- genBiggerISpec istd num_tmpl <- choose (0, length allDiskTemplates) - dts <- genUniquesList num_tmpl + dts <- genUniquesList num_tmpl arbitrary vcpu_ratio <- choose (1.0, maxVcpuRatio) spindle_ratio <- choose (1.0, maxSpindleRatio) return Types.IPolicy { Types.iPolicyMinSpec = imin diff --git a/htest/Test/Ganeti/OpCodes.hs b/htest/Test/Ganeti/OpCodes.hs index e3ed1f397..58107b278 100644 --- a/htest/Test/Ganeti/OpCodes.hs +++ b/htest/Test/Ganeti/OpCodes.hs @@ -360,7 +360,7 @@ genEmptyContainer = pure . GenericContainer $ Map.fromList [] genDiskIndices :: Gen [DiskIndex] genDiskIndices = do cnt <- choose (0, C.maxDisks) - genUniquesList cnt + genUniquesList cnt arbitrary -- | Generates a list of node names. genNodeNames :: Gen [String] diff --git a/htest/Test/Ganeti/TestCommon.hs b/htest/Test/Ganeti/TestCommon.hs index 62990499f..138782977 100644 --- a/htest/Test/Ganeti/TestCommon.hs +++ b/htest/Test/Ganeti/TestCommon.hs @@ -187,11 +187,12 @@ genFields = do vectorOf n genName -- | Generates a list of a given size with non-duplicate elements. -genUniquesList :: (Eq a, Arbitrary a) => Int -> Gen [a] -genUniquesList cnt = - foldM (\lst _ -> do - newelem <- arbitrary `suchThat` (`notElem` lst) - return (newelem:lst)) [] [1..cnt] +genUniquesList :: (Eq a, Arbitrary a, Ord a) => Int -> Gen a -> Gen [a] +genUniquesList cnt generator = do + set <- foldM (\set _ -> do + newelem <- generator `suchThat` (`Set.notMember` set) + return (Set.insert newelem set)) Set.empty [1..cnt] + return $ Set.toList set newtype SmallRatio = SmallRatio Double deriving Show instance Arbitrary SmallRatio where -- GitLab