/src/mozilla-central/intl/gtest/TestEncoding.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* Any copyright is dedicated to the Public Domain. |
2 | | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
3 | | |
4 | | #include "gtest/gtest.h" |
5 | | |
6 | | #include "mozilla/Encoding.h" |
7 | | #include <type_traits> |
8 | | |
9 | | #define ENCODING_TEST(name) TEST(EncodingTest, name) |
10 | | |
11 | | using namespace mozilla; |
12 | | |
13 | | static_assert(std::is_standard_layout<NotNull<const Encoding*>>::value, |
14 | | "NotNull<const Encoding*> must be a standard layout type."); |
15 | | |
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. |
19 | | |
20 | | ENCODING_TEST(ForLabel) |
21 | 0 | { |
22 | 0 | nsAutoCString label(" uTf-8 "); |
23 | 0 | ASSERT_EQ(Encoding::ForLabel(label), UTF_8_ENCODING); |
24 | 0 | label.AssignLiteral(" cseucpkdfmTjapanese "); |
25 | 0 | ASSERT_EQ(Encoding::ForLabel(label), EUC_JP_ENCODING); |
26 | 0 | } |
27 | | |
28 | | ENCODING_TEST(ForBOM) |
29 | 0 | { |
30 | 0 | nsAutoCString data("\xEF\xBB\xBF\x61"); |
31 | 0 | const Encoding* encoding; |
32 | 0 | size_t bomLength; |
33 | 0 | Tie(encoding, bomLength) = Encoding::ForBOM(data); |
34 | 0 | ASSERT_EQ(encoding, UTF_8_ENCODING); |
35 | 0 | ASSERT_EQ(bomLength, 3U); |
36 | 0 | data.AssignLiteral("\xFF\xFE"); |
37 | 0 | Tie(encoding, bomLength) = Encoding::ForBOM(data); |
38 | 0 | ASSERT_EQ(encoding, UTF_16LE_ENCODING); |
39 | 0 | ASSERT_EQ(bomLength, 2U); |
40 | 0 | data.AssignLiteral("\xFE\xFF"); |
41 | 0 | Tie(encoding, bomLength) = Encoding::ForBOM(data); |
42 | 0 | ASSERT_EQ(encoding, UTF_16BE_ENCODING); |
43 | 0 | ASSERT_EQ(bomLength, 2U); |
44 | 0 | data.AssignLiteral("\xEF\xBB"); |
45 | 0 | Tie(encoding, bomLength) = Encoding::ForBOM(data); |
46 | 0 | ASSERT_EQ(encoding, nullptr); |
47 | 0 | ASSERT_EQ(bomLength, 0U); |
48 | 0 | } |
49 | | |
50 | | ENCODING_TEST(Name) |
51 | 0 | { |
52 | 0 | nsAutoCString name; |
53 | 0 | UTF_8_ENCODING->Name(name); |
54 | 0 | ASSERT_TRUE(name.EqualsLiteral("UTF-8")); |
55 | 0 | GBK_ENCODING->Name(name); |
56 | 0 | ASSERT_TRUE(name.EqualsLiteral("GBK")); |
57 | 0 | } |
58 | | |
59 | | ENCODING_TEST(CanEncodeEverything) |
60 | 0 | { |
61 | 0 | ASSERT_TRUE(UTF_8_ENCODING->CanEncodeEverything()); |
62 | 0 | ASSERT_FALSE(GB18030_ENCODING->CanEncodeEverything()); |
63 | 0 | } |
64 | | |
65 | | ENCODING_TEST(IsAsciiCompatible) |
66 | 0 | { |
67 | 0 | ASSERT_TRUE(UTF_8_ENCODING->IsAsciiCompatible()); |
68 | 0 | ASSERT_FALSE(ISO_2022_JP_ENCODING->IsAsciiCompatible()); |
69 | 0 | } |