/* ----------------------------------------------------------------------------- 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 "not true" { success Test$run() } define Test { run () { if (!true) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce Bool" { success Test$run() } define Test { run () { if (!present(reduce(true))) { fail("Failed") } if (!present(reduce(true))) { fail("Failed") } if (present(reduce(true))) { fail("Failed") } if (!present(reduce(true))) { fail("Failed") } if (!present(reduce(true))) { fail("Failed") } if (!present(reduce(true))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce Char" { success Test$run() } define Test { run () { if (!present(reduce('a'))) { fail("Failed") } if (!present(reduce('a'))) { fail("Failed") } if (!present(reduce('a'))) { fail("Failed") } if (!present(reduce('a'))) { fail("Failed") } if (!present(reduce('a'))) { fail("Failed") } if (!present(reduce('a'))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce Int" { success Test$run() } define Test { run () { if (!present(reduce(1))) { fail("Failed") } if (!present(reduce(1))) { fail("Failed") } if (!present(reduce(1))) { fail("Failed") } if (!present(reduce(1))) { fail("Failed") } if (!present(reduce(1))) { fail("Failed") } if (!present(reduce(1))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce Float" { success Test$run() } define Test { run () { if (!present(reduce(1.0))) { fail("Failed") } if (!present(reduce(1.0))) { fail("Failed") } if (present(reduce(1.0))) { fail("Failed") } if (!present(reduce(1.0))) { fail("Failed") } if (!present(reduce(1.0))) { fail("Failed") } if (!present(reduce(1.0))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce String" { success Test$run() } define Test { run () { if (!present(reduce("a"))) { fail("Failed") } if (!present(reduce("a"))) { fail("Failed") } if (present(reduce("a"))) { fail("Failed") } if (present(reduce("a"))) { fail("Failed") } if (present(reduce("a"))) { fail("Failed") } if (!present(reduce("a"))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce ReadPosition success" { success Test$run() } define Test { run () { if (!present(reduce,ReadPosition>("x"))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "reduce ReadPosition fail" { success Test$run() } define Test { run () { if (present(reduce,ReadPosition>("x"))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "String LessThan" { success Test$run() } define Test { run () { if (!("x" `String$lessThan` "y")) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Bool Equals" { success Test$run() } define Test { run () { if (!(true `Bool$equals` true)) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "String Equals" { success Test$run() } define Test { run () { if (!("x" `String$equals` "x")) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Char LessThan" { success Test$run() } define Test { run () { if (!('x' `Char$lessThan` 'y')) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Char Equals" { success Test$run() } define Test { run () { if (!('x' `Char$equals` 'x')) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Int LessThan" { success Test$run() } define Test { run () { if (!(1 `Int$lessThan` 2)) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Int Equals" { success Test$run() } define Test { run () { if (!(1 `Int$equals` 1)) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Float LessThan" { success Test$run() } define Test { run () { if (!(1.0 `Float$lessThan` 2.0)) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Float Equals" { success Test$run() } define Test { run () { if (!(1.0 `Float$equals` 1.0)) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Bool is shared" { success Test$run() } define Test { run () { Bool value1 <- true // Shared because true and false are boxed constants. weak Bool value2 <- value1 if (!present(strong(value2))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "String is shared" { success Test$run() } define Test { run () { String value1 <- "x" weak String value2 <- value1 if (!present(strong(value2))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Char is not shared" { success Test$run() } define Test { run () { Char value1 <- 'x' weak Char value2 <- value1 if (present(strong(value2))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Int is not shared" { success Test$run() } define Test { run () { Int value1 <- 1 weak Int value2 <- value1 if (present(strong(value2))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Float is not shared" { success Test$run() } define Test { run () { Float value1 <- 1.1 weak Float value2 <- value1 if (present(strong(value2))) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "String Formatted" { success Test$run() } define Test { run () { String s <- ("x").formatted() if (s != "x") { fail(s) } } } concrete Test { @type run () -> () } testcase "Char Formatted" { success Test$run() } define Test { run () { String s <- ('x').formatted() if (s != "x") { fail(s) } } } concrete Test { @type run () -> () } testcase "Char octal Formatted" { success Test$run() } define Test { run () { String s <- ('\170').formatted() if (s != "x") { fail(s) } } } concrete Test { @type run () -> () } testcase "Char hex Formatted" { success Test$run() } define Test { run () { String s <- ('\x78').formatted() if (s != "x") { fail(s) } } } concrete Test { @type run () -> () } testcase "Int Formatted" { success Test$run() } define Test { run () { String s <- (1).formatted() if (s != "1") { fail(s) } } } concrete Test { @type run () -> () } testcase "Int hex Formatted" { success Test$run() } define Test { run () { String s <- (\x0010).formatted() if (s != "16") { fail(s) } } } concrete Test { @type run () -> () } testcase "Float Formatted" { success Test$run() } define Test { run () { String s <- (1.1).formatted() if (s != "1.1") { // precision might vary fail(s) } } } concrete Test { @type run () -> () } testcase "Bool Formatted" { success Test$run() } define Test { run () { String s <- (false).formatted() if (s != "false") { fail(s) } } } concrete Test { @type run () -> () } testcase "String access" { success Test$run() } define Test { run () { String s <- "abcde" Char c <- s.readPosition(3) if (c != 'd') { fail(c) } Int size <- s.readSize() if (size != 5) { fail(size) } } } concrete Test { @type run () -> () } testcase "String access negative" { crash Test$run() require "-10" require "bounds" } define Test { run () { ~ ("abc").readPosition(-10) } } concrete Test { @type run () -> () } testcase "String access past end" { crash Test$run() require "100" require "bounds" } define Test { run () { ~ ("abc").readPosition(100) } } concrete Test { @type run () -> () } testcase "String subsequence" { success Test$run() } define Test { run () { String s <- "abcde" String s2 <- s.subSequence(1,3) if (s2 != "bcd") { fail(s2) } } } concrete Test { @type run () -> () } testcase "String empty subsequence at end" { success Test$run() } define Test { run () { String s <- "" String s2 <- s.subSequence(0,0) if (s2 != "") { fail(s2) } } } concrete Test { @type run () -> () } testcase "String subsequence past end" { crash Test$run() require "100" require "size" } define Test { run () { ~ ("abc").subSequence(1,100) } } concrete Test { @type run () -> () } testcase "Int max is accurate" { success Test$run() } define Test { run () { Int x <- 9223372036854775807 - 1 if (x != 9223372036854775806) { fail(x) } } } concrete Test { @type run () -> () } testcase "Int min is accurate" { success Test$run() } define Test { run () { Int x <- -9223372036854775806 + 1 if (x != -9223372036854775805) { fail(x) } } } concrete Test { @type run () -> () } testcase "Int unsigned works properly" { success Test$run() } define Test { run () { if (\xffffffffffffffff != -1) { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Int literal too large" { error require "9223372036854775808" require "max" } define Test { run () { ~ 9223372036854775808 } } concrete Test { @type run () -> () } testcase "Int unsigned literal too large" { error require "18446744073709551616" require "max" } define Test { run () { ~ \x10000000000000000 } } concrete Test { @type run () -> () } testcase "Int literal too small" { error require "-9223372036854775807" require "min" } define Test { run () { ~ -9223372036854775807 } } concrete Test { @type run () -> () } testcase "Float exponent does not cross whitespace" { success Test$run() } concrete E10 { @type create () -> (E10) } define E10 { create () { return E10{ } } } define Test { run () { Float x <- 1.2345 E10 y <- E10$create() } } concrete Test { @type run () -> () } testcase "String allows null" { success Test$run() } define Test { run () { if ("abc\x00def" == "abc") { fail("Failed") } if (("abc\x00def").readPosition(4) != 'd') { fail("Failed") } } } concrete Test { @type run () -> () } testcase "Bool conversions" { success Test$run() } define Test { run () { Bool b1 <- (false).asBool() if (b1 != false) { fail(b1) } Bool b2 <- (true).asBool() if (b2 != true) { fail(b2) } Int i <- (true).asInt() if (i != 1) { fail(i) } Float f <- (true).asFloat() if (f != 1.0) { fail(f) } } } concrete Test { @type run () -> () } testcase "Char conversions" { success Test$run() } define Test { run () { Bool b1 <- ('\x00').asBool() if (b1 != false) { fail(b1) } Bool b2 <- ('\x10').asBool() if (b2 != true) { fail(b2) } Char c <- ('a').asChar() if (c != 'a') { fail(c) } Int i <- ('\x10').asInt() if (i != 16) { fail(i) } Float f <- ('\x10').asFloat() if (f != 16.0) { fail(f) } } } concrete Test { @type run () -> () } testcase "Int conversions" { success Test$run() } define Test { run () { Bool b1 <- (0).asBool() if (b1 != false) { fail(b1) } Bool b2 <- (16).asBool() if (b2 != true) { fail(b2) } Char c <- (97).asChar() if (c != 'a') { fail(c) } Int i <- (16).asInt() if (i != 16) { fail(i) } Float f <- (16).asFloat() if (f != 16.0) { fail(f) } } } concrete Test { @type run () -> () } testcase "Float conversions" { success Test$run() } define Test { run () { Bool b1 <- (0.0).asBool() if (b1 != false) { fail(b1) } Bool b2 <- (16.0).asBool() if (b2 != true) { fail(b2) } Int i <- (16.0).asInt() if (i != 16) { fail(i) } Float f <- (16.0).asFloat() if (f != 16.0) { fail(f) } } } concrete Test { @type run () -> () } testcase "String conversions" { success Test$run() } define Test { run () { Bool b1 <- ("").asBool() if (b1 != false) { fail(b1) } Bool b2 <- ("\x00").asBool() if (b2 != true) { fail(b2) } } } concrete Test { @type run () -> () }