Test a function: Difference between revisions

PascalABC.NET
(→‎{{header|AutoHotkey}}: not well known, i know...)
(PascalABC.NET)
(148 intermediate revisions by 84 users not shown)
Line 1:
{{task|Software EngineeringTesting}}
 
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.
{{omit from|BBC BASIC}}
 
 
;Task:
Using a well-known testing-specific library/module/suite for your language, write some tests for your language's entry in [[Palindrome]].
 
If your language does not have a testing specific library well known to the language's community then state this or omit the language.<br><br>
 
=={{header|ACL2}}==
 
Using [http://www.ccs.neu.edu/home/cce/acl2/doublecheck.html DoubleCheck]:
 
<syntaxhighlight lang="lisp">(defun reverse-split-at-r (xs i ys)
(if (zp i)
(mv xs ys)
(reverse-split-at-r (rest xs) (1- i)
(cons (first xs) ys))))
 
(defun reverse-split-at (xs i)
(reverse-split-at-r xs i nil))
 
(defun is-palindrome (str)
(let* ((lngth (length str))
(idx (floor lngth 2)))
(mv-let (xs ys)
(reverse-split-at (coerce str 'list) idx)
(if (= (mod lngth 2) 1)
(equal (rest xs) ys)
(equal xs ys)))))
 
(include-book "testing" :dir :teachpacks)
 
(check-expect (is-palindrome "abba") t)
(check-expect (is-palindrome "mom") t)
(check-expect (is-palindrome "dennis sinned") t)
(check-expect (is-palindrome "palindrome") nil)
(check-expect (is-palindrome "racecars") nil)
 
(include-book "doublecheck" :dir :teachpacks)
 
(defrandom random-palindrome ()
(let ((chars (random-list-of (random-char))))
(coerce (append chars (reverse chars))
'string)))
 
(defproperty palindrome-test
(p :value (random-palindrome))
(is-palindrome p))</syntaxhighlight>
 
=={{header|Ada}}==
 
For normal use there is pragma Assert, functioning the same as many other languages.<br>
For larger testing frameworks, there are packages like [https://github.com/AdaCore/aunit Aunit] or [https://github.com/pyjarrett/trendy_test Trendy Test]
 
<syntaxhighlight lang="ada">with Ada.Text_IO;
 
procedure Test_Function is
 
function Palindrome (Text : String) return Boolean is
begin
for Offset in 0 .. Text'Length / 2 - 1 loop
if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
return False;
end if;
end loop;
return True;
end Palindrome;
 
str1 : String := "racecar";
str2 : String := "wombat";
 
begin
begin
pragma Assert(False); -- raises an exception if assertions are switched on
Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!");
exception
when others => -- assertions are switched on -- perform the tests
pragma Assert (Palindrome (str1) = True, "Assertion on str1 failed");
pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
Ada.Text_IO.Put_Line("Test Passed!");
end;
end Test_Function;</syntaxhighlight>
 
Ada 2012 introduced a new way to specify functions and test their correctness: Pre- and Postoconditions.
 
<syntaxhighlight lang="ada"> function Palindrome (Text : String) return Boolean
with Post => Palindrome'Result =
(Text'Length < 2 or else
((Text(Text'First) = Text(Text'Last)) and then
Palindrome(Text(Text'First+1 .. Text'Last-1))));</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">palindrome?: function [s][
s = reverse s
]
 
tests: [
[true? palindrome? "aba"]
[false? palindrome? "ab" ]
]
 
loop tests => ensure</syntaxhighlight>
 
{{out}}
 
<pre></pre>
 
=={{header|AutoHotkey}}==
there is no "well known" testing library, but here is a simple testing framework: <br>
test library: assert.ahk
<syntaxhighlight lang="autohotkey">; assert.ahk
<lang AutoHotkey>
; assert.ahk
;; assert(a, b, test=2)
assert(a, b="blank", test=0)
Line 42 ⟶ 149:
 
return 1
}</syntaxhighlight>
}
</lang>
test example:
<syntaxhighlight lang="autohotkey">assert(isPalindrome("in girum imus nocte et consumimur igni"), 1
<lang AutoHotkey>
assert(isPalindrome("in girum imus nocte et consumimur igni"), 1
, "palindrome test")
assert(broken("in girum imus nocte et consumimur igni"), "works"
Line 66 ⟶ 171:
 
#Include assert.ahk
#Include palindrome.ahk</syntaxhighlight>
 
</lang>
=={{header|Brat}}==
<syntaxhighlight lang="brat">include :assert
 
palindrome? = { str |
str = str.downcase.sub /\s+/ ""
str == str.reverse
}
 
setup name: "palindrome test" {
test "is a palindrome" {
assert { palindrome? "abba" }
}
 
test "is not a palindrome" {
assert_false { palindrome? "abb" }
}
 
test "is not a string" {
assert_fail { palindrome? 1001 }
}
 
test "this test fails" {
assert { palindrome? "blah blah" }
}
}</syntaxhighlight>
 
Output:
 
<pre>Loading tests...
Running palindrome test...
(4/4) this test fails
Test failure(s):
 
1. 'this test fails': assert failed
 
4 tests, 4 assertions, 1 failures.</pre>
 
=={{header|Bruijn}}==
Bruijn has equivalency testing builtin. The <code>:test</code> instruction checks whether two terms are beta-equivalent (alpha-equivalent after beta-reduction) and prints an error if they aren't. Alpha-conversion is not needed because of the usage of De Bruijn indices. All tests in imported files get run automatically (can be disabled using a CLI flag).
<syntaxhighlight lang="bruijn">
:import std/String .
 
main [<~>0 =? 0]
 
:test (main "tacocat") ([[1]])
:test (main "bruijn") ([[1]])
</syntaxhighlight>
 
{{out}}
<pre>
ERROR test failed: (main [((0 [[[(0 (1 (0 (0 (0 (1 (1 (0 2))))))))]]]) [((0 [[[(0 (1 (0 (0 (1 (1 (1 (0 2))))))))]]]) [((0 [[[(1 (0 (1 (0 (1 (1 (1 (0 2))))))))]]]) [((0 [[[(1 (0 (0 (1 (0 (1 (1 (0 2))))))))]]]) [((0 [[[(0 (1 (0 (1 (0 (1 (1 (0 2))))))))]]]) [((0 [[[(0 (1 (1 (1 (0 (1 (1 (0 2))))))))]]]) [[0]])])])])])])]) = [[1]]
reduced to [[0]] = [[1]]
</pre>
 
=={{header|C}}==
 
<syntaxhighlight lang="c">#include <assert.h>
int IsPalindrome(char *Str);
 
int main()
{
assert(IsPalindrome("racecar"));
assert(IsPalindrome("alice"));
}
</syntaxhighlight>
 
=={{header|C sharp}}==
 
First, using the VisualStudio TestTools for unit tests. I'm testing both of the methods for palindrome detection created in the article.
 
<syntaxhighlight lang="csharp">
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PalindromeDetector.ConsoleApp;
 
namespace PalindromeDetector.VisualStudioTests
{
[TestClass]
public class VSTests
{
[TestMethod]
public void PalindromeDetectorCanUnderstandPalindrome()
{
//Microsoft.VisualStudio.QualityTools.UnitTestFramework v4.0.30319
bool expected = true;
bool actual;
actual = Program.IsPalindrome("1");
Assert.AreEqual(expected, actual);
actual = Program.IsPalindromeNonRecursive("1");
Assert.AreEqual(expected, actual);
actual = Program.IsPalindrome("ingirumimusnocteetconsumimurigni");
Assert.AreEqual(expected, actual);
actual = Program.IsPalindromeNonRecursive("ingirumimusnocteetconsumimurigni");
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PalindromeDetecotryCanUnderstandNonPalindrome()
{
bool notExpected = true;
bool actual = Program.IsPalindrome("ThisIsNotAPalindrome");
Assert.AreNotEqual(notExpected, actual);
actual = Program.IsPalindromeNonRecursive("ThisIsNotAPalindrome");
Assert.AreNotEqual(notExpected, actual);
}
}
}</syntaxhighlight>
 
Second, NUnit tests. Couldn't test these because of namespace issues with NUnit, but I'm sure they work.
<syntaxhighlight lang="csharp">
using NUnit.Framework;
using PalindromeDetector.ConsoleApp;
 
namespace PalindromeDetector.VisualStudioTests
{
[TestFixture]
public class NunitTests
{
[Test]
public void PalindromeDetectorCanUnderstandPalindrome()
{
//nunit.framework v2.0.50727
bool expected = true;
bool actual;
actual = Program.IsPalindrome("1");
Assert.AreEqual(expected, actual);
actual = Program.IsPalindromeNonRecursive("1");
Assert.AreEqual(expected, actual);
actual = Program.IsPalindrome("ingirumimusnocteetconsumimurigni");
Assert.AreEqual(expected, actual);
actual = Program.IsPalindromeNonRecursive("ingirumimusnocteetconsumimurigni");
Assert.AreEqual(expected, actual);
}
[Test]
public void PalindromeDetectorUnderstandsNonPalindrome()
{
bool notExpected = true;
bool actual;
actual = Program.IsPalindrome("NotAPalindrome");
Assert.AreEqual(notExpected, actual);
actual = Program.IsPalindromeNonRecursive("NotAPalindrome");
Assert.AreEqual(notExpected, actual);
}
}
}</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <algorithm>
#include <string>
 
constexpr bool is_palindrome(std::string_view s)
{
return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}
</syntaxhighlight>
 
 
C++ has several popular frameworks for testing. However, simple things like a palidrome
test can be tested at compile time - failing the test will cause a compilation failure.
 
<syntaxhighlight lang="cpp">void CompileTimeTests()
{
static_assert(is_palindrome("ada"));
static_assert(!is_palindrome("C++"));
 
static_assert(is_palindrome("C++")); // fails at compile time
static_assert(!is_palindrome("ada")); // fails at compile time
}
 
int main()
{
}</syntaxhighlight>
{{out}}
<pre>
/home/garbanzo/play/rosettascratch/test-a-function_constexpr.cpp: In function 'void CompileTimeTests()':
/home/garbanzo/play/rosettascratch/test-a-function_constexpr.cpp:18:30: error: static assertion failed
18 | static_assert(is_palindrome("C++")); // fails at compile time
| ~~~~~~~~~~~~~^~~~~~~
/home/garbanzo/play/rosettascratch/test-a-function_constexpr.cpp:19:17: error: static assertion failed
19 | static_assert(!is_palindrome("ada")); // fails at compile time
| ^~~~~~~~~~~~~~~~~~~~~
gmake[2]: *** [CMakeFiles/scratch.dir/build.make:286: CMakeFiles/scratch.dir/test-a-function_constexpr.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/scratch.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2
</pre>
 
 
A popular testing framework is gtest.
 
<syntaxhighlight lang="cpp">#include <gtest/gtest.h>
 
TEST(PalindromeSuite, Test1)
{
EXPECT_TRUE(is_palindrome("ada"));
EXPECT_FALSE(is_palindrome("C++"));
 
EXPECT_FALSE(is_palindrome("ada")); // will fail
EXPECT_TRUE(is_palindrome("C++")); // will fail
}</syntaxhighlight>
{{out}}
<pre>
Running main() from /var/tmp/portage/dev-cpp/gtest-1.11.0/work/googletest-release-1.11.0/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from PalindromeSuite
[ RUN ] PalindromeSuite.Test1
/home/garbanzo/play/rosettascratch/test-a-function_gtest.cpp:16: Failure
Value of: is_palindrome("ada")
Actual: true
Expected: false
/home/garbanzo/play/rosettascratch/test-a-function_gtest.cpp:17: Failure
Value of: is_palindrome("C++")
Actual: false
Expected: true
[ FAILED ] PalindromeSuite.Test1 (0 ms)
[----------] 1 test from PalindromeSuite (0 ms total)
 
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] PalindromeSuite.Test1
 
1 FAILED TEST
</pre>
 
 
Boost also has a testing framework.
 
<syntaxhighlight lang="cpp">#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
 
BOOST_AUTO_TEST_CASE( Test1 )
{
BOOST_CHECK( is_palindrome("ada") == true );
BOOST_CHECK( is_palindrome("C++") == false );
 
BOOST_CHECK( is_palindrome("ada") == false); // will fail
BOOST_CHECK( is_palindrome("C++") == true); // will fail
}</syntaxhighlight>
{{out}}
<pre>
Running 1 test case...
[path]/test-a-function_boost.cpp(20): error: in "Test1": check is_palindrome("ada") == false has failed
[path]/test-a-function_boost.cpp(21): error: in "Test1": check is_palindrome("C++") == true has failed
 
*** 2 failures are detected in the test module "Master Test Suite"
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="lisp">
(use 'clojure.test)
 
(deftest test-palindrome?
(is (palindrome? "amanaplanacanalpanama"))
(is (not (palindrome? "Test 1, 2, 3")))
 
(run-tests)
</syntaxhighlight>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defpackage :rosetta
(:use :cl
:fiveam))
(in-package :rosetta)
 
(defun palindromep (string)
(string= string (reverse string)))
 
;; A suite of tests are declared with DEF-SUITE
(def-suite palindrome-suite :description "Tests for PALINDROMEP")
 
;; Tests following IN-SUITE are in the defined suite of tests
(in-suite palindrome-suite)
 
;; Tests are declared with TEST and take an optional documentation
;; string
(test palindromep
"Basic unit tests for PALINDROMEP."
(is-true (palindromep "a"))
(is-true (palindromep ""))
(is-true (palindromep "aba"))
(is-true (palindromep "ahha"))
(is-true (palindromep "amanaplanacanalpanama"))
(is-false (palindromep "ab"))
(is-false (palindromep "abcab")))
 
(test palindromep-bad-tests
"In order to demonstrate a failing test"
(is-true (palindromep "ab")))
 
;; Property based tests are also possible using built-in generators
(test matches-even-length-palindromes
(for-all ((s (gen-string)))
(is-true (palindromep (concatenate 'string s (reverse s))))))
 
;; And counter examples can be found for failing tests. This also
;; demonstrates combining generators to create cleaner input, in this
;; case restricting characters to the range of ASCII characters and
;; only permitting alphanumeric values.
(test matches-even-length-palindromes-bad
(for-all ((s (gen-string :elements (gen-character :code (gen-integer :min 0 :max 127) :alphanumericp t))))
(is-true (palindromep (concatenate 'string s s)))))
 
#|
Tests can be executed using RUN, RUN!, and (EXPLAIN! result-list)
 
RUN! = (EXPLAIN! (RUN test))
 
Individual tests can be run or the entire suite:
 
ROSETTA> (run! 'palindrome-suite)
 
Running test suite PALINDROME-SUITE
Running test PALINDROMEP .......
Running test PALINDROMEP-BAD-TESTS f
Running test MATCHES-EVEN-LENGTH-PALINDROMES .....................................................................................................
Running test MATCHES-EVEN-LENGTH-PALINDROMES-BAD ff
Did 10 checks.
Pass: 8 (80%)
Skip: 0 ( 0%)
Fail: 2 (20%)
 
Failure Details:
--------------------------------
MATCHES-EVEN-LENGTH-PALINDROMES-BAD []:
Falsifiable with ("oMYhcqnVbjYgxT6d3").
Results collected with failure data:
Did 1 check.
Pass: 0 ( 0%)
Skip: 0 ( 0%)
Fail: 1 (100%)
 
Failure Details:
--------------------------------
MATCHES-EVEN-LENGTH-PALINDROMES-BAD []:
(PALINDROMEP (CONCATENATE 'STRING S S)) did not return a true value.
--------------------------------
 
--------------------------------
--------------------------------
PALINDROMEP-BAD-TESTS [In order to demonstrate a failing test]:
(PALINDROMEP "ab") did not return a true value.
--------------------------------
 
NIL
(#<IT.BESE.FIVEAM::TEST-FAILURE {10083B52A3}>
#<IT.BESE.FIVEAM::FOR-ALL-TEST-FAILED {1008B34963}>)
NIL
ROSETTA> (run! 'palindromep)
 
Running test PALINDROMEP .......
Did 7 checks.
Pass: 7 (100%)
Skip: 0 ( 0%)
Fail: 0 ( 0%)
 
T
NIL
NIL
|#
</syntaxhighlight>
 
=={{header|Crystal}}==
 
<syntaxhighlight lang="ruby">require "spec"
 
describe "palindrome" do
it "returns true for a word that's palindromic" do
palindrome("racecar").should be_true
end
 
it "returns false for a word that's not palindromic" do
palindrome("goodbye").should be_false
end
end
 
def palindrome(s)
s == s.reverse
end</syntaxhighlight>
 
<pre>
Finished in 179 microseconds
2 examples, 0 failures, 0 errors, 0 pending
</pre>
 
=={{header|D}}==
<syntaxhighlight lang="d">unittest {
assert(isPalindrome("racecar"));
assert(isPalindrome("bob"));
assert(!isPalindrome("alice"));
}</syntaxhighlight>
 
=={{header|Delphi}}==
 
Using built in assertions.
 
<syntaxhighlight lang="delphi"> Assert(IsPalindrome('salàlas'), 'salàlas is a valid palindrome');
Assert(IsPalindrome('Ingirumimusnocteetconsumimurigni'), 'Ingirumimusnocteetconsumimurigni is a valid palindrome');
Assert(not IsPalindrome('123'), '123 is not a valid palindrome');</syntaxhighlight>
 
Using [[wp:DUnit|DUnit]], an open source unit testing framework that is bundled with Delphi.
 
<syntaxhighlight lang="delphi"> Check(IsPalindrome('salàlas'), 'salàlas is a valid palindrome');
Check(IsPalindrome('Ingirumimusnocteetconsumimurigni'), 'Ingirumimusnocteetconsumimurigni is a valid palindrome');
Check(not IsPalindrome('123'), '123 is not a valid palindrome');</syntaxhighlight>
 
=={{header|E}}==
Line 75 ⟶ 585:
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.
 
<langsyntaxhighlight lang="e">#!/usr/bin/env rune
 
? def isPalindrome(string :String) {
Line 124 ⟶ 634:
 
? (x + xreversed).size()
# value: 131072</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
EchoLisp provides (assert <true-value?> ["fail-message"]) and (check-expect <expression> <expected-result>).
<syntaxhighlight lang="lisp">
(assert (palindrome? "aba")) → #t
(assert (palindrome? "abbbca") "palindrome fail")
💥 error: palindrome fail : assertion failed : (palindrome? abbbca)
 
(check-expect (palindrome? "aba") #t) → #t
(check-expect (palindrome? "abcda") #f) → #t
(check-expect (palindrome? "abcda") #t)
😐 warning: #t : check failed : (palindrome? abcda) → #f
(assert (palindrome? "un roc lamina l animal cornu")) → #t
</syntaxhighlight>
 
=={{header|Erlang}}==
This is a unit test so I use Eunit. For system tests "Common Test" would be used. Both are built in.
<syntaxhighlight lang="erlang">
-module( palindrome_tests ).
-compile( export_all ).
-include_lib( "eunit/include/eunit.hrl" ).
 
abcba_test() -> ?assert( palindrome:is_palindrome("abcba") ).
 
abcdef_test() -> ?assertNot( palindrome:is_palindrome("abcdef") ).
</syntaxhighlight>
{{out}}
<pre>
25> eunit:test(palindrome).
All 2 tests passed.
</pre>
 
=={{header|Euphoria}}==
 
<syntaxhighlight lang="euphoria">
--unittest in standard library 4.0+
include std/unittest.e
include palendrome.e --routines to be tested
 
object p = "12321"
 
test_equal("12321", 1, isPalindrome(p))
test_equal("r12321", 1, isPalindrome(reverse(p)))
 
test_report()
 
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
{{libheader|NUnit}}
<syntaxhighlight lang="fsharp">let palindrome (s : string) =
#light
let a = s.ToUpper().ToCharArray()
let palindrome (s : string) =
Array.rev a = a
let r = new string(s.ToUpper().ToCharArray() |> Array.rev)
r = s.ToUpper()
open NUnit.Framework
#r @"nunit\nunit.framework.dll" // add reference to the NUnit framework
 
open NUnit.Framework
[<TestFixture>]
type TestCases() = class
new() = {}[<Test>]
member [<Test>]x.Test01() =
Assert.IsTrue(palindrome "radar")
member x.Test01() =
Assert.IsTrue(palindrome "radar")
[<Test>]
member [<Test>]x.Test02() =
Assert.IsFalse(palindrome "hello")</syntaxhighlight>
member x.Test02() =
 
Assert.IsFalse(palindrome "hello")
=={{header|Factor}}==
end
By convention, if there is a vocabulary at ''x/x.factor'', then its tests belong in ''x/x-tests.factor''.
 
''palindrome/palindrome.factor''
 
<syntaxhighlight lang="factor">USING: kernel sequences ;
IN: palindrome
 
: palindrome? ( string -- ? ) dup reverse = ;</syntaxhighlight>
 
''palindrome/palindrome-tests.factor''
 
<syntaxhighlight lang="factor">USING: palindrome tools.test ;
IN: palindrome.tests
 
[ t ] [ "racecar" palindrome? ] unit-test
[ f ] [ "ferrari" palindrome? ] unit-test</syntaxhighlight>
 
To run these tests from the listener:
 
<syntaxhighlight lang="factor">( scratchpad ) "palindrome" test</syntaxhighlight>
 
Factor's tutorial, [http://docs.factorcode.org/content/article-first-program.html Your first program], uses ''palindrome?'' as its example. The tutorial shows how to create tests for ''palindrome?'' and how to fix a failing test.
 
=={{header|Fantom}}==
 
To use the built-in test library, the program must be compiled into a pod. The layout for a simple pod and its build file is given in the [http://fantom.org/doc/docIntro/HelloWorld.html#pod documentation], and also information for adding and running the [http://fantom.org/doc/docTools/Fant.html test files].
 
<syntaxhighlight lang="fantom">
class TestPalindrome : Test
{
public Void testIsPalindrome ()
{
verify(Palindrome.isPalindrome(""))
verify(Palindrome.isPalindrome("a"))
verify(Palindrome.isPalindrome("aa"))
verify(Palindrome.isPalindrome("aba"))
verifyFalse(Palindrome.isPalindrome("abb"))
verify(Palindrome.isPalindrome("salàlas"))
verify(Palindrome.isPalindrome("In girum imus nocte et consumimur igni".lower.replace(" ","")))
}
}
</syntaxhighlight>
 
=={{header|Fortran}}==
There is no standard or popular facility. Compilers usually do not even check that a function or subroutine is invoked with the correct number of parameters let alone the correct types. Testing that a function returns expected values is entirely a matter for the programmer and whatever tools that may be devised, either directly for the function in question or adapted from some other project where this had been done.
 
 
=={{header|FreeBASIC}}==
{{trans|VBA}}
<syntaxhighlight lang="freebasic">
Sub StrReverse(Byref text As String)
Dim As Integer x, lt = Len(text)
For x = 0 To lt Shr 1 - 1
Swap text[x], text[lt - x - 1]
Next x
End Sub
 
Sub Replace(Byref T As String, Byref I As String, Byref S As String, Byval A As Integer = 1)
Var p = Instr(A, T, I), li = Len(I), ls = Len(S) : If li = ls Then li = 0
Do While p
If li Then T = Left(T, p - 1) & S & Mid(T, p + li) Else Mid(T, p) = S
p = Instr(p + ls, T, I)
Loop
End Sub
 
Function IsPalindrome(Byval txt As String) As Boolean
Dim As String tempTxt = Lcase(txt), copyTxt = Lcase(txt)
Replace(tempTxt, " ", "")
Replace(copyTxt, " ", "")
StrReverse(tempTxt)
If tempTxt = copyTxt Then
Color 10
Return true
Else
Color 12
Return false
End If
End Function
 
'--- Programa Principal ---
Dim As String a(10) => {"abba", "mom", "dennis sinned", "Un roc lamina l animal cornu", _
"palindrome", "ba _ ab", "racecars", "racecar", "wombat", "in girum imus nocte et consumimur igni"}
 
Print !"¨Pal¡ndromos?\n"
For i As Byte = 0 To Ubound(a)-1
Print a(i) & " -> ";
Print IsPalindrome((a(i)))
Color 7
Next i
Sleep
</syntaxhighlight>
{{out}}
<pre>
¿Palíndromos?
 
abba -> true
mom -> true
dennis sinned -> true
Un roc lamina l animal cornu -> true
palindrome -> false
ba _ ab -> true
racecars -> false
racecar -> true
wombat -> false
in girum imus nocte et consumimur igni -> true
</pre>
 
 
=={{header|Go}}==
Using Go's standard command, go test.
<syntaxhighlight lang="go">package pal
 
import "testing"
 
func TestPals(t *testing.T) {
pals := []string{
"",
".",
"11",
"ere",
"ingirumimusnocteetconsumimurigni",
}
for _, s := range pals {
if !IsPal(s) {
t.Error("IsPal returned false on palindrome,", s)
}
}
}
 
func TestNonPals(t *testing.T) {
nps := []string{
"no",
"odd",
"salàlas",
}
for _, s := range nps {
if IsPal(s) {
t.Error("IsPal returned true on non-palindrome,", s)
}
}
}</syntaxhighlight>
Output of go test:
<pre>
PASS
ok pal 0.002s
</pre>
 
=={{header|Haskell}}==
 
A notable testing library for Haskell is QuickCheck. It works in a way particularly supported by Haskell's type inference: you provide a function return a boolean, the test, and QuickCheck automatically generates random values for the function's parameters and checks that it returns <code>True</code> for all of them.
 
<syntaxhighlight lang="haskell">import Test.QuickCheck
 
isPalindrome :: String -> Bool
isPalindrome x = x == reverse x
 
{- There is no built-in definition of how to generate random characters;
here we just specify ASCII characters. Generating strings then automatically
follows from the definition of String as list of Char. -}
instance Arbitrary Char where
arbitrary = choose ('\32', '\127')
 
-- /------------------------- the randomly-generated parameters
-- | /------------------ the constraint on the test values
-- | | /- the condition which should be true
-- v v v
main = do
putStr "Even palindromes: " >> quickCheck (\s -> isPalindrome (s ++ reverse s))
putStr "Odd palindromes: " >> quickCheck (\s -> not (null s) ==> isPalindrome (s ++ (tail.reverse) s))
putStr "Non-palindromes: " >> quickCheck (\i s -> not (null s) && 0 <= i && i < length s && i*2 /= length s
==> not (isPalindrome (take i s ++ "•" ++ drop i s)))</syntaxhighlight>
 
The <code>==&gt;</code> operator is used to constrain the randomly-generated values: the second test needs a nonempty string, and the third needs an index into the string that is not the exact middle.
 
=={{header|Icon}} and {{header|Unicon}}==
 
The fact that success and failure of expression evaluation is an integral part
of both languages means that one may want to test expressions for success or
for failure. There is no standard framework, but the following example shows
how these tests might be handled.
<syntaxhighlight lang="unicon">procedure main()
s := "ablewasiereisawelba"
assert{"test1",palindrome(s)}
assertFailure{"test2",palindrome(s)}
s := "un"||s
assert{"test3",palindrome(s)}
assertFailure{"test4",palindrome(s)}
end
 
procedure palindrome(s)
return s == reverse(s)
end
 
procedure assert(A)
if not @A[2] then write(@A[1],": failed")
end
 
procedure assertFailure(A)
if @A[2] then write(@A[1],": failed")
end</syntaxhighlight>
 
Which outputs:
<pre>
->testf
test2: failed
test3: failed
->
</pre>
 
=={{header|Insitux}}==
It is possible to mock any built-in or user-defined function, and assert values are truthy. Testing [[Palindrome_detection#Insitux|Insitux's Palindrome detection entry]].
<syntaxhighlight lang="insitux">(var palindrome? (comp (filter letter?) lower-case (= (reverse %))))
 
;Arrange
(var calls [])
(function record f
(fn (var! calls (append f))
(... (unmocked f) args)))
 
(mock comp (record comp)
filter (record filter)
letter? (record letter?)
lower-case (record lower-case)
= (record =)
reverse (record reverse))
 
(var sentence "In girum imus nocte et consumimur igni.")
 
;Act
(var result (palindrome? sentence))
 
(unmock comp filter letter? lower-case = reverse)
 
;Assert
(assert result)
 
(var occurred (freqs calls))
(assert (= (len sentence) (occurred letter?))) ;letter? is called (len sentence) times
(assert (... = 1 (map occurred [filter lower-case reverse =]))) ;other functions are called only once</syntaxhighlight>
 
=={{header|J}}==
Using the [[J:Addons/general/unittest|general/unittest Addon]] to test the <code>isPalin0</code> verb from [[Palindrome#J|Palindrome]].
 
Tests are contained in a test script <tt>c:\mypath\palindrome_test.ijs</tt> with the following contents:
<syntaxhighlight lang="j">NB. Contents of palindrome_test.ijs
 
NB. Basic testing
test_palinA=: monad define
assert isPalin0 'abcba'
assert isPalin0 'aa'
assert isPalin0 ''
assert -. isPalin0 'ab'
assert -. isPalin0 'abcdba'
)
 
NB. Can test for expected failure instead
palinB_expect=: 'assertion failure'
test_palinB=: monad define
assert isPalin0 'ab'
)</syntaxhighlight>
 
Example Usage:
<syntaxhighlight lang="j"> require 'general/unittest'
unittest 'c:\mypath\palindrome_test.ijs'
Test: c:\mypath\palindrome_test.ijs
palinA .................................. OK
palinB .................................. OK</syntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|5}}<br>
{{libheader|JUnit}}
<langsyntaxhighlight lang="java5">import static ExampleClass.pali; // or from wherever it is defined
import static ExampleClass.rPali; // or from wherever it is defined
import org.junit.*;
public class PalindromeTest extends junit.framework.TestCase {
Line 170 ⟶ 996:
@Test
public void testNonrecursivePali() throws Exception {
assertEqualsassertTrue(pali("abcba"), true);
assertEqualsassertTrue(pali("aa"), true);
assertEqualsassertTrue(pali("a"), true);
assertEqualsassertTrue(pali(""), true);
assertEqualsassertFalse(pali("ab"), false);
assertEqualsassertFalse(pali("abcdba"), false);
}
/**
Line 182 ⟶ 1,008:
@Test
public void testRecursivePali() throws Exception {
assertEqualsassertTrue(rPali("abcba"), true);
assertEqualsassertTrue(rPali("aa"), true);
assertEqualsassertTrue(rPali("a"), true);
assertEqualsassertTrue(rPali(""), true);
assertEqualsassertFalse(rPali("ab"), false);
assertEqualsassertFalse(rPali("abcdba"), false);
}
 
Line 197 ⟶ 1,023:
//some code that should throw a WhateverException
}
}</langsyntaxhighlight>
Most [[IDE]]s that support Java will have JUnit built in or will have an easy-to-use plugin for it. For those that don't use these IDEs, test classes can be run from a normal main method and their results will print to standard output:
<syntaxhighlight lang="java5">public class RunTests{
public static main(String[] args){
org.junit.runner.JUnitCore.runClasses(PalindromeTest.class/*, other classes here if you have more*/);
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
===ES6===
 
{{libheader|Mocha}}
{{works with|Node.js}}
{{works with|Browserify}}
 
<syntaxhighlight lang="javascript">const assert = require('assert');
 
describe('palindrome', () => {
const pali = require('../lib/palindrome');
 
describe('.check()', () => {
it('should return true on encountering a palindrome', () => {
assert.ok(pali.check('racecar'));
assert.ok(pali.check('abcba'));
assert.ok(pali.check('aa'));
assert.ok(pali.check('a'));
});
 
it('should return true on encountering an empty string', () => {
assert.ok(pali.check(''));
});
 
it('should return false on encountering a non-palindrome', () => {
assert.ok(!pali.check('alice'));
assert.ok(!pali.check('ab'));
assert.ok(!pali.check('abcdba'));
});
})
});</syntaxhighlight>
 
Output:
<pre>$ ls -R
.:
lib/ test/
 
./lib:
palindrome.js
 
./test:
test.js
$ mocha --harmony
 
palindrome
.check()
✓ should return true on encountering a palindrome
✓ should return true on encountering an empty string
✓ should return false on encountering a non-palindrome
 
 
3 passing (18ms)
 
$
</pre>
 
=={{header|jq}}==
 
The jq command has an option (--run-tests) for running functional tests.
 
Each test case is presented on STDIN in the form of a triplet of adjacent lines as follows:
# a jq expression on one line (possibly including a trailing comment)
# input on one line
# expected output in "compressed" form (one or more lines)
 
Comment-lines (lines beginning with #) and blank lines may be inserted between triplets.
 
A test case can include jq function definitions, but each test case is executed in isolation.
 
Here is an example of a file with four test case triplets:
<syntaxhighlight lang="sh"># Test case 1:
.
1
1
 
# Test case 2:
1+1
null
2
 
# Test case 3 (with the wrong result):
1+1
null
0
 
# A test case with a function definition:
def factorial: if . <= 0 then 1 else . * ((. - 1) | factorial) end; factorial
3
6</syntaxhighlight>
 
If the file is named, say, test.txt, then the tests can be run by executing: jq --run-tests < test.txt
 
jq 1.4 produces very verbose output because an execution trace is included. In this article, only the key output lines are shown. The output that results from running the four test cases above is, in abbreviated form, as follows:
<syntaxhighlight lang="sh">$ jq --run-tests < jq.tests
 
Testing '.' at line number 3
Testing '1+1' at line number 8
Testing '1+1' at line number 13
*** Expected 0, but got 2 for test at line number 15: 1+1
Testing 'def factorial: if . <= 0 then 1 else . * ((. - 1) | factorial) end; factorial' at line number 18
3 of 4 tests passed (0 malformed)
</syntaxhighlight>
===Testing jq Libraries===
 
For tests of jq libraries, the "import" command can be used if your jq supports it.
(The import command is not available in jq 1.4.)
 
For example, suppose the file library.jq contains the following definitions:
 
<syntaxhighlight lang="jq">def factorial: if . <= 0 then 1 else . * ((. - 1) | factorial) end;
 
def palindrome: explode as $in | ($in|reverse) == $in;</syntaxhighlight>
 
and that the file test-library.txt contains the two test triplets:
 
<syntaxhighlight lang="sh">import "library" as lib; lib::factorial
3
6
 
import "library" as lib; lib::palindrome
"salàlas"
true</syntaxhighlight>
 
Then the tests can be run by invoking jq in the usual way:
<syntaxhighlight lang="sh">jq --run-tests < test-library.txt</syntaxhighlight>
 
=={{header|Jsish}}==
 
Jsi includes unit testing in the implementation of '''jsish'''. Jsi encourages unit testing.
 
Given the [[Palindrome detection]] solution of
 
<syntaxhighlight lang="javascript">/* Palindrome detection, in Jsish */
function isPalindrome(str:string, exact:boolean=true) {
if (!exact) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return str === str.split('').reverse().join('');
}</syntaxhighlight>
 
''jsish'' allows adding ''echo mode'' lines, (which are lines with a semi-colon ; in column 1 followed by an expression, with a closing semi-colon)
 
For example:
 
<pre>;isPalindrome('BUB');
;isPalidrome('CUB');</pre>
 
These lines are echoed in a special format of '''expression ==> result''' when turned up.
 
<pre>prompt$ jsish --U palindrome.jsi
isPalindrome('BUB') ==> true
isPalindrome('CUB') ==> false</pre>
 
Ok, looks good so far. ''jsish'' also has a run unit tests mode, <code>-u</code>. Along with running basic "did the program crash", -u also handles special comment sections for expectations.
 
<syntaxhighlight lang="javascript">/* Palindrome detection, in Jsish */
function isPalindrome(str:string, exact:boolean=true) {
if (!exact) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return str === str.split('').reverse().join('');
}
 
;isPalindrome('BUB');
;isPalindrome('CUB');
 
/*
=!EXPECTSTART!=
isPalindrome('BUB') ==> true
isPalindrome('CUB') ==> false
=!EXPECTEND!=
*/</syntaxhighlight>
 
Giving
 
<pre>prompt$ jsish -u palindrome.jsi
[PASS] palindrome.jsi</pre>
 
Tests pass. The echo lines are captured, compared, and on failure will show a diff fragment.
 
Changing the expectation of CUB to true, gives
 
<pre>prompt$ jsish palindrome.jsi
[FAIL] palindrome.jsi
at line 2 of output:
output: <isPalindrome('CUB') ==> false>
expect: <isPalindrome('CUB') ==> true>
====================DIFFSTART
isPalindrome('BUB') ==> true
-isPalindrome('CUB') ==> true
+isPalindrome('CUB') ==> false
 
====================DIFFEND</pre>
 
CUB is not an exact palindrome. Putting that back to pass again, and adding a few other tests, in particular for exercising the ''exact palindrome'' flag that ignores case and punctuation when set to false. (A false palindrome so to speak).
 
<syntaxhighlight lang="javascript">/* Palindrome detection, in Jsish */
function isPalindrome(str:string, exact:boolean=true) {
if (!exact) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return str === str.split('').reverse().join('');
}
 
;isPalindrome('BUB');
;isPalindrome('CUB');
;isPalindrome('Bub');
;isPalindrome('Bub', false);
;isPalindrome('Never odd or even', false);
;isPalindrome('In girum imus nocte et consumimur igni', false);
;isPalindrome('A man, a plan, a canal; Panama!', false);
;isPalindrome('A man, a plan, a canal; Panama!', true);
 
/*
=!EXPECTSTART!=
isPalindrome('BUB') ==> true
isPalindrome('CUB') ==> false
=!EXPECTEND!=
*/</syntaxhighlight>
 
That's all good, but after looking at --U output, it'll mean an extra edit to the file to add the expectations. ''jsish'' to the rescue, and automatically updating the expectation block:
 
<pre>prompt$ jsish -u -update true palindrom.jsi
Updated palindrome.jsi</pre>
 
Which now looks like
 
<syntaxhighlight lang="javascript">/* Palindrome detection, in Jsish */
function isPalindrome(str:string, exact:boolean=true) {
if (!exact) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return str === str.split('').reverse().join('');
}
 
;isPalindrome('BUB');
;isPalindrome('CUB');
;isPalindrome('Bub');
;isPalindrome('Bub', false);
;isPalindrome('Never odd or even', false);
;isPalindrome('In girum imus nocte et consumimur igni', false);
;isPalindrome('A man, a plan, a canal; Panama!', false);
;isPalindrome('A man, a plan, a canal; Panama!', true);
 
/*
=!EXPECTSTART!=
isPalindrome('BUB') ==> true
isPalindrome('CUB') ==> false
isPalindrome('Bub') ==> false
isPalindrome('Bub', false) ==> true
isPalindrome('Never odd or even', false) ==> true
isPalindrome('In girum imus nocte et consumimur igni', false) ==> true
isPalindrome('A man, a plan, a canal; Panama!', false) ==> true
isPalindrome('A man, a plan, a canal; Panama!', true) ==> false
=!EXPECTEND!=
*/</syntaxhighlight>
 
Easy peasy. ''Maybe too easy'', auto update of unit tests should not be run until you know you have valid tests and known results. If there is a bug in the expression, <code>-u -update true</code> will gladly update a script with invalid results. The <code>-update true</code> feature should be treated as a helper, not a "turn off brain now" crutch. Very handy on initial create, or when messaging changes in code under test, but to be treated with respect and care.
 
Echo lines are not the only thing captured by <code>jsish -u</code> mode. All outputs are captured and can be compared in the EXPECT block. It even allows for sample input testing.
 
<syntaxhighlight lang="javascript">/* Interaction testing */
var trial = console.input();
puts(trial.replace(/a/g, 'b'));
 
/*
=!INPUTSTART!=
abccba
=!INPUTEND!=
*/
 
/*
=!EXPECTSTART!=
bbccbb
=!EXPECTEND!=
*/</syntaxhighlight>
 
{{out}}
<pre>prompt$ jsish -u test-inputs.jsi
[PASS] test-inputs.jsi
 
prompt$ jsish test-inputs.jsi
abc from user
bbc from user</pre>
 
When in unit testing, no user input was required, the <code>console.input()</code> method was fed data from the INPUTSTART block.
When not in unit testing mode, code just runs as expected, and data was accepted from the keyboard.
 
You can ship ''jsish'' scripts with tests in and they will be ignored during normal operation. If that seems appropriate for purpose and not a max-speed production deployment.
 
There is more support for unit testing in Jsi. <code>assert(expression)</code> is ignored by default, and turned on during test mode for instance. Entire directories can be evaluated in test mode, by naming a directory instead of a filename.
 
See https://jsish.org/doc/Testing.html for more details.
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">using Base.Test
include("Palindrome_detection.jl")
 
# Simple test
@test palindrome("abcdcba")
@test !palindrome("abd")
 
# Test sets
@testset "palindromes" begin
@test palindrome("aaaaa")
@test palindrome("abcba")
@test palindrome("1")
@test palindrome("12321")
end
 
@testset "non-palindromes" begin
@test !palindrome("abc")
@test !palindrome("a11")
@test !palindrome("012")
end</syntaxhighlight>
 
{{out}}
<pre>Test Summary: | Pass Total
palindromes | 4 4
Test Summary: | Pass Total
non-palindromes | 3 3</pre>
 
=={{header|Kotlin}}==
Kotlin can use various JVM testing frameworks including its own kotlin-test module. However, for simple cases, it is easier to use the 'assert' function built into its standard library which will throw an AssertionError if the condition is false and assertions are enabled using java's -ea option when the application is run:
<syntaxhighlight lang="scala">// version 1.1.3
 
fun isPalindrome(s: String) = (s == s.reversed())
 
fun main(args: Array<String>) {
val testCases = listOf("racecar", "alice", "eertree", "david")
for (testCase in testCases) {
try {
assert(isPalindrome(testCase)) { "$testCase is not a palindrome" }
}
catch (ae: AssertionError) {
println(ae.message)
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
alice is not a palindrome
david is not a palindrome
</pre>
 
=={{header|Lasso}}==
The following example uses the [https://bitbucket.org/bfad/lspec/ LSpec Library]:
 
<syntaxhighlight lang="lasso">// Taken from the Lasso entry in Palindrome page
define isPalindrome(text::string) => {
local(_text = string(#text)) // need to make copy to get rid of reference issues
#_text -> replace(regexp(`(?:$|\W)+`), -ignorecase)
local(reversed = string(#_text))
#reversed -> reverse
return #_text == #reversed
}
 
// The tests
describe(::isPalindrome) => {
it(`throws an error when not passed a string`) => {
expect->error =>{
isPalindrome(43)
}
}
 
it(`returns true if the string is the same forward and backwords`) => {
expect(isPalindrome('abba'))
}
 
it(`returns false if the string is different forward and backwords`) => {
expect(not isPalindrome('aab'))
}
 
it(`ignores spaces and punctuation`) => {
expect(isPalindrome(`Madam, I'm Adam`))
}
}
 
// Run the tests and get the summary
// (This normally isn't in the code as the test suite is run via command-line.)
lspec->stop</syntaxhighlight>
 
{{out}}
<pre>....
 
Finished in 0.157030 seconds
4 tests, 0 failures</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">assert( ispalindrome("ABCBA") )
assert( ispalindrome("ABCDE") )</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">myFun[x_] := Block[{y},y = x^2; Assert[y > 5]; Sin[y]]
On[Assert];myFun[1.0]</syntaxhighlight>
{{out}}
<pre>Assert::asrtf: Assertion y>5 failed.
0.841471</pre>
 
=={{header|NetRexx}}==
{{libheader|JUnit}}
<syntaxhighlight lang="netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols binary
 
import junit.framework.TestCase
import RCPalindrome
 
class RCTestAFunction public final extends TestCase
 
method setUp public
return
 
method tearDown public
return
 
method testIsPal public signals AssertionError
 
assertTrue(RCPalindrome.isPal(Rexx 'abcba'))
assertTrue(RCPalindrome.isPal(Rexx 'aa'))
assertTrue(RCPalindrome.isPal(Rexx 'a'))
assertTrue(RCPalindrome.isPal(Rexx ''))
assertFalse(RCPalindrome.isPal(Rexx 'ab'))
assertFalse(RCPalindrome.isPal(Rexx 'abcdba'))
 
return
 
method except signals RuntimeException
signal RuntimeException()
 
method main(args = String[]) public constant
 
testResult = org.junit.runner.JUnitCore.runClasses([RCTestAFunction.class])
secs = Rexx testResult.getRunTime / 1000.0
 
if testResult.wasSuccessful then say 'Tests successful'
else say 'Tests failed'
say ' failure count:' testResult.getFailureCount
say ' ignore count:' testResult.getIgnoreCount
say ' run count:' testResult.getRunCount
say ' run time:' secs.format(null, 3)
 
return
</syntaxhighlight>
;Output
<pre>
Tests successful
failure count: 0
ignore count: 0
run count: 1
run time: 0.015
</pre>
 
=={{header|Nim}}==
Using assertions (no output means all tests were correct; this only works with debug builds!):
<syntaxhighlight lang="nim">proc reversed(s: string): string =
result = newString(s.len)
for i, c in s:
result[s.high - i] = c
 
proc isPalindrome(s: string): bool =
s == reversed(s)
 
when isMainModule:
assert(isPalindrome(""))
assert(isPalindrome("a"))
assert(isPalindrome("aa"))
assert(not isPalindrome("baa"))
assert(isPalindrome("baab"))
assert(isPalindrome("ba_ab"))
assert(not isPalindrome("ba_ ab"))
assert(isPalindrome("ba _ ab"))
assert(not isPalindrome("abab"))</syntaxhighlight>
 
Using the “unittest” module:
<syntaxhighlight lang="nim">import unittest
 
proc reversed(s: string): string =
result = newString(s.len)
for i, c in s:
result[s.high - i] = c
 
proc isPalindrome(s: string): bool =
s == reversed(s)
 
when isMainModule:
suite "palindrome":
test "empty string":
check isPalindrome ""
 
test "string of length 1":
check isPalindrome "a"
 
test "string of length 2":
check isPalindrome "aa"
 
test "string of length 3":
check isPalindrome "aaa"
 
test "no palindrome":
check isPalindrome("foo") == false</syntaxhighlight>
 
{{out}}:
<pre>[OK] empty string
 
[OK] string of length 1
 
[OK] string of length 2
 
[OK] string of length 3
 
[OK] no palindrome</pre>
 
=={{header|OCaml}}==
 
Using the library [http://www.xs4all.nl/~mmzeeman/ocaml/ OUnit].
 
The module '''Palindrome''' is where are compiled the two functions ''is_palindrome'' and ''rem_space'' from [[Palindrome#OCaml|this page]]. We put these two functions in a file named ''palindrome.ml'' and compile it with ''ocamlc -c palindrome.ml -o palindrome.cmo'', then with the code below in the file ''palindrome_tests.ml'' we execute the tests with this command line:
ocaml unix.cma -I +oUnit oUnit.cma palindrome.cmo palindrome_tests.ml
 
<syntaxhighlight lang="ocaml">open OUnit
open Palindrome
 
let test_palindrome_1 _ =
assert_equal true (is_palindrome "aba")
 
let test_palindrome_2 _ =
assert_equal true (is_palindrome "abba")
 
let test_palindrome_3 _ =
assert_equal true (is_palindrome "abacidAdicaba")
 
let test_palindrome_4 _ =
assert_equal false (is_palindrome "xREty5kgPMO")
 
let test_palindrome_5 _ =
assert_equal true (is_palindrome(rem_space "in girum imus nocte et consumimur igni"))
 
 
let suite = "Test Palindrome" >::: ["test_palindrome_1" >:: test_palindrome_1;
"test_palindrome_2" >:: test_palindrome_2;
"test_palindrome_3" >:: test_palindrome_3;
"test_palindrome_4" >:: test_palindrome_4;
"test_palindrome_5" >:: test_palindrome_5]
let _ =
run_test_tt_main suite</syntaxhighlight>
 
=={{header|Odin}}==
 
<syntaxhighlight lang="odin">// To run execute: odin test Test_a_function.odin -file
package main
 
import "core:testing"
import "core:strings"
 
is_palindrome :: proc(s: string) -> bool {
return s == strings.reverse(s)
}
 
@(test)
test_is_palindrome :: proc(t: ^testing.T) {
palindromes := []string{"", "a", "aa", "aba", "racecar"}
for i in palindromes {
if !is_palindrome(i) {
testing.errorf(t, "is_palindrome returned false on palindrome %s", i)
}
}
 
non_palindromes := []string{"ab", "abaa", "aaba", "abcdba"}
for i in non_palindromes {
if is_palindrome(i) {
testing.errorf(t, "is_palindrome returned true on non-palindrome %s", i)
}
}
}</syntaxhighlight>
 
=={{header|Oforth}}==
Unit tests are a built-in functionality. If Oforth is run using --t option, all tests are checked. Otherwise, tests are not checked :
<syntaxhighlight lang="oforth">test: [ "abcd" isPalindrome ]
test: ["abba" isPalindrome ]
test: [ "abcba" isPalindrome ]</syntaxhighlight>
 
=={{header|PARI/GP}}==
PARI/GP comes with a testing framework for gp. Testing the palindrome function (if converted with gp2c and added to PARI) would consist of adding lines like
<pre>? ispal("abc")
0
? ispal("aba")
1</pre>
 
=={{header|Pascal}}==
See [[Test_a_function#Delphi | Delphi]]
 
=={{header|PascalABC.NET}}==
Run this program in IDE using Ctrl-Shift-T.
 
<syntaxhighlight lang="delphi">
uses NUnitABC;
 
function IsPalindrome(s: string) := s = s[::-1];
 
[Test]
procedure Test1;
begin
Assert.AreEqual(IsPalindrome('abba'),True);
Assert.AreNotEqual(IsPalindrome('abcd'),True);
end;
 
begin
end.
</syntaxhighlight>
{{out}}
<pre>
Итог теста
Summary: Tests passed
Number of tests: 1 , Passed: 1 , Not passed: 0 , Warnings: 0
Test started : 8:02:38
Test completed : 8:02:38
Duration: 0.006 sec
</pre>
 
 
=={{header|Perl}}==
A test file, with a ''.t'' suffix, is just a Perl program that prints a test stream in [http://testanything.org/wiki/index.php/Main_Page Test Anything Protocol] (TAP). There are modules to help print things in the correct TAP format; Perl 5 bundles Test, Test::Simple and Test::More.
 
This example uses Test and requires the ''Palindrome.pm'' file from [[Palindrome detection#Perl]].
 
{{trans|Raku}}
 
<syntaxhighlight lang="perl"># ptest.t
use strict;
use warnings;
 
use Test;
 
my %tests;
BEGIN {
# plan tests before loading Palindrome.pm
%tests = (
'A man, a plan, a canal: Panama.' => 1,
'My dog has fleas' => 0,
"Madam, I'm Adam." => 1,
'1 on 1' => 0,
'In girum imus nocte et consumimur igni' => 1,
'' => 1,
);
 
# plan 4 tests per string
plan tests => (keys(%tests) * 4);
}
 
use Palindrome;
 
for my $key (keys %tests) {
$_ = lc $key; # convert to lowercase
s/[\W_]//g; # keep only alphanumeric characters
 
my $expect = $tests{$key};
my $note = ("\"$key\" should " . ($expect ? '' : 'not ') .
"be a palindrome.");
 
ok palindrome == $expect, 1, "palindrome: $note";
ok palindrome_c == $expect, 1, "palindrome_c: $note";
ok palindrome_r == $expect, 1, "palindrome_r: $note";
ok palindrome_e == $expect, 1, "palindrome_e: $note";
}</syntaxhighlight>
 
The program produces TAP output.
 
<pre>$ perl ptest.t
1..24
# Running under perl version 5.010001 for openbsd
# Current time local: Mon Jan 31 17:44:06 2011
# Current time GMT: Mon Jan 31 22:44:06 2011
# Using Test.pm version 1.25_02
ok 1
ok 2
ok 3
...
ok 24</pre>
 
The first line '1..24' plans for 24 tests, so that one can detect if the script aborted itself before it finished all the tests. A line like 'ok 1' is a passing test, 'not ok 1' is a failing test.
 
To find those 'not ok' lines, one can run a TAP harness. The "prove" program is a TAP harness that comes with Perl. (By default, "prove" runs all the test files in the "t" subdirectory. I did not make "t", so I will run "prove ptest.t" instead.)
 
<pre>$ prove ptest.t
ptest.t .. ok
All tests successful.
Files=1, Tests=24, 0 wallclock secs ( 0.00 usr 0.02 sys + 0.01 cusr 0.00 csys = 0.03 CPU)
Result: PASS</pre>
 
All the tests passed! But suppose that palindrome_e() returned the wrong answer for the empty string "", then a test would fail.
 
<pre>$ perl ptest.t
...
ok 3
not ok 4
# Test 4 got: "" (ptest.t at line 36)
# Expected: "1" (palindrome_e: "" should be a palindrome.)
# ptest.t line 36 is: ok palindrome_e == $expect, 1, "palindrome_e: $note";
ok 5
...
</pre>
 
<pre>$ prove ptest.t
ptest.t .. 1/24 # Test 4 got: "" (ptest.t at line 36)
# Expected: "1" (palindrome_e: "" should be a palindrome.)
# ptest.t line 36 is: ok palindrome_e == $expect, 1, "palindrome_e: $note";
ptest.t .. Failed 1/24 subtests
 
Test Summary Report
-------------------
ptest.t (Wstat: 0 Tests: 24 Failed: 1)
Failed test: 4
Files=1, Tests=24, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.02 cusr 0.00 csys = 0.05 CPU)
Result: FAIL</pre>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
The only golden rule here is to invoke test_summary() after any [block of] tests have been run, everything else is optional.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">is_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">==</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #000080;font-style:italic;">--set_test_verbosity(TEST_QUIET) -- default, no output when third call removed
--set_test_verbosity(TEST_SUMMARY) -- first and last line only [w or w/o ""]
--set_test_verbosity(TEST_SHOW_FAILED) -- first and last two lines only</span>
<span style="color: #7060A8;">set_test_verbosity</span><span style="color: #0000FF;">(</span><span style="color: #004600;">TEST_SHOW_ALL</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- as shown in last two cases below
--set_test_abort(TEST_ABORT) -- abort(1) on failure, after showing the summary
--set_test_abort(TEST_QUIET) -- quietly carry on, the default
--set_test_abort(TEST_CRASH) -- abort immmediately on failure (w/o summary)
--set_test_pause(TEST_PAUSE_FAIL) -- pause on failure, the default
--set_test_pause(TEST_QUIET) -- disable pause on failure
--set_test_pause(TEST_PAUSE) -- always pause</span>
<span style="color: #7060A8;">set_test_module</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"palindromes"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- optional, w/o first line is omitted</span>
<span style="color: #7060A8;">test_true</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abba"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"abba"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">test_true</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abba"</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- no desc makes success hidden...
-- ...and failure somewhat laconic</span>
<span style="color: #7060A8;">test_false</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"not abc"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">test_true</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_palindrome</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"failure"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"failure"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">test_summary</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
Note the default behaviour, if set_test_verbosity() is not invoked, is no output and carry on as normal when all tests pass.<br>
Also note that set_test_pause() has no effect under pwa/p2js and the program will always carry on regardless of any failure, <br>
unless set_test_abort() other than TEST_QUIET is in force. You can of course invoke these routines in a platform-dependent <br>
way, for instance pause on the desktop but abort/stop completely in the browser, should that help.
 
Should you invoke a bunch of tests but forget the final test_summary(), then apart from set_test_abort(TEST_CRASH) any
failures ''will'' slip by completely unnoticed.
{{out}}
The following shows example output from various set_test_verbosity() settings, with and without any failures.
 
TEST_QUIET with a failing test (no output or pause if no failing tests)<br>
TEST_SUMMARY with a failure, same output
<pre>
palindromes:
 
4 tests run, 3 passed, 1 failed, 75% success
Press any key to continue...
</pre>
TEST_SUMMARY with no failure (and without set_test_pause(TEST_PAUSE))<br>
TEST_SHOW_FAILED with no failure, ditto
<pre>
palindromes:
 
3 tests run, 3 passed, 0 failed, 100% success
</pre>
TEST_SHOW_FAILED with a failure
<pre>
palindromes:
failed: failure
 
4 tests run, 3 passed, 1 failed, 75% success
Press any key to continue...
</pre>
TEST_SHOW_ALL with a failure
<pre>
palindromes:
passed: abba
passed: not abc
failed: failure
 
4 tests run, 3 passed, 1 failed, 75% success
Press any key to continue...
</pre>
TEST_SHOW_ALL with no failure (and without set_test_pause(TEST_PAUSE))
<pre>
palindromes:
passed: abba
passed: not abc
 
3 tests run, 3 passed, 0 failed, 100% success
</pre>
 
=={{header|PicoLisp}}==
The '[http://software-lab.de/doc/refT.html#test test]' function is
built into PicoLisp.
<syntaxhighlight lang="picolisp">(de palindrome? (S)
(= (setq S (chop S)) (reverse S)) )
 
(test T (palindrome? "racecar"))
(test NIL (palindrome? "ferrari"))</syntaxhighlight>
 
=={{header|Prolog}}==
SWI-Prolog has an inbuilt unit test functionality which is run automatically when building.
 
It can also be run by using the run_tests predicate.
 
<syntaxhighlight lang="prolog">palindrome(Word) :- name(Word,List), reverse(List,List).
 
:- begin_tests(palindrome).
 
test(valid_palindrome) :- palindrome('ingirumimusnocteetconsumimurigni').
test(invalid_palindrome, [fail]) :- palindrome('this is not a palindrome').
 
:- end_tests(palindrome).</syntaxhighlight>
 
=={{header|PureBasic}}==
PureBasic allows for definition of Assert() and other tools & the debugger is integrated into the native editor.
<syntaxhighlight lang="purebasic">Macro DoubleQuote
; Needed for the Assert-Macro below
" ; " second dlbquote to prevent Rosettas misshighlighting of following code. Remove comment before execution!
EndMacro
Macro Assert(TEST,MSG="")
CompilerIf #PB_Compiler_Debugger
If Not (TEST)
If MSG<>"": Debug MSG: EndIf
Temp$="Fail: "+DoubleQuote#TEST#DoubleQuote
Debug Temp$+", Line="+Str(#PB_Compiler_Line)+" in "+#PB_Compiler_File
CallDebugger
EndIf
CompilerEndIf
EndMacro
 
Procedure IsPalindrome(StringToTest.s)
If StringToTest=ReverseString(StringToTest)
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
 
text1$="racecar"
text2$="wisconsin"
Assert(IsPalindrome(text1$), "Catching this would be a fail")
Assert(IsPalindrome(text2$), "Catching this is correct")</syntaxhighlight>
 
=={{header|Python}}==
This uses the [[wp:doctest|doctest]] module from the Python standard library. This allows copies of tests run in an interactive session to be re-used as tests.
 
<langsyntaxhighlight lang="python">def is_palindrome(s):
'''
>>> is_palindrome('')
Line 240 ⟶ 1,935:
 
if __name__ == "__main__":
_test() </langsyntaxhighlight>
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:
 
Line 319 ⟶ 2,014:
14 passed and 0 failed.
Test passed.</pre>
 
=={{header|Quackery}}==
 
Quackery does not have a testing specific library well known to the language's community.
 
=={{header|R}}==
{{libheader|RUnit}}
 
See also the functions defineTestSuite and runTestSuite.
<syntaxhighlight lang="r">checkTrue(palindroc("aba")) # TRUE
checkTrue(!palindroc("ab")) # TRUE
checkException(palindroc()) # TRUE
checkTrue(palindroc("")) # Error. Uh-oh, there's a bug in the function</syntaxhighlight>
 
=={{header|Racket}}==
 
Racket has a built-in unit testing library. Tests can be specified next to function implementations or in a testing submodule.
 
<syntaxhighlight lang="racket">
#lang racket
(module+ test (require rackunit))
 
;; from the Palindrome entry
(define (palindromb str)
(let* ([lst (string->list (string-downcase str))]
[slst (remove* '(#\space) lst)])
(string=? (list->string (reverse slst)) (list->string slst))))
 
;; this test module is not loaded unless it is
;; specifically requested for testing, allowing internal
;; unit test specification
(module+ test
(check-true (palindromb "racecar"))
(check-true (palindromb "avoova"))
(check-false (palindromb "potato")))
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>use Test;
 
sub palin( Str $string) { so $string.lc.comb(/\w/) eq $string.flip.lc.comb(/\w/) }
 
for
'A man, a plan, a canal: Panama.' => True,
'My dog has fleas' => False,
"Madam, I'm Adam." => True,
'1 on 1' => False,
'In girum imus nocte et consumimur igni' => True,
'' => True
{
my ($test, $expected-result) = .kv;
is palin($test), $expected-result,
"\"$test\" is {$expected-result??''!!'not '}a palindrome.";
}
 
done-testing;</syntaxhighlight>
{{out}}
<pre>1..6
ok 1 - "1 on 1" is not a palindrome.
ok 2 - "My dog has fleas" is not a palindrome.
ok 3 - "A man, a plan, a canal: Panama." is a palindrome.
ok 4 - "" is a palindrome.
ok 5 - "Madam, I'm Adam." is a palindrome.
ok 6 - "In girum imus nocte et consumimur igni" is a palindrome.</pre>
 
=={{header|Retro}}==
Retro includes a library for creating automated tests. This is used for checking the standard libraries shipped with Retro.
 
<syntaxhighlight lang="retro">needs assertion'
needs hash'
 
: palindrome? ( $-f ) dup ^hash'hash [ ^strings'reverse ^hash'hash ] dip = ;
 
with assertion'
: t0 ( - ) "hello" palindrome? 0 assert= ; assertion
: t1 ( - ) "ingirumimusnocteetconsumimurigni" palindrome? -1 assert= ; assertion
: test ( - ) t0 t1 ;
test</syntaxhighlight>
 
=={{header|REXX}}==
There is no official suite of programs, but the writers of the various REXX interpreters each have their own.
<br><br>Listed below are two such examples that are used to "stress" the interpreter.
===stress REXX keywords (used as variables)===
{This was originally written in some form of FORTRAN.}
<syntaxhighlight lang="rexx">/*REXX program stresses various REXX functions (BIFs), many BIFs are used as variables. */
signal=(interpret=value); value=(interpret<parse); do upper=value to value; end
exit=upper*upper*upper*upper-value-upper; say=' '; return=say say say; with.=signal
do then=value to exit; pull=''; do otherwise= upper to then-,
value; select=otherwise-value; if.otherwise=with.otherwise+with.select; end
if.value=value; if.then=value; do otherwise=value to exit-then; pull=pull,
say''say; end; do otherwise=value to then; pull=pull center(if.otherwise,,
length(return)); end; say pull; do otherwise=value to exit; with.otherwise=,
if.otherwise; end; end; exit 0 /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=''':'''}}
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
</pre>
 
===stress test some REXX BIFs===
This stress tests some of the REXX built-in functions (BIFs).
<syntaxhighlight lang="rexx">/*REXX program shows a secret message to the terminal by using REXX built─in functions. */
z.=' '; z= 12-25-2002; y= z; w= -y
z.0= translate( right( time('c'), substr(z, 4, z==y)))
z.1= left( substr( format(z, 2, z==y, , z==y.1), 5), z==y)
z.2= copies( right( symbol('z.'20), z==y), left(w, 1))
z.3= translate( right( date('w'), z==y))
z.5= right( form(), z==y)
z.6= x2c( d2x( x2d( c2x( substr( symbol( substr(z, 2)), 2, z==y))) - 1))
z.7= right( symbol('z.' || (z\==z) ), z==y)
z.8= substr( form(), (z==y) + left(w, 1), z==y)
z.9= reverse( left( form(), z==y))
z.10= left( substr( form(), 6), z==y)
z.11= right( datatype(z), z==y)
z.12= substr( symbol(left(z, z=z)), left(w, 1), z==y)
z.13= left( form(), z==y)
 
do z=-31 to 31; z.32= z.32 || z.z; end
say
say z.32
say
exit 0 /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=''':'''}}
<pre>
 
MERRY CHRISTMAS
 
</pre>
Because the REXX language is interpreted, many REXX stress tests are "simple" programs like those listed above, albeit highly obfuscated.
<br><br>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
assert(IsPalindrome("racecar"))
assert(IsPalindrome("alice"))
</syntaxhighlight>
 
=={{header|Ruby}}==
 
{{libheader|test/unit.rb}}
===test/unit===
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.
Ruby comes with a 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)
 
<syntaxhighlight lang="ruby">def palindrome?(s)
s == s.reverse
end
Line 348 ⟶ 2,193:
assert(palindrome?("ab"), "this test case fails on purpose")
end
end</langsyntaxhighlight>
 
<pre>$ ruby palindrome.rb
Loaded suite palindrome
Line 361 ⟶ 2,207:
 
5 tests, 5 assertions, 1 failures, 0 errors</pre>
 
===minitest===
Many Ruby hackers have switched from 'test/unit' to other testing libraries. Some of these libraries provide the 'describe' block, which is just a pretty way to make a test case. [http://rspec.info/ RSpec], [https://github.com/chneukirchen/bacon Bacon] and [http://bfts.rubyforge.org/minitest/ minitest] are such libraries.
 
This example uses Minitest, which comes with Ruby 1.9. (But if you have Ruby 1.8, then you can still install Minitest as a gem, using [[:Category:RubyGems|RubyGems]].)
 
<syntaxhighlight lang="ruby"># palindrome.rb
def palindrome?(s)
s == s.reverse
end
require 'minitest/spec'
require 'minitest/autorun'
describe "palindrome? function" do
it "returns true if arg is a palindrome" do
(palindrome? "aba").must_equal true
end
it "returns false if arg is not a palindrome" do
palindrome?("ab").must_equal false
end
 
it "raises NoMethodError if arg is without #reverse" do
proc { palindrome? 42 }.must_raise NoMethodError
end
 
it "raises ArgumentError if wrong number of args" do
proc { palindrome? "a", "b" }.must_raise ArgumentError
end
 
it "passes a failing test" do
palindrome?("ab").must_equal true, "this test case fails on purpose"
end
end</syntaxhighlight>
 
<pre>$ ruby19 palindrome.rb
Loaded suite palindrome
Started
..F..
Finished in 0.000629 seconds.
 
1) Failure:
test_0005_passes_a_failing_test(PalindromeFunctionSpec) [palindrome.rb:26]:
this test case fails on purpose.
Expected false, not true.
 
5 tests, 5 assertions, 1 failures, 0 errors, 0 skips
 
Test run options: --seed 40770</pre>
 
=={{header|Rust}}==
Rust supports two ways of writing unit tests out of the box. Most tests are written
as functions with <code>#[test]</code> attribute. When running <code>cargo test</code>
(Cargo is the build and package manager), these functions are executed. The test
functions are usually placed in a separate module and excluded from regular builds;
as the example shows, the whole module with tests has <code>#[cfg(test)]</code>
attribute which has this effect.
 
The other way is using documentation comments. The documentation can contain code
snippets and <code>rustdoc</code> (or <code>cargo</code>) compiles and runs these
code snippets as tests (it is possible to specify the expected outcome, including
the situation that the snippet should fail to compile). Note that the documentation
tests work for libraries only (as for Rust 1.46) and are not run for binary crates.
 
In either case, the tests are supposed to use <code>assert!</code> macro (and its
variants <code>assert_eq!</code> and <code>assert_ne!</code>) to check the expected
outcomes. These macros can be used in regular code as well and always execute. When
an assertion should be checked in debug builds only, <code>debug_assert!</code> and
its variants can be used instead.
 
<syntaxhighlight lang="rust">/// Tests if the given string slice is a palindrome (with the respect to
/// codepoints, not graphemes).
///
/// # Examples
///
/// ```
/// # use playground::palindrome::is_palindrome;
/// assert!(is_palindrome("abba"));
/// assert!(!is_palindrome("baa"));
/// ```
pub fn is_palindrome(s: &str) -> bool {
let half = s.len();
s.chars().take(half).eq(s.chars().rev().take(half))
}
 
#[cfg(test)]
mod tests {
 
use super::is_palindrome;
 
#[test]
fn test_is_palindrome() {
assert!(is_palindrome("abba"));
}
}</syntaxhighlight>
 
While unit tests are written together with the tested code (and thus may employ the
white-box test approach), the code may be accompanied yet with integration tests,
conventionally provided in the <code>tests/</code> folder. Integration tests are
considered external and use the public interface as any other external code.
 
=={{header|Scala}}==
{{libheader|ScalaCheck}}
 
There are three main Scala testing libraries: ScalaCheck, ScalaTest and Specs. The first is
shown here, being similar to Haskell's QuickCheck.
 
<syntaxhighlight lang="scala">import org.scalacheck._
import Prop._
import Gen._
 
object PalindromeCheck extends Properties("Palindrome") {
property("A string concatenated with its reverse is a palindrome") =
forAll { s: String => isPalindrome(s + s.reverse) }
property("A string concatenated with any character and its reverse is a palindrome") =
forAll { (s: String, c: Char) => isPalindrome(s + c + s.reverse) }
property("If the first half of a string is equal to the reverse of its second half, it is a palindrome") =
forAll { (s: String) => s.take(s.length / 2) != s.drop((s.length + 1) / 2).reverse || isPalindrome(s) }
property("If the first half of a string is different than the reverse of its second half, it isn't a palindrome") =
forAll { (s: String) => s.take(s.length / 2) == s.drop((s.length + 1) / 2).reverse || !isPalindrome(s) }
}</syntaxhighlight>
 
Output:
 
<pre>
+ Palindrome.A string concatenated with its reverse is a palindrome: OK, pa
ssed 100 tests.
+ Palindrome.A string concatenated with any character and its reverse is a
palindrome: OK, passed 100 tests.
+ Palindrome.If the first half of a string is equal to the reverse of its s
econd half, it is a palindrome: OK, passed 100 tests.
+ Palindrome.If the first half of a string is different than the reverse of
its second half, it isn't a palindrome: OK, passed 100 tests.
</pre>
 
=={{header|Scheme}}==
 
{{libheader|Scheme/SRFIs}}
 
SRFI 64 is a popular test library.
 
<syntaxhighlight lang="scheme">
(import (srfi 64))
(test-begin "palindrome-tests")
(test-assert (palindrome? "ingirumimusnocteetconsumimurigni"))
(test-assert (not (palindrome? "This is not a palindrome")))
(test-equal #t (palindrome? "ingirumimusnocteetconsumimurigni")) ; another of several test functions
(test-end)
</syntaxhighlight>
 
The library reports the number of pass/fail tests at the end; the report may be customised.
Also, a detailed log file is created, showing the results of each test.
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
{{libheader|db2unit}}
<syntaxhighlight lang="sql pl">
CREATE OR REPLACE PROCEDURE TEST_MY_TEST()
BEGIN
DECLARE EXPECTED INTEGER;
DECLARE ACTUAL INTEGER;
CALL DB2UNIT.REGISTER_MESSAGE('My first test');
SET EXPECTED = 2;
SET ACTUAL = 1+1;
CALL DB2UNIT.ASSERT_INT_EQUALS('Same value', EXPECTED, ACTUAL);
END @
</syntaxhighlight>
Output:
<pre>
db2 => CALL DB2UNIT.RUN_SUITE('DB2UNIT_EXAMPLE');
 
 
Result set 1
--------------
 
TEST FINAL_STATE MICROSECONDS MESSAGE
---------------- ----------- ------------ ----------------------------------------------------------------
Before Suite - - Starting execution
TEST_MY_TEST Passed 29585 Executing TEST_MY_TEST
After Suite - - Finishing execution
- - 1 tests were executed
- - 1 tests passed
- - 0 tests failed
- - 0 tests with errors
 
7 record(s) selected.
 
 
Result set 2
--------------
 
TIME EXECUTION_ID STATUS MESSAGE
-------- ------------ --------------------- --------------------------------------------------------------
20:43:47 52613 Initialization db2unit is licensed under the terms of the GPL v3
20:43:47 52613 Initialization Execution of DB2UNIT_EXAMPLE with ID 52613
20:43:47 52613 Prepare Report The reports table created: DB2UNIT_EXAMPLE.REPORT_TESTS
20:43:48 52613 Calculating time Total execution time is: 0 seconds
 
4 record(s) selected.
 
Return Status = 0
</pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Cocoa
import XCTest
 
class PalindromTests: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testPalindrome() {
// This is an example of a functional test case.
XCTAssert(isPalindrome("abcba"), "Pass")
XCTAssert(isPalindrome("aa"), "Pass")
XCTAssert(isPalindrome("a"), "Pass")
XCTAssert(isPalindrome(""), "Pass")
XCTAssert(isPalindrome("ab"), "Pass") // Fail
XCTAssert(isPalindrome("aa"), "Pass")
XCTAssert(isPalindrome("abcdba"), "Pass") // Fail
}
func testPalindromePerformance() {
// This is an example of a performance test case.
self.measureBlock() {
var _is = isPalindrome("abcba")
}
}
}</syntaxhighlight>
 
=={{header|Tailspin}}==
<syntaxhighlight lang="tailspin">
templates palindrome
[$...] -> #
when <=$(last..first:-1)> do '$...;' !
end palindrome
 
test 'palindrome filter'
assert 'rotor' -> palindrome <='rotor'> 'rotor is a palindrome'
assert ['rosetta' -> palindrome] <=[]> 'rosetta is not a palindrome'
end 'palindrome filter'
</syntaxhighlight>
 
=={{header|Tcl}}==
Testing with Tcl is just about universally performed with the <tt>tcltest</tt> package, which was originally developed for testing Tcl itself, and which is a standard part of a <tt>[[tclsh]]</tt> installation.
 
{{libheader|tcltest}}
<langsyntaxhighlight lang="tcl">package require tcltest 2
source palindrome.tcl; # Assume that this is what loads the implementation of ‘palindrome’
 
tcltest::test palindrome-1 {check for palindromicity} -body {
palindrome abcdedcba
Line 374 ⟶ 2,477:
palindrome
} -returnCodes error -result "wrong # args: should be \"palindrome s\""
 
tcltest::cleanupTests</lang>
tcltest::cleanupTests</syntaxhighlight>
If placed in a file called <tt>palindrome.test</tt>, the following output is produced when it is executed:
<pre>palindrome.test: Total 3 Passed 3 Skipped 0 Failed 0</pre>
Note that only a small fraction of the features of the testing framework are demonstrated here. In particular, it does not show off management of conditional execution, the application of setup and cleanup code, and how these things are assembled into a whole test suite for a large system.
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="sh">#!/bin/bash
 
is_palindrome() {
local s1=$1
local s2=$(echo $1 | tr -d "[ ,!:;.'\"]" | tr '[A-Z]' '[a-z]')
 
if [[ $s2 = $(echo $s2 | rev) ]]
then
echo "[$s1] is a palindrome"
else
echo "[$s1] is NOT a palindrome"
fi
}
</syntaxhighlight>
 
is_palindrome "A man, a plan, a canal, Panama!"
is_palindrome "Madam, I'm Adam"
is_palindrome "1 on 1"</a>
 
=={{header|VBA}}==
Using the StrReverse function after deleted spaces
<syntaxhighlight lang="vb">
Option Explicit
 
Sub Test_a_function()
Dim a, i&
a = Array("abba", "mom", "dennis sinned", "Un roc lamina l animal cornu", "palindrome", "ba _ ab", "racecars", "racecar", "wombat", "in girum imus nocte et consumimur igni")
For i = 0 To UBound(a)
Debug.Print a(i) & " is a palidrome ? " & IsPalindrome(CStr(a(i)))
Next
End Sub
 
Function IsPalindrome(txt As String) As Boolean
Dim tempTxt As String
tempTxt = LCase(Replace(txt, " ", ""))
IsPalindrome = (tempTxt = StrReverse(tempTxt))
End Function
</syntaxhighlight>
{{out}}
<pre>abba is a palidrome ? True
mom is a palidrome ? True
dennis sinned is a palidrome ? True
Un roc lamina l animal cornu is a palidrome ? True
palindrome is a palidrome ? False
ba _ ab is a palidrome ? True
racecars is a palidrome ? False
racecar is a palidrome ? True
wombat is a palidrome ? False
in girum imus nocte et consumimur igni is a palidrome ? True</pre>
 
=={{header|Wren}}==
{{libheader|Wren-test}}
<syntaxhighlight lang="wren">import "./module" for Expect, Suite, ConsoleReporter
 
var isPal = Fn.new { |word| word == ((word.count > 0) ? word[-1..0] : "") }
 
var words = ["rotor", "rosetta", "step on no pets", "été", "wren", "🦊😀🦊"]
var expected = [true, false, true, true, false, true]
 
var TestPal = Suite.new("Pal") { |it|
it.suite("'isPal' function:") { |it|
for (i in 0...words.count) {
it.should("return '%(expected[i])' for '%(words[i])' is palindrome") {
Expect.call(isPal.call(words[i])).toEqual(expected[i])
}
}
}
}
 
var reporter = ConsoleReporter.new()
TestPal.run(reporter)
reporter.epilogue()</syntaxhighlight>
 
{{out}}
<pre>
Palindromes:
'isPal' function:
✓ should return 'true' for 'rotor' is palindrome
✓ should return 'false' for 'rosetta' is palindrome
✓ should return 'true' for 'step on no pets' is palindrome
✓ should return 'true' for 'été' is palindrome
✓ should return 'false' for 'wren' is palindrome
✓ should return 'true' for '🦊😀🦊' is palindrome
 
 
==== Tests Summary ====
6 tests, 6 passed, 0 failed, 0 errors, 0 skipped (1 ms)
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn pali(text){
if (text.len()<2) return(False);
text==text.reverse();
}</syntaxhighlight>
<syntaxhighlight lang="zkl">tester:=TheVault.Test.UnitTester.UnitTester(__FILE__);
// spaces make this not a palindrome
tester.testRun(pali.fp("red rum sir is murder"), Void,False,__LINE__);</syntaxhighlight>
Need to create a closure (.fp) so the unit test is what runs the test function and can catch any errors the test function throws.
{{output}}
<pre>
===================== Unit Test 1 =====================
Test 1 passed!
</pre>
A test file:
<syntaxhighlight lang="zkl">tester:=TheVault.Test.UnitTester.UnitTester(__FILE__);
fcn pali(text){
if (text.len()<2) return(False);
text==text.reverse();
}
tester.testRun(pali.fp("red rum sir is murder" - " "), Void,True,__LINE__);
tester.testRun(pali.fp("red rum sir is murder"), Void,True,__LINE__); //bad test
tester.testSrc("var R=(1+2)",Void,Void,3,__LINE__); // you can test source too
 
tester.stats();
returnClass(tester);</syntaxhighlight>
<syntaxhighlight lang="zkl">zkl paliTest.zkl</syntaxhighlight>
{{output}}
<pre>
======== Unit Test 1 =====paliTest.zkl==6========
Test 1 passed!
======== Unit Test 2 =====paliTest.zkl==7========
Result and expected result are different: False True
Test 2 failed. I hate it when that happens (line 7).
======== Unit Test 3 =====paliTest.zkl==8========
Test 3 passed!
3 tests completed.
Passed test(s): 2 (of 3)
Failed test(s): 1, tests L(2)
</pre>
If you had a collection of files to test:
<syntaxhighlight lang="zkl">zkl Test.testThemAll -- paliTest.zkl</syntaxhighlight>
{{output}}
<pre>
... as above
---------.--------.----------
---------|00:00:00|----------
-----------------------------
paliTest.zkl
3 tests completed.
Passed test(s): 2 (of 3)
Failed test(s): 1, tests L(2)
 
Executive summary: 1 pass in 00:00:00
3 tests completed (1 files)
Passed test(s): 2 (of 3)
Failed test(s): 1
Flawed test(s): 0
Failed files(s): 0
 
That used 6 simultaneous VMs, 4 of which were threads.
Number of VMs consumed: 30
</pre>
 
{{omit from|GUISS}}
{{omit from|TPP}}
{{omit from|Yorick|Yorick has no testing library}}
222

edits