Posted by Tom Moertel
Fri, 10 Sep 2004 16:00:00 GMT
Just a quick note to mention LectroTest, a Perl project I have been working on recently. It’s inspired by QuickCheck for the Haskell programming language. The motivation is that traditional unit testing requires programmers to spell out each and every case to test, which seems like an awful lot of work, much of which probably isn’t necessary. Even so, the payoff of unit testing is real, and we ought to do it. Thus the goal becomes to reduce the pain while keeping the gain.
Rather than working at the level of individual test cases, why not tell the computer about the shape of the “test space” and let the computer generate the cases automatically? That’s what LectroTest does. You declare properties and assert that they hold over a certain test space. LectroTest then generates random cases from within the space and tests your assertion for each. If LectroTest is able to “break” whatever you’re testing, it emits a counterexample that you can feed back into your code to debug the error. (Counterexamples also make for nice regression tests.)
If you write code in Perl and want another option for testing your code, give LectroTest a look.
Posted in programming, perl, testing
Tags lectrotest, perl, testing
no comments
no trackbacks

Posted by Tom Moertel
Thu, 22 Jul 2004 22:25:00 GMT
I discovered only recently about Google’s challenge to find the first ten-digit prime number in the consecutive digits of e. For fun, I solved the challenge (and the follow-up challenge), but the interesting part is a cool algorithm that I came across to compute the digits of e, one by one.
The algorithm lets you compute the digits sequentially without any pre-commitment to the total number of digits you’ll eventually take. The technique behind the algorithm can be used to compute other things, and so it’s worth a look.
If you’re interested, please take a look at my HuSi diary entry on the subject, Google challenge task and computing the digits of e, where I explain the method in detail. I’ll also refer you to Jeremy Gibbons’s paper An Unbounded Spigot Algorithm for the Digits of Pi, which is the inspiration for the method.
Posted in programming, functional programming
no comments
no trackbacks

Posted by Tom Moertel
Sat, 13 Mar 2004 17:00:00 GMT
In another draft of his article about Scheme programming, jacob shows how he approaches the problem of writing a TCP port scanner. That seemed like a fun problem, and so I whipped up this version in one of my favorite programming languages, Haskell:
module Main (main) where
import Control.Concurrent
import Control.Exception
import Data.Maybe
import Network
import Network.BSD
import System.Environment
import System.Exit
import System.IO
main :: IO ()
main = do
args <- getArgs
case args of
[host, from, to] -> withSocketsDo $
scanRange host [read from .. read to]
_ -> usage
usage = do
hPutStrLn stderr "Usage: Portscan host from_port to_port"
exitFailure
scanRange host ports =
mapM (threadWithChannel . scanPort host . fromIntegral) ports >>=
mapM_ hitCheck
where
hitCheck mvar = takeMVar mvar >>= maybe (return ()) printHit
printHit port = putStrLn =<< showService port
threadWithChannel action = do
mvar <- newEmptyMVar
forkIO (action >>= putMVar mvar)
return mvar
scanPort host port =
withDefault Nothing (tryPort >> return (Just port))
where
tryPort = connectTo host (PortNumber port) >>= hClose
showService port =
withDefault (show port) $ do
service <- getServiceByPort port "tcp"
return (show port ++ " " ++ serviceName service)
withDefault defaultVal action =
handle (const $ return defaultVal) action
Example usage:
$ ./Portscan internalhost.moertel.com 1 1000
21 ftp
22 ssh
80 http
111 sunrpc
139 netbios-ssn
443 https
445 microsoft-ds
631 ipp
709
720
Care to give it a try in your favorite programming language?
Posted in programming, haskell
Tags haskell, portscan
3 comments
no trackbacks
