1 /*
2  * Copyright (C) 2018 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 #include "gtest/gtest.h"
18 
19 #include "berberis/base/bit_util.h"
20 #include "berberis/guest_abi/function_wrappers.h"
21 #include "berberis/guest_state/guest_state.h"
22 #include "berberis/runtime_primitives/host_function_wrapper_impl.h"
23 
24 namespace berberis {
25 
26 namespace {
27 
TEST(TrampolineFuncGenerator,IntRes)28 TEST(TrampolineFuncGenerator, IntRes) {
29   struct Callee {
30     static int foo() { return 1; }
31   };
32 
33   TrampolineFunc func = GetTrampolineFunc<int(void)>();
34 
35   ProcessState state{};
36 
37   func(reinterpret_cast<void*>(Callee::foo), &state);
38 
39   EXPECT_EQ(1u, state.cpu.r[0]);
40 }
41 
TEST(TrampolineFuncGenerator,FloatArgs)42 TEST(TrampolineFuncGenerator, FloatArgs) {
43   struct Callee {
44     static void foo(void* p, float x, float y) {
45       EXPECT_EQ(nullptr, p);
46       EXPECT_EQ(0.5f, x);
47       EXPECT_EQ(0.75f, y);
48     }
49   };
50 
51   TrampolineFunc func = GetTrampolineFunc<void(void*, float, float)>();
52 
53   ProcessState state{};
54 
55   state.cpu.r[0] = 0u;
56   state.cpu.r[1] = bit_cast<uint32_t>(0.5f);
57   state.cpu.r[2] = bit_cast<uint32_t>(0.75f);
58 
59   EXPECT_NO_FATAL_FAILURE(func(reinterpret_cast<void*>(Callee::foo), &state));
60 }
61 
62 }  // namespace
63 
64 }  // namespace berberis
65