• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 "ani_gtest.h"
17 
18 // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg, modernize-avoid-c-arrays)
19 namespace ark::ets::ani::testing {
20 
21 class StringNewUtf8Test : public AniTest {};
TEST_F(StringNewUtf8Test,StringNewUtf8_NullUtf8String)22 TEST_F(StringNewUtf8Test, StringNewUtf8_NullUtf8String)
23 {
24     ani_string result = nullptr;
25     const ani_size size = 10U;
26     auto status = env_->String_NewUTF8(nullptr, size, &result);
27     ASSERT_EQ(status, ANI_INVALID_ARGS);
28     ASSERT_EQ(result, nullptr);
29 }
30 
TEST_F(StringNewUtf8Test,StringNewUtf8_EmptyString)31 TEST_F(StringNewUtf8Test, StringNewUtf8_EmptyString)
32 {
33     ani_string result = nullptr;
34     auto status = env_->String_NewUTF8("", 0U, &result);
35     ASSERT_EQ(status, ANI_OK);
36     ASSERT_NE(result, nullptr);
37 }
38 
TEST_F(StringNewUtf8Test,StringNewUtf8_LongString)39 TEST_F(StringNewUtf8Test, StringNewUtf8_LongString)
40 {
41     const int32_t longStringSize = 10000U;
42     std::string longString(longStringSize, 'a');
43     ani_string result = nullptr;
44     auto status = env_->String_NewUTF8(longString.c_str(), longString.size(), &result);
45     ASSERT_EQ(status, ANI_OK);
46     ASSERT_NE(result, nullptr);
47 }
48 
TEST_F(StringNewUtf8Test,StringNewUtf8_ValidMultibyteString)49 TEST_F(StringNewUtf8Test, StringNewUtf8_ValidMultibyteString)
50 {
51     const std::string example {"测测试emoji����"};
52     ani_string result = nullptr;
53     auto status = env_->String_NewUTF8(example.c_str(), example.size(), &result);
54     ASSERT_EQ(status, ANI_OK);
55     ASSERT_NE(result, nullptr);
56 
57     ani_string result2 = nullptr;
58     status = env_->String_NewUTF8(example.c_str(), example.size(), &result2);
59     ASSERT_EQ(status, ANI_OK);
60     ASSERT_NE(result, nullptr);
61     const uint32_t bufferSize = 30U;
62     char utfBuffer[bufferSize] = {0U};
63     ani_size resultSize = 0U;
64     env_->String_GetUTF8SubString(result2, 0U, example.size(), utfBuffer, sizeof(utfBuffer), &resultSize);
65     ASSERT_EQ(resultSize, example.size());
66     ASSERT_STREQ(utfBuffer, example.c_str());
67 }
68 
TEST_F(StringNewUtf8Test,StringNewUtf8_NullEnv)69 TEST_F(StringNewUtf8Test, StringNewUtf8_NullEnv)
70 {
71     const std::string example {"example"};
72     ani_string string = nullptr;
73     auto status = env_->c_api->String_NewUTF8(nullptr, example.c_str(), example.size(), &string);
74     ASSERT_EQ(status, ANI_INVALID_ARGS);
75 }
76 
TEST_F(StringNewUtf8Test,StringNewUtf8_SmallerSize)77 TEST_F(StringNewUtf8Test, StringNewUtf8_SmallerSize)
78 {
79     const std::string example {"example"};
80     ani_string result = nullptr;
81     ani_status status = env_->String_NewUTF8(example.c_str(), example.size() - 1U, &result);
82     ASSERT_EQ(status, ANI_OK);
83     ASSERT_NE(result, nullptr);
84 
85     ani_string result2 = nullptr;
86     status = env_->String_NewUTF8(example.c_str(), example.size(), &result2);
87     ASSERT_EQ(status, ANI_OK);
88     ASSERT_NE(result, nullptr);
89     const uint32_t bufferSize = 10U;
90     char utfBuffer[bufferSize] = {0U};
91     ani_size resultSize = 0U;
92     env_->String_GetUTF8SubString(result2, 0U, example.size() - 1U, utfBuffer, sizeof(utfBuffer), &resultSize);
93     ASSERT_STREQ(utfBuffer, "exampl");
94 }
95 
TEST_F(StringNewUtf8Test,StringNewUtf8_ZeroSize)96 TEST_F(StringNewUtf8Test, StringNewUtf8_ZeroSize)
97 {
98     const std::string example {"example"};
99     ani_string result = nullptr;
100     ani_status status = env_->String_NewUTF8(example.c_str(), 0U, &result);
101     ASSERT_EQ(status, ANI_OK);
102     ASSERT_NE(result, nullptr);
103 
104     ani_string result2 = nullptr;
105     status = env_->String_NewUTF8(example.c_str(), example.size(), &result2);
106     ASSERT_EQ(status, ANI_OK);
107     ASSERT_NE(result, nullptr);
108     const uint32_t bufferSize = 20U;
109     char utfBuffer[bufferSize] = {0U};
110     ani_size resultSize = 0U;
111     env_->String_GetUTF8SubString(result2, 0U, 0U, utfBuffer, sizeof(utfBuffer), &resultSize);
112     ASSERT_STREQ(utfBuffer, "");
113 }
114 
TEST_F(StringNewUtf8Test,StringNewUtf8_Repeat)115 TEST_F(StringNewUtf8Test, StringNewUtf8_Repeat)
116 {
117     const std::string example {"测测试emoji����"};
118     ani_string result = nullptr;
119     const int32_t loopCount = 3;
120     for (int32_t i = 0; i < loopCount; ++i) {
121         auto status = env_->String_NewUTF8(example.c_str(), example.size(), &result);
122         ASSERT_EQ(status, ANI_OK);
123         ASSERT_NE(result, nullptr);
124     }
125 }
126 }  // namespace ark::ets::ani::testing
127 
128 // NOLINTEND(cppcoreguidelines-pro-type-vararg, modernize-avoid-c-arrays)