#include "ustring.h" #include "utf.h" #include "logger.h" #include "threadpool.h" #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////////////////// // // Данные для тестов // // UTF-8 const unsigned char helloStrUtf8[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a, 0xd0, 0x9f, 0xd1, 0x80, 0xd0, 0xb8, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1, 0x82, 0x2c, 0x20, 0xd0, 0x9c, 0xd0, 0xb8, 0xd1, 0x80, 0x21, 0x0a, 0xe2, 0x88, 0x86, 0x20, 0x3d, 0x20, 0xf0, 0x9d, 0x9b, 0xbb, 0xc2, 0xb2, 0x0a, 0x00 }; // UTF-16LE const unsigned char helloStrUtf16le[] = { 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x57, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x21, 0x00, 0x0a, 0x00, 0x1f, 0x04, 0x40, 0x04, 0x38, 0x04, 0x32, 0x04, 0x35, 0x04, 0x42, 0x04, 0x2c, 0x00, 0x20, 0x00, 0x1c, 0x04, 0x38, 0x04, 0x40, 0x04, 0x21, 0x00, 0x0a, 0x00, 0x06, 0x22, 0x20, 0x00, 0x3d, 0x00, 0x20, 0x00, 0x35, 0xd8, 0xfb, 0xde, 0xb2, 0x00, 0x0a, 0x00, 0x00, 0x00 }; // UTF-16BE const unsigned char helloStrUtf16be[] = { 0x00, 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x57, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x21, 0x00, 0x0a, 0x04, 0x1f, 0x04, 0x40, 0x04, 0x38, 0x04, 0x32, 0x04, 0x35, 0x04, 0x42, 0x00, 0x2c, 0x00, 0x20, 0x04, 0x1c, 0x04, 0x38, 0x04, 0x40, 0x00, 0x21, 0x00, 0x0a, 0x22, 0x06, 0x00, 0x20, 0x00, 0x3d, 0x00, 0x20, 0xd8, 0x35, 0xde, 0xfb, 0x00, 0xb2, 0x00, 0x0a, 0x00, 0x00 }; // UCS-4 (UTF-32LE на x86) const unsigned char helloStrUcs4[] = { 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x1f, 0x04, 0x00, 0x00, 0x40, 0x04, 0x00, 0x00, 0x38, 0x04, 0x00, 0x00, 0x32, 0x04, 0x00, 0x00, 0x35, 0x04, 0x00, 0x00, 0x42, 0x04, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x1c, 0x04, 0x00, 0x00, 0x38, 0x04, 0x00, 0x00, 0x40, 0x04, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x06, 0x22, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xfb, 0xd6, 0x01, 0x00, 0xb2, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // UTF-32BE const unsigned char helloStrUtf32be[] = { 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x04, 0x1f, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x04, 0x38, 0x00, 0x00, 0x04, 0x32, 0x00, 0x00, 0x04, 0x35, 0x00, 0x00, 0x04, 0x42, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x1c, 0x00, 0x00, 0x04, 0x38, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x22, 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x00, 0x20, 0x00, 0x01, 0xd6, 0xfb, 0x00, 0x00, 0x00, 0xb2, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00 }; // Escaped const char helloStrEscaped[] = "Hello World!\n\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442, \\u041C\\u0438\\u0440!\n\\u2206 = \\U0001D6FB\\u00B2\n"; // Длина тестовой строки в символах const std::size_t helloStrLen = 33; // Некорректные строки для теста обработки ошибок const unsigned char badStrUtf8[] = { 0xd0, 0xdf, 0x00 }; const unsigned char badStr2Utf8[] = { 0xf8, 0x9f, 0x00 }; const unsigned char badStrUcs4[] = { 0xf8, 0x9f, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00 }; const unsigned char badStrUtf16le[] = { 0x35, 0xc8, 0xfb, 0xde, 0x00, 0x00 }; const unsigned char badStr2Utf16le[] = { 0x35, 0xd8, 0xfb, 0xce, 0x00, 0x00 }; // Для теста конкатенации // "Hello " const unsigned char helloPart1Ucs4[] = { 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // "big" const unsigned char helloPart2Ucs4[] = { 0x62, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // " world" const unsigned char helloPart3Ucs4[] = { 0x20, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // "Hello big world" const unsigned char helloResultUcs4[] = { 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; /////////////////////////////////////////////////////////////////////////////////////////// // // Тесты // // // Определение длины символа UTF-8 // TEST(Convert, utf8_symbol_size) { ASSERT_EQ(1, utf8_get_symbol_size((const char*)&helloStrUtf8[0], false)); ASSERT_EQ(2, utf8_get_symbol_size((const char*)&helloStrUtf8[15], false)); ASSERT_EQ(4, utf8_get_symbol_size((const char*)&helloStrUtf8[41], false)); } // // Определение длины символа UTF-8 по символу UCS-4 // TEST(Convert, utf8_symbol_size_by_ucs4_symbol) { ASSERT_EQ(1, ucs4_get_utf8_symbol_size(*(const char32_t*)&helloStrUcs4[0], false)); ASSERT_EQ(2, ucs4_get_utf8_symbol_size(*(const char32_t*)&helloStrUcs4[52], false)); ASSERT_EQ(4, ucs4_get_utf8_symbol_size(*(const char32_t*)&helloStrUcs4[120], false)); } // // Определение длины строки UTF-8 // TEST(Convert, utf8_length) { ASSERT_EQ(helloStrLen, utf8_str_len((const char*)helloStrUtf8, false)); } // // Определение длины строки UTF-8 по строке UCS-4 // TEST(Convert, utf8_str_len_by_ucs4_str) { ASSERT_EQ(sizeof(helloStrUtf8) - 1, ucs4_get_utf8_str_bytes((const char32_t*)helloStrUcs4, false)); } // // Определение длины символа UTF-16LE // TEST(Convert, utf16le_symbol_size) { ASSERT_EQ(2, utf16_get_symbol_size((const char16_t*)&helloStrUtf16le[0])); ASSERT_EQ(4, utf16_get_symbol_size((const char16_t*)&helloStrUtf16le[60])); } // // Определение длины символа UTF-16BE // TEST(Convert, utf16be_symbol_size) { ASSERT_EQ(2, utf16_get_symbol_size((const char16_t*)&helloStrUtf16be[0], BYTE_ORDER_BIG_ENDIAN)); ASSERT_EQ(4, utf16_get_symbol_size((const char16_t*)&helloStrUtf16be[60], BYTE_ORDER_BIG_ENDIAN)); } // // Определение длины символа UTF-16 по символу UCS-4 // TEST(Convert, utf16_symbol_size_by_ucs4_symbol) { ASSERT_EQ(2, ucs4_get_utf16_symbol_size(*(const char32_t*)&helloStrUcs4[0])); ASSERT_EQ(4, ucs4_get_utf16_symbol_size(*(const char32_t*)&helloStrUcs4[120])); } // // Определение длины строки UTF-16LE // TEST(Convert, utf16le_length) { ASSERT_EQ(helloStrLen, utf16_str_len((const char16_t*)helloStrUtf16le)); } // // Определение длины строки UTF-16BE // TEST(Convert, utf16be_length) { ASSERT_EQ(helloStrLen, utf16_str_len((const char16_t*)helloStrUtf16be, BYTE_ORDER_BIG_ENDIAN)); } TEST(Convert, utf16_str_len_by_ucs4_str) { ASSERT_EQ(sizeof(helloStrUtf16le) - 2, ucs4_get_utf16_str_bytes((const char32_t*)helloStrUcs4)); } // // Определение длины строки UTF-32 // TEST(Convert, utf32_length) { ASSERT_EQ(helloStrLen, utf32_str_len((const char32_t*)helloStrUcs4)); } // // Конвертирование UTF-8 в UCS-4 // TEST(Convert, utf8_to_ucs4) { std::size_t len = utf8_str_len((const char*)helloStrUtf8, false); char32_t* data = new char32_t[len]; utf8_to_ucs4((const char*)helloStrUtf8, data, len, false); EXPECT_EQ(0, memcmp(helloStrUcs4, data, len*sizeof(char32_t))); delete[] data; } // // Конвертирование UCS-4 в UTF-8 // TEST(Convert, ucs4_to_utf8) { std::size_t len = ucs4_get_utf8_str_bytes((const char32_t*)helloStrUcs4, false); std::size_t symbols = utf32_str_len((const char32_t*)helloStrUcs4); char* data = new char[len]; ucs4_to_utf8((const char32_t*)helloStrUcs4, data, symbols, false); EXPECT_EQ(0, memcmp(helloStrUtf8, data, len)); delete[] data; } // // Конвертирование UTF-16LE в UCS-4 // TEST(Convert, utf16le_to_ucs4) { std::size_t len = utf16_str_len((const char16_t*)helloStrUtf16le); char32_t* data = new char32_t[len]; utf16_to_ucs4((const char16_t*)helloStrUtf16le, data, len); EXPECT_EQ(0, memcmp(helloStrUcs4, data, len*sizeof(char32_t))); delete[] data; } // // Конвертирование UCS-4 в UTF-16LE // TEST(Convert, ucs4_to_utf16le) { std::size_t len = ucs4_get_utf16_str_bytes((const char32_t*)helloStrUcs4); std::size_t symbols = utf32_str_len((const char32_t*)helloStrUcs4); char16_t* data = new char16_t[len]; ucs4_to_utf16((const char32_t*)helloStrUcs4, data, symbols); EXPECT_EQ(0, memcmp(helloStrUtf16le, data, len)); delete[] data; } // // Конвертирование UTF-16BE в UCS-4 // TEST(Convert, utf16be_to_ucs4) { std::size_t len = utf16_str_len((const char16_t*)helloStrUtf16be); char32_t* data = new char32_t[len]; utf16_to_ucs4((const char16_t*)helloStrUtf16be, data, len, BYTE_ORDER_BIG_ENDIAN); EXPECT_EQ(0, memcmp(helloStrUcs4, data, len*sizeof(char32_t))); delete[] data; } // // Конвертирование UCS-4 в UTF-16BE // TEST(Convert, ucs4_to_utf16be) { std::size_t len = ucs4_get_utf16_str_bytes((const char32_t*)helloStrUcs4); std::size_t symbols = utf32_str_len((const char32_t*)helloStrUcs4); char16_t* data = new char16_t[len]; ucs4_to_utf16((const char32_t*)helloStrUcs4, data, symbols, BYTE_ORDER_BIG_ENDIAN); EXPECT_EQ(0, memcmp(helloStrUtf16be, data, len)); delete[] data; } ///////////////////////////////////////////////////////////////////////////////////////////////////// // // Тесты на ошибки конвертирования // // // Первый байт двухбайтового символа содержит единицы в пяти старших разрядах // TEST(ConvertError, utf8_length_error) { EXPECT_THROW(utf8_str_len((const char*)badStr2Utf8, false), bad_conversion); } // // Второй байт двухбайтового символа содержит единицы в двух старших разрядах // TEST(ConvertError, utf8_decode_error) { std::size_t len = utf8_str_len((const char*)badStrUtf8, false); char32_t* data = new char32_t[len]; EXPECT_THROW(utf8_to_ucs4((const char*)badStrUtf8, data, len, false), bad_conversion); delete[] data; } // // Некорректное перевое слово суррогатной пары // TEST(ConvertError, utf16_decode_error) { std::size_t len = utf16_str_len((const char16_t*)badStrUtf16le); char32_t* data = new char32_t[len]; EXPECT_THROW(utf16_to_ucs4((const char16_t*)badStrUtf16le, data, len), bad_conversion); delete[] data; } // // Некорректное второе слово суррогатной пары // TEST(ConvertError, utf16_decode_error2) { std::size_t len = utf16_str_len((const char16_t*)badStr2Utf16le); char32_t* data = new char32_t[len]; EXPECT_THROW(utf16_to_ucs4((const char16_t*)badStr2Utf16le, data, len), bad_conversion); delete[] data; } // // Код символа не входит в диапазон символов UCS-4 // TEST(ConvertError, ucs4_wrong_symbol) { EXPECT_THROW(ucs4_get_utf8_symbol_size(*(const char32_t*)badStrUcs4, false), bad_conversion); EXPECT_THROW(ucs4_get_utf16_symbol_size(*(const char32_t*)badStrUcs4), bad_conversion); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Тесты класса ustring // // // Конструктор из UTF-8 // TEST(Ustring, utf8_constructor) { ustring str((const char*)helloStrUtf8); ustring str2("Hello\\u0020world!", true); ustring str3("Hello\\u0020world!", false); ustring str4 = "Hello\\u0020world!"; EXPECT_EQ(0, memcmp(helloStrUcs4, str.utf32_str(), sizeof(helloStrUcs4))); EXPECT_TRUE(str2 == "Hello world!"); EXPECT_TRUE(str3 == "Hello\\u0020world!"); EXPECT_TRUE(str4 == "Hello\\u0020world!"); } // // Конструктор из UTF-16LE // TEST(Ustring, utf16_constructor) { ustring str((const char16_t*)helloStrUtf16le); EXPECT_EQ(0, memcmp(helloStrUcs4, str.utf32_str(), sizeof(helloStrUcs4))); } // // Конструктор из UTF-32LE // TEST(Ustring, utf32_constructor) { ustring str((const char32_t*)helloStrUcs4); EXPECT_EQ(0, memcmp(helloStrUcs4, str.utf32_str(), sizeof(helloStrUcs4))); } // // Оператор UTF-8 // TEST(Ustring, utf8_cast_operator) { ustring str((const char32_t*)helloStrUcs4); EXPECT_EQ(0, memcmp(helloStrUtf8, str.utf8_str(), sizeof(helloStrUtf8))); EXPECT_EQ(0, memcmp(str.utf8_str(false, true), helloStrEscaped, sizeof(helloStrEscaped))); } // // Оператор UTF-16LE // TEST(Ustring, utf16_cast_operator) { ustring str((const char32_t*)helloStrUcs4); EXPECT_EQ(0, memcmp(helloStrUtf16le, str.utf16_str(), sizeof(helloStrUtf16le))); } // // Конкатенация строк (operator+=) // TEST(Ustring, concat_op_plus_assign) { ustring s1((const char32_t*)helloPart1Ucs4); ustring s2((const char32_t*)helloPart2Ucs4); ustring s3((const char32_t*)helloPart3Ucs4); s1 += s2; s1 += s3; EXPECT_EQ(0, memcmp(helloResultUcs4, s1.utf32_str(), sizeof(helloResultUcs4))); } // // Конкатенация строк (operator+) // TEST(Ustring, concat_op_plus) { ustring s1((const char32_t*)helloPart1Ucs4); //ustring s2((const char32_t*)helloPart2Ucs4); //ustring s3((const char32_t*)helloPart3Ucs4); ustring res = s1 + "big" + " world"; EXPECT_EQ(0, memcmp(helloResultUcs4, res.utf32_str(), sizeof(helloResultUcs4))); } // // Поиск символа в строке // TEST(Ustring, find_symbol) { ustring str((const char32_t*)helloResultUcs4); EXPECT_EQ(4, str.find('o')); EXPECT_EQ(11, str.find('o', 8)); } // // Поиск подстроки в строке // TEST(Ustring, find_string) { ustring str = "Hello World! asdf qwerty asdf"; EXPECT_EQ(13, str.find("asdf")); EXPECT_EQ(7, str.find("orld")); EXPECT_EQ(25, str.find("asdf", 14)); } // // Вывод в поток std::ostream // TEST(Ustring, std_ostream_output) { std::stringstream ss; ustring str((const char32_t*)helloResultUcs4); ss << str; EXPECT_EQ(0, strcmp(ss.str().c_str(), "Hello big world")); } // // Ввод из std::istream // TEST(Ustring, std_istream_intput) { std::stringstream ss; ustring str; ss << "big"; ss >> str; EXPECT_EQ(0, memcmp(helloPart2Ucs4, str.utf32_str(), sizeof(helloPart2Ucs4))); } // // Проверка на равенство // TEST(Ustring, equality) { ustring str((const char32_t*)helloResultUcs4); ustring str2; EXPECT_TRUE(str == "Hello big world"); EXPECT_TRUE(str2 == ""); } // // Установление нового значения строки // TEST(Ustring, assign_new_value) { ustring s1 = "first string"; ustring s2 = "second string"; s2.assign(s1.utf32_str(false, current_byte_order()) + 6, 6); EXPECT_TRUE(s2 == "string"); } // // Возвращение подстроки // TEST(Ustring, substring) { ustring str = "Hello big world!"; ustring str2 = str.substr(6, 3); ustring str3 = str.substr(6, ustring::npos); EXPECT_TRUE(str2 == "big"); EXPECT_TRUE(str3 == "big world!"); } // // Стирание части строки // TEST(Ustring, erasing) { ustring str = "Hello big world!"; str.erase(6,4); EXPECT_TRUE(str == "Hello world!"); str.erase(5, ustring::npos); EXPECT_TRUE(str == "Hello"); } // // Вставка строки в строку // TEST(Ustring, insert_string) { ustring str = "bla-bla"; str.insert(0, "begin "); EXPECT_TRUE(str == "begin bla-bla"); str.insert(6, "bla "); EXPECT_TRUE(str == "begin bla bla-bla"); str.insert(17, " end"); EXPECT_TRUE(str == "begin bla bla-bla end"); } // // Замена подстроки строкой // TEST(Ustring, replace_string) { ustring str = "111 hello 111 world 111"; ustring str2 = str, str3 = str; str.replace("111", "2222"); EXPECT_TRUE(str == "2222 hello 2222 world 2222"); str2.replace("111", "22"); EXPECT_TRUE(str2 == "22 hello 22 world 22"); str3.replace("111", "222"); EXPECT_TRUE(str3 == "222 hello 222 world 222"); str2 += " more 22 and 22 more 22"; str2.replace("22", "33"); EXPECT_TRUE(str2 == "33 hello 33 world 33 more 33 and 33 more 33"); } /////////////////////////////////////////////////////////////////////////////////////////////// // // threadpool demo // void func(std::size_t n) { for(std::size_t j = 0; j < 100; ++j) { ulog.log_info("thread %, line %", n, j); } std::cout << "end of thread " << n << std::endl; } int geti(int i) { return i; } class foo { public: int bar(int n) { return n; } }; void run_threadpool_demo() { threadpool pool(4); std::vector> vec; foo f; for (std::size_t i = 0; i < 100; ++i) { //std::future fut = pool.add_task([i] { return i; }); std::future fut = pool.add_task(&foo::bar, &f, (int)i); vec.push_back(std::move(fut)); } for(std::size_t i = 0; i < 100; ++i) { std::cout << "res[" << i << "] = " << vec[i].get() << std::endl; } } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }