Skip to content

Commit

Permalink
Added CI tests (#8)
Browse files Browse the repository at this point in the history
* Updated README.md

* Added github workflow

* Added github workflow

* Added tests

---------

Co-authored-by: danilarassohin <danilarassohin@gmail.com>
  • Loading branch information
CrissNamon and danilarassohin committed Mar 23, 2023
1 parent 72f7a0b commit b3775af
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build

on:
push:
branches: [ "dev" ]
pull_request:
branches: [ "main", "dev" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B test --file pom.xml
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# CompaJ Project
[![Build](https://github.com/CrissNamon/compaj/actions/workflows/maven.yml/badge.svg)](https://github.com/CrissNamon/compaj/actions/workflows/maven.yml)

This is the home of the CompaJ Project - open source, cross-platform programming and numeric computing platform for math modelling.

<details>
Expand Down Expand Up @@ -31,13 +33,13 @@ CompaJ has modular structure based on Maven modules.
___
#### Lang
CompaJ uses Groovy under the hood with some tweaks. You can learn Groovy on its official [site](https://groovy-lang.org/documentation.html).
Documentation about CompaJ syntax will be released soon.
Documentation about CompaJ syntax available in [Wiki](https://github.com/CrissNamon/compaj/wiki/CompaJ-Lang).

#### Math
CompaJ uses [Apache Commons Math](https://commons.apache.org/proper/commons-math/userguide/index.html) library for math operations.
CompaJ uses [Apache Commons Math](https://commons.apache.org/proper/commons-math/userguide/index.html) library for math operations. Learn about CompaJ math features in [Wiki](https://github.com/CrissNamon/compaj/wiki/CompaJ-Math).

#### Visualization
CompaJ uses widgets system for visualization in _WorkSpace_. Documentation will be released soon.
CompaJ uses widgets system for visualization in _WorkSpace_. Documentation available in [Wiki](https://github.com/CrissNamon/compaj/wiki/CompaJ-WorkSpace)

#### Other
All documentation and necessary information with tutorials will be released soon.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String toString() {
}

@Override
public double value(double[] doubles) {
public double value(double... doubles) {
return value(ArrayUtils.toObject(doubles));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void computeDerivatives(double v, double[] y, double[] yDot)
throws MaxCountExceededException, DimensionMismatchException {
int i = 0;
for (NamedFunction<String, Double, Double> entry : fns().values()) {
yDot[i] = entry.value(new Double[] {});
yDot[i] = entry.value();
i++;
}
if (iterator > 0) {
Expand All @@ -61,7 +61,8 @@ public String toString() {
return baseModel.toString();
}

public void a(NamedFunction<String, Double, Double>... e) {
@SafeVarargs
public final void a(NamedFunction<String, Double, Double>... e) {
baseModel.a(e);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tech.hiddenproject.compaj.core.data.base;

import java.util.Random;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("RealFunctionTest should > ")
public class RealFunctionTest {

@Test
@DisplayName("bind and execute function success")
public void valueTest() {
int expected = 5;

RealFunction realFunction = new RealFunction("MyFunction");
realFunction.b(x -> x[0] + x[1]);
Assertions.assertEquals(expected, realFunction.value(3, 2));
}

@Test
@DisplayName("bind single value and execute function success")
public void singleValueTest() {
RealFunction realFunction = new RealFunction("MyFunction");

double expected = new Random().nextDouble();
realFunction.b(expected);

Assertions.assertEquals(expected, realFunction.value());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tech.hiddenproject.compaj.core.data.base;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("RealVectorFunctionTest should > ")
public class RealVectorFunctionTest {

@Test
@DisplayName("bind and execute function success")
public void valueTest() {
Double[] expected = new Double[]{0d, 1d};

RealVectorFunction realFunction = new RealVectorFunction("MyFunction");
realFunction.b(x -> x);

Assertions.assertEquals(expected, realFunction.value(expected));
}

@Test
@DisplayName("bind single value and execute function success")
public void singleValueTest() {
Double[] expected = new Double[]{0d, 1d};

RealVectorFunction realFunction = new RealVectorFunction("MyFunction");
realFunction.b(expected);

Assertions.assertEquals(expected, realFunction.value());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public abstract class CompaJScriptBase extends Script {

private static Set<Extension> extensions;
private static final Set<Extension> extensions;

static {
extensions = new HashSet<>();
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
<module>gui</module>
<module>lang-extensions</module>
</modules>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
Expand All @@ -52,6 +66,16 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>*.java</include>
</includes>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
6 changes: 3 additions & 3 deletions repl/src/main/java/tech/hiddenproject/compaj/repl/CompaJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CompaJ {

private Translator translator;

{
static {
CompaJScriptBase.addExtension(new StarterExtension());
CompaJScriptBase.addExtension(new MathExtension());
CompaJScriptBase.addExtension(new ArrayRealVectorExtension());
Expand All @@ -42,7 +42,7 @@ public class CompaJ {
CompaJScriptBase.addExtension(new AgentExtension());
}

{
static {
GroovyTranslator.getImportCustomizer()
.addImports(normalImports);
}
Expand All @@ -54,7 +54,7 @@ public static void readFile(String url) {
try {
Path path = Paths.get(url);
String script = new String(Files.readAllBytes(path));
getInstance().getTranslator().evaluate(script);
readInput(script);
} catch (IOException e) {
System.out.println("Error: " + e.getLocalizedMessage());
}
Expand Down
16 changes: 9 additions & 7 deletions repl/src/main/java/tech/hiddenproject/compaj/repl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@

public class Main {

private static Translator translator;
public static void main(String... args) {
init();
processArgs(args);
System.out.println("Welcome to CompaJ REPL!");
System.out.println("Version 0.0.3");
processInput();
}

public static void main(String[] args) {
protected static void init() {
TranslatorUtils translatorUtils =
new GroovyTranslatorUtils();
GroovyTranslator.getImportCustomizer()
.addImports("tech.hiddenproject.compaj.repl.CompaJ")
.addStaticImport("tech.hiddenproject.compaj.repl.CompaJ", "exit");
translator = new GroovyTranslator(translatorUtils);
Translator translator = new GroovyTranslator(translatorUtils);
CompaJ.getInstance().setTranslator(translator);
processArgs(args);
System.out.println("Welcome to CompaJ REPL!");
System.out.println("Version 0.0.3");
processInput();
}

private static void processArgs(String... args) {
Expand Down
97 changes: 97 additions & 0 deletions repl/src/test/java/tech/hiddenproject/compaj/repl/REPLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package tech.hiddenproject.compaj.repl;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
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 tech.hiddenproject.compaj.extension.CompaJComplex;

@DisplayName("REPLTest should > ")
public class REPLTest {

private static final PrintStream STANDARD_OUT = System.out;
private static final ByteArrayOutputStream OUTPUT_CAPTOR = new ByteArrayOutputStream();
private static final ClassLoader CLASS_LOADER = REPLTest.class.getClassLoader();

@BeforeAll
public static void init() {
System.setOut(new PrintStream(OUTPUT_CAPTOR));
Main.init();
}

@BeforeEach
public void resetCaptor() {
OUTPUT_CAPTOR.reset();
}

@AfterAll
public static void release() {
System.setOut(STANDARD_OUT);
}

@Test
@DisplayName("read input from string")
public void inputStringTest() {
String expected = "5";

String script = "print " + expected;
CompaJ.readInput(script);

Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim());
}

@Test
@DisplayName("read input from file")
public void inputFileTest() {
URL fileUrl = CLASS_LOADER.getResource("test_script_1.cjn");

Assertions.assertNotNull(fileUrl);

String expected = "5";

CompaJ.readFile(fileUrl.getPath());

Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim());
}

@Test
@DisplayName("load CompaJComplex extension")
public void complexExtensionTest() {
String expected = new CompaJComplex(0, 1).toString();

String script = "print new CompaJComplex(0, 1)";
CompaJ.readInput(script);

Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim());
}

@Test
@DisplayName("load Math extension")
public void mathExtensionTest() {
String expected = doubleList().stream()
.map(Math::sin)
.collect(Collectors.toList()).toString();

String script = "print sin(" + doubleList().toString() + ")";

CompaJ.readInput("print \"\"");
CompaJ.readInput(script);

Assertions.assertEquals(expected, OUTPUT_CAPTOR.toString().trim());
}

private List<Double> doubleList() {
return DoubleStream.iterate(0.0, d -> d <= 10.0, d -> d + 1)
.boxed()
.collect(Collectors.toList());
}
}
1 change: 1 addition & 0 deletions repl/src/test/resources/test_script_1.cjn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print 5

0 comments on commit b3775af

Please sign in to comment.