diff --git a/unit_tests/projects/vectormath_unit_tests_android/app/build.gradle b/unit_tests/projects/vectormath_unit_tests_android/app/build.gradle index d532e79..5712b0f 100644 --- a/unit_tests/projects/vectormath_unit_tests_android/app/build.gradle +++ b/unit_tests/projects/vectormath_unit_tests_android/app/build.gradle @@ -17,6 +17,7 @@ android { externalNativeBuild { cmake { cppFlags '-std=c++17' + arguments "-DANDROID_TOOLCHAIN=clang" } } } diff --git a/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/CMakeLists.txt b/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/CMakeLists.txt index f867e81..9b5bc18 100644 --- a/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/CMakeLists.txt +++ b/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/CMakeLists.txt @@ -22,12 +22,21 @@ add_library( # Sets the name of the library. # Provides a relative path to your source file(s). ../../../../../../cases/test_math_ex.cpp + + ../../../../../../cases/test_int32.cpp + ../../../../../../cases/test_uint32.cpp + ../../../../../../cases/test_mat4.cpp ../../../../../../cases/test_vec2.cpp ../../../../../../cases/test_vec3.cpp ../../../../../../cases/test_vec4.cpp + ../../../../../../cases/test_vec4_swizzles.cpp - native-lib.cpp + ../../../../../../cases/test_vec2_swizzles_clang.cpp + ../../../../../../cases/test_vec3_swizzles_clang.cpp + ../../../../../../cases/test_vec4_swizzles_clang.cpp + + vectormath_unit_tests_android.cpp ) include_directories( # Sets the name of the library. @@ -60,4 +69,13 @@ target_link_libraries( # Specifies the target library. # Links the target library to the log library # included in the NDK. - ${log-lib}) \ No newline at end of file + ${log-lib}) + +target_compile_options( + vectormath_unit_tests_android + + PRIVATE + + -DVECTORMATH_USE_EXACT_PRECISION + -DVECTORMATH_USE_CLANG_EXT + ) \ No newline at end of file diff --git a/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/native-lib.cpp b/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/vectormath_unit_tests_android.cpp similarity index 56% rename from unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/native-lib.cpp rename to unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/vectormath_unit_tests_android.cpp index bbe8ac4..ca764c1 100644 --- a/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/native-lib.cpp +++ b/unit_tests/projects/vectormath_unit_tests_android/app/src/main/cpp/vectormath_unit_tests_android.cpp @@ -1,5 +1,10 @@ #include #include +#include + +#define UNIT_TEST_EXIT(exit_code) end_unit_test(exit_code) + +static void end_unit_test(int exit_code); #define RUN_UNIT_TESTS #define UNIT_TESTS_MAIN runAllTestCases @@ -9,9 +14,19 @@ extern "C" JNIEXPORT jboolean JNICALL Java_io_github_maihd_vectormath_1unit_1tests_1android_MainActivity_runAllTestCases( JNIEnv* env, jobject /* this */) { - (void)env; + JavaVM* vm; + env->GetJavaVM(&vm); + vm->AttachCurrentThread(&env, 0); const char* argv[] = {}; const int argc = 0; return runAllTestCases(argc, argv) == 0; -} \ No newline at end of file +} + +static void end_unit_test(int exit_code) +{ + pthread_exit(nullptr); + exit(exit_code); +} + +//! EOF diff --git a/unit_tests/projects/vectormath_unit_tests_android/app/src/main/java/io/github/maihd/vectormath_unit_tests_android/MainActivity.kt b/unit_tests/projects/vectormath_unit_tests_android/app/src/main/java/io/github/maihd/vectormath_unit_tests_android/MainActivity.kt index e71c358..b31c175 100644 --- a/unit_tests/projects/vectormath_unit_tests_android/app/src/main/java/io/github/maihd/vectormath_unit_tests_android/MainActivity.kt +++ b/unit_tests/projects/vectormath_unit_tests_android/app/src/main/java/io/github/maihd/vectormath_unit_tests_android/MainActivity.kt @@ -2,6 +2,7 @@ package io.github.maihd.vectormath_unit_tests_android import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.os.HandlerThread import android.widget.TextView import io.github.maihd.vectormath_unit_tests_android.databinding.ActivityMainBinding @@ -16,7 +17,12 @@ class MainActivity : AppCompatActivity() { setContentView(binding.root) // Example of a call to a native method - binding.sampleText.text = if (runAllTestCases()) "Test Succeed!" else "Test failed"; + binding.sampleText.text = "Running unit tests" + + val thread = Thread("vectormath_unit_tests") + thread.run { + binding.sampleText.text = if (runAllTestCases()) "Test Succeed!" else "Test failed" + } } /** diff --git a/unit_tests/test_framework.h b/unit_tests/test_framework.h index 6904e07..49366bb 100644 --- a/unit_tests/test_framework.h +++ b/unit_tests/test_framework.h @@ -113,6 +113,10 @@ extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(void); #pragma comment(lib, "User32.lib") #endif +#if defined(__ANDROID__) +#include +#endif + static UnitTest* gUnitTests = nullptr; static UnitTest* gCurrentUnitTest = nullptr; @@ -158,7 +162,13 @@ static_assert(!IS_DEFINED(__UNDEFINED_MACRO), "IS_DEFINED is wrong!"); #define UNIT_TEST_GOTO_FILE_COMMAND "code --goto %s:%d" #endif -#ifdef EMSCRIPTEN +#ifdef __ANDROID__ +#define PRINTF(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "vectormath", fmt, ##__VA_ARGS__) +#else +#define PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) +#endif + +#if defined(EMSCRIPTEN) || defined(__ANDROID__) #define PRINTF_COLOR_RED #define PRINTF_COLOR_BLUE #define PRINTF_COLOR_BLACK @@ -177,6 +187,10 @@ static_assert(!IS_DEFINED(__UNDEFINED_MACRO), "IS_DEFINED is wrong!"); #define PRINTF_STRING_GREEN(string) PRINTF_COLOR_GREEN string PRINTF_COLOR_BLACK #define PRINTF_STRING_YELLOW(string) PRINTF_COLOR_YELLOW string PRINTF_COLOR_BLACK +#ifndef UNIT_TEST_EXIT +#define UNIT_TEST_EXIT(exit_code) exit(exit_code) +#endif + static void NotifyProgammer(const char* title, const char* message); UnitTest::UnitTest(const char* name, UnitTestFunc* func, const char* file, int line) @@ -264,7 +278,7 @@ void UnitTest::HandleExitAfterFailed() if (!IS_DEFINED(CONTINUE_UNIT_TEST_ON_FAIL)) { - exit(gUnitTestsExitCode); + UNIT_TEST_EXIT(gUnitTestsExitCode); } } @@ -279,7 +293,7 @@ int main(const int argc, const char* argv[]) gUnitTestsLogHeader = argv[1]; } - printf( + PRINTF( PRINTF_STRING_BLUE("%s") " Start running " PRINTF_STRING_BLUE("%d") " unit tests...\n", gUnitTestsLogHeader, gUnitTestsCount ); @@ -296,7 +310,7 @@ int main(const int argc, const char* argv[]) gUnitTestsRunCount++; // Run unit test - printf( + PRINTF( PRINTF_STRING_BLUE("%s")" Running new unit test (%d/" PRINTF_STRING_BLUE("%d") ")\n" " Name: " PRINTF_STRING_YELLOW("%s") "\n" " Location: " PRINTF_STRING_YELLOW("%s:%d") "\n", @@ -329,7 +343,7 @@ int main(const int argc, const char* argv[]) break; } - printf( + PRINTF( " Status: %s\n\n", statusName ); @@ -344,7 +358,7 @@ int main(const int argc, const char* argv[]) // Prompt complete information if (gUnitTestsRunCount != gUnitTestsCount) { - printf( + PRINTF( PRINTF_STRING_BLUE("%s") " " PRINTF_STRING_RED("FAILURE") @@ -354,7 +368,7 @@ int main(const int argc, const char* argv[]) } else { - printf( + PRINTF( PRINTF_STRING_BLUE("%s") " " PRINTF_STRING_GREEN("SUCCESS") @@ -365,7 +379,7 @@ int main(const int argc, const char* argv[]) } // Display stats - printf( + PRINTF( " - Untested: " PRINTF_STRING_YELLOW("%d") " unit tests.\n" " - Succeed: " PRINTF_STRING_GREEN("%d") " unit tests.\n" " - Failed: " PRINTF_STRING_RED("%d") " unit tests.\n" @@ -381,6 +395,8 @@ void NotifyProgammer(const char* title, const char* message) #if defined(_WIN32) MessageBeep(MIM_ERROR); MessageBoxA(NULL, message, title, MB_OK); + #elif defined(__ANDROID__) + __android_log_print(ANDROID_LOG_ERROR, "vectormath", "%s: %s", title, message); #else printf("%s", message); fgetc(stdin);