catchorg / catchorg/Catch2

How to integrate Catch2 with NDK?

Open
#1,352 5 comments 0 reactions 0 assignees View on GitHub
Query Resolved - pending review
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

I don't think we can [Supplying main() yourself](https://github.com/catchorg/Catch2/blob/master/docs/own-main.md#supplying-main-yourself) in NDK environment. There is no `main` entry point function in a NDK lib.

Following is what I have done to use Catch2 in a NDK based lib.

__native-lib.cpp__
```cpp
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

extern "C"
JNIEXPORT JNICALL
int Java_com_example_someproj_MyTest_runTest(JNIEnv *env, jclass type) {
char *argv[] = {"whatever"};
int result = Catch::Session().run(1, argv);//fake `argc` and `argv`
return result;
}

unsigned int Factorial( unsigned int number ) {
return number <= 1 ? number : Factorial(number-1)*number;
}

//some example tests
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}
```

__MyTest.java__
```java
package com.example.someproj;

public class MyTest {
static {
System.loadLibrary("native-lib");
}
public void runNativeTest() {//this is called somewhere to execute Catch2 tests
assertTrue("Some test doesn't not past.", 0 == runTest());
}

public static native int runTest();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.