Skip to content

Commit

Permalink
unit_test: better android test
Browse files Browse the repository at this point in the history
  • Loading branch information
maihd committed Mar 27, 2024
1 parent 8e089a4 commit 9a8ef2a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ android {
externalNativeBuild {
cmake {
cppFlags '-std=c++17'
arguments "-DANDROID_TOOLCHAIN=clang"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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})
${log-lib})

target_compile_options(
vectormath_unit_tests_android

PRIVATE

-DVECTORMATH_USE_EXACT_PRECISION
-DVECTORMATH_USE_CLANG_EXT
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <jni.h>
#include <string>
#include <pthread.h>

#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
Expand All @@ -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;
}
}

static void end_unit_test(int exit_code)
{
pthread_exit(nullptr);
exit(exit_code);
}

//! EOF
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
}
}

/**
Expand Down
32 changes: 24 additions & 8 deletions unit_tests/test_framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(void);
#pragma comment(lib, "User32.lib")
#endif

#if defined(__ANDROID__)
#include <android/log.h>
#endif

static UnitTest* gUnitTests = nullptr;
static UnitTest* gCurrentUnitTest = nullptr;

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -264,7 +278,7 @@ void UnitTest::HandleExitAfterFailed()

if (!IS_DEFINED(CONTINUE_UNIT_TEST_ON_FAIL))
{
exit(gUnitTestsExitCode);
UNIT_TEST_EXIT(gUnitTestsExitCode);
}
}

Expand All @@ -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
);
Expand All @@ -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",
Expand Down Expand Up @@ -329,7 +343,7 @@ int main(const int argc, const char* argv[])
break;
}

printf(
PRINTF(
" Status: %s\n\n", statusName
);

Expand All @@ -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")
Expand All @@ -354,7 +368,7 @@ int main(const int argc, const char* argv[])
}
else
{
printf(
PRINTF(
PRINTF_STRING_BLUE("%s")
" "
PRINTF_STRING_GREEN("SUCCESS")
Expand All @@ -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"
Expand All @@ -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);
Expand Down

0 comments on commit 9a8ef2a

Please sign in to comment.