1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/grammar/utils/locale-shard-map.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 namespace libtextclassifier3::grammar {
23 namespace {
24
25 using ::testing::SizeIs;
26
TEST(LocaleShardMapTest,HandlesSimpleShard)27 TEST(LocaleShardMapTest, HandlesSimpleShard) {
28 LocaleShardMap locale_shard_map = LocaleShardMap::CreateLocaleShardMap(
29 {"ar-EG", "bn-BD", "cs-CZ", "da-DK", "de-DE", "en-US", "es-ES", "fi-FI",
30 "fr-FR", "gu-IN", "id-ID", "it-IT", "ja-JP", "kn-IN", "ko-KR", "ml-IN",
31 "mr-IN", "nl-NL", "no-NO", "pl-PL", "pt-BR", "ru-RU", "sv-SE", "ta-IN",
32 "te-IN", "th-TH", "tr-TR", "uk-UA", "ur-PK", "vi-VN", "zh-TW"});
33
34 EXPECT_EQ(locale_shard_map.GetNumberOfShards(), 31);
35 for (int i = 0; i < 31; i++) {
36 EXPECT_THAT(locale_shard_map.GetLocales(i), SizeIs(1));
37 }
38 EXPECT_EQ(locale_shard_map.GetLocales(0)[0], Locale::FromBCP47("ar-EG"));
39 EXPECT_EQ(locale_shard_map.GetLocales(8)[0], Locale::FromBCP47("fr-FR"));
40 EXPECT_EQ(locale_shard_map.GetLocales(16)[0], Locale::FromBCP47("mr-IN"));
41 EXPECT_EQ(locale_shard_map.GetLocales(24)[0], Locale::FromBCP47("te-IN"));
42 EXPECT_EQ(locale_shard_map.GetLocales(30)[0], Locale::FromBCP47("zh-TW"));
43 }
44
TEST(LocaleTagShardTest,HandlesWildCard)45 TEST(LocaleTagShardTest, HandlesWildCard) {
46 LocaleShardMap locale_shard_map = LocaleShardMap::CreateLocaleShardMap({"*"});
47 EXPECT_EQ(locale_shard_map.GetNumberOfShards(), 1);
48 EXPECT_THAT(locale_shard_map.GetLocales(0), SizeIs(1));
49 }
50
TEST(LocaleTagShardTest,HandlesMultipleLocalePerShard)51 TEST(LocaleTagShardTest, HandlesMultipleLocalePerShard) {
52 LocaleShardMap locale_shard_map =
53 LocaleShardMap::CreateLocaleShardMap({"ar-EG,bn-BD,cs-CZ", "en-*"});
54 EXPECT_EQ(locale_shard_map.GetNumberOfShards(), 2);
55 EXPECT_EQ(locale_shard_map.GetLocales(0)[0], Locale::FromBCP47("ar-EG"));
56 EXPECT_EQ(locale_shard_map.GetLocales(0)[1], Locale::FromBCP47("bn-BD"));
57 EXPECT_EQ(locale_shard_map.GetLocales(0)[2], Locale::FromBCP47("cs-CZ"));
58 EXPECT_EQ(locale_shard_map.GetLocales(1)[0], Locale::FromBCP47("en"));
59
60 EXPECT_EQ(locale_shard_map.GetShard("ar-EG,bn-BD,cs-CZ"), 0);
61 EXPECT_EQ(locale_shard_map.GetShard("bn-BD,cs-CZ,ar-EG"), 0);
62 EXPECT_EQ(locale_shard_map.GetShard("bn-BD,ar-EG,cs-CZ"), 0);
63 EXPECT_EQ(locale_shard_map.GetShard("ar-EG,cs-CZ,bn-BD"), 0);
64 }
65
TEST(LocaleTagShardTest,HandlesEmptyLocaleTag)66 TEST(LocaleTagShardTest, HandlesEmptyLocaleTag) {
67 LocaleShardMap locale_shard_map =
68 LocaleShardMap::CreateLocaleShardMap({"", "en-US"});
69 EXPECT_EQ(locale_shard_map.GetNumberOfShards(), 2);
70 EXPECT_THAT(locale_shard_map.GetLocales(0), SizeIs(0));
71 EXPECT_THAT(locale_shard_map.GetLocales(1), SizeIs(1));
72 EXPECT_EQ(locale_shard_map.GetLocales(1)[0], Locale::FromBCP47("en-US"));
73 }
74
75 } // namespace
76 } // namespace libtextclassifier3::grammar
77