new tasks

This commit is contained in:
Ingy döt Net 2013-04-09 00:46:50 -07:00
parent 2a4d27cea0
commit 80737d5a6a
1194 changed files with 15353 additions and 1 deletions

View 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.

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1 @@
assert(a == 42 && "Error message");

View 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;
}

View file

@ -0,0 +1,2 @@
(let [i 42]
(assert (= i 42)))

View file

@ -0,0 +1,8 @@
class TEST
feature assert(val: INTEGER) is
require
val = 42;
do
print("Thanks for the 42!%N");
end
end

View 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

View 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

View file

@ -0,0 +1,8 @@
package main
func main() {
x := 43
if x != 42 {
panic(42)
}
}

View 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

View 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
}

View file

@ -0,0 +1,3 @@
a = 5
assert (a == 42)
assert (a == 42,'\''..a..'\' is not the answer to life, the universe, and everything')

View 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()
?>

View file

@ -0,0 +1,2 @@
use autodie;
open my $fh, '<', 'file'; # automatically throws an exception on failure

View 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;

View file

@ -0,0 +1,2 @@
use Carp::Assert;
assert($a == 42);

View file

@ -0,0 +1,4 @@
is $a, 42;
ok $a == 42;
cmp_ok $a, '==', 42, 'The answer should be 42';
# etc.

View file

@ -0,0 +1 @@
open my $fh, '<', 'file' or die "Cannot open file: $!\n"; # $! contains the error message from the last error

View file

@ -0,0 +1,4 @@
(let N 41
(unless (= N 42) (quit "Incorrect N" N)) ) # 'quit' throws an error
41 -- Incorrect N
?

View 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

View file

@ -0,0 +1,3 @@
...
~(assert (= N 42)) # Exists only in debug mode
...

View file

@ -0,0 +1,2 @@
test(A):-
assertion(A==42).

View 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

View file

@ -0,0 +1 @@
stopifnot(a==42)

View 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'

View file

@ -0,0 +1,5 @@
#lang racket
(define x 80)
(unless (= x 42)
(error "a is not 42")) ; will error

View 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!

View 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

View file

@ -0,0 +1,7 @@
class MAIN is
main is
i ::= 41;
assert i = 42; -- fatal
-- ...
end;
end;

View file

@ -0,0 +1,2 @@
require(a == 42)
require(a == 42, "a isn't equal to 42")

View 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"))

View 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")

View file

@ -0,0 +1,2 @@
(let ((x 42))
(assert (and (integer? x) (= x 42))))

View file

@ -0,0 +1,2 @@
self assert: (... somethingMustEvaluateToTrue.. )
self should:[ some code ] raise: someException "ensures that an exception is raised

View file

@ -0,0 +1 @@
self assert: (... somethingMustEvaluateToTrue.. ) "implemented in Object"

View file

@ -0,0 +1,3 @@
foo := 41.
...
self assert: (foo == 42).

View file

@ -0,0 +1,4 @@
package require control
set x 5
control::assert {$x == 42}