Testing your first program
Factor handbook > Your first program

Prev:Writing some logic in your first program
Next:Extending your first program

Your palindrome.factor file should look like the following after the previous section:
! Copyright (C) 2012 Your name. ! See http://factorcode.org/license.txt for BSD license. USING: kernel sequences ; IN: palindrome : palindrome? ( string -- ? ) dup reverse = ;

We will now test our new word in the listener. If you haven't done so already, add the palindrome vocabulary to the listener's vocabulary search path:
USE: palindrome

Next, push a string on the stack (by surrounding text with quotes in the listener and then hitting ENTER):

Note that the stack display in the listener now shows this string. Having supplied the input, we call our word:

The stack display should now have a boolean false - f - which is the word's output. Since “hello” is not a palindrome, this is what we expect. We can get rid of this boolean by calling drop. The stack should be empty after this is done.

Now, let's try it with a palindrome; we will push the string and call the word in the same line of code:
"racecar" palindrome?

The stack should now contain a boolean true - t. We can print it and drop it using the . word:

What we just did is called interactive testing. A more advanced technique which comes into play with larger programs is Unit testing.

Create a test harness file using the scaffold tool:
"palindrome" scaffold-tests

Now, open the file named palindrome-tests.factor; it is located in the same directory as palindrome.factor, and it was created by the scaffold tool.

We will add some unit tests, which are similar to the interactive tests we did above. Unit tests are defined with the unit-test word, which takes a sequence of expected outputs, and a piece of code. It runs the code, and asserts that it outputs the expected values.

Add the following two lines to palindrome-tests.factor:
{ f } [ "hello" palindrome? ] unit-test { t } [ "racecar" palindrome? ] unit-test

Now, you can run unit tests:
"palindrome" test

It should report that all your tests have been run and there were no test failures, displaying the following output:

Unit Test: { { f } [ "hello" palindrome? ] } Unit Test: { { t } [ "racecar" palindrome? ] }

Now you can read about Extending your first program.