Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Assertions
note: Basic language learning

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

View file

@ -0,0 +1,3 @@
V a = 5
assert(a == 42)
assert(a == 42, Error message)

View 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

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

View file

@ -0,0 +1,7 @@
PROGRAM assertions CONTEXT VOID
USE standard,environment
BEGIN
INT a := 43;
"Oops!" ASSERT ( a = 42 )
END
FINISH

View file

@ -0,0 +1,6 @@
begin
integer a;
a := 43;
assert a = 42;
write( "this won't appear" )
end.

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

View file

@ -0,0 +1 @@
pragma Assert (A = 42, "Oops!");

View file

@ -0,0 +1,3 @@
with Ada.Assertions; use Ada.Assertions;
...
Assert (A = 42, "Oops!");

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

View file

@ -0,0 +1,6 @@
integer x;
x = 41;
if (x != 42) {
error("x is not 42");
}

View file

@ -0,0 +1,2 @@
String myStr = 'test;
System.assert(myStr == 'something else', 'Assertion Failed Message');

View file

@ -0,0 +1,2 @@
Integer i = 5;
System.assertEquals(6, i, 'Expected 6, received ' + i);

View file

@ -0,0 +1,2 @@
Integer i = 5;
System.assertNotEquals(5, i, 'Expected different value than ' + i);

View file

@ -0,0 +1,2 @@
a: 42
ensure [a = 42]

View file

@ -0,0 +1,8 @@
a := 42
Assert(a > 10)
Assert(a < 42) ; throws exception
Assert(bool){
If !bool
throw Exception("Expression false", -1)
}

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

View file

@ -0,0 +1 @@
A=42??Returnʳ

View 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

View file

@ -0,0 +1,6 @@
PROCassert(a% = 42)
END
DEF PROCassert(bool%)
IF NOT bool% THEN ERROR 100, "Assertion failed"
ENDPROC

View 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

View file

@ -0,0 +1,4 @@
squish import :assert :assertions
assert_equal 42 42
assert_equal 13 42 #Raises an exception

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

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

View 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

View file

@ -0,0 +1 @@
Trace.Listeners.Add(new ConsoleTraceListener())

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 @@
assert(a == 42 && "Error message");

View file

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

View file

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

View file

@ -0,0 +1,11 @@
MODULE Assertions;
VAR
x: INTEGER;
PROCEDURE DoIt*;
BEGIN
x := 41;
ASSERT(x = 42);
END DoIt;
END Assertions.
Assertions.DoIt

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

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

View file

@ -0,0 +1 @@
Assert(a = 42, 'Not 42!');

View file

@ -0,0 +1,8 @@
procedure UniversalAnswer(var a : Integer);
require
a = 42;
begin
// code here
ensure
a = 42;
end;

View file

@ -0,0 +1,4 @@
main() {
var i = 42;
assert( i == 42 );
}

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

View file

@ -0,0 +1 @@
Assert(a = 42);

View file

@ -0,0 +1 @@
{$ASSERTIONS OFF}

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

View file

@ -0,0 +1,2 @@
var x = 42
assert(42, x)

View 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

View file

@ -0,0 +1 @@
ASSERT(a = 42,'A is not 42!',FAIL);

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

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,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,11 @@
ExUnit.start
defmodule AssertionTest do
use ExUnit.Case
def return_5, do: 5
test "not equal" do
assert 42 == return_5
end
end

View file

@ -0,0 +1,3 @@
(require 'cl-lib)
(let ((x 41))
(cl-assert (= x 42) t "This shouldn't happen"))

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,7 @@
type fourty_two(integer i)
return i = 42
end type
fourty_two i
i = 41 -- type-check failure

View file

@ -0,0 +1,4 @@
let test x =
assert (x = 42)
test 43

View 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

View file

@ -0,0 +1,2 @@
USING: kernel ;
42 assert=

View file

@ -0,0 +1,4 @@
variable a
: assert a @ 42 <> throw ;
41 a ! assert

View 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

View 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

View file

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

View file

@ -0,0 +1,3 @@
def checkTheAnswer = {
assert it == 42 : "This: " + it + " is not the answer!"
}

View file

@ -0,0 +1,4 @@
println "before 42..."
checkTheAnswer(42)
println "before 'Hello Universe'..."
checkTheAnswer("Hello Universe")

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

View 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

View file

@ -0,0 +1 @@
assert n = 42

View file

@ -0,0 +1 @@
assert valueA == valueB;

View file

@ -0,0 +1,2 @@
if (valueA != valueB)
throw new AssertionError();

View file

@ -0,0 +1 @@
assert valueA == valueB : "valueA is not 42";

View file

@ -0,0 +1 @@
assert valueA == valueB : valueA;

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

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

View 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

View 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

View file

@ -0,0 +1,4 @@
fun main() {
val a = 42
assert(a == 43)
}

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

View file

@ -0,0 +1,6 @@
local(a) = 8
fail_if(
#a != 42,
error_code_runtimeAssertion,
error_msg_runtimeAssertion + ": #a is not 42"
)

View 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

View 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

View file

@ -0,0 +1 @@
? { n = 42 };

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,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

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

View file

@ -0,0 +1 @@
assert(x == 42,'x = %d, not 42.',x);

View file

@ -0,0 +1,3 @@
x = 3;
assert(x == 42,'Assertion Failed: x = %d, not 42.',x);
??? Assertion Failed: x = 3, not 42.

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 @@
Assert[var===42]

View file

@ -0,0 +1 @@
def assert(expr t) = if not (t): errmessage("assertion failed") fi enddef;

View file

@ -0,0 +1,3 @@
n := 41;
assert(n=42);
message "ok";

View file

@ -0,0 +1 @@
<*ASSERT a = 42*>

View file

@ -0,0 +1,6 @@
a = 42
assert(a==42)
assert(a, 42)
assert(a==42, "Not 42!")
assert(a, 42, "Not 42!")

View file

@ -0,0 +1,2 @@
a = 5
assert (a = 42)

View file

@ -0,0 +1 @@
assert (foo == 42, $"foo == $foo, not 42.")

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

View file

@ -0,0 +1,2 @@
var a = 42
assert(a == 42, "Not 42!")

View file

@ -0,0 +1,2 @@
var a = 42
doAssert(a == 42, "Not 42!")

View file

@ -0,0 +1,5 @@
module main
demand 5==42
end

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

View file

@ -0,0 +1,7 @@
MODULE Assertions;
VAR
a: INTEGER;
BEGIN
a := 40;
ASSERT(a = 42);
END Assertions.

View file

@ -0,0 +1,8 @@
class Test {
function : Main(args : String[]) ~ Nil {
if(args->Size() = 1) {
a := args[0]->ToInt();
Runtime->Assert(a = 42);
};
}
}

View file

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

View 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