1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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
16 #include <gtest/gtest.h>
17
18 #include <cstdint>
19
20 #include "runtime/include/runtime_options.h"
21 #include "runtime/interpreter/frame.h"
22
23 namespace panda::test {
24
CreateFrame(size_t nregs,Method * method,Frame * prev)25 Frame *CreateFrame(size_t nregs, Method *method, Frame *prev)
26 {
27 Frame *mem = static_cast<Frame *>(aligned_alloc(8U, panda::Frame::GetSize(nregs)));
28 return (new (mem) panda::Frame(method, prev, nregs));
29 }
30
FreeFrame(Frame * f)31 void FreeFrame(Frame *f)
32 {
33 std::free(f);
34 }
35
TEST(Frame,Test)36 TEST(Frame, Test)
37 {
38 Frame *f = panda::test::CreateFrame(2, nullptr, nullptr);
39 f->GetVReg(0).MarkAsObject();
40 EXPECT_TRUE(f->GetVReg(0).HasObject());
41
42 f->GetVReg(0).MarkAsPrimitive();
43 EXPECT_FALSE(f->GetVReg(0).HasObject());
44
45 int64_t v64 = 0x1122334455667788;
46 f->GetVReg(0).MarkAsObject();
47 f->GetVReg(0).SetPrimitive(v64);
48 EXPECT_EQ(f->GetVReg(0).GetLong(), v64);
49 EXPECT_EQ(f->GetVReg(0).GetAs<int64_t>(), v64);
50
51 f->GetVReg(1).MarkAsObject();
52 f->GetVReg(1).MoveFrom(f->GetVReg(0));
53 EXPECT_FALSE(f->GetVReg(0).HasObject());
54 EXPECT_EQ(f->GetVReg(0).Get(), static_cast<int32_t>(v64));
55
56 f->GetVReg(1).MarkAsObject();
57 f->GetVReg(1).MoveFrom(f->GetVReg(0));
58 EXPECT_FALSE(f->GetVReg(0).HasObject());
59 EXPECT_EQ(f->GetVReg(0).GetLong(), v64);
60
61 ObjectHeader *ptr = reinterpret_cast<ObjectHeader *>(0x11223344);
62 f->GetVReg(0).SetReference(ptr);
63 f->GetVReg(1).MarkAsPrimitive();
64 f->GetVReg(1).MoveFromObj(f->GetVReg(0));
65 EXPECT_TRUE(f->GetVReg(0).HasObject());
66 EXPECT_EQ(f->GetVReg(0).GetReference(), ptr);
67
68 int32_t v32 = 0x11223344;
69 f->GetVReg(0).MarkAsObject();
70 f->GetVReg(0).SetPrimitive(v32);
71 EXPECT_EQ(f->GetVReg(0).Get(), v32);
72 EXPECT_EQ(f->GetVReg(0).GetAs<int32_t>(), v32);
73
74 int16_t v16 = 0x1122;
75 f->GetVReg(0).MarkAsObject();
76 f->GetVReg(0).SetPrimitive(v16);
77 EXPECT_EQ(f->GetVReg(0).Get(), v16);
78 EXPECT_EQ(f->GetVReg(0).GetAs<int32_t>(), v16);
79
80 int8_t v8 = 0x11;
81 f->GetVReg(0).MarkAsObject();
82 f->GetVReg(0).SetPrimitive(v8);
83 EXPECT_EQ(f->GetVReg(0).Get(), v8);
84 EXPECT_EQ(f->GetVReg(0).GetAs<int32_t>(), v8);
85
86 float f32 = 123.5;
87 f->GetVReg(0).MarkAsObject();
88 f->GetVReg(0).SetPrimitive(f32);
89 EXPECT_EQ(f->GetVReg(0).GetFloat(), f32);
90 EXPECT_EQ(f->GetVReg(0).GetAs<float>(), f32);
91
92 double f64 = 456.7;
93 f->GetVReg(0).MarkAsObject();
94 f->GetVReg(0).SetPrimitive(f64);
95 EXPECT_EQ(f->GetVReg(0).GetDouble(), f64);
96 EXPECT_EQ(f->GetVReg(0).GetAs<double>(), f64);
97
98 panda::test::FreeFrame(f);
99 }
100
101 } // namespace panda::test
102