Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Test-a-function/00-META.yaml
Normal file
3
Task/Test-a-function/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Test_a_function
|
||||
note: Testing
|
||||
5
Task/Test-a-function/00-TASK.txt
Normal file
5
Task/Test-a-function/00-TASK.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
;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>
|
||||
|
||||
36
Task/Test-a-function/ACL2/test-a-function.acl2
Normal file
36
Task/Test-a-function/ACL2/test-a-function.acl2
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
(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))
|
||||
28
Task/Test-a-function/Ada/test-a-function-1.ada
Normal file
28
Task/Test-a-function/Ada/test-a-function-1.ada
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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;
|
||||
5
Task/Test-a-function/Ada/test-a-function-2.ada
Normal file
5
Task/Test-a-function/Ada/test-a-function-2.ada
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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))));
|
||||
10
Task/Test-a-function/Arturo/test-a-function.arturo
Normal file
10
Task/Test-a-function/Arturo/test-a-function.arturo
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
palindrome?: function [s][
|
||||
s = reverse s
|
||||
]
|
||||
|
||||
tests: [
|
||||
[true? palindrome? "aba"]
|
||||
[false? palindrome? "ab" ]
|
||||
]
|
||||
|
||||
loop tests => ensure
|
||||
38
Task/Test-a-function/AutoHotkey/test-a-function-1.ahk
Normal file
38
Task/Test-a-function/AutoHotkey/test-a-function-1.ahk
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
; 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
|
||||
}
|
||||
21
Task/Test-a-function/AutoHotkey/test-a-function-2.ahk
Normal file
21
Task/Test-a-function/AutoHotkey/test-a-function-2.ahk
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
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
|
||||
24
Task/Test-a-function/Brat/test-a-function.brat
Normal file
24
Task/Test-a-function/Brat/test-a-function.brat
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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" }
|
||||
}
|
||||
}
|
||||
7
Task/Test-a-function/C++/test-a-function-1.cpp
Normal file
7
Task/Test-a-function/C++/test-a-function-1.cpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
constexpr bool is_palindrome(std::string_view s)
|
||||
{
|
||||
return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
|
||||
}
|
||||
12
Task/Test-a-function/C++/test-a-function-2.cpp
Normal file
12
Task/Test-a-function/C++/test-a-function-2.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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()
|
||||
{
|
||||
}
|
||||
10
Task/Test-a-function/C++/test-a-function-3.cpp
Normal file
10
Task/Test-a-function/C++/test-a-function-3.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#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
|
||||
}
|
||||
11
Task/Test-a-function/C++/test-a-function-4.cpp
Normal file
11
Task/Test-a-function/C++/test-a-function-4.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#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
|
||||
}
|
||||
34
Task/Test-a-function/C-sharp/test-a-function-1.cs
Normal file
34
Task/Test-a-function/C-sharp/test-a-function-1.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Task/Test-a-function/C-sharp/test-a-function-2.cs
Normal file
35
Task/Test-a-function/C-sharp/test-a-function-2.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Task/Test-a-function/C/test-a-function.c
Normal file
8
Task/Test-a-function/C/test-a-function.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <assert.h>
|
||||
int IsPalindrome(char *Str);
|
||||
|
||||
int main()
|
||||
{
|
||||
assert(IsPalindrome("racecar"));
|
||||
assert(IsPalindrome("alice"));
|
||||
}
|
||||
7
Task/Test-a-function/Clojure/test-a-function.clj
Normal file
7
Task/Test-a-function/Clojure/test-a-function.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(use 'clojure.test)
|
||||
|
||||
(deftest test-palindrome?
|
||||
(is (palindrome? "amanaplanacanalpanama"))
|
||||
(is (not (palindrome? "Test 1, 2, 3")))
|
||||
|
||||
(run-tests)
|
||||
100
Task/Test-a-function/Common-Lisp/test-a-function.lisp
Normal file
100
Task/Test-a-function/Common-Lisp/test-a-function.lisp
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
(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
|
||||
|#
|
||||
15
Task/Test-a-function/Crystal/test-a-function.crystal
Normal file
15
Task/Test-a-function/Crystal/test-a-function.crystal
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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
|
||||
5
Task/Test-a-function/D/test-a-function.d
Normal file
5
Task/Test-a-function/D/test-a-function.d
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
unittest {
|
||||
assert(isPalindrome("racecar"));
|
||||
assert(isPalindrome("bob"));
|
||||
assert(!isPalindrome("alice"));
|
||||
}
|
||||
3
Task/Test-a-function/Delphi/test-a-function-1.delphi
Normal file
3
Task/Test-a-function/Delphi/test-a-function-1.delphi
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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');
|
||||
3
Task/Test-a-function/Delphi/test-a-function-2.delphi
Normal file
3
Task/Test-a-function/Delphi/test-a-function-2.delphi
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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');
|
||||
50
Task/Test-a-function/E/test-a-function.e
Normal file
50
Task/Test-a-function/E/test-a-function.e
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#!/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
|
||||
9
Task/Test-a-function/EchoLisp/test-a-function.l
Normal file
9
Task/Test-a-function/EchoLisp/test-a-function.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(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
|
||||
7
Task/Test-a-function/Erlang/test-a-function.erl
Normal file
7
Task/Test-a-function/Erlang/test-a-function.erl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
-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") ).
|
||||
10
Task/Test-a-function/Euphoria/test-a-function.euphoria
Normal file
10
Task/Test-a-function/Euphoria/test-a-function.euphoria
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
--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()
|
||||
16
Task/Test-a-function/F-Sharp/test-a-function.fs
Normal file
16
Task/Test-a-function/F-Sharp/test-a-function.fs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
let palindrome (s : string) =
|
||||
let a = s.ToUpper().ToCharArray()
|
||||
Array.rev a = a
|
||||
|
||||
|
||||
open NUnit.Framework
|
||||
|
||||
[<TestFixture>]
|
||||
type TestCases() =
|
||||
[<Test>]
|
||||
member x.Test01() =
|
||||
Assert.IsTrue(palindrome "radar")
|
||||
|
||||
[<Test>]
|
||||
member x.Test02() =
|
||||
Assert.IsFalse(palindrome "hello")
|
||||
4
Task/Test-a-function/Factor/test-a-function-1.factor
Normal file
4
Task/Test-a-function/Factor/test-a-function-1.factor
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
USING: kernel sequences ;
|
||||
IN: palindrome
|
||||
|
||||
: palindrome? ( string -- ? ) dup reverse = ;
|
||||
5
Task/Test-a-function/Factor/test-a-function-2.factor
Normal file
5
Task/Test-a-function/Factor/test-a-function-2.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
USING: palindrome tools.test ;
|
||||
IN: palindrome.tests
|
||||
|
||||
[ t ] [ "racecar" palindrome? ] unit-test
|
||||
[ f ] [ "ferrari" palindrome? ] unit-test
|
||||
1
Task/Test-a-function/Factor/test-a-function-3.factor
Normal file
1
Task/Test-a-function/Factor/test-a-function-3.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
( scratchpad ) "palindrome" test
|
||||
13
Task/Test-a-function/Fantom/test-a-function.fantom
Normal file
13
Task/Test-a-function/Fantom/test-a-function.fantom
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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(" ","")))
|
||||
}
|
||||
}
|
||||
42
Task/Test-a-function/FreeBASIC/test-a-function.basic
Normal file
42
Task/Test-a-function/FreeBASIC/test-a-function.basic
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
31
Task/Test-a-function/Go/test-a-function.go
Normal file
31
Task/Test-a-function/Go/test-a-function.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Task/Test-a-function/Haskell/test-a-function.hs
Normal file
20
Task/Test-a-function/Haskell/test-a-function.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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)))
|
||||
20
Task/Test-a-function/Icon/test-a-function.icon
Normal file
20
Task/Test-a-function/Icon/test-a-function.icon
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
16
Task/Test-a-function/J/test-a-function-1.j
Normal file
16
Task/Test-a-function/J/test-a-function-1.j
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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'
|
||||
)
|
||||
5
Task/Test-a-function/J/test-a-function-2.j
Normal file
5
Task/Test-a-function/J/test-a-function-2.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
require 'general/unittest'
|
||||
unittest 'c:\mypath\palindrome_test.ijs'
|
||||
Test: c:\mypath\palindrome_test.ijs
|
||||
palinA .................................. OK
|
||||
palinB .................................. OK
|
||||
48
Task/Test-a-function/Java/test-a-function-1.java
Normal file
48
Task/Test-a-function/Java/test-a-function-1.java
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
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 {
|
||||
@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 {
|
||||
assertTrue(pali("abcba"));
|
||||
assertTrue(pali("aa"));
|
||||
assertTrue(pali("a"));
|
||||
assertTrue(pali(""));
|
||||
assertFalse(pali("ab"));
|
||||
assertFalse(pali("abcdba"));
|
||||
}
|
||||
/**
|
||||
* Test the rPali(...) method.
|
||||
*/
|
||||
@Test
|
||||
public void testRecursivePali() throws Exception {
|
||||
assertTrue(rPali("abcba"));
|
||||
assertTrue(rPali("aa"));
|
||||
assertTrue(rPali("a"));
|
||||
assertTrue(rPali(""));
|
||||
assertFalse(rPali("ab"));
|
||||
assertFalse(rPali("abcdba"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect a WhateverExcpetion
|
||||
*/
|
||||
@Test(expected=WhateverException.class)
|
||||
public void except(){
|
||||
//some code that should throw a WhateverException
|
||||
}
|
||||
}
|
||||
5
Task/Test-a-function/Java/test-a-function-2.java
Normal file
5
Task/Test-a-function/Java/test-a-function-2.java
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
public class RunTests{
|
||||
public static main(String[] args){
|
||||
org.junit.runner.JUnitCore.runClasses(PalindromeTest.class/*, other classes here if you have more*/);
|
||||
}
|
||||
}
|
||||
24
Task/Test-a-function/JavaScript/test-a-function.js
Normal file
24
Task/Test-a-function/JavaScript/test-a-function.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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'));
|
||||
});
|
||||
})
|
||||
});
|
||||
19
Task/Test-a-function/Jq/test-a-function-1.jq
Normal file
19
Task/Test-a-function/Jq/test-a-function-1.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# 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
|
||||
8
Task/Test-a-function/Jq/test-a-function-2.jq
Normal file
8
Task/Test-a-function/Jq/test-a-function-2.jq
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$ 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)
|
||||
3
Task/Test-a-function/Jq/test-a-function-3.jq
Normal file
3
Task/Test-a-function/Jq/test-a-function-3.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def factorial: if . <= 0 then 1 else . * ((. - 1) | factorial) end;
|
||||
|
||||
def palindrome: explode as $in | ($in|reverse) == $in;
|
||||
7
Task/Test-a-function/Jq/test-a-function-4.jq
Normal file
7
Task/Test-a-function/Jq/test-a-function-4.jq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import "library" as lib; lib::factorial
|
||||
3
|
||||
6
|
||||
|
||||
import "library" as lib; lib::palindrome
|
||||
"salàlas"
|
||||
true
|
||||
1
Task/Test-a-function/Jq/test-a-function-5.jq
Normal file
1
Task/Test-a-function/Jq/test-a-function-5.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
jq --run-tests < test-library.txt
|
||||
7
Task/Test-a-function/Jsish/test-a-function-1.jsish
Normal file
7
Task/Test-a-function/Jsish/test-a-function-1.jsish
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* 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('');
|
||||
}
|
||||
17
Task/Test-a-function/Jsish/test-a-function-2.jsish
Normal file
17
Task/Test-a-function/Jsish/test-a-function-2.jsish
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* 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!=
|
||||
*/
|
||||
23
Task/Test-a-function/Jsish/test-a-function-3.jsish
Normal file
23
Task/Test-a-function/Jsish/test-a-function-3.jsish
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* 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!=
|
||||
*/
|
||||
29
Task/Test-a-function/Jsish/test-a-function-4.jsish
Normal file
29
Task/Test-a-function/Jsish/test-a-function-4.jsish
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* 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!=
|
||||
*/
|
||||
15
Task/Test-a-function/Jsish/test-a-function-5.jsish
Normal file
15
Task/Test-a-function/Jsish/test-a-function-5.jsish
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* Interaction testing */
|
||||
var trial = console.input();
|
||||
puts(trial.replace(/a/g, 'b'));
|
||||
|
||||
/*
|
||||
=!INPUTSTART!=
|
||||
abccba
|
||||
=!INPUTEND!=
|
||||
*/
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
bbccbb
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
20
Task/Test-a-function/Julia/test-a-function.julia
Normal file
20
Task/Test-a-function/Julia/test-a-function.julia
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
15
Task/Test-a-function/Kotlin/test-a-function.kotlin
Normal file
15
Task/Test-a-function/Kotlin/test-a-function.kotlin
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Task/Test-a-function/Lasso/test-a-function.lasso
Normal file
37
Task/Test-a-function/Lasso/test-a-function.lasso
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// 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
|
||||
2
Task/Test-a-function/Lua/test-a-function.lua
Normal file
2
Task/Test-a-function/Lua/test-a-function.lua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
assert( ispalindrome("ABCBA") )
|
||||
assert( ispalindrome("ABCDE") )
|
||||
2
Task/Test-a-function/Mathematica/test-a-function.math
Normal file
2
Task/Test-a-function/Mathematica/test-a-function.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
myFun[x_] := Block[{y},y = x^2; Assert[y > 5]; Sin[y]]
|
||||
On[Assert];myFun[1.0]
|
||||
43
Task/Test-a-function/NetRexx/test-a-function.netrexx
Normal file
43
Task/Test-a-function/NetRexx/test-a-function.netrexx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* 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
|
||||
18
Task/Test-a-function/Nim/test-a-function-1.nim
Normal file
18
Task/Test-a-function/Nim/test-a-function-1.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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"))
|
||||
26
Task/Test-a-function/Nim/test-a-function-2.nim
Normal file
26
Task/Test-a-function/Nim/test-a-function-2.nim
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
26
Task/Test-a-function/OCaml/test-a-function.ocaml
Normal file
26
Task/Test-a-function/OCaml/test-a-function.ocaml
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
26
Task/Test-a-function/Odin/test-a-function.odin
Normal file
26
Task/Test-a-function/Odin/test-a-function.odin
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Task/Test-a-function/Oforth/test-a-function.fth
Normal file
3
Task/Test-a-function/Oforth/test-a-function.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
test: [ "abcd" isPalindrome ]
|
||||
test: ["abba" isPalindrome ]
|
||||
test: [ "abcba" isPalindrome ]
|
||||
37
Task/Test-a-function/Perl/test-a-function.pl
Normal file
37
Task/Test-a-function/Perl/test-a-function.pl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# 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";
|
||||
}
|
||||
31
Task/Test-a-function/Phix/test-a-function.phix
Normal file
31
Task/Test-a-function/Phix/test-a-function.phix
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
(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>
|
||||
<!--
|
||||
5
Task/Test-a-function/PicoLisp/test-a-function.l
Normal file
5
Task/Test-a-function/PicoLisp/test-a-function.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(de palindrome? (S)
|
||||
(= (setq S (chop S)) (reverse S)) )
|
||||
|
||||
(test T (palindrome? "racecar"))
|
||||
(test NIL (palindrome? "ferrari"))
|
||||
8
Task/Test-a-function/Prolog/test-a-function.pro
Normal file
8
Task/Test-a-function/Prolog/test-a-function.pro
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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).
|
||||
27
Task/Test-a-function/PureBasic/test-a-function.basic
Normal file
27
Task/Test-a-function/PureBasic/test-a-function.basic
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
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")
|
||||
39
Task/Test-a-function/Python/test-a-function.py
Normal file
39
Task/Test-a-function/Python/test-a-function.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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()
|
||||
4
Task/Test-a-function/R/test-a-function.r
Normal file
4
Task/Test-a-function/R/test-a-function.r
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
checkTrue(palindroc("aba")) # TRUE
|
||||
checkTrue(!palindroc("ab")) # TRUE
|
||||
checkException(palindroc()) # TRUE
|
||||
checkTrue(palindroc("")) # Error. Uh-oh, there's a bug in the function
|
||||
9
Task/Test-a-function/REXX/test-a-function-1.rexx
Normal file
9
Task/Test-a-function/REXX/test-a-function-1.rexx
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/*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. */
|
||||
21
Task/Test-a-function/REXX/test-a-function-2.rexx
Normal file
21
Task/Test-a-function/REXX/test-a-function-2.rexx
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/*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. */
|
||||
16
Task/Test-a-function/Racket/test-a-function.rkt
Normal file
16
Task/Test-a-function/Racket/test-a-function.rkt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#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")))
|
||||
18
Task/Test-a-function/Raku/test-a-function.raku
Normal file
18
Task/Test-a-function/Raku/test-a-function.raku
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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;
|
||||
10
Task/Test-a-function/Retro/test-a-function.retro
Normal file
10
Task/Test-a-function/Retro/test-a-function.retro
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
2
Task/Test-a-function/Ring/test-a-function.ring
Normal file
2
Task/Test-a-function/Ring/test-a-function.ring
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
assert(IsPalindrome("racecar"))
|
||||
assert(IsPalindrome("alice"))
|
||||
26
Task/Test-a-function/Ruby/test-a-function-1.rb
Normal file
26
Task/Test-a-function/Ruby/test-a-function-1.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
28
Task/Test-a-function/Ruby/test-a-function-2.rb
Normal file
28
Task/Test-a-function/Ruby/test-a-function-2.rb
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# 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
|
||||
25
Task/Test-a-function/Rust/test-a-function.rust
Normal file
25
Task/Test-a-function/Rust/test-a-function.rust
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/// 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"));
|
||||
}
|
||||
}
|
||||
9
Task/Test-a-function/SQL-PL/test-a-function.sql
Normal file
9
Task/Test-a-function/SQL-PL/test-a-function.sql
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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 @
|
||||
18
Task/Test-a-function/Scala/test-a-function.scala
Normal file
18
Task/Test-a-function/Scala/test-a-function.scala
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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) }
|
||||
|
||||
}
|
||||
6
Task/Test-a-function/Scheme/test-a-function.ss
Normal file
6
Task/Test-a-function/Scheme/test-a-function.ss
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(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)
|
||||
32
Task/Test-a-function/Swift/test-a-function.swift
Normal file
32
Task/Test-a-function/Swift/test-a-function.swift
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Task/Test-a-function/Tailspin/test-a-function.tailspin
Normal file
9
Task/Test-a-function/Tailspin/test-a-function.tailspin
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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'
|
||||
14
Task/Test-a-function/Tcl/test-a-function.tcl
Normal file
14
Task/Test-a-function/Tcl/test-a-function.tcl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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
|
||||
} -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
|
||||
13
Task/Test-a-function/UNIX-Shell/test-a-function.sh
Normal file
13
Task/Test-a-function/UNIX-Shell/test-a-function.sh
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#!/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
|
||||
}
|
||||
15
Task/Test-a-function/VBA/test-a-function.vba
Normal file
15
Task/Test-a-function/VBA/test-a-function.vba
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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
|
||||
20
Task/Test-a-function/Wren/test-a-function.wren
Normal file
20
Task/Test-a-function/Wren/test-a-function.wren
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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()
|
||||
4
Task/Test-a-function/Zkl/test-a-function-1.zkl
Normal file
4
Task/Test-a-function/Zkl/test-a-function-1.zkl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fcn pali(text){
|
||||
if (text.len()<2) return(False);
|
||||
text==text.reverse();
|
||||
}
|
||||
3
Task/Test-a-function/Zkl/test-a-function-2.zkl
Normal file
3
Task/Test-a-function/Zkl/test-a-function-2.zkl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
tester:=TheVault.Test.UnitTester.UnitTester(__FILE__);
|
||||
// spaces make this not a palindrome
|
||||
tester.testRun(pali.fp("red rum sir is murder"), Void,False,__LINE__);
|
||||
11
Task/Test-a-function/Zkl/test-a-function-3.zkl
Normal file
11
Task/Test-a-function/Zkl/test-a-function-3.zkl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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);
|
||||
1
Task/Test-a-function/Zkl/test-a-function-4.zkl
Normal file
1
Task/Test-a-function/Zkl/test-a-function-4.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
zkl paliTest.zkl
|
||||
1
Task/Test-a-function/Zkl/test-a-function-5.zkl
Normal file
1
Task/Test-a-function/Zkl/test-a-function-5.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
zkl Test.testThemAll -- paliTest.zkl
|
||||
Loading…
Add table
Add a link
Reference in a new issue