diff options
author | Daria Volvenkova <d.volvenkova@corp.mail.ru> | 2018-11-26 17:16:27 +0300 |
---|---|---|
committer | Vladimir Byko-Ianko <bykoianko@gmail.com> | 2018-11-28 08:59:15 +0300 |
commit | c15be91b207bbf97033a3750817d6cc5bf63b28d (patch) | |
tree | bdc69fecaed078569ea4c75b500d43dc4155b00a /descriptions | |
parent | cabf907b3dfdfaecc1fdb6f4752c7d5e8c226b8c (diff) |
[descriptions] Descriptions tests added.
Diffstat (limited to 'descriptions')
-rw-r--r-- | descriptions/descriptions_tests/descriptions_tests.cpp | 250 | ||||
-rw-r--r-- | descriptions/serdes.cpp | 6 | ||||
-rw-r--r-- | descriptions/serdes.hpp | 4 |
3 files changed, 231 insertions, 29 deletions
diff --git a/descriptions/descriptions_tests/descriptions_tests.cpp b/descriptions/descriptions_tests/descriptions_tests.cpp index e37592b6cc..076d5d93b4 100644 --- a/descriptions/descriptions_tests/descriptions_tests.cpp +++ b/descriptions/descriptions_tests/descriptions_tests.cpp @@ -5,6 +5,7 @@ #include "coding/reader.hpp" #include "coding/writer.hpp" +#include <cstdint> #include <map> #include <string> #include <utility> @@ -12,9 +13,33 @@ using namespace descriptions; +using RawDescriptionsCollection = std::map<FeatureIndex, std::map<LangCode, std::string>>; + +template <typename Reader> +std::string GetDescription(Reader & reader, FeatureIndex fid, std::vector<int8_t> const & langPriority) +{ + Deserializer des; + std::string description; + des.Deserialize(reader, fid, langPriority, description); + return description; +} + +DescriptionsCollection Convert(RawDescriptionsCollection const & rawDescriptionsCollection) +{ + DescriptionsCollection descriptionsCollection; + for (auto const & featureDesc : rawDescriptionsCollection) + { + StringUtf8Multilang str; + for (auto const & translation : featureDesc.second) + str.AddString(translation.first, translation.second); + descriptionsCollection.emplace_back(featureDesc.first, std::move(str)); + } + return descriptionsCollection; +} + UNIT_TEST(Descriptions_SerDes) { - std::map<FeatureIndex, std::map<LangCode, std::string>> data = + RawDescriptionsCollection const data = { {100, {{10, "Description of feature 100, language 10."}, {11, "Описание фичи 100, язык 11."}}}, {101, {{11, "Описание фичи 101, язык 11."}}}, @@ -22,40 +47,209 @@ UNIT_TEST(Descriptions_SerDes) {10, "Description of feature 102, language 10."}}} }; - DescriptionsCollection descriptionsCollection; - for (auto const & featureDesc : data) + auto testData = [](MemReader & reader) { - StringUtf8Multilang str; - for (auto const & translation : featureDesc.second) - str.AddString(translation.first, translation.second); - descriptionsCollection.emplace_back(featureDesc.first, std::move(str)); - } + TEST_EQUAL(GetDescription(reader, 102, {11, 10}), "Описание фичи 102, язык 11.", ()); + TEST_EQUAL(GetDescription(reader, 100, {12, 10}), "Description of feature 100, language 10.", ()); + TEST_EQUAL(GetDescription(reader, 101, {12}), "", ()); + TEST_EQUAL(GetDescription(reader, 0, {10, 11}), "", ()); + TEST_EQUAL(GetDescription(reader, 102, {10}), "Description of feature 102, language 10.", ()); + }; - std::vector<uint8_t> buffer; { - Serializer ser(std::move(descriptionsCollection)); - MemWriter<decltype(buffer)> writer(buffer); - ser.Serialize(writer); + std::vector<uint8_t> buffer; + { + auto descriptionsCollection = Convert(data); + Serializer ser(std::move(descriptionsCollection)); + MemWriter<decltype(buffer)> writer(buffer); + ser.Serialize(writer); + } + + MemReader reader(buffer.data(), buffer.size()); + + testData(reader); } - std::string description1; - std::string description2; - std::string description3; - std::string description4; - std::string description5; { - Deserializer des; + size_t const kDummyBytesCount = 100; + std::vector<uint8_t> buffer(kDummyBytesCount); + { + auto descriptionsCollection = Convert(data); + Serializer ser(std::move(descriptionsCollection)); + MemWriter<decltype(buffer)> writer(buffer); + writer.Seek(kDummyBytesCount); + ser.Serialize(writer); + + std::vector<uint8_t> buffer2(kDummyBytesCount); + writer.Write(buffer2.data(), buffer2.size()); + } + MemReader reader(buffer.data(), buffer.size()); - des.Deserialize(reader, 102, {11, 10}, description1); - des.Deserialize(reader, 100, {12, 10}, description2); - des.Deserialize(reader, 101, {12}, description3); - des.Deserialize(reader, 0, {10, 11}, description4); - des.Deserialize(reader, 102, {10}, description5); + auto subReader = reader.SubReader(kDummyBytesCount, buffer.size() - kDummyBytesCount); + + testData(subReader); } +} - TEST_EQUAL(description1, "Описание фичи 102, язык 11.", ()); - TEST_EQUAL(description2, "Description of feature 100, language 10.", ()); - TEST_EQUAL(description3, "", ()); - TEST_EQUAL(description4, "", ()); - TEST_EQUAL(description5, "Description of feature 102, language 10.", ()); +UNIT_TEST(Descriptions_Html) +{ + RawDescriptionsCollection const data = + { {100, {{1, "<div class=\"section\">\n" + "<p lang=\"en\">Map data © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> contributors, <a href=\"http://opendatacommons.org/licenses/odbl/\">ODbL</a>.</p>\n" + "</div>"}, + {2, "<div class=\"section\">\n" + "<p lang=\"ru\">Картографические данные © участники <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>, <a href=\"http://opendatacommons.org/licenses/odbl/\">ODbL</a>.</p>\n" + "</div>"}, + {3, "<div class=\"section\">\n" + "<p lang=\"vi\">Dữ liệu bản đồ © Cộng tác viên của <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>, ODbL.</p>\n" + "</div>"}, + {4, "<div class=\"section\">\n" + "<p lang=\"tr\">Harita verileri © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> katkıları, ODbL.</p>\n" + "</div>"}, + {5, "<div class=\"section\">\n" + "<p lang=\"th\">ข้อมูลแผนที่ © ผู้มีส่วนร่วม <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> ODbL.</p>\n" + "</div>"}, + {6, "<div class=\"section\">\n" + "<p lang=\"sv\">Map data © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>-bidragsgivare, ODbL.</p>\n" + "</div>"}, + {7, "<div class=\"section\">\n" + "<p lang=\"es\">Contribuidores de los datos de mapas ©" + " <a href=\"https://www.openstreetmap.org/\">OpenStreetMap</a>, Licencia ODbL.</p>\n" + "</div>"}, + {8, "<div class=\"section\">\n" + "<p lang=\"pt\">Dados de mapas de contribuintes do ©" + " <a href=\"https://www.openstreetmap.org/\">OpenStreetMap</a>, ODbL.</p>\n" + "</div>"}, + {9, "<div class=\"section\">\n" + "<p lang=\"pl\">Dane map © Współautorzy" + " <a href=\"https://www.openstreetmap.org/\">OpenStreetMap</a>, ODbL.</p>\n" + "</div>"}, + {10, "<div class=\"section\">\n" + "<p lang=\"nb\">Kartdata © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> bidragsytere, ODbL.</p>\n" + "</div>"}, + {11, "<div class=\"section\">\n" + "<p lang=\"ko\">지도 데이터 © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> contributors, ODbL.</p>\n" + "</div>"}, + {12, "<div class=\"section\">\n" + "<p lang=\"ja\">地図データ © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>貢献者、ODbL。</p>\n" + "</div>"}, + {13, "<div class=\"section\">\n" + "<p lang=\"it\">Dati delle mappe © Contenuti <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>, ODbL.</p>\n" + "</div>"}, + {14, "<div class=\"section\">\n" + "<p lang=\"id\">Data Peta © Kontributor <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>, ODbL.</p>\n" + "</div>"}, + {15, "<div class=\"section\">\n" + "<p lang=\"hu\">Térképadat © az <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> közreműködői, ODbL.</p>\n" + "</div>"}, + {16, "<div class=\"section\">\n" + "<p lang=\"de\">Kartendaten © <a href=\"http://www.opentreetmap.org/\">" + "OpenStreetMap</a>-Mitwirkende ODbL.</p>\n" + "</div>"}, + {17, "<div class=\"section\">\n" + "<p lang=\"fr\">Données de la carte sous © des contributeurs d'" + "<a href=\"https://www.openstreetmap.org/\">OpenStreetMap</a>, licence ODbL.</p>\n" + "</div>"}, + {18, "<div class=\"section\">\n" + "<p lang=\"fi\">Karttatiedot © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>-avustajat, ODbL.</p>\n" + "</div>"}, + {19, "<div class=\"section\">\n" + "<p lang=\"nl\">Kaartgegevens © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> bjdragers, ODbL.</p>\n" + "</div>"}, + {20, "<div class=\"section\">\n" + "<p lang=\"cs\">Mapová data © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> přispěvatelé, ODbL.</p>\n" + "</div>"}, + {21, "<div class=\"section\">\n" + "<p lang=\"zh-Hans\">地图数据 © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> 贡献者, ODbL.</p>\n" + "</div>"}, + {22, "<div class=\"section\">\n" + "<p lang=\"zh-Hant\">地圖數據 © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> 貢獻者, ODbL.</p>\n" + "</div>"}, + {23, "<div class=\"section\">\n" + "<p lang=\"ar\">المساهمون في بيانات خريطة © <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a> و ODbL.</p>\n" + "</div>"}, + {24, "<div class=\"section\">\n" + "<p lang=\"uk\">Картографічні дані © учасники <a href=\"https://www.openstreetmap.org/\">" + "OpenStreetMap</a>, ODbL.</p>" + "</div>"}}}, + {101, {{1, "<p lang=\"en\"><strong>maps.me</strong> would not be possible without the generous contributions of " + "the following projects:</p>\n"}, + {2, "<p lang=\"ru\">Приложение <strong>maps.me</strong> было бы невозможно без участия следующих" + " проектов:</p>\n"}, + {3, "<p lang=\"vi\"><strong>maps.me</strong> sẽ không thành hiện thực nếu không có sự đóng góp hào phóng" + " từ các dự án sau:</p>\n"}, + {4, "<p lang=\"tr\"><strong>maps.me</strong>, aşağıdaki projelerin cömert katkıları olmadan mümkün " + "olmazdı:</p>\n"}, + {5, "<p lang=\"th\"><strong>maps.me</strong> จะสำเร็จลุล่วงไม่ได้เลยหากปราศจากความเอื้อเฟื้อเพื่อการร่วมมือของโป" + "รเจกต์ดังต่อไปนี้:</p>\n"}, + {6, "<p lang=\"sv\"><strong>maps.me</strong> skulle inte vara möjlig utan följande projekts generösa" + " bidrag:</p>\n"}, + {7, "<p lang=\"es\"><strong>maps.me</strong> no sería posible sin las generosas aportaciones de los" + " siguientes proyectos:</p>\n"}, + {8, "<p lang=\"pt\">O <strong>maps.me</strong> não seria possível sem as contribuições generosas dos " + "seguintes projetos:</p>\n"}, + {9, "<p lang=\"pl\">Aplikacja <strong>maps.me</strong> nie powstałaby bez znaczącego wkładu ze strony " + "twórców poniższych projektów:</p>\n"}, + {10, "<p lang=\"nb\"><strong>maps.me</strong> ville ikke vært mulig uten de generøse bidragene fra " + "følgende prosjekter:</p>\n"}, + {11, "<p lang=\"ko\"><strong>maps.me</strong>는 다음 프로젝트의 아낌없는 기부없이 가능하지 않습니다:</p>\n"}, + {12, "<p lang=\"ja\"><strong>maps.me</strong>は次のプロジェクトの手厚い貢献なしには不可能です:</p>\n"}, + {13, "<p lang=\"it\"><strong>maps.me</strong> non sarebbe realizzabile senza il generoso contributo " + "dei seguenti progetti:</p>\n"}, + {14, "<p lang=\"id\"><strong>maps.me</strong> tidak mungkin tercipta tanpa kontribusi yang tulus dari " + "proyek-proyek berikut ini:</p>\n"}, + {15, "<p lang=\"hu\">A <strong>maps.me</strong> nem jöhetett volna létre az alábbi projektek nagylelkű " + "közreműködése nélkül:</p>\n"}, + {16, "<p lang=\"de\"><strong>maps.me</strong> wäre ohne die großzügigen Spenden der folgenden Projekte " + "nicht möglich:</p>\n"}, + {17, "<p lang=\"fr\">L'existence de <strong>maps.me</strong> serait impossible sans les généreuses " + "contributions des projets suivants :</p>\n"}, + {18, "<p lang=\"fi\"><strong>maps.me</strong> ei olisi mahdollinen ilman seuraavien projektien aulista " + "tukea:</p>\n"}, + {19, "<p lang=\"nl\"><strong>maps.me</strong> zou niet mogelijk zijn zonder de genereuze bijdragen voor " + "de volgende projecten:</p>\n"}, + {20, "<p lang=\"cs\"><strong>maps.me</strong> by nemohlo existovat bez štědrých přispění následujících " + "projektů:</p>\n"}, + {21, "<p lang=\"zh-Hans\">沒有下面項目的慷慨貢獻,<strong>maps.me</strong> 不可能出現:</p>\n"}, + {22, "<p lang=\"zh-Hant\">沒有下面項目的慷慨貢獻,<strong>maps.me</strong> 不可能出現:</p>\n"}, + {23, "<p lang=\"ar\"> ما كان لـ <strong>maps.me" + "</strong> أن تأتي للوجود بدون المساهمات العظيمة للمشاريع التالية:</p>"}, + {24, "<p lang=\"uk\"><strong>maps.me</strong> був би неможливим без щедрої участі таких проектів:</p>"}}}, + }; + + { + std::vector<uint8_t> buffer; + { + auto descriptionsCollection = Convert(data); + Serializer ser(std::move(descriptionsCollection)); + MemWriter<decltype(buffer)> writer(buffer); + ser.Serialize(writer); + } + + MemReader reader(buffer.data(), buffer.size()); + + for (auto const & featureDesc : data) + { + for (auto const & translation : featureDesc.second) + TEST_EQUAL(GetDescription(reader, featureDesc.first, {translation.first}), translation.second, ()); + } + } } diff --git a/descriptions/serdes.cpp b/descriptions/serdes.cpp index 75da40dc8b..e5d6fbfd1d 100644 --- a/descriptions/serdes.cpp +++ b/descriptions/serdes.cpp @@ -7,6 +7,8 @@ namespace descriptions Serializer::Serializer(DescriptionsCollection && descriptions) : m_descriptions(std::move(descriptions)) { + CHECK(!m_descriptions.empty(), ()); + std::sort(m_descriptions.begin(), m_descriptions.end(), base::LessBy(&FeatureDescription::m_featureIndex)); m_langMetaCollection.reserve(m_descriptions.size()); @@ -16,10 +18,14 @@ Serializer::Serializer(DescriptionsCollection && descriptions) for (size_t i = 0; i < m_descriptions.size(); ++i) { auto & index = m_descriptions[i]; + CHECK(!index.m_description.IsEmpty(), ()); LangMeta langMeta; index.m_description.ForEach([this, &stringsCount, &langMeta, i](LangCode lang, std::string const & str) { + CHECK_GREATER_OR_EQUAL(lang, 0, ()); + CHECK(lang < StringUtf8Multilang::kMaxSupportedLanguages, ()); + CHECK(!str.empty(), ()); ++stringsCount; auto & group = m_groupedByLang[lang]; langMeta.insert(std::make_pair(lang, static_cast<StringIndex>(group.size()))); diff --git a/descriptions/serdes.hpp b/descriptions/serdes.hpp index 8c072a09e4..e05e38e713 100644 --- a/descriptions/serdes.hpp +++ b/descriptions/serdes.hpp @@ -52,6 +52,7 @@ using DescriptionsCollection = std::vector<FeatureDescription>; /// Section name: "descriptions". /// Description: keeping text descriptions of features in different languages. /// Section tables: +/// * version /// * header /// * sorted feature ids vector /// * vector of unordered maps with language codes and string indices of corresponding translations of a description @@ -60,7 +61,8 @@ using DescriptionsCollection = std::vector<FeatureDescription>; class Serializer { public: - /// \param descriptions unsorted collection of feature descriptions. + /// \param descriptions A non-empty unsorted collection of feature descriptions. + /// FeatureDescription::m_description must contain non-empty translations. explicit Serializer(DescriptionsCollection && descriptions); template <typename Sink> |