• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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/coretypes/array.h"
20 #include "runtime/include/runtime.h"
21 
22 namespace ark::coretypes::test {
23 
24 class ArrayTest : public testing::Test {
25 public:
ArrayTest()26     ArrayTest()
27     {
28         // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL);
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_ = ark::MTManagedThread::GetCurrent();
34         thread_->ManagedCodeBegin();
35     }
36 
~ArrayTest()37     ~ArrayTest() override
38     {
39         thread_->ManagedCodeEnd();
40         Runtime::Destroy();
41         // Logger::Destroy();
42     }
43 
44     NO_COPY_SEMANTIC(ArrayTest);
45     NO_MOVE_SEMANTIC(ArrayTest);
46 
47 private:
48     ark::MTManagedThread *thread_;
49     RuntimeOptions options_;
50 };
51 
GetArrayObjectSize(ark::Class * klass,size_t n)52 static size_t GetArrayObjectSize(ark::Class *klass, size_t n)
53 {
54     return sizeof(Array) + klass->GetComponentSize() * n;
55 }
56 
TestArrayObjectSize(ClassRoot classRoot,uint32_t n)57 static void TestArrayObjectSize(ClassRoot classRoot, uint32_t n)
58 {
59     std::string msg = "Test with class_root ";
60     msg += std::to_string(static_cast<int>(classRoot));
61 
62     LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
63     auto *klass = Runtime::GetCurrent()->GetClassLinker()->GetExtension(ctx)->GetClassRoot(classRoot);
64 
65     Array *array = Array::Create(klass, n);
66     ASSERT_NE(array, nullptr) << msg;
67 
68     ASSERT_EQ(array->ObjectSize(klass->GetComponentSize()), GetArrayObjectSize(klass, n)) << msg;
69 }
70 
TEST_F(ArrayTest,ObjectSize)71 TEST_F(ArrayTest, ObjectSize)
72 {
73     // NOLINTBEGIN(readability-magic-numbers)
74     TestArrayObjectSize(ClassRoot::ARRAY_U1, 10U);
75     TestArrayObjectSize(ClassRoot::ARRAY_I8, 10U);
76     TestArrayObjectSize(ClassRoot::ARRAY_U8, 10U);
77     TestArrayObjectSize(ClassRoot::ARRAY_I16, 10U);
78     TestArrayObjectSize(ClassRoot::ARRAY_U16, 10U);
79     TestArrayObjectSize(ClassRoot::ARRAY_I32, 10U);
80     TestArrayObjectSize(ClassRoot::ARRAY_U32, 10U);
81     TestArrayObjectSize(ClassRoot::ARRAY_I64, 10U);
82     TestArrayObjectSize(ClassRoot::ARRAY_U64, 10U);
83     TestArrayObjectSize(ClassRoot::ARRAY_F32, 10U);
84     TestArrayObjectSize(ClassRoot::ARRAY_F64, 10U);
85     // NOLINTEND(readability-magic-numbers)
86 }
87 
88 }  // namespace ark::coretypes::test
89