Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Assertions/00-META.yaml
Normal file
3
Task/Assertions/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Assertions
|
||||
note: Basic language learning
|
||||
9
Task/Assertions/00-TASK.txt
Normal file
9
Task/Assertions/00-TASK.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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.
|
||||
|
||||
|
||||
;Task:
|
||||
Show an assertion in your language by asserting that an integer variable is equal to '''42'''.
|
||||
<br><br>
|
||||
|
||||
3
Task/Assertions/11l/assertions.11l
Normal file
3
Task/Assertions/11l/assertions.11l
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
V a = 5
|
||||
assert(a == 42)
|
||||
assert(a == 42, ‘Error message’)
|
||||
5
Task/Assertions/68000-Assembly/assertions.68000
Normal file
5
Task/Assertions/68000-Assembly/assertions.68000
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
CMP.L #42,D0
|
||||
BEQ continue
|
||||
ILLEGAL ;causes an immediate jump to the illegal instruction vector.
|
||||
continue:
|
||||
; rest of program
|
||||
5
Task/Assertions/ALGOL-68/assertions-1.alg
Normal file
5
Task/Assertions/ALGOL-68/assertions-1.alg
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
OP ASSERT = (VECTOR [] CHAR assertion,BOOL valid) VOID:
|
||||
IF NOT valid
|
||||
THEN type line on terminal(assertion);
|
||||
terminal error( 661 {invalid assertion } )
|
||||
FI;
|
||||
7
Task/Assertions/ALGOL-68/assertions-2.alg
Normal file
7
Task/Assertions/ALGOL-68/assertions-2.alg
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
PROGRAM assertions CONTEXT VOID
|
||||
USE standard,environment
|
||||
BEGIN
|
||||
INT a := 43;
|
||||
"Oops!" ASSERT ( a = 42 )
|
||||
END
|
||||
FINISH
|
||||
6
Task/Assertions/ALGOL-W/assertions.alg
Normal file
6
Task/Assertions/ALGOL-W/assertions.alg
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
begin
|
||||
integer a;
|
||||
a := 43;
|
||||
assert a = 42;
|
||||
write( "this won't appear" )
|
||||
end.
|
||||
21
Task/Assertions/AWK/assertions.awk
Normal file
21
Task/Assertions/AWK/assertions.awk
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
BEGIN {
|
||||
meaning = 6 * 7
|
||||
assert(meaning == 42, "Integer mathematics failed")
|
||||
assert(meaning == 42)
|
||||
meaning = strtonum("42 also known as forty-two")
|
||||
assert(meaning == 42, "Built-in function failed")
|
||||
meaning = "42"
|
||||
assert(meaning == 42, "Dynamic type conversion failed")
|
||||
meaning = 6 * 9
|
||||
assert(meaning == 42, "Ford Prefect's experiment failed")
|
||||
print "That's all folks"
|
||||
exit
|
||||
}
|
||||
|
||||
# Errormsg is optional, displayed if assertion fails
|
||||
function assert(cond, errormsg){
|
||||
if (!cond) {
|
||||
if (errormsg != "") print errormsg
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
1
Task/Assertions/Ada/assertions-1.ada
Normal file
1
Task/Assertions/Ada/assertions-1.ada
Normal file
|
|
@ -0,0 +1 @@
|
|||
pragma Assert (A = 42, "Oops!");
|
||||
3
Task/Assertions/Ada/assertions-2.ada
Normal file
3
Task/Assertions/Ada/assertions-2.ada
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
with Ada.Assertions; use Ada.Assertions;
|
||||
...
|
||||
Assert (A = 42, "Oops!");
|
||||
10
Task/Assertions/Ada/assertions-3.ada
Normal file
10
Task/Assertions/Ada/assertions-3.ada
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
procedure Find_First
|
||||
(List : in Array_Type;
|
||||
Value : in Integer;
|
||||
Found : out Boolean;
|
||||
Position : out Positive) with
|
||||
Depends => ((Found, Position) => (List, Value)),
|
||||
Pre => (List'Length > 0),
|
||||
Post =>
|
||||
(if Found then Position in List'Range and then List (Position) = Value
|
||||
else Position = List'Last);
|
||||
6
Task/Assertions/Aime/assertions.aime
Normal file
6
Task/Assertions/Aime/assertions.aime
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
integer x;
|
||||
|
||||
x = 41;
|
||||
if (x != 42) {
|
||||
error("x is not 42");
|
||||
}
|
||||
2
Task/Assertions/Apex/assertions-1.apex
Normal file
2
Task/Assertions/Apex/assertions-1.apex
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
String myStr = 'test;
|
||||
System.assert(myStr == 'something else', 'Assertion Failed Message');
|
||||
2
Task/Assertions/Apex/assertions-2.apex
Normal file
2
Task/Assertions/Apex/assertions-2.apex
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Integer i = 5;
|
||||
System.assertEquals(6, i, 'Expected 6, received ' + i);
|
||||
2
Task/Assertions/Apex/assertions-3.apex
Normal file
2
Task/Assertions/Apex/assertions-3.apex
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Integer i = 5;
|
||||
System.assertNotEquals(5, i, 'Expected different value than ' + i);
|
||||
2
Task/Assertions/Arturo/assertions.arturo
Normal file
2
Task/Assertions/Arturo/assertions.arturo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a: 42
|
||||
ensure [a = 42]
|
||||
8
Task/Assertions/AutoHotkey/assertions-1.ahk
Normal file
8
Task/Assertions/AutoHotkey/assertions-1.ahk
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
a := 42
|
||||
Assert(a > 10)
|
||||
Assert(a < 42) ; throws exception
|
||||
|
||||
Assert(bool){
|
||||
If !bool
|
||||
throw Exception("Expression false", -1)
|
||||
}
|
||||
6
Task/Assertions/AutoHotkey/assertions-2.ahk
Normal file
6
Task/Assertions/AutoHotkey/assertions-2.ahk
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
if (a != 42)
|
||||
{
|
||||
OutputDebug, "a != 42" ; sends output to a debugger if connected
|
||||
ListVars ; lists values of local and global variables
|
||||
Pause ; pauses the script, use ExitApp to exit instead
|
||||
}
|
||||
1
Task/Assertions/Axe/assertions.axe
Normal file
1
Task/Assertions/Axe/assertions.axe
Normal file
|
|
@ -0,0 +1 @@
|
|||
A=42??Returnʳ
|
||||
6
Task/Assertions/BASIC256/assertions.basic
Normal file
6
Task/Assertions/BASIC256/assertions.basic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
subroutine assert (condition, message)
|
||||
if not condition then print "ASSERTION FAIED: ";message: throwerror 1
|
||||
end subroutine
|
||||
|
||||
call assert(1+1=2, "but I don't expect this assertion to fail"): rem Does not throw an error
|
||||
rem call assert(1+1=3, "and rightly so"): rem Throws an error
|
||||
6
Task/Assertions/BBC-BASIC/assertions.basic
Normal file
6
Task/Assertions/BBC-BASIC/assertions.basic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
PROCassert(a% = 42)
|
||||
END
|
||||
|
||||
DEF PROCassert(bool%)
|
||||
IF NOT bool% THEN ERROR 100, "Assertion failed"
|
||||
ENDPROC
|
||||
24
Task/Assertions/BaCon/assertions.bacon
Normal file
24
Task/Assertions/BaCon/assertions.bacon
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
' Assertions
|
||||
answer = assertion(42)
|
||||
PRINT "The ultimate answer is indeed ", answer
|
||||
|
||||
PRINT "Now, expect a failure, unless NDEBUG defined at compile time"
|
||||
answer = assertion(41)
|
||||
PRINT answer
|
||||
END
|
||||
|
||||
' Ensure the given number is the ultimate answer
|
||||
FUNCTION assertion(NUMBER i)
|
||||
|
||||
' BaCon can easily be intimately integrated with C
|
||||
USEH
|
||||
#include <assert.h>
|
||||
END USEH
|
||||
|
||||
' If the given expression is not true, abort the program
|
||||
USEC
|
||||
assert(i == 42);
|
||||
END USEC
|
||||
|
||||
RETURN i
|
||||
END FUNCTION
|
||||
4
Task/Assertions/Brat/assertions.brat
Normal file
4
Task/Assertions/Brat/assertions.brat
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
squish import :assert :assertions
|
||||
|
||||
assert_equal 42 42
|
||||
assert_equal 13 42 #Raises an exception
|
||||
10
Task/Assertions/C++/assertions.cpp
Normal file
10
Task/Assertions/C++/assertions.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <cassert> // assert.h also works
|
||||
|
||||
int main()
|
||||
{
|
||||
int a;
|
||||
// ... input or change a here
|
||||
|
||||
assert(a == 42); // Aborts program if a is not 42, unless the NDEBUG macro was defined
|
||||
// when including <cassert>, in which case it has no effect
|
||||
}
|
||||
21
Task/Assertions/C-sharp/assertions-1.cs
Normal file
21
Task/Assertions/C-sharp/assertions-1.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System.Diagnostics; // Debug and Trace are in this namespace.
|
||||
|
||||
static class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
int a = 0;
|
||||
|
||||
Console.WriteLine("Before");
|
||||
|
||||
// Always hit.
|
||||
Trace.Assert(a == 42, "Trace assertion failed");
|
||||
|
||||
Console.WriteLine("After Trace.Assert");
|
||||
|
||||
// Only hit in debug builds.
|
||||
Debug.Assert(a == 42, "Debug assertion failed");
|
||||
|
||||
Console.WriteLine("After Debug.Assert");
|
||||
}
|
||||
}
|
||||
21
Task/Assertions/C-sharp/assertions-2.cs
Normal file
21
Task/Assertions/C-sharp/assertions-2.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
Imports System.Diagnostics
|
||||
' Note: VB Visual Studio projects have System.Diagnostics imported by default,
|
||||
' along with several other namespaces.
|
||||
|
||||
Module Program
|
||||
Sub Main()
|
||||
Dim a As Integer = 0
|
||||
|
||||
Console.WriteLine("Before")
|
||||
|
||||
' Always hit.
|
||||
Trace.Assert(a = 42, "Trace assertion failed: The Answer was incorrect")
|
||||
|
||||
Console.WriteLine("After Trace.Assert")
|
||||
|
||||
' Only hit in debug builds.
|
||||
Debug.Assert(a = 42, "Debug assertion failed: The Answer was incorrect")
|
||||
|
||||
Console.WriteLine("After Debug.Assert")
|
||||
End Sub
|
||||
End Module
|
||||
1
Task/Assertions/C-sharp/assertions-3.cs
Normal file
1
Task/Assertions/C-sharp/assertions-3.cs
Normal file
|
|
@ -0,0 +1 @@
|
|||
Trace.Listeners.Add(new ConsoleTraceListener())
|
||||
9
Task/Assertions/C/assertions-1.c
Normal file
9
Task/Assertions/C/assertions-1.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;
|
||||
}
|
||||
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");
|
||||
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)))
|
||||
2
Task/Assertions/Common-Lisp/assertions.lisp
Normal file
2
Task/Assertions/Common-Lisp/assertions.lisp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(let ((x 42))
|
||||
(assert (and (integerp x) (= 42 x)) (x)))
|
||||
11
Task/Assertions/Component-Pascal/assertions.pas
Normal file
11
Task/Assertions/Component-Pascal/assertions.pas
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
MODULE Assertions;
|
||||
VAR
|
||||
x: INTEGER;
|
||||
PROCEDURE DoIt*;
|
||||
BEGIN
|
||||
x := 41;
|
||||
ASSERT(x = 42);
|
||||
END DoIt;
|
||||
END Assertions.
|
||||
|
||||
Assertions.DoIt
|
||||
8
Task/Assertions/Crystal/assertions.crystal
Normal file
8
Task/Assertions/Crystal/assertions.crystal
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class AssertionError < Exception
|
||||
end
|
||||
|
||||
def assert(predicate : Bool, msg = "The asserted condition was false")
|
||||
raise AssertionError.new(msg) unless predicate
|
||||
end
|
||||
|
||||
assert(12 == 42, "It appears that 12 doesn't equal 42")
|
||||
36
Task/Assertions/D/assertions.d
Normal file
36
Task/Assertions/D/assertions.d
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import std.exception: enforce;
|
||||
|
||||
int foo(in bool condition) pure nothrow
|
||||
in {
|
||||
// Assertions are used in contract programming.
|
||||
assert(condition);
|
||||
} out(result) {
|
||||
assert(result > 0);
|
||||
} body {
|
||||
if (condition)
|
||||
return 42;
|
||||
|
||||
// assert(false) is never stripped from the code, it generates an
|
||||
// error in debug builds, and it becomes a HALT instruction in
|
||||
// -release mode.
|
||||
//
|
||||
// It's used as a mark by the D type system. If you remove this
|
||||
// line the compiles gives an error:
|
||||
//
|
||||
// Error: function assertions.foo no return exp;
|
||||
// or assert(0); at end of function
|
||||
assert(false, "This can't happen.");
|
||||
}
|
||||
|
||||
void main() pure {
|
||||
int x = foo(true);
|
||||
|
||||
// A regular assertion, it throws an error.
|
||||
// Use -release to disable it.
|
||||
// It can be used in nothrow functions.
|
||||
assert(x == 42, "x is not 42");
|
||||
|
||||
// This throws an exception and it can't be disabled.
|
||||
// There are some different versions of this lazy function.
|
||||
enforce(x == 42, "x is not 42");
|
||||
}
|
||||
1
Task/Assertions/DWScript/assertions-1.dw
Normal file
1
Task/Assertions/DWScript/assertions-1.dw
Normal file
|
|
@ -0,0 +1 @@
|
|||
Assert(a = 42, 'Not 42!');
|
||||
8
Task/Assertions/DWScript/assertions-2.dw
Normal file
8
Task/Assertions/DWScript/assertions-2.dw
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
procedure UniversalAnswer(var a : Integer);
|
||||
require
|
||||
a = 42;
|
||||
begin
|
||||
// code here
|
||||
ensure
|
||||
a = 42;
|
||||
end;
|
||||
4
Task/Assertions/Dart/assertions-1.dart
Normal file
4
Task/Assertions/Dart/assertions-1.dart
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
main() {
|
||||
var i = 42;
|
||||
assert( i == 42 );
|
||||
}
|
||||
14
Task/Assertions/Dart/assertions-2.dart
Normal file
14
Task/Assertions/Dart/assertions-2.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:test/test.dart';
|
||||
|
||||
main() {
|
||||
int i=42;
|
||||
int j=41;
|
||||
|
||||
test('i equals 42?', (){
|
||||
expect( i, equals(42) );
|
||||
});
|
||||
|
||||
test('j equals 42?', (){
|
||||
expect( j, equals(42) );
|
||||
});
|
||||
}
|
||||
1
Task/Assertions/Delphi/assertions-1.delphi
Normal file
1
Task/Assertions/Delphi/assertions-1.delphi
Normal file
|
|
@ -0,0 +1 @@
|
|||
Assert(a = 42);
|
||||
1
Task/Assertions/Delphi/assertions-2.delphi
Normal file
1
Task/Assertions/Delphi/assertions-2.delphi
Normal file
|
|
@ -0,0 +1 @@
|
|||
{$ASSERTIONS OFF}
|
||||
21
Task/Assertions/Delphi/assertions-3.delphi
Normal file
21
Task/Assertions/Delphi/assertions-3.delphi
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
program TestAssert;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
{.$ASSERTIONS OFF} // remove '.' to disable assertions
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
var
|
||||
a: Integer;
|
||||
|
||||
begin
|
||||
try
|
||||
Assert(a = 42);
|
||||
except
|
||||
on E:Exception do
|
||||
Writeln(E.Classname, ': ', E.Message);
|
||||
end;
|
||||
Readln;
|
||||
end.
|
||||
2
Task/Assertions/Dyalect/assertions.dyalect
Normal file
2
Task/Assertions/Dyalect/assertions.dyalect
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var x = 42
|
||||
assert(42, x)
|
||||
5
Task/Assertions/E/assertions.e
Normal file
5
Task/Assertions/E/assertions.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
require(a == 42) # default message, "Required condition failed"
|
||||
|
||||
require(a == 42, "The Answer is Wrong.") # supplied message
|
||||
|
||||
require(a == 42, fn { `Off by ${a - 42}.` }) # computed only on failure
|
||||
1
Task/Assertions/ECL/assertions.ecl
Normal file
1
Task/Assertions/ECL/assertions.ecl
Normal file
|
|
@ -0,0 +1 @@
|
|||
ASSERT(a = 42,'A is not 42!',FAIL);
|
||||
9
Task/Assertions/EchoLisp/assertions.l
Normal file
9
Task/Assertions/EchoLisp/assertions.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(assert (integer? 42)) → #t ;; success returns true
|
||||
|
||||
;; error and return to top level if not true;
|
||||
(assert (integer? 'quarante-deux))
|
||||
⛔ error: assert : assertion failed : (#integer? 'quarante-deux)
|
||||
|
||||
;; assertion with message (optional)
|
||||
(assert (integer? 'quarante-deux) "☝️ expression must evaluate to the integer 42")
|
||||
💥 error: ☝️ expression must evaluate to the integer 42 : assertion failed : (#integer? 'quarante-deux)
|
||||
12
Task/Assertions/Eiffel/assertions-1.e
Normal file
12
Task/Assertions/Eiffel/assertions-1.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
|
||||
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
|
||||
11
Task/Assertions/Elixir/assertions.elixir
Normal file
11
Task/Assertions/Elixir/assertions.elixir
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
ExUnit.start
|
||||
|
||||
defmodule AssertionTest do
|
||||
use ExUnit.Case
|
||||
|
||||
def return_5, do: 5
|
||||
|
||||
test "not equal" do
|
||||
assert 42 == return_5
|
||||
end
|
||||
end
|
||||
3
Task/Assertions/Emacs-Lisp/assertions.l
Normal file
3
Task/Assertions/Emacs-Lisp/assertions.l
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(require 'cl-lib)
|
||||
(let ((x 41))
|
||||
(cl-assert (= x 42) t "This shouldn't happen"))
|
||||
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
|
||||
7
Task/Assertions/Euphoria/assertions.euphoria
Normal file
7
Task/Assertions/Euphoria/assertions.euphoria
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
type fourty_two(integer i)
|
||||
return i = 42
|
||||
end type
|
||||
|
||||
fourty_two i
|
||||
|
||||
i = 41 -- type-check failure
|
||||
4
Task/Assertions/F-Sharp/assertions.fs
Normal file
4
Task/Assertions/F-Sharp/assertions.fs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
let test x =
|
||||
assert (x = 42)
|
||||
|
||||
test 43
|
||||
14
Task/Assertions/FBSL/assertions.fbsl
Normal file
14
Task/Assertions/FBSL/assertions.fbsl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#APPTYPE CONSOLE
|
||||
|
||||
DECLARE asserter
|
||||
|
||||
FUNCTION Assert(expression)
|
||||
DIM cmd AS STRING = "DIM asserter AS INTEGER = (" & expression & ")"
|
||||
EXECLINE(cmd, 1)
|
||||
IF asserter = 0 THEN PRINT "Assertion: ", expression, " failed"
|
||||
END FUNCTION
|
||||
|
||||
Assert("1<2")
|
||||
Assert("1>2")
|
||||
|
||||
PAUSE
|
||||
2
Task/Assertions/Factor/assertions.factor
Normal file
2
Task/Assertions/Factor/assertions.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
USING: kernel ;
|
||||
42 assert=
|
||||
4
Task/Assertions/Forth/assertions.fth
Normal file
4
Task/Assertions/Forth/assertions.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
variable a
|
||||
: assert a @ 42 <> throw ;
|
||||
|
||||
41 a ! assert
|
||||
8
Task/Assertions/FreeBASIC/assertions.basic
Normal file
8
Task/Assertions/FreeBASIC/assertions.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
' FB 1.05.0 Win64
|
||||
' requires compilation with -g switch
|
||||
|
||||
Dim a As Integer = 5
|
||||
Assert(a = 6)
|
||||
'The rest of the code will not be executed
|
||||
Print a
|
||||
Sleep
|
||||
19
Task/Assertions/GAP/assertions.gap
Normal file
19
Task/Assertions/GAP/assertions.gap
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# See section 7.5 of reference manual
|
||||
|
||||
# GAP has assertions levels. An assertion is tested if its level
|
||||
# is less then the global level.
|
||||
|
||||
# Set global level
|
||||
SetAssertionLevel(10);
|
||||
|
||||
a := 1;
|
||||
Assert(20, a > 1, "a should be greater than one");
|
||||
# nothing happens
|
||||
|
||||
a := 1;
|
||||
Assert(4, a > 1, "a should be greater than one");
|
||||
# error
|
||||
|
||||
# Show current global level
|
||||
AssertionLevel();
|
||||
# 10
|
||||
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)
|
||||
}
|
||||
}
|
||||
3
Task/Assertions/Groovy/assertions-1.groovy
Normal file
3
Task/Assertions/Groovy/assertions-1.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def checkTheAnswer = {
|
||||
assert it == 42 : "This: " + it + " is not the answer!"
|
||||
}
|
||||
4
Task/Assertions/Groovy/assertions-2.groovy
Normal file
4
Task/Assertions/Groovy/assertions-2.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
println "before 42..."
|
||||
checkTheAnswer(42)
|
||||
println "before 'Hello Universe'..."
|
||||
checkTheAnswer("Hello Universe")
|
||||
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
|
||||
5
Task/Assertions/Icon/assertions-1.icon
Normal file
5
Task/Assertions/Icon/assertions-1.icon
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
...
|
||||
runerr(n,( expression ,"Assertion/error - message.")) # Throw (and possibly trap) an error number n if expression succeeds.
|
||||
...
|
||||
stop(( expression ,"Assertion/stop - message.")) # Terminate program if expression succeeds.
|
||||
...
|
||||
12
Task/Assertions/Icon/assertions-2.icon
Normal file
12
Task/Assertions/Icon/assertions-2.icon
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
$define DEBUG 1 # this allows the assertions to go through
|
||||
|
||||
procedure check (a)
|
||||
if DEBUG then stop (42 = a, " is invalid value for 'a'")
|
||||
write (a)
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
check (10)
|
||||
check (42)
|
||||
check (12)
|
||||
end
|
||||
1
Task/Assertions/J/assertions.j
Normal file
1
Task/Assertions/J/assertions.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert n = 42
|
||||
1
Task/Assertions/Java/assertions-1.java
Normal file
1
Task/Assertions/Java/assertions-1.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert valueA == valueB;
|
||||
2
Task/Assertions/Java/assertions-2.java
Normal file
2
Task/Assertions/Java/assertions-2.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
if (valueA != valueB)
|
||||
throw new AssertionError();
|
||||
1
Task/Assertions/Java/assertions-3.java
Normal file
1
Task/Assertions/Java/assertions-3.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert valueA == valueB : "valueA is not 42";
|
||||
1
Task/Assertions/Java/assertions-4.java
Normal file
1
Task/Assertions/Java/assertions-4.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert valueA == valueB : valueA;
|
||||
23
Task/Assertions/JavaScript/assertions.js
Normal file
23
Task/Assertions/JavaScript/assertions.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function check() {
|
||||
try {
|
||||
if (isNaN(answer)) throw '$answer is not a number';
|
||||
if (answer != 42) throw '$answer is not 42';
|
||||
}
|
||||
catch(err) {
|
||||
console.log(err);
|
||||
answer = 42;
|
||||
}
|
||||
finally { console.log(answer); }
|
||||
}
|
||||
|
||||
console.count('try'); // 1
|
||||
let answer;
|
||||
check();
|
||||
|
||||
console.count('try'); // 2
|
||||
answer = 'fourty two';
|
||||
check();
|
||||
|
||||
console.count('try'); // 3
|
||||
answer = 23;
|
||||
check();
|
||||
19
Task/Assertions/Jq/assertions-1.jq
Normal file
19
Task/Assertions/Jq/assertions-1.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
def assert(exp; $msg):
|
||||
def m: $msg | if type == "string" then . else [.[]] | join(":") end;
|
||||
if env.JQ_ASSERT then
|
||||
(exp as $e | if $e then . else . as $in | "assertion violation @ \(m) => \($e)" | debug | $in end)
|
||||
else . end;
|
||||
|
||||
def assert(exp):
|
||||
if env.JQ_ASSERT then
|
||||
(exp as $e | if $e then . else . as $in | "assertion violation: \($e)" | debug | $in end)
|
||||
else . end;
|
||||
|
||||
def asserteq(x;y;$msg):
|
||||
def m: $msg | if type == "string" then . else [.[]] | join(":") end;
|
||||
def s: (if $msg then m + ": " else "" end) + "\(x) != \(y)";
|
||||
if env.JQ_ASSERT then
|
||||
if x == y then .
|
||||
else . as $in | "assertion violation @ \(s)" | debug | $in
|
||||
end
|
||||
else . end;
|
||||
13
Task/Assertions/Jq/assertions-2.jq
Normal file
13
Task/Assertions/Jq/assertions-2.jq
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# File: example.jq
|
||||
# This example assumes the availability of the $__loc__ function
|
||||
# and that assert.jq is in the same directory as example.jq.
|
||||
|
||||
include "assert" {search: "."};
|
||||
|
||||
def test:
|
||||
"This is an input"
|
||||
| 0 as $x
|
||||
| assert($x == 42; $__loc__),
|
||||
asserteq($x; 42; $__loc__);
|
||||
|
||||
test
|
||||
11
Task/Assertions/Julia/assertions.julia
Normal file
11
Task/Assertions/Julia/assertions.julia
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
const x = 5
|
||||
|
||||
# @assert macro checks the supplied conditional expression, with the expression
|
||||
# returned in the failed-assertion message
|
||||
@assert x == 42
|
||||
# ERROR: LoadError: AssertionError: x == 42
|
||||
|
||||
# Julia also has type assertions of the form, x::Type which can be appended to
|
||||
# variable for type-checking at any point
|
||||
x::String
|
||||
# ERROR: LoadError: TypeError: in typeassert, expected String, got Int64
|
||||
4
Task/Assertions/Kotlin/assertions-1.kotlin
Normal file
4
Task/Assertions/Kotlin/assertions-1.kotlin
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fun main() {
|
||||
val a = 42
|
||||
assert(a == 43)
|
||||
}
|
||||
8
Task/Assertions/Kotlin/assertions-2.kotlin
Normal file
8
Task/Assertions/Kotlin/assertions-2.kotlin
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fun findName(names: Map<String, String>, firstName: String) {
|
||||
require(names.isNotEmpty()) { "Please pass a non-empty names map" } // IllegalArgumentException
|
||||
val lastName = requireNotNull(names[name]) { "names is expected to contain name" } // IllegalArgumentException
|
||||
|
||||
val fullName = "$firstName $lastName"
|
||||
check(fullName.contains(" ")) { "fullname was expected to have a space...?" } // IllegalStateException
|
||||
return fullName
|
||||
}
|
||||
6
Task/Assertions/Lasso/assertions.lasso
Normal file
6
Task/Assertions/Lasso/assertions.lasso
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
local(a) = 8
|
||||
fail_if(
|
||||
#a != 42,
|
||||
error_code_runtimeAssertion,
|
||||
error_msg_runtimeAssertion + ": #a is not 42"
|
||||
)
|
||||
14
Task/Assertions/Liberty-BASIC/assertions.basic
Normal file
14
Task/Assertions/Liberty-BASIC/assertions.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
a=42
|
||||
call assert a=42
|
||||
print "passed"
|
||||
|
||||
a=41
|
||||
call assert a=42
|
||||
print "failed (we never get here)"
|
||||
end
|
||||
|
||||
sub assert cond
|
||||
if cond=0 then 'simulate error, mentioning "AssertionFailed"
|
||||
AssertionFailed(-1)=0
|
||||
end if
|
||||
end sub
|
||||
17
Task/Assertions/Lingo/assertions.lingo
Normal file
17
Task/Assertions/Lingo/assertions.lingo
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-- in a movie script
|
||||
on assert (ok, message)
|
||||
if not ok then
|
||||
if not voidP(message) then _player.alert(message)
|
||||
abort -- exits from current call stack, i.e. also from the caller function
|
||||
end if
|
||||
end
|
||||
|
||||
-- anywhere in the code
|
||||
on test
|
||||
x = 42
|
||||
assert(x=42, "Assertion 'x=42' failed")
|
||||
put "this shows up"
|
||||
x = 23
|
||||
assert(x=42, "Assertion 'x=42' failed")
|
||||
put "this will never show up"
|
||||
end
|
||||
1
Task/Assertions/Lisaac/assertions.lisaac
Normal file
1
Task/Assertions/Lisaac/assertions.lisaac
Normal file
|
|
@ -0,0 +1 @@
|
|||
? { n = 42 };
|
||||
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')
|
||||
61
Task/Assertions/M2000-Interpreter/assertions-1.m2000
Normal file
61
Task/Assertions/M2000-Interpreter/assertions-1.m2000
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
Module Assert {
|
||||
\\ This is a global object named Rec
|
||||
Global Group Rec {
|
||||
Private:
|
||||
document doc$="Error List at "+date$(today)+" "+time$(now)+{
|
||||
}
|
||||
Public:
|
||||
lastfilename$="noname.err"
|
||||
Module Error(a$) {
|
||||
if a$="" then exit
|
||||
.doc$<=" "+a$+{
|
||||
}
|
||||
flush error
|
||||
}
|
||||
Module Reset {
|
||||
Clear .doc$
|
||||
}
|
||||
Module Display {
|
||||
Report .doc$
|
||||
}
|
||||
Module SaveIt {
|
||||
.lastfilename$<=replace$("/", "-","Err"+date$(today)+str$(now, "-nn-mm")+".err")
|
||||
Save.Doc .doc$,.lastfilename$
|
||||
}
|
||||
}
|
||||
Module Checkit {
|
||||
Function Error1 (x) {
|
||||
if x<10 then Print "Normal" : exit
|
||||
=130 ' error code
|
||||
}
|
||||
Call Error1(5)
|
||||
Try ok {
|
||||
Call Error1(100)
|
||||
}
|
||||
If not Ok then Rec.Error Error$ : Flush Error
|
||||
|
||||
Test "breakpoint A" ' open Control form, show code as executed, press next or close it
|
||||
|
||||
Try {
|
||||
Test
|
||||
Report "Run this"
|
||||
Error "Hello"
|
||||
Report "Not run this"
|
||||
}
|
||||
Rec.Error Error$
|
||||
|
||||
Module Error1 (x) {
|
||||
if x<10 then Print "Normal" : exit
|
||||
Error "Big Error"
|
||||
}
|
||||
Try ok {
|
||||
Error1 100
|
||||
}
|
||||
If Error then Rec.Error Error$
|
||||
}
|
||||
Checkit
|
||||
Rec.Display
|
||||
Rec.SaveIt
|
||||
win "notepad.exe", dir$+Rec.lastfilename$
|
||||
}
|
||||
Assert
|
||||
6
Task/Assertions/M2000-Interpreter/assertions-2.m2000
Normal file
6
Task/Assertions/M2000-Interpreter/assertions-2.m2000
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// escape off // using escape off interpreter skip assert statements
|
||||
x=random(0, 1)*10
|
||||
try {
|
||||
assert x=10
|
||||
print x
|
||||
}
|
||||
1
Task/Assertions/MATLAB/assertions-1.m
Normal file
1
Task/Assertions/MATLAB/assertions-1.m
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert(x == 42,'x = %d, not 42.',x);
|
||||
3
Task/Assertions/MATLAB/assertions-2.m
Normal file
3
Task/Assertions/MATLAB/assertions-2.m
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
x = 3;
|
||||
assert(x == 42,'Assertion Failed: x = %d, not 42.',x);
|
||||
??? Assertion Failed: x = 3, not 42.
|
||||
3
Task/Assertions/Maple/assertions.maple
Normal file
3
Task/Assertions/Maple/assertions.maple
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" );
|
||||
1
Task/Assertions/Mathematica/assertions.math
Normal file
1
Task/Assertions/Mathematica/assertions.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Assert[var===42]
|
||||
1
Task/Assertions/Metafont/assertions-1.metafont
Normal file
1
Task/Assertions/Metafont/assertions-1.metafont
Normal file
|
|
@ -0,0 +1 @@
|
|||
def assert(expr t) = if not (t): errmessage("assertion failed") fi enddef;
|
||||
3
Task/Assertions/Metafont/assertions-2.metafont
Normal file
3
Task/Assertions/Metafont/assertions-2.metafont
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
n := 41;
|
||||
assert(n=42);
|
||||
message "ok";
|
||||
1
Task/Assertions/Modula-3/assertions.mod3
Normal file
1
Task/Assertions/Modula-3/assertions.mod3
Normal file
|
|
@ -0,0 +1 @@
|
|||
<*ASSERT a = 42*>
|
||||
6
Task/Assertions/NGS/assertions.ngs
Normal file
6
Task/Assertions/NGS/assertions.ngs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
a = 42
|
||||
|
||||
assert(a==42)
|
||||
assert(a, 42)
|
||||
assert(a==42, "Not 42!")
|
||||
assert(a, 42, "Not 42!")
|
||||
2
Task/Assertions/Nanoquery/assertions.nanoquery
Normal file
2
Task/Assertions/Nanoquery/assertions.nanoquery
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a = 5
|
||||
assert (a = 42)
|
||||
1
Task/Assertions/Nemerle/assertions-1.nemerle
Normal file
1
Task/Assertions/Nemerle/assertions-1.nemerle
Normal file
|
|
@ -0,0 +1 @@
|
|||
assert (foo == 42, $"foo == $foo, not 42.")
|
||||
13
Task/Assertions/Nemerle/assertions-2.nemerle
Normal file
13
Task/Assertions/Nemerle/assertions-2.nemerle
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using Nemerle.Assertions;
|
||||
|
||||
class SampleClass
|
||||
{
|
||||
public SomeMethod (input : list[int]) : int
|
||||
requires input.Length > 0 // requires keyword indicates precondition,
|
||||
// there can be more than one condition per method
|
||||
{ ... }
|
||||
|
||||
public AnotherMethod (input : string) : list[char]
|
||||
ensures value.Length > 0 // ensures keyword indicates postcondition
|
||||
{ ... } // value is a special symbol that indicates the method's return value
|
||||
}
|
||||
2
Task/Assertions/Nim/assertions-1.nim
Normal file
2
Task/Assertions/Nim/assertions-1.nim
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var a = 42
|
||||
assert(a == 42, "Not 42!")
|
||||
2
Task/Assertions/Nim/assertions-2.nim
Normal file
2
Task/Assertions/Nim/assertions-2.nim
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var a = 42
|
||||
doAssert(a == 42, "Not 42!")
|
||||
5
Task/Assertions/Nutt/assertions.nutt
Normal file
5
Task/Assertions/Nutt/assertions.nutt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
module main
|
||||
|
||||
demand 5==42
|
||||
|
||||
end
|
||||
3
Task/Assertions/OCaml/assertions.ocaml
Normal file
3
Task/Assertions/OCaml/assertions.ocaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
let a = get_some_value () in
|
||||
assert (a = 42); (* throws Assert_failure when a is not 42 *)
|
||||
(* evaluate stuff to return here when a is 42 *)
|
||||
7
Task/Assertions/Oberon-2/assertions.oberon
Normal file
7
Task/Assertions/Oberon-2/assertions.oberon
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
MODULE Assertions;
|
||||
VAR
|
||||
a: INTEGER;
|
||||
BEGIN
|
||||
a := 40;
|
||||
ASSERT(a = 42);
|
||||
END Assertions.
|
||||
8
Task/Assertions/Objeck/assertions.objeck
Normal file
8
Task/Assertions/Objeck/assertions.objeck
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class Test {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
if(args->Size() = 1) {
|
||||
a := args[0]->ToInt();
|
||||
Runtime->Assert(a = 42);
|
||||
};
|
||||
}
|
||||
}
|
||||
1
Task/Assertions/Objective-C/assertions-1.m
Normal file
1
Task/Assertions/Objective-C/assertions-1.m
Normal file
|
|
@ -0,0 +1 @@
|
|||
NSAssert(a == 42, @"Error message");
|
||||
1
Task/Assertions/Objective-C/assertions-2.m
Normal file
1
Task/Assertions/Objective-C/assertions-2.m
Normal file
|
|
@ -0,0 +1 @@
|
|||
NSAssert1(a == 42, @"a is not 42, a is actually %d", a); # has 1 formatting arg, so use NSAssert"1"
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue