[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

clifford.beshers clifford.beshers at linspire.com
Fri Apr 23 15:11:07 UTC 2010


The following commit has been merged in the master branch:
commit 4179660d9405373a341995d98359871789dc3571
Author: clifford.beshers <clifford.beshers at linspire.com>
Date:   Tue Sep 12 06:55:33 2006 +0100

    Functions to merge two sorted lists into a sorted whole.
    
    
    Unit tests are done using QuickCheck properties wrapped in HUnit test format.

diff --git a/MissingH/List.hs b/MissingH/List.hs
index afdd3fd..f39e99b 100644
--- a/MissingH/List.hs
+++ b/MissingH/List.hs
@@ -30,7 +30,9 @@ This module provides various helpful utilities for dealing with lists.
 Written by John Goerzen, jgoerzen\@complete.org
 -}
 
-module MissingH.List(-- * Tests
+module MissingH.List(-- * Merging
+                     merge, mergeBy,
+                     -- * Tests
                      startswith, endswith, contains, hasAny,
                      -- * Association List Utilities
                      {- | These functions are designed to augment the
@@ -63,6 +65,42 @@ import System.IO.Unsafe
 import Control.Monad.State(State, get, put)
 import Data.Maybe(isJust)
 
+
+{- | Merge two sorted lists into a single, sorted whole.
+
+Example:
+
+> merge [1,3,5] [1,2,4,6] -> [1,1,2,3,4,5,6]
+
+QuickCheck test property:
+
+prop_merge xs ys =
+    merge (sort xs) (sort ys) == sort (xs ++ ys)
+          where types = xs :: [Int]
+-}
+merge ::  (Ord a) => [a] -> [a] -> [a]
+merge = mergeBy (compare)
+
+{- | Merge two sorted lists using into a single, sorted whole,
+allowing the programmer to specify the comparison function.
+
+QuickCheck test property:
+
+prop_mergeBy xs ys =
+    mergeBy cmp (sortBy cmp xs) (sortBy cmp ys) == sortBy cmp (xs ++ ys)
+          where types = xs :: [ (Int, Int) ]
+                cmp (x1,_) (x2,_) = compare x1 x2
+-}
+mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
+mergeBy cmp [] ys = ys
+mergeBy cmp xs [] = xs
+mergeBy cmp (allx@(x:xs)) (ally@(y:ys)) 
+        -- Ordering derives Eq, Ord, so the comparison below is valid.
+        -- Explanation left as an exercise for the reader.
+        -- Someone please put this code out of its misery.
+    | (x `cmp` y) <= EQ = x : mergeBy cmp xs ally
+    | otherwise = y : mergeBy cmp allx ys
+
 {- | Returns true if the given list starts with the specified elements;
 false otherwise.  (This is an alias for "Data.List.isPrefixOf".)
 
diff --git a/testsrc/Listtest.hs b/testsrc/Listtest.hs
index b2a7d4f..c7e80bf 100644
--- a/testsrc/Listtest.hs
+++ b/testsrc/Listtest.hs
@@ -19,6 +19,23 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 module Listtest(tests) where
 import Test.HUnit
 import MissingH.List
+-- module QUnit where
+
+import System.Random hiding (split)
+import Data.List
+-- import Test.HUnit
+import Test.QuickCheck as QC
+
+qunit :: (QC.Testable a) => String -> a -> Test
+qunit lbl property =
+    TestLabel lbl $ TestCase $
+              do stdGen <- newStdGen
+                 case generate 100 stdGen (evaluate property) of
+                   (Result (Just True) _ _ ) -> return ()
+                   (Result res stamp args) -> assertFailure $ show (res, stamp, args)
+
+
+
 
 test_delFromAL = 
     let f :: [(String, Int)] -> [(String, Int)] -> Test
@@ -214,7 +231,24 @@ test_spanList =
            f (contains "foo") "foo" ("f", "oo")]
 
 
-tests = TestList [TestLabel "delFromAL" (TestList test_delFromAL),
+test_merge =
+    qunit "prop_merge" prop_merge
+
+prop_merge xs ys =
+    merge (sort xs) (sort ys) == sort (xs ++ ys)
+          where types = xs :: [Int]
+
+test_mergeBy =
+    qunit "test_mergeBy" prop_mergeBy
+
+prop_mergeBy xs ys =
+    mergeBy cmp (sortBy cmp xs) (sortBy cmp ys) == sortBy cmp (xs ++ ys)
+          where types = xs :: [Int]
+                cmp = compare
+
+tests = TestList [test_merge,
+                  test_mergeBy,
+                  TestLabel "delFromAL" (TestList test_delFromAL),
                   TestLabel "uniq" (TestList test_uniq),
                   TestLabel "addToAL" (TestList test_addToAL),
                   TestLabel "split" (TestList test_split),

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list