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 "runtime/include/class-inl.h"
19 #include "runtime/include/class_linker.h"
20 #include "runtime/include/coretypes/array.h"
21 #include "runtime/include/runtime.h"
22
23 namespace panda::coretypes::test {
24
25 class ArrayTest : public testing::Test {
26 public:
ArrayTest()27 ArrayTest()
28 {
29 // We need to create a runtime instance to be able to create strings.
30 options_.SetShouldLoadBootPandaFiles(false);
31 options_.SetShouldInitializeIntrinsics(false);
32 Runtime::Create(options_);
33 thread_ = panda::MTManagedThread::GetCurrent();
34 thread_->ManagedCodeBegin();
35 }
36
~ArrayTest()37 ~ArrayTest()
38 {
39 thread_->ManagedCodeEnd();
40 Runtime::Destroy();
41 }
42
43 protected:
44 panda::MTManagedThread *thread_ {nullptr};
45 RuntimeOptions options_;
46 };
47
GetArrayObjectSize(panda::Class * klass,size_t n)48 static size_t GetArrayObjectSize(panda::Class *klass, size_t n)
49 {
50 return sizeof(Array) + klass->GetComponentSize() * n;
51 }
52
TestArrayObjectSize(ClassRoot class_root,uint32_t n)53 static void TestArrayObjectSize(ClassRoot class_root, uint32_t n)
54 {
55 std::string msg = "Test with class_root ";
56 msg += static_cast<int>(class_root);
57
58 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
59 auto *klass = Runtime::GetCurrent()->GetClassLinker()->GetExtension(ctx)->GetClassRoot(class_root);
60
61 Array *array = Array::Create(klass, n);
62 ASSERT_NE(array, nullptr) << msg;
63
64 ASSERT_EQ(array->ObjectSize(), GetArrayObjectSize(klass, n)) << msg;
65 }
66
TEST_F(ArrayTest,ObjectSize)67 TEST_F(ArrayTest, ObjectSize)
68 {
69 TestArrayObjectSize(ClassRoot::ARRAY_U1, 10);
70 TestArrayObjectSize(ClassRoot::ARRAY_I8, 10);
71 TestArrayObjectSize(ClassRoot::ARRAY_U8, 10);
72 TestArrayObjectSize(ClassRoot::ARRAY_I16, 10);
73 TestArrayObjectSize(ClassRoot::ARRAY_U16, 10);
74 TestArrayObjectSize(ClassRoot::ARRAY_I32, 10);
75 TestArrayObjectSize(ClassRoot::ARRAY_U32, 10);
76 TestArrayObjectSize(ClassRoot::ARRAY_I64, 10);
77 TestArrayObjectSize(ClassRoot::ARRAY_U64, 10);
78 TestArrayObjectSize(ClassRoot::ARRAY_F32, 10);
79 TestArrayObjectSize(ClassRoot::ARRAY_F64, 10);
80 }
81
82 } // namespace panda::coretypes::test
83