[Haskell-beginners] hunit question

Daniel Fischer daniel.is.fischer at web.de
Tue Feb 2 17:03:48 EST 2010


Am Dienstag 02 Februar 2010 22:46:20 schrieb Joe Van Dyk:
> On Tue, Feb 2, 2010 at 1:34 PM, Daniel Fischer <daniel.is.fischer at web.de> 
wrote:
> > Am Dienstag 02 Februar 2010 22:18:50 schrieb Joe Van Dyk:
> >> I have a little binary search function and am trying to write tests
> >> for it in hunit.
> >>
> >> The below approach doesn't compile, because I'm attempting to build a
> >> list of tuples of different types, which isn't working for me.
> >>
> >> What's the appropriate way to do this test?
> >
> > QuickCheck, I'd say. Let that create more testcases than you could
> > bother to write out.
> >
> > Anyway, for testing lists of different types, you'd need separate
> > tests (properties/assertions).
> >
> > *But* you don't need that. Because the algorithm is general (works the
> > same way for all types in Ord), the only way it could be correct on
> > one type but not on another is if the Ord instance of one (or both) of
> > the types is incorrect.
> >
> > So it's sufficient to test on [Int].
>
> Good point, but I like to prove that to myself.  :D
>
> Any chance anyone can show me how to write a QuickCheck test for this?
>
> Joe

prop_binary_search :: Int -> [Int] -> Bool
prop_binary_search x xs =
    let ys = sort xs 
    in binary_search x ys == elemIndex x ys


ghci> quickCheck prop_binary_search
+++ OK, passed 100 tests.


More information about the Beginners mailing list