Test a function: Difference between revisions
(→Java: Added implementation) |
(→{{header|Java}}: Show setUp and tearDown and annotations) |
||
Line 5: | Line 5: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|5}}<br> |
{{works with|Java|5}}<br> |
||
{{libheader| |
{{libheader|JUnit}} |
||
<lang |
<lang java5>import ExampleClass.pali; // or from wherever it is defined |
||
import ExampleClass.rPali; // 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 { |
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 the pali(...) method. |
||
*/ |
*/ |
||
@Test |
|||
public void testNonrecursivePali() throws Exception { |
public void testNonrecursivePali() throws Exception { |
||
assertEquals(pali("abcba"), true); |
assertEquals(pali("abcba"), true); |
||
Line 23: | Line 36: | ||
* Test the rPali(...) method. |
* Test the rPali(...) method. |
||
*/ |
*/ |
||
@Test |
|||
public void testRecursivePali() throws Exception { |
public void testRecursivePali() throws Exception { |
||
assertEquals(rPali("abcba"), true); |
assertEquals(rPali("abcba"), true); |
Revision as of 16:56, 29 May 2009
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
<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); }
}</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
<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>