197 lines
7.9 KiB
C#
197 lines
7.9 KiB
C#
// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
|
|
//
|
|
// This file is part of Mocha.NET.
|
|
//
|
|
// Mocha.NET is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Mocha.NET 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 Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
using Mocha.Core.Oop;
|
|
using Mocha.Core.Oop.Methods;
|
|
|
|
namespace Mocha.Core.Tests.MethodTests
|
|
{
|
|
|
|
public class CalculateNumericMethodTests : MethodTestsBase
|
|
{
|
|
[Test]
|
|
public void Add_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_value = 1.0M;
|
|
decimal test_addition = 3.0M;
|
|
decimal test_result = test_value + test_addition;
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_value, ArithmeticOperator.Add, test_addition);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
[Test]
|
|
public void Subtract_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_1 = 6.0M;
|
|
decimal test_2 = 4.0M;
|
|
decimal test_result = test_2 - test_1;
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_2, ArithmeticOperator.Subtract, test_1);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
[Test]
|
|
public void Multiply_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_1 = 3.0M;
|
|
decimal test_2 = 9.0M;
|
|
decimal test_result = test_1 * test_2;
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Multiply, test_2);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
[Test]
|
|
public void Divide_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_1 = 28.0M;
|
|
decimal test_2 = 7.0M;
|
|
decimal test_result = test_1 / test_2;
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Divide, test_2);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
[Test]
|
|
public void Power_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_1 = 4.0M;
|
|
decimal test_2 = 3.0M;
|
|
decimal test_result = (decimal)Math.Pow((double)test_1, (double)test_2);
|
|
decimal test_result2 = (test_1 * test_1 * test_1);
|
|
Assert.That(test_result, Is.EqualTo(test_result2));
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Power, test_2);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
[Test]
|
|
public void Root_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_1 = 4.0M;
|
|
decimal test_2 = 2.0M;
|
|
decimal test_result = (decimal)Math.Sqrt((double)test_1);
|
|
decimal test_result2 = 2;
|
|
Assert.That(test_result, Is.EqualTo(test_result2));
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Root, test_2);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
[Test]
|
|
public void Logarithm_Two_Literal_Numbers_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_1 = 100.0M;
|
|
decimal test_2 = 10.0M;
|
|
decimal test_result = 2;
|
|
Assert.That(test_result, Is.EqualTo(test_result));
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Logarithm, test_2);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void Add_GA_Plus_Literal_Number_Test()
|
|
{
|
|
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
|
|
decimal test_value = 20.0M;
|
|
|
|
InstanceHandle irAttribute = Oms.GetInstance(KnownAttributeGuids.Numeric.MaximumLength);
|
|
Oms.SetAttributeValue(irTestClassInstance, irAttribute, test_value);
|
|
// `Test Class Instance`.`Maximum Length` is now 20.0M
|
|
|
|
decimal test_addition = 5.0M;
|
|
decimal test_result = test_value + test_addition;
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
GetAttributeMethod ih_GA = builder.CreateGetAttributeMethod(irTestClass, "get", "Maximum Length", irAttribute);
|
|
|
|
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), ih_GA, ArithmeticOperator.Add, test_addition);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
|
|
// below is REQUIRED, else we get "cannot call non-static method without instance reference"
|
|
context.SetWorkData(irTestClass, irTestClassInstance);
|
|
|
|
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
|
|
object value = context.GetWorkData(ih_test);
|
|
|
|
Assert.That(value, Is.EqualTo(test_result));
|
|
}
|
|
}
|
|
|
|
} |