Test a function

From Rosetta Code
Revision as of 13:49, 7 June 2009 by rosettacode>Dkf (corrected categorization of task)
Task
Test a function
You are encouraged to solve this task according to the task description, using any language you may know.

Using a well known testing specific library/module/suite for your language, write some tests for your languages entry in Palindrome. If your language does not have a testing specific library well known to the languages community then state this or omit the language.


Java

Works with: Java version 5


Library: JUnit

<lang java5>import ExampleClass.pali; // or from wherever it is defined import ExampleClass.rPali; // or from wherever it is defined import org.junit.*; public class PalindromeTest extends junit.framework.TestCase {

   @Before
   public void setUp(){
       //runs before each test
       //set up instance variables, network connections, etc. needed for all tests
   }
   @After
   public void tearDown(){
       //runs after each test
       //clean up instance variables (close files, network connections, etc.).
   }
   /**
    * Test the pali(...) method.
    */
   @Test
   public void testNonrecursivePali() throws Exception {
       assertEquals(pali("abcba"), true);
       assertEquals(pali("aa"), true);
       assertEquals(pali("a"), true);
       assertEquals(pali(""), true);
       assertEquals(pali("ab"), false);
       assertEquals(pali("abcdba"), false);
   }
   /**
    * Test the rPali(...) method.
    */
   @Test
   public void testRecursivePali() throws Exception {
       assertEquals(rPali("abcba"), true);
       assertEquals(rPali("aa"), true);
       assertEquals(rPali("a"), true);
       assertEquals(rPali(""), true);
       assertEquals(rPali("ab"), false);
       assertEquals(rPali("abcdba"), false);
   }
   /**
    * Expect a WhateverExcpetion
    */
   @Test(expected=WhateverException.class)
   public void except(){
       //some code that should throw a WhateverException
   }

}</lang>

Python

This uses the doctest module from the Python standard library. This allows copies of tests run in an interactive session to be re-used as tests.

<lang python>def is_palindrome(s):

   
       >>> is_palindrome()
       True
       >>> is_palindrome('a')
       True
       >>> is_palindrome('aa')
       True
       >>> is_palindrome('baa')
       False
       >>> is_palindrome('baab')
       True
       >>> is_palindrome('ba_ab')
       True
       >>> is_palindrome('ba_ ab')
       False
       >>> is_palindrome('ba _ ab')
       True
       >>> is_palindrome('ab'*2)
       False
       >>> x = 'ab' *2**15
       >>> len(x)
       65536
       >>> xreversed = x[::-1]
       >>> is_palindrome(x+xreversed)
       True
       >>> len(x+xreversed)
       131072
       >>> 
   
   return s == s[::-1]

def _test():

   import doctest
   doctest.testmod()
   #doctest.testmod(verbose=True)

if __name__ == "__main__":

   _test() </lang>

When run in the form as shown above there is no output as all tests pass. If the alternative doctest.testmod line is used with verbose=True, then the following output is produced:

Trying:
    is_palindrome('')
Expecting:
    True
ok
Trying:
    is_palindrome('a')
Expecting:
    True
ok
Trying:
    is_palindrome('aa')
Expecting:
    True
ok
Trying:
    is_palindrome('baa')
Expecting:
    False
ok
Trying:
    is_palindrome('baab')
Expecting:
    True
ok
Trying:
    is_palindrome('ba_ab')
Expecting:
    True
ok
Trying:
    is_palindrome('ba_ ab')
Expecting:
    False
ok
Trying:
    is_palindrome('ba _ ab')
Expecting:
    True
ok
Trying:
    is_palindrome('ab'*2)
Expecting:
    False
ok
Trying:
    x = 'ab' *2**15
Expecting nothing
ok
Trying:
    len(x)
Expecting:
    65536
ok
Trying:
    xreversed = x[::-1]
Expecting nothing
ok
Trying:
    is_palindrome(x+xreversed)
Expecting:
    True
ok
Trying:
    len(x+xreversed)
Expecting:
    131072
ok
2 items had no tests:
    __main__
    __main__._test
1 items passed all tests:
  14 tests in __main__.is_palindrome
14 tests in 3 items.
14 passed and 0 failed.
Test passed.

Tcl

Library: tcltest

<lang tcl>package require tcltest 2 tcltest::test palindrome-1 {check for palindromicity} -body {

   palindrome abcdedcba

} -result 1 tcltest::test palindrome-2 {check for non-palindromicity} -body {

   palindrome abcdef

} -result 0 tcltest::test palindrome-3 {check for palindrome error} -body {

   palindrome

} -returnCodes error -result "wrong # args: should be \"palindrome s\"" tcltest::cleanupTests</lang>