1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 #include "gtest/gtest.h"
6 #include "mozilla/Encoding.h"
9 #define ENCODING_TEST(name) TEST(EncodingTest, name)
11 using namespace mozilla
;
13 static_assert(std::is_standard_layout
<NotNull
<const Encoding
*>>::value
,
14 "NotNull<const Encoding*> must be a standard layout type.");
16 // These tests mainly test that the C++ interface seems to
17 // reach the Rust code. More thorough testing of the back
18 // end is done in Rust.
20 ENCODING_TEST(ForLabel
) {
21 nsAutoCString
label(" uTf-8 ");
22 ASSERT_EQ(Encoding::ForLabel(label
), UTF_8_ENCODING
);
23 label
.AssignLiteral(" cseucpkdfmTjapanese ");
24 ASSERT_EQ(Encoding::ForLabel(label
), EUC_JP_ENCODING
);
27 ENCODING_TEST(ForBOM
) {
28 nsAutoCString
data("\xEF\xBB\xBF\x61");
29 const Encoding
* encoding
;
31 std::tie(encoding
, bomLength
) = Encoding::ForBOM(data
);
32 ASSERT_EQ(encoding
, UTF_8_ENCODING
);
33 ASSERT_EQ(bomLength
, 3U);
34 data
.AssignLiteral("\xFF\xFE");
35 std::tie(encoding
, bomLength
) = Encoding::ForBOM(data
);
36 ASSERT_EQ(encoding
, UTF_16LE_ENCODING
);
37 ASSERT_EQ(bomLength
, 2U);
38 data
.AssignLiteral("\xFE\xFF");
39 std::tie(encoding
, bomLength
) = Encoding::ForBOM(data
);
40 ASSERT_EQ(encoding
, UTF_16BE_ENCODING
);
41 ASSERT_EQ(bomLength
, 2U);
42 data
.AssignLiteral("\xEF\xBB");
43 std::tie(encoding
, bomLength
) = Encoding::ForBOM(data
);
44 ASSERT_EQ(encoding
, nullptr);
45 ASSERT_EQ(bomLength
, 0U);
50 UTF_8_ENCODING
->Name(name
);
51 ASSERT_TRUE(name
.EqualsLiteral("UTF-8"));
52 GBK_ENCODING
->Name(name
);
53 ASSERT_TRUE(name
.EqualsLiteral("GBK"));
56 ENCODING_TEST(CanEncodeEverything
) {
57 ASSERT_TRUE(UTF_8_ENCODING
->CanEncodeEverything());
58 ASSERT_FALSE(GB18030_ENCODING
->CanEncodeEverything());
61 ENCODING_TEST(IsAsciiCompatible
) {
62 ASSERT_TRUE(UTF_8_ENCODING
->IsAsciiCompatible());
63 ASSERT_FALSE(ISO_2022_JP_ENCODING
->IsAsciiCompatible());