|
| 1 | +//===-- Unittests for the CharacterConverter class (utf32 -> 8) -----------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/__support/common.h" |
| 10 | +#include "src/__support/wchar/character_converter.h" |
| 11 | +#include "src/__support/wchar/mbstate.h" |
| 12 | + |
| 13 | +#include "test/UnitTest/Test.h" |
| 14 | + |
| 15 | +TEST(LlvmLibcCharacterConverterUTF32To8Test, OneByte) { |
| 16 | + LIBC_NAMESPACE::internal::mbstate state; |
| 17 | + LIBC_NAMESPACE::internal::CharacterConverter cr(&state); |
| 18 | + cr.clear(); |
| 19 | + |
| 20 | + // utf8 1-byte encodings are identical to their utf32 representations |
| 21 | + char32_t utf32_A = 0x41; // 'A' |
| 22 | + cr.push(utf32_A); |
| 23 | + auto popped = cr.pop_utf8(); |
| 24 | + ASSERT_TRUE(popped.has_value()); |
| 25 | + ASSERT_EQ(static_cast<char>(popped.value()), 'A'); |
| 26 | + ASSERT_TRUE(cr.isComplete()); |
| 27 | + |
| 28 | + char32_t utf32_B = 0x42; // 'B' |
| 29 | + cr.push(utf32_B); |
| 30 | + popped = cr.pop_utf8(); |
| 31 | + ASSERT_TRUE(popped.has_value()); |
| 32 | + ASSERT_EQ(static_cast<char>(popped.value()), 'B'); |
| 33 | + ASSERT_TRUE(cr.isComplete()); |
| 34 | + |
| 35 | + // should error if we try to pop another utf8 byte out |
| 36 | + popped = cr.pop_utf8(); |
| 37 | + ASSERT_FALSE(popped.has_value()); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(LlvmLibcCharacterConverterUTF32To8Test, TwoByte) { |
| 41 | + LIBC_NAMESPACE::internal::mbstate state; |
| 42 | + LIBC_NAMESPACE::internal::CharacterConverter cr(&state); |
| 43 | + cr.clear(); |
| 44 | + |
| 45 | + // testing utf32: 0xff -> utf8: 0xc3 0xbf |
| 46 | + char32_t utf32 = 0xff; |
| 47 | + cr.push(utf32); |
| 48 | + auto popped = cr.pop_utf8(); |
| 49 | + ASSERT_TRUE(popped.has_value()); |
| 50 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xc3); |
| 51 | + ASSERT_TRUE(!cr.isComplete()); |
| 52 | + popped = cr.pop_utf8(); |
| 53 | + ASSERT_TRUE(popped.has_value()); |
| 54 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xbf); |
| 55 | + ASSERT_TRUE(cr.isComplete()); |
| 56 | + |
| 57 | + // testing utf32: 0x58e -> utf8: 0xd6 0x8e |
| 58 | + utf32 = 0x58e; |
| 59 | + cr.push(utf32); |
| 60 | + popped = cr.pop_utf8(); |
| 61 | + ASSERT_TRUE(popped.has_value()); |
| 62 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xd6); |
| 63 | + ASSERT_TRUE(!cr.isComplete()); |
| 64 | + popped = cr.pop_utf8(); |
| 65 | + ASSERT_TRUE(popped.has_value()); |
| 66 | + ASSERT_EQ(static_cast<int>(popped.value()), 0x8e); |
| 67 | + ASSERT_TRUE(cr.isComplete()); |
| 68 | + |
| 69 | + // should error if we try to pop another utf8 byte out |
| 70 | + popped = cr.pop_utf8(); |
| 71 | + ASSERT_FALSE(popped.has_value()); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(LlvmLibcCharacterConverterUTF32To8Test, ThreeByte) { |
| 75 | + LIBC_NAMESPACE::internal::mbstate state; |
| 76 | + LIBC_NAMESPACE::internal::CharacterConverter cr(&state); |
| 77 | + cr.clear(); |
| 78 | + |
| 79 | + // testing utf32: 0xac15 -> utf8: 0xea 0xb0 0x95 |
| 80 | + char32_t utf32 = 0xac15; |
| 81 | + cr.push(utf32); |
| 82 | + auto popped = cr.pop_utf8(); |
| 83 | + ASSERT_TRUE(popped.has_value()); |
| 84 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xea); |
| 85 | + ASSERT_TRUE(!cr.isComplete()); |
| 86 | + popped = cr.pop_utf8(); |
| 87 | + ASSERT_TRUE(popped.has_value()); |
| 88 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xb0); |
| 89 | + ASSERT_TRUE(!cr.isComplete()); |
| 90 | + popped = cr.pop_utf8(); |
| 91 | + ASSERT_TRUE(popped.has_value()); |
| 92 | + ASSERT_EQ(static_cast<int>(popped.value()), 0x95); |
| 93 | + ASSERT_TRUE(cr.isComplete()); |
| 94 | + |
| 95 | + // testing utf32: 0x267b -> utf8: 0xe2 0x99 0xbb |
| 96 | + utf32 = 0x267b; |
| 97 | + cr.push(utf32); |
| 98 | + popped = cr.pop_utf8(); |
| 99 | + ASSERT_TRUE(popped.has_value()); |
| 100 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xe2); |
| 101 | + ASSERT_TRUE(!cr.isComplete()); |
| 102 | + popped = cr.pop_utf8(); |
| 103 | + ASSERT_TRUE(popped.has_value()); |
| 104 | + ASSERT_EQ(static_cast<int>(popped.value()), 0x99); |
| 105 | + ASSERT_TRUE(!cr.isComplete()); |
| 106 | + popped = cr.pop_utf8(); |
| 107 | + ASSERT_TRUE(popped.has_value()); |
| 108 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xbb); |
| 109 | + ASSERT_TRUE(cr.isComplete()); |
| 110 | + |
| 111 | + // should error if we try to pop another utf8 byte out |
| 112 | + popped = cr.pop_utf8(); |
| 113 | + ASSERT_FALSE(popped.has_value()); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(LlvmLibcCharacterConverterUTF32To8Test, FourByte) { |
| 117 | + LIBC_NAMESPACE::internal::mbstate state; |
| 118 | + LIBC_NAMESPACE::internal::CharacterConverter cr(&state); |
| 119 | + cr.clear(); |
| 120 | + |
| 121 | + // testing utf32: 0x1f921 -> utf8: 0xf0 0x9f 0xa4 0xa1 |
| 122 | + char32_t utf32 = 0x1f921; |
| 123 | + cr.push(utf32); |
| 124 | + auto popped = cr.pop_utf8(); |
| 125 | + ASSERT_TRUE(popped.has_value()); |
| 126 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xf0); |
| 127 | + ASSERT_TRUE(!cr.isComplete()); |
| 128 | + popped = cr.pop_utf8(); |
| 129 | + ASSERT_TRUE(popped.has_value()); |
| 130 | + ASSERT_EQ(static_cast<int>(popped.value()), 0x9f); |
| 131 | + ASSERT_TRUE(!cr.isComplete()); |
| 132 | + popped = cr.pop_utf8(); |
| 133 | + ASSERT_TRUE(popped.has_value()); |
| 134 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xa4); |
| 135 | + ASSERT_TRUE(!cr.isComplete()); |
| 136 | + popped = cr.pop_utf8(); |
| 137 | + ASSERT_TRUE(popped.has_value()); |
| 138 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xa1); |
| 139 | + ASSERT_TRUE(cr.isComplete()); |
| 140 | + |
| 141 | + // testing utf32: 0x12121 -> utf8: 0xf0 0x92 0x84 0xa1 |
| 142 | + utf32 = 0x12121; |
| 143 | + cr.push(utf32); |
| 144 | + popped = cr.pop_utf8(); |
| 145 | + ASSERT_TRUE(popped.has_value()); |
| 146 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xf0); |
| 147 | + ASSERT_TRUE(!cr.isComplete()); |
| 148 | + popped = cr.pop_utf8(); |
| 149 | + ASSERT_TRUE(popped.has_value()); |
| 150 | + ASSERT_EQ(static_cast<int>(popped.value()), 0x92); |
| 151 | + ASSERT_TRUE(!cr.isComplete()); |
| 152 | + popped = cr.pop_utf8(); |
| 153 | + ASSERT_TRUE(popped.has_value()); |
| 154 | + ASSERT_EQ(static_cast<int>(popped.value()), 0x84); |
| 155 | + ASSERT_TRUE(!cr.isComplete()); |
| 156 | + popped = cr.pop_utf8(); |
| 157 | + ASSERT_TRUE(popped.has_value()); |
| 158 | + ASSERT_EQ(static_cast<int>(popped.value()), 0xa1); |
| 159 | + ASSERT_TRUE(cr.isComplete()); |
| 160 | + |
| 161 | + // should error if we try to pop another utf8 byte out |
| 162 | + popped = cr.pop_utf8(); |
| 163 | + ASSERT_FALSE(popped.has_value()); |
| 164 | +} |
| 165 | + |
| 166 | +TEST(LlvmLibcCharacterConverterUTF32To8Test, CantPushMidConversion) { |
| 167 | + LIBC_NAMESPACE::internal::mbstate state; |
| 168 | + LIBC_NAMESPACE::internal::CharacterConverter cr(&state); |
| 169 | + cr.clear(); |
| 170 | + |
| 171 | + // testing utf32: 0x12121 -> utf8: 0xf0 0x92 0x84 0xa1 |
| 172 | + char32_t utf32 = 0x12121; |
| 173 | + ASSERT_EQ(cr.push(utf32), 0); |
| 174 | + auto popped = cr.pop_utf8(); |
| 175 | + ASSERT_TRUE(popped.has_value()); |
| 176 | + |
| 177 | + // can't push a utf32 without finishing popping the utf8 bytes out |
| 178 | + int err = cr.push(utf32); |
| 179 | + ASSERT_EQ(err, -1); |
| 180 | +} |
0 commit comments