Test a function: Difference between revisions
(→{{header|AutoHotkey}}: not well known, i know...) |
|||
Line 2: | Line 2: | ||
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. |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
there is no "well known" testing library, but here is a simple testing framework: |
there is no "well known" testing library, but here is a simple testing framework: <br> |
||
test library: assert.ahk |
test library: assert.ahk |
||
<lang AutoHotkey> |
<lang AutoHotkey> |
Revision as of 17:09, 16 June 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.
AutoHotkey
there is no "well known" testing library, but here is a simple testing framework:
test library: assert.ahk
<lang AutoHotkey>
- assert.ahk
- assert(a, b, test=2)
assert(a, b="blank", test=0) {
if (b = "blank")
{
if !a msgbox % "blank value" return 0
}
if equal_list(a, b, "`n") return 0 else msgbox % test . ":`n" . a . "`nexpected:`n" . b
}
!r::reload
- equal_list(a, b, delimiter)
equal_list(a, b, delimiter) {
loop, parse, b, %delimiter% { if instr(a, A_LoopField) continue else return 0 } loop, parse, a, %delimiter% { if instr(b, A_LoopField) continue else return 0 }
return 1
} </lang> test example: <lang AutoHotkey> assert(isPalindrome("in girum imus nocte et consumimur igni"), 1 , "palindrome test") assert(broken("in girum imus nocte et consumimur igni"), "works" , "broken test") /* output:
testPalindrome.ahk
broken test: broken expected: works
- /
broken(x){ return "broken" }
- Include assert.ahk
- Include palindrome.ahk
</lang>
E
The standard testing tool in E is Updoc, a system which takes test scripts formatted in the same style as a REPL session and verifies that executing them produces the specified result values.
<lang e>#!/usr/bin/env rune
? def isPalindrome(string :String) { > def upper := string.toUpperCase() > def last := upper.size() - 1 > for i => c ? (upper[last - i] != c) in upper(0, upper.size() // 2) { > return false > } > return true > }
? isPalindrome("")
- value: true
? isPalindrome("a")
- value: true
? isPalindrome("aa")
- value: true
? isPalindrome("baa")
- value: false
? isPalindrome("baab")
- value: true
? isPalindrome("ba_ab")
- value: true
? isPalindrome("ba_ ab")
- value: false
? isPalindrome("ba _ ab")
- value: true
? isPalindrome("ab"*2)
- value: false
? def x := "ab" * 2**15; null
? x.size()
- value: 65536
? def xreversed := "ba" * 2**15; null
? isPalindrome(x + xreversed)
- value: true
? (x + xreversed).size()
- value: 131072</lang>
F#
#light let palindrome (s : string) = let r = new string(s.ToUpper().ToCharArray() |> Array.rev) r = s.ToUpper() #r @"nunit\nunit.framework.dll" // add reference to the NUnit framework open NUnit.Framework [<TestFixture>] type TestCases = class new() = {} [<Test>] member x.Test01() = Assert.IsTrue(palindrome "radar") [<Test>] member x.Test02() = Assert.IsFalse(palindrome "hello") end
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); }
/** * 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.
Ruby
Using the Ruby unit testing package, all you have to do is to create a subclass of Test::Unit::Testcase that contains methods that begin with "test_". The package will create a test suite and run it for you. <lang ruby>def palindrome?(s)
s == s.reverse
end
require 'test/unit' class MyTests < Test::Unit::TestCase
def test_palindrome_ok assert(palindrome? "aba") end
def test_palindrome_nok assert_equal(false, palindrome?("ab")) end
def test_object_without_reverse assert_raise(NoMethodError) {palindrome? 42} end
def test_wrong_number_args assert_raise(ArgumentError) {palindrome? "a", "b"} end
def test_show_failing_test assert(palindrome?("ab"), "this test case fails on purpose") end
end</lang>
$ ruby palindrome.rb Loaded suite palindrome Started ...F. Finished in 0.018 seconds. 1) Failure: test_show_failing_test(MyTests) [palindrome.rb:24]: this test case fails on purpose. <false> is not true. 5 tests, 5 assertions, 1 failures, 0 errors
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>