Skip to content

Commit

Permalink
Initial implementation of exp( number ) and number.exp()
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcopley committed Jan 14, 2024
1 parent d00c6f5 commit 60b486a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/main/java/ortus/boxlang/runtime/bifs/global/math/Exp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* 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.
*/

package ortus.boxlang.runtime.bifs.global.math;

import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.bifs.BoxMember;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.BoxLangType;

@BoxBIF
@BoxMember( type = BoxLangType.NUMERIC )
public class Exp extends BIF {

/**
* Constructor
*/
public Exp() {
super();
declaredArguments = new Argument[] {
new Argument( true, "numeric", Key.number )
};
}

/**
* Calculates the exponent whose base is e that represents a number.
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.number The number to calculate the exponent for.
*/
public Object invoke( IBoxContext context, ArgumentsScope arguments ) {
double value = arguments.getAsDouble( Key.number );
return StrictMath.exp( value );
}
}
90 changes: 90 additions & 0 deletions src/test/java/ortus/boxlang/runtime/bifs/global/math/ExpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* 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.
*/
package ortus.boxlang.runtime.bifs.global.math;

import static com.google.common.truth.Truth.assertThat;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.context.ScriptingBoxContext;
import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.VariablesScope;

public class ExpTest {

static BoxRuntime instance;
static IBoxContext context;
static IScope variables;
static Key result = new Key( "result" );

@BeforeAll
public static void setUp() {
instance = BoxRuntime.getInstance( true );
context = new ScriptingBoxContext( instance.getRuntimeContext() );
variables = context.getScopeNearby( VariablesScope.name );
}

@AfterAll
public static void teardown() {
instance.shutdown();
}

@BeforeEach
public void setupEach() {
variables.clear();
}

@DisplayName( "It returns the exponent" )
@Test
public void testItReturnsExponent() {
instance.executeSource(
"""
result = exp(1);
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.exp( 1 ) );
instance.executeSource(
"""
result = exp(0.5);
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.exp( 0.5 ) );
}

@DisplayName( "It returns the exponent using member function" )
@Test
public void testItReturnsExponentUsingMemberFunction() {
instance.executeSource(
"""
result = (1).exp();
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.exp( 1 ) );
instance.executeSource(
"""
result = (0.5).exp();
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.exp( 0.5 ) );
}
}

0 comments on commit 60b486a

Please sign in to comment.