Phil Trelford's Array
POKE 36879, 255

Running TAP

August 14, 2014 00:23 by phil

The Test Anything Protocol (TAP) is a text-based protocol for test results:

 1..4
 ok 1 - Input file opened
 not ok 2 - First line of the input valid
 ok 3 - Read the rest of the file
 not ok 4 - Summarized correctly # TODO Not written yet

 

I think the idea is an good one, a simple cross-platform human readable standard for formatting test results. There are TAP producers and consumers for Perl, Java, JavaScript etc. allowing you to join up tests for cross-platform projects.

NUnit runner

Over the last week or so I’ve created a TAP runner F# script for NUnit test methods. It supports the majority of NUnit’s attributes including ExpectedException, TimeOut and test generation with TestCase, TestCaseSource, Values, etc., .

The runner can be used in a console app to produce TAP output to the console or directly in F# interactive for running tests embedded in a script.

TAP run

Tests can be organized in classes:

type TAPExample () =
    [<Test>] member __.``input file opened`` () = Assert.Pass()
    [<Test>] member __.``First line of the input valid`` () = Assert.Fail()
    [<Test>] member __.``Read the rest of the file`` () = Assert.Pass()
    [<Test>] member __.``Summarized correctly`` () = Assert.Fail()

Tap.Run typeof<TAPExample>

or in modules:

let [<Test>] ``input file opened`` () = Assert.Pass()
let [<Test>] ``First line of the input valid`` () = Assert.Fail()
let [<Test>] ``Read the rest of the file`` () = Assert.Pass()
let [<Test>] ``Summarized correctly`` () = Assert.Fail()
type Marker = interface end
Tap.Run typeof<Marker>.DeclaringType

Note: the marker interface is used to reflect the module’s type.

Console output

In the console test cases are marked in red or green:

image


Debugging

If you create an F# Tutorial project you get both, an F# script file that runs as a console application allowing you to set breakpoints in your script with the Visual Studio debugger.

image


Prototyping

When I’m prototyping a new feature I typically use the F# interactive environment for quick feedback and to do exploratory testing. The TAP runner lets you create and run NUnit  formatted tests directly in the script file before later promoting to a full fat project for use in a continuous build environment.

F# Scripting

Interested in learning more about F# scripting, pop along to the Phil Nash’s talk at the F#unctional Londoners tonight.


Tags:
Categories: .Net | F#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Ocean Revival

August 10, 2014 15:20 by phil

This weekend I had the pleasure of sitting on the Ocean Q&A panel at Revival 2014 in Wolverhampton. I worked at Ocean in Manchester in my early 20s on titles like Jurassic Park (PC & Amiga) and Addams Family Values (SNES & Megadrive). It was fun to reminisce about the good old days with other former Ocean employees and people who’d played the games.

Ocean panel

The panel closely coincided with the public release of The History of Ocean Software book by Chris Wilkins which was funded as a Kickstarter:

Ocean the history

There were plenty of old games to play at the event too. I particularly enjoyed Rez on a PS2, Omega Race on a Vic-20 and a Flappy Bird clone on a Commodore 64.

Flappy Bird C64

When we got home, my 7yo and I pulled the Vic-20 out of the garage, and played some more Omega Race with the joystick we’d just picked up:



My 7yo has been picking up Python recently, with a Coding Club - Python Basics book.

One of the tasks is to print out the 5 times table:

number = 1
while i <= 12:
    print(number,"x 5 =",number*5)
    number = number + 1

Funnily enough Vic-20 Basic (circa 1981) was easily up to the challenge too:

5 times table

And good old FizzBuzz was no bother either:

FizzBuzz Vic-20

Then my son had a go at Magic 8-ball, but sadly lost the code he’d spent a while typing in when it is closed, so we re-wrote it again in F# so there was less to type:

let answers =[
   "Go for it!"
   "No way Jose!"
   "I'm not sure. Ask me again."
   "Fear of the unknown is what imprisons us."
   "It would be madness to do that!"
   "Only you can save mankind!"
   "Makes no difference to me, do or don't - whatever."
   "Yes, I think on balance that is the right choice"
   ]

printfn "Welcome to Magic 8-Ball."

printfn "Ask me for advice and then press enter to shake me"
System.Console.ReadLine() |> ignore

for i = 1 to 4 do printfn "Shaking..."

let rand = System.Random()
let choice = rand.Next(answers.Length)

printfn "%s" (answers.[choice])

Why not dig out your old computers and have some programming and games fun! :)
Tags:
Categories: Python | Basic | F#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Code Golf

August 2, 2014 11:22 by phil

Last month Grant Crofton popped down from Leeds to the F#unctional Londoners meetup at Skills Matter to run a fun hands on code golf session. The idea of code golf is to implement a specific algorithm in the fewest possible characters. This is not the kind of thing you should be doing in enterprise code, but it is fun, and an interesting way of exploring features in a programming language.

On the night we attempted condensed versions of FizzBuzz and 99 bottles of beer, with Ross and I winning the first challenge and Simon & Adrian the second.

FizzBuzz Score99 Bottles Score

Thanks again to Grant for a really fun session.

F# FizzBuzz

A while back I strived to squeeze an F# implementation of FizzBuzz into a tweet, and with white space removed it weighs in at 104 characters (excluding line breaks):

for n=1 to 100 do 
 printfn"%s"
  (match n%3,n%5 with 0,0->"FizzBuzz"|0,_->"Fizz"|_,0->"Buzz"|_,_->string n)

The implementation, although quite clear, requires a fair number of characters for the pattern matching portion.

After some head scratching we came up with the idea of using a lookup to select the string to display:

N %3 N % 5 Index Output
0 0 0 N
>0 0 1 “Fizz”
0 >0 2 “Buzz”
>0 >0 3 “FizzBuzz”

This took the implementation down to 89 characters (without line breaks):

for i=1 to 100 do
 printfn"%s"["FizzBuzz";"Buzz";"Fizz";string i].[sign(i%5)*2+sign(i%3)]

Another trick is to abuse the sign function, to get 1 if the result of the modulus is above 0 and 0 otherwise.

The lookup trick can be used in other languages, and here’s a few examples, just for fun.

VB.Net FizzBuzz

VB.Net has a reputation for being a little verbose, but using the lookup trick it was possible to it get down to 96 characters (excluding line breaks):

For i=1 To 100:
Console.WriteLine({i,"Fizz","Buzz","FizzBuzz"}(-(i Mod 3=0)-(i Mod 5=0)*2))
:Next

In VB.Net true values translate to –1 and false to 0. This allowed me to simply negate the result of i % N = 0 to compute an index.

Python FizzBuzz

Using a similar trick in Python, where true translates to 1 and 0 to false, I was able to get to a very respectable 79 characters (excluding line breaks):

for x in range(1,101):
 print([x,"Fizz","Buzz","FizzBuzz"][(x%3==0)+2*(x%5==0)])

Python’s simple print function also helped to keep the character count down.

Amanda FizzBuzz

Amanda is a variant of David Turner’s quintessential functional programming language Miranda. Amanda runs on Windows, and is used for teaching FP at some universities.

Using a list comprehension it was possible to squeeze in to a mere 67 characters:

[["FizzBuzz","Buzz","Fizz",itoa x]!(min[1,x%3]+min[1,x%5]*2)|x<-[1..100]]

Note: this is cheating a little as we are not explicitly writing to the console.

APL FizzBuzz

APL is a very mature language, dating back to the 1960s, and is still used commercially today. It also wins hands down in code golf with just 54 characters:

⍪{'FizzBuzz' 'Buzz' 'Fizz'⍵[1+(2×1⌊5|⍵)+1⌊3|⍵]}¨⍳100

APL is particularly strong at matrix processing and provides single character representations for common operations:

Notation Name Meaning
B Index generator Creates a list from 1 to B
¨ Each for each loop
Table Produces a one column matrix
B Floor Greatest integer less than or equal to B

Try APL in your browser now.

Challenge

Can you beat the APL implementation of FizzBuzz?

Have fun!


Tags:
Categories: F# | Basic | Python | Haskell | APL
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed