From af9935ba0e0c6779b47ee484d198f63a8e9b8f6d Mon Sep 17 00:00:00 2001 From: rusefillc Date: Wed, 6 Mar 2024 09:42:51 -0500 Subject: [PATCH] only: JNI for test coverage fix #3965 --- .github/workflows/build-android.yaml | 5 - .github/workflows/build-rusEFI-console.yaml | 5 - java_console/io/build.gradle | 11 -- .../java/com/rusefi/native_/EngineLogic.java | 32 ---- .../java/com/rusefi/native_/JniSandbox.java | 40 ----- .../java/com/rusefi/native_/JniUnitTest.java | 91 ------------ unit_tests/Makefile | 1 - unit_tests/jenkins.sh | 40 ----- .../native/com_rusefi_native__EngineLogic.h | 101 ------------- unit_tests/native/native_impl.cpp | 139 ------------------ unit_tests/unit_test_rules.mk | 22 --- 11 files changed, 487 deletions(-) delete mode 100644 java_console/io/src/main/java/com/rusefi/native_/EngineLogic.java delete mode 100644 java_console/io/src/test/java/com/rusefi/native_/JniSandbox.java delete mode 100644 java_console/io/src/test/java/com/rusefi/native_/JniUnitTest.java delete mode 100644 unit_tests/jenkins.sh delete mode 100644 unit_tests/native/com_rusefi_native__EngineLogic.h delete mode 100644 unit_tests/native/native_impl.cpp diff --git a/.github/workflows/build-android.yaml b/.github/workflows/build-android.yaml index 2f0ddd4576..6df1a936cc 100644 --- a/.github/workflows/build-android.yaml +++ b/.github/workflows/build-android.yaml @@ -39,11 +39,6 @@ jobs: working-directory: . run: gcc -v - - name: Build Native Unit Tests as shared library - working-directory: ./unit_tests/ - # we have a unit test of JNI thus we need to build shared library - run: make -j4 SANITIZE=no build/lib_rusefi_test - - name: Build Android env: RUSEFI_ANDROID_KEYPASS: ${{ secrets.RUSEFI_ANDROID_KEYPASS }} diff --git a/.github/workflows/build-rusEFI-console.yaml b/.github/workflows/build-rusEFI-console.yaml index 7bc4c4026a..873d5a0f21 100644 --- a/.github/workflows/build-rusEFI-console.yaml +++ b/.github/workflows/build-rusEFI-console.yaml @@ -45,11 +45,6 @@ jobs: - name: Print GCC version run: gcc -v - - name: Build Native Unit Tests as shared library - working-directory: ./unit_tests/ - # we have a unit test of JNI thus we need to build shared library - run: make -j4 SANITIZE=no build/lib_rusefi_test - - name: Test console # at the moment 'jar' task does not depend on tests?! maybe because tests take some time? working-directory: ./java_tools diff --git a/java_console/io/build.gradle b/java_console/io/build.gradle index c7cf26c5b6..cff788d8f2 100644 --- a/java_console/io/build.gradle +++ b/java_console/io/build.gradle @@ -32,14 +32,3 @@ dependencies { testFixturesApi global_libs.annotations testFixturesApi global_libs.junit5api } - -tasks.register('copyJniHeader', Copy) { - dependsOn("compileJava") - from 'build/generated/sources/headers/java/main' - into "../../unit_tests/native" - include 'com*.h' -} - -test { - dependsOn("copyJniHeader") -} diff --git a/java_console/io/src/main/java/com/rusefi/native_/EngineLogic.java b/java_console/io/src/main/java/com/rusefi/native_/EngineLogic.java deleted file mode 100644 index bd1b8e4751..0000000000 --- a/java_console/io/src/main/java/com/rusefi/native_/EngineLogic.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.rusefi.native_; - -/** - * Native interface allowing java to invoke rusEFI unit_tests library - * - * See com_rusefi_native__EngineLogic.h - * See native_impl.cpp - */ -public class EngineLogic { - public static native String getVersion(); - - // huh? I am having trouble making this method static? - public static native void resetTest(); - - public native void setSensor(String sensorTypeName, double value); - - public native byte[] getConfiguration(); - - public native void setConfiguration(byte[] content, int offset, int size); - - public native void setEngineType(int engineType); - - public native void initTps(); - - public native void invokePeriodicCallback(); - - public native void invokeEtbCycle(); - - public native void burnRequest(); - - public native byte[] getOutputs(); -} diff --git a/java_console/io/src/test/java/com/rusefi/native_/JniSandbox.java b/java_console/io/src/test/java/com/rusefi/native_/JniSandbox.java deleted file mode 100644 index e9d40c273c..0000000000 --- a/java_console/io/src/test/java/com/rusefi/native_/JniSandbox.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.rusefi.native_; - -import com.rusefi.FileLog; - -import javax.xml.bind.SchemaOutputResolver; -import java.io.File; -import java.util.Arrays; - -/** - * Following VM option is needed to launch: - * -Djava.library.path=../unit_tests/build - */ -public class JniSandbox { - - private static final String LIBNAME = "_rusefi_test"; - - public static void main(String[] args) { - loadLibrary(); - - EngineLogic engineLogic = new EngineLogic(); - System.out.println(engineLogic.getVersion()); - engineLogic.setSensor("clt", 90); - engineLogic.setConfiguration(new byte[4], 24, 14); - } - - public static void loadLibrary() { - String libPath = System.getProperty("java.library.path"); - System.out.println("Looking for libraries at " + libPath); - String[] list = new File(libPath).list((dir, name) -> name.contains(LIBNAME)); - System.out.println("Matching files: " + Arrays.toString(list)); - if (list.length == 0) { - if (FileLog.isWindows()) { - System.out.println("Have you invoked `make -j4 SANITIZE=no build/_rusefi_test'?"); - } else { - System.out.println("Have you invoked `make -j4 SANITIZE=no build/lib_rusefi_test`?"); - } - } - System.loadLibrary(LIBNAME); - } -} diff --git a/java_console/io/src/test/java/com/rusefi/native_/JniUnitTest.java b/java_console/io/src/test/java/com/rusefi/native_/JniUnitTest.java deleted file mode 100644 index 100390f69a..0000000000 --- a/java_console/io/src/test/java/com/rusefi/native_/JniUnitTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.rusefi.native_; - -import com.opensr5.ConfigurationImage; -import com.rusefi.config.Field; -import com.rusefi.config.generated.Fields; -import com.rusefi.core.Sensor; -import com.rusefi.enums.SensorType; -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static com.rusefi.config.generated.Fields.TS_FILE_VERSION; -import static com.rusefi.config.generated.Fields.engine_type_e_FRANKENSO_MAZDA_MIATA_2003; -import static com.rusefi.core.FileUtil.littleEndianWrap; -import static junit.framework.Assert.*; - -public class JniUnitTest { - private static final double EPS = 0.001; - - @Before - public void reset() { - JniSandbox.loadLibrary(); - EngineLogic.resetTest(); - } - - @Test - public void run() { - String version = EngineLogic.getVersion(); - assertTrue("Got " + version, version.contains("Hello")); - - EngineLogic engineLogic = new EngineLogic(); - engineLogic.invokePeriodicCallback(); - - assertEquals(TS_FILE_VERSION, (int) getValue(engineLogic.getOutputs(), Sensor.FIRMWARE_VERSION)); - - assertEquals(14.0, getValue(engineLogic.getOutputs(), Sensor.afrTarget)); - - double veValue = getValue(engineLogic.getOutputs(), Sensor.veValue); - assertTrue("veValue", veValue > 40 && veValue < 90); - -// assertEquals(18.11, getValue(engineLogic.getOutputs(), Sensor.runningFuel)); - - engineLogic.setSensor(SensorType.Rpm.name(), 4000); - engineLogic.invokePeriodicCallback(); - assertEquals(4000.0, getValue(engineLogic.getOutputs(), Sensor.RPMValue)); - - // assertEquals(18.11, getValue(engineLogic.getOutputs(), Sensor.runningFuel)); - - assertEquals(0.25096, getValue(engineLogic.getOutputs(), Sensor.sdAirMassInOneCylinder), 0.0001); - - engineLogic.setEngineType(engine_type_e_FRANKENSO_MAZDA_MIATA_2003); - assertEquals(3.76, getField(engineLogic, Fields.GEARRATIO1), EPS); - } - - @Test - public void testEtbStuff() { - EngineLogic engineLogic = new EngineLogic(); - - engineLogic.setSensor(SensorType.Tps1Primary.name(), 30); - engineLogic.setSensor(SensorType.Tps1Secondary.name(), 30); - - engineLogic.burnRequest(); // hack: this is here to initialize engine helper prior to mocking sensors - - engineLogic.setSensor(SensorType.AcceleratorPedalPrimary.name(), 40); - engineLogic.setSensor(SensorType.AcceleratorPedalSecondary.name(), 40); - - engineLogic.setConfiguration(new byte[]{3}, Fields.TPS1_1ADCCHANNEL.getTotalOffset(), 1); - engineLogic.setConfiguration(new byte[]{3}, Fields.TPS1_2ADCCHANNEL.getTotalOffset(), 1); - engineLogic.setConfiguration(new byte[]{3}, Fields.THROTTLEPEDALPOSITIONADCCHANNEL.getTotalOffset(), 1); - engineLogic.setConfiguration(new byte[]{3}, Fields.THROTTLEPEDALPOSITIONSECONDADCCHANNEL.getTotalOffset(), 1); - - engineLogic.initTps(); - engineLogic.burnRequest(); - System.out.println("engineLogic.invokeEtbCycle"); - engineLogic.invokeEtbCycle(); - - assertEquals("ETB duty", 120.42, getValue(engineLogic.getOutputs(), Sensor.etb1DutyCycle)); - } - - private double getField(EngineLogic engineLogic, Field field) { - byte[] configuration = engineLogic.getConfiguration(); - assertNotNull("configuration", configuration); - return field.getValue(new ConfigurationImage(configuration), field.getScale()); - } - - private double getValue(byte[] outputs, Sensor sensor) { - ByteBuffer bb = littleEndianWrap(outputs, sensor.getOffset(), 4); - return sensor.getValueForChannel(bb) * sensor.getScale(); - } -} diff --git a/unit_tests/Makefile b/unit_tests/Makefile index d77e1a3ec6..2f876408ec 100644 --- a/unit_tests/Makefile +++ b/unit_tests/Makefile @@ -37,7 +37,6 @@ CPPSRC += $(ALLCPPSRC) \ $(PROJECT_DIR)/config/boards/hellen/hellen_board_id.cpp \ $(PROJECT_DIR)/hw_layer/drivers/can/can_hw.cpp \ $(PROJECT_DIR)/../unit_tests/logicdata.cpp \ - $(PROJECT_DIR)/../unit_tests/native/native_impl.cpp \ $(PROJECT_DIR)/../unit_tests/main.cpp \ $(PROJECT_DIR)/../unit_tests/global_mocks.cpp \ $(PROJECT_DIR)/../unit_tests/mocks.cpp \ diff --git a/unit_tests/jenkins.sh b/unit_tests/jenkins.sh deleted file mode 100644 index 67df86f044..0000000000 --- a/unit_tests/jenkins.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - echo "The PATH is ${PATH}" - cd "${WORKSPACE}" - echo "start in workspace ${PWD}" - - cd firmware - echo "CD to ${PWD}" - - rm -fR .dep - rm -fR build - make - - if [ ! -f build/rusefi.hex ]; then - echo "Firmware compilation failed" - exit -1 - fi - - cd "${WORKSPACE}/win32_algo_tests" - echo "CD to ${PWD}" - - rm -fR .dep - rm -fR build - make - if [ ! -f build/rusefi_test ]; then - echo "test compilation failed" - exit -1 - fi - -# we want to terminate if test fails -set -e - - # invoke the tests - hopefully error code would be propagated? - build/rusefi_test - -cd "${WORKSPACE}/java_console" -echo "CD to ${PWD}" - -#JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64 -#ant - diff --git a/unit_tests/native/com_rusefi_native__EngineLogic.h b/unit_tests/native/com_rusefi_native__EngineLogic.h deleted file mode 100644 index 414966856f..0000000000 --- a/unit_tests/native/com_rusefi_native__EngineLogic.h +++ /dev/null @@ -1,101 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_rusefi_native__EngineLogic */ - -#ifndef _Included_com_rusefi_native__EngineLogic -#define _Included_com_rusefi_native__EngineLogic -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: com_rusefi_native__EngineLogic - * Method: getVersion - * Signature: ()Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_com_rusefi_native_1_EngineLogic_getVersion - (JNIEnv *, jclass); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: resetTest - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_resetTest - (JNIEnv *, jclass); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: setSensor - * Signature: (Ljava/lang/String;D)V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_setSensor - (JNIEnv *, jobject, jstring, jdouble); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: getConfiguration - * Signature: ()[B - */ -JNIEXPORT jbyteArray JNICALL Java_com_rusefi_native_1_EngineLogic_getConfiguration - (JNIEnv *, jobject); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: setConfiguration - * Signature: ([BII)V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_setConfiguration - (JNIEnv *, jobject, jbyteArray, jint, jint); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: setEngineType - * Signature: (I)V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_setEngineType - (JNIEnv *, jobject, jint); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: initTps - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_initTps - (JNIEnv *, jobject); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: invokePeriodicCallback - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_invokePeriodicCallback - (JNIEnv *, jobject); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: invokeEtbCycle - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_invokeEtbCycle - (JNIEnv *, jobject); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: burnRequest - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_burnRequest - (JNIEnv *, jobject); - -/* - * Class: com_rusefi_native__EngineLogic - * Method: getOutputs - * Signature: ()[B - */ -JNIEXPORT jbyteArray JNICALL Java_com_rusefi_native_1_EngineLogic_getOutputs - (JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/unit_tests/native/native_impl.cpp b/unit_tests/native/native_impl.cpp deleted file mode 100644 index e5c3050838..0000000000 --- a/unit_tests/native/native_impl.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * native_impl.cpp - * -# TODO: add validation to assert that we do not have Windows slash in JAVA_HOME variable -# for instance "C:/Progra~1/Zulu/zulu-11" would be good "C:\Progra~1\Zulu\zulu-11" would be bad - * - * see -I$(JAVA_HOME) - * - * @date Feb 26, 2022 - * @author Andrey Belomutskiy, (c) 2012-2022 - */ - -#include "pch.h" -#include "com_rusefi_native__EngineLogic.h" -#include "auto_generated_sensor.h" -#include "tunerstudio.h" -#include "live_data.h" -#include "init.h" - -#include - -static std::unique_ptr ethPtr; - -static EngineTestHelper* getEth() { - if (!ethPtr) { - printf("make_unique(TEST_ENGINE)\n"); - ethPtr = std::make_unique(engine_type_e::TEST_ENGINE); - } - return ethPtr.get(); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_resetTest - (JNIEnv *, jclass) { - printf("[native] resetTest\n"); - ethPtr = nullptr; -} - -JNIEXPORT jstring JNICALL Java_com_rusefi_native_1_EngineLogic_getVersion(JNIEnv * env, jclass) { - const char msg[60] = "Hello from unit tests"; - const char* fileName = msg; - jstring result = env->NewStringUTF(msg); - printf("[native] hello\n"); - return result; -} - -JNIEXPORT jbyteArray JNICALL Java_com_rusefi_native_1_EngineLogic_getConfiguration(JNIEnv *env, jobject instance) { - jbyteArray retVal = env->NewByteArray(sizeof(engine_configuration_s)); - jbyte *buf = env->GetByteArrayElements(retVal, NULL); - EngineTestHelper* eth = getEth(); - - memcpy(buf, (const void*)ð->persistentConfig.engineConfiguration, sizeof(engine_configuration_s)); - env->ReleaseByteArrayElements(retVal, buf, 0); - - return retVal; -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_setConfiguration(JNIEnv *env, jobject instance, - jbyteArray data, jint offset, jint size) { - EngineTestHelper* eth = getEth(); - - printf("[native] setConfiguration offset=%d size=%d\n", offset, size); - jbyte *buf = env->GetByteArrayElements(data, NULL); - int printSize = size > 4 ? 4 : size; - for (int i = 0;i < printSize;i++) { - printf("[native] buf[%d] %x\n", i, buf[i]); - } - memcpy(((uint8_t*)ð->persistentConfig.engineConfiguration) + offset, buf, size); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_setSensor - (JNIEnv *env, jobject instance, jstring sensorName, jdouble sensorValue) { - - const char *sensorNameNative = env->GetStringUTFChars(sensorName, 0); - - SensorType type = findSensorTypeByName(sensorNameNative); - - printf("[native] Setting [%s] to %f\n", getSensorType(type), sensorValue); - Sensor::setMockValue(type, sensorValue); - - env->ReleaseStringUTFChars(sensorName, sensorNameNative); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_setEngineType - (JNIEnv *, jobject, jint engineType) { - EngineTestHelper* eth = getEth(); - resetConfigurationExt((engine_type_e)engineType); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_invokePeriodicCallback - (JNIEnv *, jobject) { - - EngineTestHelper* eth = getEth(); - eth->engine.periodicSlowCallback(); - eth->engine.periodicFastCallback(); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_invokeEtbCycle - (JNIEnv *, jobject) { - EngineTestHelper* eth = getEth(); - Engine *engine = ð->engine; - - printf("[native] invokeEtbCycle\n"); - for (int i = 0; i < ETB_COUNT; i++) { - if (auto etb = engine->etbControllers[i]) { - etb->update(); - } - } -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_burnRequest - (JNIEnv *, jobject) { - printf("[native] onBurnRequest\n"); - getEth();// just to make sure we have initialized - onBurnRequest(); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_initTps - (JNIEnv *, jobject) { - printf("[native] initTps\n"); - getEth();// just to make sure we have initialized - initTps(); -} - -JNIEXPORT void JNICALL Java_com_rusefi_native_1_EngineLogic_initTps2 - (JNIEnv *, jobject) { - printf("[native] initTps2\n"); -} - - -JNIEXPORT jbyteArray JNICALL Java_com_rusefi_native_1_EngineLogic_getOutputs(JNIEnv * env, jobject instance) { - jbyteArray retVal = env->NewByteArray(TS_TOTAL_OUTPUT_SIZE); - jbyte *buf = env->GetByteArrayElements(retVal, NULL); - EngineTestHelper* eth = getEth(); - updateTunerStudioState(); - copyRange((uint8_t*)buf, getLiveDataFragments(), 0, TS_TOTAL_OUTPUT_SIZE); - env->ReleaseByteArrayElements(retVal, buf, 0); - - return retVal; -} diff --git a/unit_tests/unit_test_rules.mk b/unit_tests/unit_test_rules.mk index c8c5835339..8a8632d9b1 100644 --- a/unit_tests/unit_test_rules.mk +++ b/unit_tests/unit_test_rules.mk @@ -135,28 +135,6 @@ OD = $(TRGT)objdump HEX = $(CP) -O ihex BIN = $(CP) -O binary -ifndef JAVA_HOME -$(error JAVA_HOME is undefined - due to JNI integration unit tests depend on JAVA_HOME) -endif - -ifneq (1,$(words [$(JAVA_HOME)])) -$(error JAVA_HOME $(JAVA_HOME) seems to contain spaces this would not work well. please use folder name without space often progra~1) -endif - -AOPT = -fPIC -I$(JAVA_HOME)/include - -ifeq ($(OS),Windows_NT) -# TODO: add validation to assert that we do not have Windows slash in JAVA_HOME variable -# for instance "C:/Progra~1/Zulu/zulu-11" would be good "C:\Progra~1\Zulu\zulu-11" would be bad - AOPT += -I$(JAVA_HOME)/include/win32 -else - ifeq ($(IS_MAC),yes) - AOPT += -I$(JAVA_HOME)/include/darwin - else - AOPT += -I$(JAVA_HOME)/include/linux - endif -endif - # Define C warning options here CWARN = -Wall -Wextra -Wstrict-prototypes -pedantic -Wmissing-prototypes -Wold-style-definition