Skip to main content

module lang::flybytes::tests::ArithmeticTests

rascal-0.42.0
org.rascalmpl.flybytes-0.2.11

Usage​

import lang::flybytes::tests::ArithmeticTests;

Dependencies​

import lang::flybytes::Compiler;
import lang::flybytes::Mirror;
import lang::flybytes::api::JavaLang;
import Node;
import IO;
import util::Math;

alias BinOp​

Exp (Exp, Exp)

alias UnOp​

Exp (Exp)

function binOpClass​

Class binOpClass(Type t, BinOp op)

function unOpClass​

Class unOpClass(Type t, UnOp op)

function testBinOp​

bool testBinOp(Class c, Type t, num lhs, num rhs, num answer)

function testUnOp​

bool testUnOp(Class c, Type t, num arg, num answer)

function testBinOpRange​

bool testBinOpRange(Class c, Type t, num lhs, num rhs, real answer)

function testUnOpRange​

bool testUnOpRange(Class c, Type t, num arg, real answer)

function fit​

real fit(float(), real r)

real fit(double(), real r)

function round​

real round(float(),  real f)

real round(double(), real f)

default int round(Type _, int f)

function realVal​

real realVal(float(), Mirror r)

real realVal(double(), Mirror r)

function intVal​

int  intVal(Type _, Mirror r)

Tests

test testNeg1​

test bool testNeg1(int i)
= all(t <- exactArithmeticTypes,
I := i % maxIntValue(t), testUnOp(unOpClass(t, neg), t, I, -1 * I));

test testAdd2​

test bool testAdd2(int i, int j) 
= all (t <- exactArithmeticTypes,
I := (i % maxIntValue(t)) / 2,
J := (j % maxIntValue(t)) / 2,
testBinOp(binOpClass(t, add), t, I, J, I + J));

test testMul2​

test bool testMul2(int i, int j) 
= all (t <- exactArithmeticTypes,
I := (i % 10),
J := (j % 10),
testBinOp(binOpClass(t, mul), t, I, J, I * J));

test testSub2​

test bool testSub2(int i, int j) 
= all (t <- exactArithmeticTypes,
I := (i % maxIntValue(t)) / 2,
J := ((j % maxIntValue(t)) / 2),
testBinOp(binOpClass(t, sub), t, I, J, I - J));

test testDivInt​

test bool testDivInt(int i, int j) 
= all (t <- exactArithmeticTypes,
I := (i % maxIntValue(t)),
J := abs(((j % maxIntValue(t)) / 2)) + 1, // never 0,
testBinOp(binOpClass(t, div), t, I, J, I / J));

test testRem​

test bool testRem(int i, int j) 
= all (t <- exactArithmeticTypes,
I := (i % maxIntValue(t)),
J := abs(((j % maxIntValue(t)) / 2)) + 1, // never 0,
testBinOp(binOpClass(t, rem), t, I, J, I % J));

test testAdd​

test bool testAdd(real i, real j) 
= all (t <- floatingPointTypes,
I := fit(t, 1. / (i + .1)), // stick with numbers in +/-[0,1] we can manage
J := fit(t, 1. / (j + .1)), // stick with numbers in +/-[0,1] we can manage
testBinOpRange(binOpClass(t, add), t, I, J, fit(t, I + J)));

test testMul​

test bool testMul(real i, real j) 
= all (t <- floatingPointTypes,
I := fit(t, 1. / (i + .1)), // stick with numbers in +/-[0,1] we can manage
J := fit(t, 1. / (j + .1)), // stick with numbers in +/-[0,1] we can manage
testBinOpRange(binOpClass(t, mul), t, I, J, fit(t, I * J)));

test testSub​

test bool testSub(real i, real j) 
= all (t <- floatingPointTypes,
I := fit(t, 1. / (i + .1)), // stick with numbers in +/-[0,1] we can manage
J := fit(t, 1. / (j + .1)), // stick with numbers in +/-[0,1] we can manage
testBinOpRange(binOpClass(t, sub), t, I, J, fit(t, I - J)));

test testDivReal​

test bool testDivReal(real i, real j) 
= all (t <- floatingPointTypes,
I := fit(t, 1. / (i + .1)), // stick with numbers in [0,1] we can manage,
J := abs(fit(t, (1. / (j + .1)) + 1.)), // // stick with numbers in [1,2] we can manage
testBinOpRange(binOpClass(t, div), t, I, J, fit(t, I / J)));

test testNeg​

test bool testNeg(real i)
= all(t <- floatingPointTypes,
I := fit(t, 1. / (i + .1)), // stick with numbers in [0,1] we can manage,
testUnOpRange(unOpClass(t, neg), t, I, -1 * I));