1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef CTS_TESTS_TESTS_NEURALNETWORKS_JAVA_TEST_JNI_UTILS_H
19 #define CTS_TESTS_TESTS_NEURALNETWORKS_JAVA_TEST_JNI_UTILS_H
20 
21 #include <android/log.h>
22 #include <jni.h>
23 
24 #include <cstdlib>
25 #include <cstring>
26 
27 // NOTE: All of the following macros requires a local variable `env` of type `JNIEnv*`.
28 
29 // Check the condition, signal a test failure and return if the condition is not met.
30 #define ASSERT_OR_RETURN(condition)                                                       \
31     if ((condition))                                                                      \
32         ;                                                                                 \
33     else if (!::android::nn::cts::fail(env, "assert failed on (%s) at %s:%d", #condition, \
34                                        __FILE__, __LINE__))                               \
35         ;                                                                                 \
36     else                                                                                  \
37         return
38 
39 #define ASSERT_OR_RETURN_FALSE(condition) ASSERT_OR_RETURN((condition)) false
40 #define ASSERT_OR_RETURN_NULL(condition) ASSERT_OR_RETURN((condition)) nullptr
41 #define ASSERT_OR_RETURN_ZERO(condition) ASSERT_OR_RETURN((condition)) 0
42 #define ASSERT_OR_RETURN_DEFAULT(condition) \
43     ASSERT_OR_RETURN((condition)) {}
44 
45 // Check and return if there is already a test failure
46 #define RETURN_IF_FAILED                  \
47     if (!::android::nn::cts::failed(env)) \
48         ;                                 \
49     else                                  \
50         return
51 
52 namespace android::nn::cts {
53 
54 // Check if there is already a test failure
failed(JNIEnv * env)55 inline bool failed(JNIEnv* env) {
56     return env->ExceptionCheck();
57 }
58 
59 // Throw a java exception to signal a test failure
60 // Will always return true for ASSERT_OR_RETURN
fail(JNIEnv * env,const char * format,...)61 inline bool fail(JNIEnv* env, const char* format, ...) {
62     // Return if a test failure is already signaled
63     if (failed(env)) return true;
64 
65     // Construct error message
66     va_list args;
67     va_start(args, format);
68     char* msg;
69     vasprintf(&msg, format, args);
70     va_end(args);
71 
72     // Throw exception
73     jclass testFailureClass = env->FindClass("android/neuralnetworks/cts/TestFailure");
74     env->ThrowNew(testFailureClass, msg);
75     free(msg);
76     return true;
77 }
78 
79 } // namespace android::nn::cts
80 
81 #endif // CTS_TESTS_TESTS_NEURALNETWORKS_JAVA_TEST_JNI_UTILS_H
82