Skip to main content

module lang::flybytes::tests::ArithmeticTests

rascal-0.34.0
flybytes-0.2.3

Usage

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

Source code

http://github.com/usethesource/flybytes/blob/main/src/lang/flybytes/tests/ArithmeticTests.rsc

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 val

real val(float(), Mirror r)

real val(double(), Mirror r)

default int val(Type _, Mirror r)

Tests

test testNeg1

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

test testAdd2

test bool testAdd2(int i, int j) 
= all (t <- exactArithmeticTypes,
I := (i % maxValue(t)) / 2,
J := (j % maxValue(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 % maxValue(t)) / 2,
J := ((j % maxValue(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 % maxValue(t)),
J := abs(((j % maxValue(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 % maxValue(t)),
J := abs(((j % maxValue(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));