/* Copyright (C) 2015-2016 LiveCode Ltd. This file is part of LiveCode. LiveCode is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v3 as published by the Free Software Foundation. LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with LiveCode. If not see . */ module com.livecode.arithmetic.tests public handler TestUnarySigns() variable tNum put 10 into tNum test "unary−" when -tNum is -10 put -10 into tNum test "unary+" when +tNum is -10 end handler public handler TestAdd() variable tInt as Integer put 10 into tInt add 2 to tInt test "add (syntax, int)" when tInt = 12 test "add (operator, int)" when tInt + 1 = 13 add 1.9 to tInt test diagnostic "Expecting an error" broken test "add (syntax, int+real)" when tInt = 13 because "bug 14592" variable tReal as Real put 10 into tReal add 1.9 to tReal test "add (syntax, real)" when tReal = 11.9 test "add (operator, real)" when tReal + 0.2 = 12.1 variable tNumber put 10 into tNumber add -1.9 to tNumber test "add (syntax, number)" when tNumber = 8.1 test "add (operator, number)" when tNumber + -1 = 7.1 end handler public handler TestSubtract() variable tInt as Integer put 10 into tInt subtract 2 from tInt test "subtract (syntax, int)" when tInt = 8 test "subtract (operator, int)" when tInt - -2 = 10 subtract 1.9 from tInt test diagnostic "Expecting an error" broken test "subtract (syntax, int-real)" when tInt = 6 because "bug 14592" variable tReal as Real put 10 into tReal subtract 1.9 from tReal test "subtract (syntax, real)" when tReal = 8.1 test "subtract (operator, real)" when tReal - 0.25 = 7.85 variable tNumber put 10 into tNumber subtract -1.9 from tNumber test "subtract (syntax, number)" when tNumber = 11.9 test "subtract (operator, number)" when tNumber - 1 = 10.9 end handler public handler TestMultiply() variable tInt as Integer put 10 into tInt multiply tInt by 2 test "multiply (syntax, int)" when tInt = 20 test "multiply (operator, int)" when tInt * 2 = 40 variable tReal as Real put 10 into tReal multiply tReal by -0.5 test "multiply (syntax, real)" when tReal = -5 test "multiply (operator, real)" when tReal * 3.5 = -17.5 variable tNumber put 10 into tNumber multiply tNumber by 0.5 test "multiply (syntax, number)" when tNumber = 5 test "multiply (operator, number)" when tNumber * 3.5 = 17.5 end handler public handler TestDivide() variable tInt as Integer put 10 into tInt divide tInt by 2 test "divide (syntax, int)" when tInt = 5 test "divide (operator, int)" when 30 / tInt = 6 divide tInt by 2 test diagnostic "Expecting trunc" broken test "divide (syntax, int)" when tInt = 2 because "bug 14592" variable tReal as Real put 10 into tReal divide tReal by -0.5 test "divide (syntax, real)" when tReal = -20 test "divide (operator, real)" when 50 / tReal = -2.5 variable tNumber put 10 into tNumber divide tNumber by 0.5 test "divide (syntax, number)" when tNumber = 20 test "divide (operator, number)" when tNumber / 8 = 2.5 end handler private handler ModInvariantDiagnostic(in pX, in pY, in pZ, in pR) test diagnostic pX formatted as string && "mod" && pY formatted as string test diagnostic pY formatted as string && "*" && \ pZ formatted as string && "+" && \ pR formatted as string && "=" && \ (pY * pZ + pR) formatted as string end handler private handler ModInvariantInteger(in pX as Integer, in pY as Integer) \ returns Boolean variable tZ as Integer variable tR as Integer put (pX / pY) into tZ put (pX mod pY) into tR ModInvariantDiagnostic(pX, pY, tZ, tR) return (pY * tZ + tR) is pX end handler private handler ModInvariantReal(in pX as Real, in pY as Real) \ returns Boolean variable tZ as Real variable tR as Real put the trunc of (pX / pY) into tZ -- because there is no "div" operator put (pX mod pY) into tR ModInvariantDiagnostic(pX, pY, tZ, tR) return (pY * tZ + tR) is pX end handler public handler TestModInteger() broken test "mod int (+ve, +ve)" when ModInvariantInteger(10, 3) because "bug 14592" broken test "mod (-ve, +ve)" when ModInvariantInteger(-10, 3) because "bug 14592" broken test "mod int (+ve, -ve)" when ModInvariantInteger(10, -3) because "bug 14592" broken test "mod int (-ve, -ve)" when ModInvariantInteger(-10, -3) because "bug 14592" test "mod real (+ve, +ve)" when ModInvariantReal(16, 3.75) test "mod real (-ve, +ve)" when ModInvariantReal(-16, 3.75) test "mod real (+ve, -ve)" when ModInvariantReal(16, -3.75) test "mod real (-ve, -ve)" when ModInvariantReal(-16, -3.75) end handler public handler TestWrap() test "wrap (+ve, +ve)" when 5 wrap 3 = 2 test "wrap (+ve, -ve)" when 5 wrap -3 = 2 test "wrap (-ve, -ve)" when -5 wrap -3 = -2 test "wrap (-ve, +ve)" when -5 wrap 3 = -2 end handler public handler TestGreaterThan() test "> (int, real)" when 5.1 > 5 test "> (real, int)" when -5 > -5.1 test "> (equal)" when not 5.0 > 5 test "not > (int, real)" when not 5.1 > 6 test "not > (real, int)" when not -6 > -5.1 end handler public handler TestGreaterThanEqual() test ">= (int, real)" when 5.1 >= 5 test ">= (real, int)" when -5 >= -5.1 test ">= (equal)" when 5.0 >= 5 test "not >= (int, real)" when not 5.1 >= 6 test "not >= (real, int)" when not -6 >= -5.1 end handler public handler TestLessThan() test "not < (int, real)" when not 5.1 < 5 test "not < (real, int)" when not -5 < -5.1 test "< (equal)" when not 5.0 < 5 test "< (int, real)" when 5.1 < 6 test "< (real, int)" when -6 < -5.1 end handler public handler TestLessThanEqual() test "not <= (int, real)" when not 5.1 <= 5 test "not <= (real, int)" when not -5 <= -5.1 test "<= (equal)" when 5.0 <= 5 test "<= (int, real)" when 5.1 <= 6 test "<= (real, int)" when -6 <= -5.1 end handler public handler TestFormatString() test "formatted as string (int)" when (-1) formatted as string is "-1" test "formatted as string (real)" when (-1.1) formatted as string is "-1.1" variable tString as String format -1 as string into tString test "format as string (int)" when tString is "-1" format -1.1 as string into tString test "format as string (real)" when tString is "-1.1" end handler public handler TestParseString() test "parsed (int)" when "-1" parsed as number = -1 test "parsed (real)" when "+1.1" parsed as number = 1.1 test "parsed (nothing)" when "x" parsed as number is nothing parse "-1" as number test "parse (int)" when the result is -1 parse "+1.1" as number test "parse (real)" when the result is 1.1 parse "x" as number test "parse (nothing)" when the result is nothing end handler public handler TestParseStringList() test "parsed list" when ["-1", "+2.2"] parsed as list of number is [-1, 2.2] variable tList parse ["-1", "+2.2", "x"] as list of number put the result into tList test "parse list (defined)" when element 1 to 2 of tList is [-1, 2.2] test "parse list (nothing)" when element 3 of tList is nothing end handler end module