xref: /aosp_15_r20/external/fbjni/test/jni/utf16toUTF8_test.cpp (revision 65c59e023c5336bbd4a23be7af78407e3d80e7e7)
1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <fbjni/detail/utf8.h>
20 
21 using namespace std;
22 using namespace facebook::jni;
23 
TEST(Utf16toUTF8_test,nullUtf16String)24 TEST(Utf16toUTF8_test, nullUtf16String) {
25   EXPECT_TRUE(detail::utf16toUTF8(NULL, 10).empty());
26 }
27 
TEST(Utf16toUTF8_test,negativeUtf16StringLength)28 TEST(Utf16toUTF8_test, negativeUtf16StringLength) {
29   std::vector<uint16_t> utf16String = {'a'};
30   EXPECT_TRUE(detail::utf16toUTF8(utf16String.data(), -1).empty());
31 }
32 
TEST(Utf16toUTF8_test,zeroUtf16StringLength)33 TEST(Utf16toUTF8_test, zeroUtf16StringLength) {
34   std::vector<uint16_t> utf16String = {'a'};
35   EXPECT_TRUE(detail::utf16toUTF8(utf16String.data(), 0).empty());
36 }
37 
TEST(Utf16toUTF8_test,badFormedUtf16String)38 TEST(Utf16toUTF8_test, badFormedUtf16String) {
39   std::vector<uint16_t> utf16String = {'a', 'b', 'c', 0xD800};
40   auto utf8String = detail::utf16toUTF8(utf16String.data(), utf16String.size());
41   EXPECT_EQ(utf8String.size(), 6);
42   EXPECT_EQ(utf8String, "abc\xED\xA0\x80");
43 }
44 
TEST(Utf16toUTF8_test,goodUtf16String)45 TEST(Utf16toUTF8_test, goodUtf16String) {
46   std::vector<uint16_t> utf16String = {'a', 0x0123, 0x1234, 0xD812, 0xDC34};
47   auto utf8String = detail::utf16toUTF8(utf16String.data(), utf16String.size());
48   EXPECT_EQ(utf8String.size(), 10);
49   EXPECT_EQ(utf8String, "a\xC4\xA3\xE1\x88\xB4\xF0\x94\xA0\xB4");
50 }
51 
main(int argc,char ** argv)52 int main(int argc, char** argv) {
53   testing::InitGoogleTest(&argc, argv);
54   return RUN_ALL_TESTS();
55 }
56