new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
3
Task/Assertions/0DESCRIPTION
Normal file
3
Task/Assertions/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Assertions are a way of breaking out of code when there is an error or an unexpected input. Some languages throw [[exceptions]] and some treat it as a break point.
|
||||
|
||||
Show an assertion in your language by asserting that an integer variable is equal to 42.
|
||||
2
Task/Assertions/1META.yaml
Normal file
2
Task/Assertions/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
1
Task/Assertions/C/assertions-2.c
Normal file
1
Task/Assertions/C/assertions-2.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert(a == 42 && "Error message");
|
||||
9
Task/Assertions/C/assertions.c
Normal file
9
Task/Assertions/C/assertions.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <assert.h>
|
||||
|
||||
int main(){
|
||||
int a;
|
||||
/* ...input or change a here */
|
||||
assert(a == 42); /* aborts program when a is not 42, unless the NDEBUG macro was defined */
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
Task/Assertions/Clojure/assertions.clj
Normal file
2
Task/Assertions/Clojure/assertions.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(let [i 42]
|
||||
(assert (= i 42)))
|
||||
8
Task/Assertions/Eiffel/assertions-2.e
Normal file
8
Task/Assertions/Eiffel/assertions-2.e
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class TEST
|
||||
feature assert(val: INTEGER) is
|
||||
require
|
||||
val = 42;
|
||||
do
|
||||
print("Thanks for the 42!%N");
|
||||
end
|
||||
end
|
||||
12
Task/Assertions/Eiffel/assertions.e
Normal file
12
Task/Assertions/Eiffel/assertions.e
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class MAIN
|
||||
creation main
|
||||
feature main is
|
||||
local
|
||||
test: TEST;
|
||||
do
|
||||
create test;
|
||||
|
||||
io.read_integer;
|
||||
test.assert(io.last_integer);
|
||||
end
|
||||
end
|
||||
10
Task/Assertions/Erlang/assertions.erl
Normal file
10
Task/Assertions/Erlang/assertions.erl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
1> N = 42.
|
||||
42
|
||||
2> N = 43.
|
||||
** exception error: no match of right hand side value 43
|
||||
3> N = 42.
|
||||
42
|
||||
4> 44 = N.
|
||||
** exception error: no match of right hand side value 42
|
||||
5> 42 = N.
|
||||
42
|
||||
8
Task/Assertions/Go/assertions.go
Normal file
8
Task/Assertions/Go/assertions.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
x := 43
|
||||
if x != 42 {
|
||||
panic(42)
|
||||
}
|
||||
}
|
||||
5
Task/Assertions/Haskell/assertions.hs
Normal file
5
Task/Assertions/Haskell/assertions.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import Control.Exception
|
||||
|
||||
main = let a = someValue in
|
||||
assert (a == 42) -- throws AssertionFailed when a is not 42
|
||||
somethingElse -- what to return when a is 42
|
||||
8
Task/Assertions/Java/assertions.java
Normal file
8
Task/Assertions/Java/assertions.java
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
public static void main(String[] args){
|
||||
int a;
|
||||
//...input or change a here
|
||||
assert a == 42;//throws an AssertionError when a is not 42
|
||||
assert a == 42 : "Error message"; //throws an AssertionError
|
||||
//when a is not 42 with "Error message" for the message
|
||||
//the error message can be any non-void expression
|
||||
}
|
||||
3
Task/Assertions/Lua/assertions.lua
Normal file
3
Task/Assertions/Lua/assertions.lua
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a = 5
|
||||
assert (a == 42)
|
||||
assert (a == 42,'\''..a..'\' is not the answer to life, the universe, and everything')
|
||||
6
Task/Assertions/PHP/assertions.php
Normal file
6
Task/Assertions/PHP/assertions.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
$a = 5
|
||||
#...input or change $a here
|
||||
assert($a == 42) # when $a is not 42, take appropriate actions,
|
||||
# which is set by assert_options()
|
||||
?>
|
||||
2
Task/Assertions/Perl/assertions-2.pl
Normal file
2
Task/Assertions/Perl/assertions-2.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
use autodie;
|
||||
open my $fh, '<', 'file'; # automatically throws an exception on failure
|
||||
9
Task/Assertions/Perl/assertions-3.pl
Normal file
9
Task/Assertions/Perl/assertions-3.pl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
my $a = 5;
|
||||
#...input or change $a here
|
||||
$a == 42 or die "Error message\n";
|
||||
# or, alternatively:
|
||||
die "Error message\n" unless $a == 42;
|
||||
# or:
|
||||
die "Error message\n" if not $a == 42;
|
||||
# or just:
|
||||
die "Error message\n" if $a != 42;
|
||||
2
Task/Assertions/Perl/assertions-4.pl
Normal file
2
Task/Assertions/Perl/assertions-4.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
use Carp::Assert;
|
||||
assert($a == 42);
|
||||
4
Task/Assertions/Perl/assertions-5.pl
Normal file
4
Task/Assertions/Perl/assertions-5.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
is $a, 42;
|
||||
ok $a == 42;
|
||||
cmp_ok $a, '==', 42, 'The answer should be 42';
|
||||
# etc.
|
||||
1
Task/Assertions/Perl/assertions.pl
Normal file
1
Task/Assertions/Perl/assertions.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
open my $fh, '<', 'file' or die "Cannot open file: $!\n"; # $! contains the error message from the last error
|
||||
4
Task/Assertions/PicoLisp/assertions-2.l
Normal file
4
Task/Assertions/PicoLisp/assertions-2.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(let N 41
|
||||
(unless (= N 42) (quit "Incorrect N" N)) ) # 'quit' throws an error
|
||||
41 -- Incorrect N
|
||||
?
|
||||
5
Task/Assertions/PicoLisp/assertions-3.l
Normal file
5
Task/Assertions/PicoLisp/assertions-3.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(let N 41
|
||||
(unless (= N 42) (! setq N 42)) ) # '!' is a breakpoint
|
||||
(setq N 42) # Manually fix the value
|
||||
! # Hit ENTER to leave the breakpoint
|
||||
-> 42
|
||||
3
Task/Assertions/PicoLisp/assertions.l
Normal file
3
Task/Assertions/PicoLisp/assertions.l
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
...
|
||||
~(assert (= N 42)) # Exists only in debug mode
|
||||
...
|
||||
2
Task/Assertions/Prolog/assertions.pro
Normal file
2
Task/Assertions/Prolog/assertions.pro
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
test(A):-
|
||||
assertion(A==42).
|
||||
6
Task/Assertions/Python/assertions.py
Normal file
6
Task/Assertions/Python/assertions.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
a = 5
|
||||
#...input or change a here
|
||||
assert a == 42 # throws an AssertionError when a is not 42
|
||||
assert a == 42, "Error message" # throws an AssertionError
|
||||
# when a is not 42 with "Error message" for the message
|
||||
# the error message can be any expression
|
||||
1
Task/Assertions/R/assertions.r
Normal file
1
Task/Assertions/R/assertions.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
stopifnot(a==42)
|
||||
24
Task/Assertions/REXX/assertions.rexx
Normal file
24
Task/Assertions/REXX/assertions.rexx
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* REXX ***************************************************************
|
||||
* There's no assert feature in Rexx. That's how I'd implement it
|
||||
* 10.08.2012 Walter Pachl
|
||||
**********************************************************************/
|
||||
x.=42
|
||||
x.2=11
|
||||
Do i=1 By 1
|
||||
Call assert x.i,42
|
||||
End
|
||||
Exit
|
||||
assert:
|
||||
Parse Arg assert_have,assert_should_have
|
||||
If assert_have\==assert_should_have Then Do
|
||||
Say 'Assertion fails in line' sigl
|
||||
Say 'expected:' assert_should_have
|
||||
Say ' found:' assert_have
|
||||
Say sourceline(sigl)
|
||||
Say 'Look around'
|
||||
Trace ?R
|
||||
Nop
|
||||
Signal Syntax
|
||||
End
|
||||
Return
|
||||
Syntax: Say 'program terminated'
|
||||
5
Task/Assertions/Racket/assertions-2.rkt
Normal file
5
Task/Assertions/Racket/assertions-2.rkt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#lang racket
|
||||
|
||||
(define x 80)
|
||||
(unless (= x 42)
|
||||
(error "a is not 42")) ; will error
|
||||
13
Task/Assertions/Racket/assertions.rkt
Normal file
13
Task/Assertions/Racket/assertions.rkt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#lang racket
|
||||
|
||||
(define/contract x
|
||||
(=/c 42) ; make sure x = 42
|
||||
42)
|
||||
|
||||
(define/contract f
|
||||
(-> number? (or/c 'yes 'no)) ; function contract
|
||||
(lambda (x)
|
||||
(if (= 42 x) 'yes 'no)))
|
||||
|
||||
(f 42) ; succeeds
|
||||
(f "foo") ; contract error!
|
||||
11
Task/Assertions/Ruby/assertions.rb
Normal file
11
Task/Assertions/Ruby/assertions.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
require "test/unit/assertions"
|
||||
include Test::Unit::Assertions
|
||||
|
||||
n = 5
|
||||
begin
|
||||
assert_equal(42, n)
|
||||
rescue Exception => e
|
||||
# Ruby 1.8: e is a Test::Unit::AssertionFailedError
|
||||
# Ruby 1.9: e is a MiniTest::Assertion
|
||||
puts e
|
||||
end
|
||||
7
Task/Assertions/Sather/assertions.sa
Normal file
7
Task/Assertions/Sather/assertions.sa
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class MAIN is
|
||||
main is
|
||||
i ::= 41;
|
||||
assert i = 42; -- fatal
|
||||
-- ...
|
||||
end;
|
||||
end;
|
||||
2
Task/Assertions/Scala/assertions-2.scala
Normal file
2
Task/Assertions/Scala/assertions-2.scala
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require(a == 42)
|
||||
require(a == 42, "a isn't equal to 42")
|
||||
4
Task/Assertions/Scala/assertions-3.scala
Normal file
4
Task/Assertions/Scala/assertions-3.scala
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
println(a.ensuring(a == 42))
|
||||
println(a.ensuring(a == 42, "a isn't equal to 42"))
|
||||
println(a.ensuring(_ == 42))
|
||||
println(a.ensuring(_ == 42, "a isn't equal to 42"))
|
||||
4
Task/Assertions/Scala/assertions.scala
Normal file
4
Task/Assertions/Scala/assertions.scala
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
assert(a == 42)
|
||||
assert(a == 42, "a isn't equal to 42")
|
||||
assume(a == 42)
|
||||
assume(a == 42, "a isn't equal to 42")
|
||||
2
Task/Assertions/Scheme/assertions.ss
Normal file
2
Task/Assertions/Scheme/assertions.ss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(let ((x 42))
|
||||
(assert (and (integer? x) (= x 42))))
|
||||
2
Task/Assertions/Smalltalk/assertions-2.st
Normal file
2
Task/Assertions/Smalltalk/assertions-2.st
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
self assert: (... somethingMustEvaluateToTrue.. )
|
||||
self should:[ some code ] raise: someException "ensures that an exception is raised
|
||||
1
Task/Assertions/Smalltalk/assertions-3.st
Normal file
1
Task/Assertions/Smalltalk/assertions-3.st
Normal file
|
|
@ -0,0 +1 @@
|
|||
self assert: (... somethingMustEvaluateToTrue.. ) "implemented in Object"
|
||||
3
Task/Assertions/Smalltalk/assertions.st
Normal file
3
Task/Assertions/Smalltalk/assertions.st
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
foo := 41.
|
||||
...
|
||||
self assert: (foo == 42).
|
||||
4
Task/Assertions/Tcl/assertions.tcl
Normal file
4
Task/Assertions/Tcl/assertions.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package require control
|
||||
|
||||
set x 5
|
||||
control::assert {$x == 42}
|
||||
Loading…
Add table
Add a link
Reference in a new issue