/* ----------------------------------------------------------------------------- Copyright 2020 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] testcase "Int operations" { success } unittest plus { \ Testing.checkEquals(8 + 2,10) } unittest minus { \ Testing.checkEquals(8 - 2,6) } unittest times { \ Testing.checkEquals(8 * 2,16) } unittest divide { \ Testing.checkEquals(8 / 2,4) } unittest modulo { \ Testing.checkEquals(8 % 2,0) } unittest left { \ Testing.checkEquals(1 << 2,4) } unittest right { \ Testing.checkEquals(7 >> 1,3) } unittest xor { \ Testing.checkEquals(7 ^ 2 ,5) } unittest and { \ Testing.checkEquals(7 & ~2,5) } unittest or { \ Testing.checkEquals(5 | 2 ,7) } unittest arithmetic { \ Testing.checkEquals(\x10 + 1 * 2 - 8 / 2 - 3 % 2,13) } unittest bitwise { \ Testing.checkEquals(1 << 4 | 7 >> 1 & ~2,17) } unittest sameOperator { \ Testing.checkEquals(2 - 1 - 1,0) } unittest lessThan { if (!(1 < 2)) { fail("Failed") } } unittest lessEquals { if (!(1 <= 2)) { fail("Failed") } } unittest equals { if (!(1 == 1)) { fail("Failed") } } unittest notEquals { if (!(1 != 2)) { fail("Failed") } } unittest greaterThan { if (!(2 > 1)) { fail("Failed") } } unittest greaterEquals { if (!(2 >= 1)) { fail("Failed") } } unittest arithmeticCompare { if (!(2 + 1 < 2 + 3)) { fail("Failed") } } testcase "Bool operations" { success } unittest precedence { scoped { Bool x <- 1 < 2 && 3 <= 4 } in if (!x) { fail(x) } } unittest trueEqualsTrue { if (!(true == true)) { fail("Failed") } } unittest falseEqualsFalse { if (!(false == false)) { fail("Failed") } } unittest falseNotEqualsTrue { if (!(false != true)) { fail("Failed") } } unittest compareWithLogic { scoped { Bool x <- 1 + 2 < 4 && 3 >= 1 * 2 + 1 } in if (!x) { fail(x) } } unittest rightAssociative { Int x <- 2 Bool y <- true && (x <- 3) == 2 || x == 3 \ Testing.checkEquals(x,3) \ Testing.checkEquals(y,true) } unittest shortCircuit { Int x <- 2 Bool y <- true || (x <- 3) == 2 || x == 3 \ Testing.checkEquals(x,2) \ Testing.checkEquals(y,true) } testcase "Float operations" { success } unittest precedence { \ Testing.checkEquals(16.0 + 1.0 * 2.0 - 8.0 / 2.0 - 3.0 / 3.0,13.0) } unittest lessThan { if (!(1.0 < 2.0)) { fail("Failed") } } unittest lessEquals { if (!(1.0 <= 2.0)) { fail("Failed") } } unittest equals { if (!(1.0 == 1.0)) { fail("Failed") } } unittest notEquals { if (!(1.0 != 2.0)) { fail("Failed") } } unittest greaterThan { if (!(2.0 > 1.0)) { fail("Failed") } } unittest greaterEquals { if (!(2.0 >= 1.0)) { fail("Failed") } } unittest arithmeticCompare { if (!(2.0 + 1.0 < 2.0 + 3.0)) { fail("Failed") } } testcase "String operations" { success } unittest add { \ Testing.checkEquals("x" + "y" + "z","xyz") } unittest lessThan { if (!("x" < "y")) { fail("Failed") } } unittest lessEquals { if (!("x" <= "y")) { fail("Failed") } } unittest equals { if (!("x" == "x")) { fail("Failed") } } unittest notEquals { if (!("x" != "y")) { fail("Failed") } } unittest greaterThan { if (!("y" > "x")) { fail("Failed") } } unittest greaterEquals { if (!("y" >= "x")) { fail("Failed") } } unittest arithmeticCompare { if (!("x" + "w" < "x" + "y")) { fail("Failed") } } testcase "Char operations" { success } unittest minus { \ Testing.checkEquals('z' - 'a',25) } unittest lessThan { if (!('x' < 'y')) { fail("Failed") } } unittest lessEquals { if (!('x' <= 'y')) { fail("Failed") } } unittest equals { if (!('x' == 'x')) { fail("Failed") } } unittest notEquals { if (!('x' != 'y')) { fail("Failed") } } unittest greaterThan { if (!('y' > 'x')) { fail("Failed") } } unittest greaterEquals { if (!('y' >= 'x')) { fail("Failed") } } unittest arithmeticCompare { if (!('d' - 'a' == 3)) { fail("Failed") } } testcase "Int + Bool" { error require "Int.+Bool" } define Test { run () { \ \x10 + false } } concrete Test { @type run () -> () } testcase "Int + String" { error require "Int.+String" } define Test { run () { \ \x10 + "" } } concrete Test { @type run () -> () } testcase "minus String" { error require "String.+String" } define Test { run () { \ "x" - "x" } } concrete Test { @type run () -> () } testcase "arithmetic Bool" { error require "Bool.+Bool" } define Test { run () { \ true - false } } concrete Test { @type run () -> () } testcase "Bool comparison" { error require "Bool.+Int" } define Test { run () { \ (1 < 2) < 3 } } concrete Test { @type run () -> () }