Test a function: Difference between revisions
m (→{{header|Python}}: Grammar.) |
(→Tcl: Added implementation) |
||
Line 122: | Line 122: | ||
14 passed and 0 failed. |
14 passed and 0 failed. |
||
Test passed.</pre> |
Test passed.</pre> |
||
=={{header|Tcl}}== |
|||
{{libheader|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> |
Revision as of 20:58, 28 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.
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>