all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
2
Task/Test-a-function/0DESCRIPTION
Normal file
2
Task/Test-a-function/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{{omit from|BBC BASIC}}
|
||||
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.
|
||||
2
Task/Test-a-function/1META.yaml
Normal file
2
Task/Test-a-function/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Testing
|
||||
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.ada
Normal file
28
Task/Test-a-function/Ada/test-a-function.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;
|
||||
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" }
|
||||
}
|
||||
}
|
||||
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 (= true (palindrome? "amanaplanacanalpanama")))
|
||||
(is (= false (palindrome? "Test 1, 2, 3"))))
|
||||
|
||||
(run-tests)
|
||||
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
|
||||
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()
|
||||
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(" ","")))
|
||||
}
|
||||
}
|
||||
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)))
|
||||
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*/);
|
||||
}
|
||||
}
|
||||
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
|
||||
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
|
||||
17
Task/Test-a-function/Perl-6/test-a-function.pl6
Normal file
17
Task/Test-a-function/Perl-6/test-a-function.pl6
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use Test;
|
||||
|
||||
my %tests =
|
||||
'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,
|
||||
;
|
||||
|
||||
plan %tests.elems;
|
||||
|
||||
for %tests.kv -> $test, $expected-result {
|
||||
is palin($test), $expected-result,
|
||||
"\"$test\" is {$expected-result??''!!'not '}a palindrome.";
|
||||
}
|
||||
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";
|
||||
}
|
||||
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"))
|
||||
27
Task/Test-a-function/PureBasic/test-a-function.purebasic
Normal file
27
Task/Test-a-function/PureBasic/test-a-function.purebasic
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
|
||||
10
Task/Test-a-function/REXX/test-a-function-1.rexx
Normal file
10
Task/Test-a-function/REXX/test-a-function-1.rexx
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* This REXX uses a lot of REXX keywords 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
|
||||
19
Task/Test-a-function/REXX/test-a-function-2.rexx
Normal file
19
Task/Test-a-function/REXX/test-a-function-2.rexx
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/*REXX program to show a secret message. */
|
||||
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
|
||||
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")))
|
||||
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
|
||||
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
|
||||
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) }
|
||||
|
||||
}
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue