• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 panda::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_ = panda::MTManagedThread::GetCurrent();
34         thread_->ManagedCodeBegin();
35     }
36 
~ArrayTest()37     ~ArrayTest()
38     {
39         thread_->ManagedCodeEnd();
40         Runtime::Destroy();
41         // Logger::Destroy();
42     }
43 
44 protected:
45     panda::MTManagedThread *thread_;
46     RuntimeOptions options_;
47 };
48 
GetArrayObjectSize(panda::Class * klass,size_t n)49 static size_t GetArrayObjectSize(panda::Class *klass, size_t n)
50 {
51     return sizeof(Array) + klass->GetComponentSize() * n;
52 }
53 
TestArrayObjectSize(ClassRoot class_root,uint32_t n)54 static void TestArrayObjectSize(ClassRoot class_root, uint32_t n)
55 {
56     std::string msg = "Test with class_root ";
57     msg += static_cast<int>(class_root);
58 
59     LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
60     auto *klass = Runtime::GetCurrent()->GetClassLinker()->GetExtension(ctx)->GetClassRoot(class_root);
61 
62     Array *array = Array::Create(klass, n);
63     ASSERT_NE(array, nullptr) << msg;
64 
65     ASSERT_EQ(array->ObjectSize(klass->GetComponentSize()), GetArrayObjectSize(klass, n)) << msg;
66 }
67 
TEST_F(ArrayTest,ObjectSize)68 TEST_F(ArrayTest, ObjectSize)
69 {
70     TestArrayObjectSize(ClassRoot::ARRAY_U1, 10);
71     TestArrayObjectSize(ClassRoot::ARRAY_I8, 10);
72     TestArrayObjectSize(ClassRoot::ARRAY_U8, 10);
73     TestArrayObjectSize(ClassRoot::ARRAY_I16, 10);
74     TestArrayObjectSize(ClassRoot::ARRAY_U16, 10);
75     TestArrayObjectSize(ClassRoot::ARRAY_I32, 10);
76     TestArrayObjectSize(ClassRoot::ARRAY_U32, 10);
77     TestArrayObjectSize(ClassRoot::ARRAY_I64, 10);
78     TestArrayObjectSize(ClassRoot::ARRAY_U64, 10);
79     TestArrayObjectSize(ClassRoot::ARRAY_F32, 10);
80     TestArrayObjectSize(ClassRoot::ARRAY_F64, 10);
81 }
82 
83 }  // namespace panda::coretypes::test
84