Test a function: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
(→‎Java: Added implementation)
Line 1: Line 1:
{{task}}
{{task}}
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.
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.

<br clear=all>
=={{header|Java}}==
{{works with|Java|5}}<br>
{{libheader|junit}}
<lang java>import ExampleClass.pali; // or from wherever it is defined
import ExampleClass.rPali; // or from wherever it is defined
public class PalindromeTest extends junit.framework.TestCase {
/**
* Test the pali(...) method.
*/
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.
*/
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);
}
}</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 14:47, 29 May 2009

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 java>import ExampleClass.pali; // or from wherever it is defined import ExampleClass.rPali; // or from wherever it is defined public class PalindromeTest extends junit.framework.TestCase {

   /**
    * Test the pali(...) method.
    */
   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.
    */
   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);
   }

}</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>