1 // Copyright (C) 2024 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 class GTestifierTest : public testing::Test {
18  public:
GTestifierTest(std::function<int ()> func,std::function<bool (int)> predicate,std::string test_name)19   GTestifierTest(std::function<int()> func, std::function<bool(int)> predicate,
20                  std::string test_name)
21       : child_test_(func), predicate_(predicate), test_name_(test_name) {}
TestBody()22   void TestBody() {
23     int result = child_test_();
24     bool pass = predicate_ ? predicate_(result) : result == 0;
25     if (!pass) {
26       FAIL() << "Test " << test_name_ << " failed, result " << result;
27     }
28   }
29 
30  private:
31   std::function<int()> child_test_;
32   std::function<bool(int)> predicate_;
33   std::string test_name_;
34 };
35 
registerGTestifierTest(const char * test_suite_name,const char * test_name,const char * file,int line,int (* func)(),bool (* predicate)(int))36 extern "C" void registerGTestifierTest(const char* test_suite_name, const char* test_name,
37                                        const char* file, int line, int (*func)(),
38                                        bool (*predicate)(int)) {
39   testing::RegisterTest(
40       test_suite_name, test_name, nullptr, nullptr, file, line,
41       [=]() -> GTestifierTest* { return new GTestifierTest(func, predicate, test_name); });
42 }
43