X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMisc.hs;h=d0eb7286559ecd0b5098fd67afd38bcbb0179e1c;hb=b494ff845304fe1f255130539e271e6bfc2a5d3e;hp=34c682e31a7609e590898311885ea397fed1f6c4;hpb=26f0a2204a0bdb41b1d6aa5430544c404b77e638;p=spline3.git diff --git a/src/Misc.hs b/src/Misc.hs index 34c682e..d0eb728 100644 --- a/src/Misc.hs +++ b/src/Misc.hs @@ -1,8 +1,16 @@ +{-# LANGUAGE BangPatterns #-} -- | The Misc module contains helper functions that seem out of place -- anywhere else. module Misc where +import qualified Data.Vector as V (Vector, elem, empty, filter) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) +import Test.Framework.Providers.QuickCheck2 (testProperty) +import Test.HUnit (Assertion, assertEqual) +import Test.QuickCheck + -- | The standard factorial function. See -- for @@ -17,11 +25,12 @@ where -- 24 -- factorial :: Int -> Int -factorial n - | n <= 1 = 1 - | n > 20 = error "integer overflow in factorial function" - | otherwise = product [1..n] - +factorial !n = + go 1 n + where + go !acc !i + | i <= 1 = acc + | otherwise = go (acc * i) (i - 1) -- | Takes a three-dimensional list, and flattens it into a -- one-dimensional one. @@ -35,11 +44,78 @@ flatten :: [[[a]]] -> [a] flatten xs = concat $ concat xs +-- | Switch the x and z dimensions of a three-dimensional list. +transpose_xz :: [[[a]]] -> [[[a]]] +transpose_xz [] = [] +transpose_xz [[]] = [[]] +transpose_xz [[[]]] = [[[]]] +transpose_xz m = + [[[ m !! x !! y !! z | x <- [0..xsize]] + | y <- [0..ysize]] + | z <- [0..zsize]] + where + zsize = (length m) - 1 + ysize = length (head m) - 1 + xsize = length (head $ head m) - 1 + -- | Takes a list, and returns True if its elements are pairwise -- equal. Returns False otherwise. +-- +-- Only used in tests. +-- all_equal :: (Eq a) => [a] -> Bool all_equal xs = all (== first_element) other_elements where first_element = head xs other_elements = tail xs + + +-- | Returns 'True' if the vectors xs and ys are disjoint, 'False' +-- otherwise. +-- +-- Examples: +-- +-- >>> let xs = Data.Vector.fromList [1,2,3] +-- >>> let ys = Data.Vector.fromList [4,5,6] +-- >>> disjoint xs ys +-- True +-- +-- >>> let ys = Data.Vector.fromList [3,4,5] +-- >>> disjoint xs ys +-- False +-- +-- Only used in tests. +-- +disjoint :: (Eq a) => V.Vector a -> V.Vector a -> Bool +disjoint xs ys = + intersect xs ys == V.empty + where + intersect :: (Eq a) => V.Vector a -> V.Vector a -> V.Vector a + intersect ws zs = + V.filter (`V.elem` zs) ws + +prop_factorial_greater :: Int -> Property +prop_factorial_greater n = + n <= 20 ==> factorial n >= n + + +test_flatten1 :: Assertion +test_flatten1 = + assertEqual "flatten actually works" expected_list actual_list + where + target = [[[1::Int]], [[2, 3]]] + expected_list = [1, 2, 3] + actual_list = flatten target + + +misc_tests :: Test.Framework.Test +misc_tests = + testGroup "Misc Tests" [ + testCase "flatten (1)" test_flatten1 ] + + +misc_properties :: Test.Framework.Test +misc_properties = + testGroup "Misc Properties" [ + testProperty "factorial greater" prop_factorial_greater ]