From dbb5e0490eef3b4dc35bbb8086543bbae236eb26 Mon Sep 17 00:00:00 2001 From: Brad Howes Date: Thu, 14 Sep 2023 22:23:18 +0200 Subject: [PATCH] Additional tests. --- Tests/MathParserTests/MathParserTests.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Tests/MathParserTests/MathParserTests.swift b/Tests/MathParserTests/MathParserTests.swift index 0f5dbaa..833b746 100644 --- a/Tests/MathParserTests/MathParserTests.swift +++ b/Tests/MathParserTests/MathParserTests.swift @@ -384,6 +384,14 @@ final class MathParserTests: XCTestCase { let parser = MathParser(enableImpliedMultiplication: true) let token = parser.parse("2+5") XCTAssertEqual(7, token?.eval()) + let token2 = parser.parse("t+4") + XCTAssertEqual(7, token2?.eval("t", value: 3)) + } + + func testEval() { + let parser = MathParser(enableImpliedMultiplication: false) + let token = parser.parse("t+4") + XCTAssertEqual(7, token?.eval("t", value: 3)) } func testBuggyImpliedMultiplication() { @@ -648,4 +656,16 @@ error: unexpected input XCTAssertEqual(2 * pow(3, 4) * 5, parser.parse("2 * 3 ^ 4 * 5")?.value) XCTAssertEqual(2 * pow(3, pow(4, 5)), parser.parse("2 * 3 ^ 4 ^ 5")?.value) } + + func testDegTrig() { + var unaryFunctions = MathParser.defaultUnaryFunctions + unaryFunctions["sin"] = { sin($0 * Double.pi / 180.0) } + unaryFunctions["cos"] = { cos($0 * Double.pi / 180.0) } + var binaryFunctions = MathParser.defaultBinaryFunctions + binaryFunctions["atan2"] = { atan2($0, $1) * 180.0 / Double.pi } + let parser = MathParser(unaryFunctionDict: unaryFunctions, binaryFunctionDict: binaryFunctions) + XCTAssertEqual(sin(Double.pi / 6), parser.parse("sin(30)")?.value) + XCTAssertEqual(cos(Double.pi / 3), parser.parse("cos(60)")?.value) + XCTAssertEqual(atan2(1.0, 1.0) * 180.0 / Double.pi, parser.parse("atan2(1.0, 1.0)")?.value) + } }