tesseract  5.0.0-alpha-619-ge9db
validator_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 #include "validator.h"
13 
14 #include "gmock/gmock.h" // for testing::ElementsAreArray
15 #include "include_gunit.h"
16 
17 namespace tesseract {
18 namespace {
19 
20 class TestableValidator : public Validator {
21  public:
22  static ViramaScript TestableMostFrequentViramaScript(
23  const std::vector<char32>& utf32) {
24  return MostFrequentViramaScript(utf32);
25  }
26 };
27 
28 // The majority of Validator is tested by the script-specific tests of its
29 // subclasses, but the MostFrequentViramaScript function is worth a unittest.
30 TEST(ValidatorTest, MostFrequentViramaScript) {
31  // The most frequent virama script should come out correct, despite
32  // distractions from other scripts.
33  EXPECT_EQ(ViramaScript::kTelugu,
34  TestableValidator::TestableMostFrequentViramaScript({0xc05}));
35  // It is still Telugu surrounded by Latin.
36  EXPECT_EQ(ViramaScript::kTelugu,
37  TestableValidator::TestableMostFrequentViramaScript(
38  {'a', 0xc05, 'b', 'c'}));
39  // But not still Telugu surrounded by Devanagari.
40  EXPECT_EQ(ViramaScript::kDevanagari,
41  TestableValidator::TestableMostFrequentViramaScript(
42  {0x905, 0xc05, 0x906, 0x907}));
43  EXPECT_EQ(ViramaScript::kKannada,
44  TestableValidator::TestableMostFrequentViramaScript(
45  {0xc85, 0xc05, 0xc86, 0xc87}));
46  EXPECT_EQ(ViramaScript::kBengali,
47  TestableValidator::TestableMostFrequentViramaScript(
48  {0x985, 0xc05, 0x986, 0x987}));
49  // Danda and double Danda don't count as Devanagari, as they are common.
50  EXPECT_EQ(ViramaScript::kTelugu,
51  TestableValidator::TestableMostFrequentViramaScript(
52  {0x964, 0xc05, 0x965, 0x965}));
53 }
54 
55 // ValidateCleanAndSegment doesn't modify the input by much, but its
56 // transformation should be idempotent. (Doesn't change again if re-applied.)
57 TEST(ValidatorTest, Idempotency) {
58  std::vector<char32> str1(
59  {0xd24, 0xd23, 0xd32, 0xd4d, '\'', 0x200d, 0x200c, 0x200d, 0x200c});
60  std::vector<char32> str2(
61  {0xd24, 0xd23, 0xd32, 0xd4d, 0x200c, 0x200d, 0x200c, 0x200d, '\''});
62  std::vector<std::vector<char32>> result1, result2, result3, result4;
64  GraphemeNormMode::kSingleString, true, str1, &result1));
66  GraphemeNormMode::kSingleString, true, result1[0], &result2));
67  EXPECT_EQ(result1.size(), result2.size());
68  EXPECT_THAT(result2[0], testing::ElementsAreArray(result1[0]));
70  GraphemeNormMode::kSingleString, true, str2, &result3));
72  GraphemeNormMode::kSingleString, true, result3[0], &result4));
73  EXPECT_EQ(result3.size(), result4.size());
74  EXPECT_THAT(result4[0], testing::ElementsAreArray(result3[0]));
75 }
76 
77 } // namespace
78 } // namespace tesseract
tesseract::ViramaScript
ViramaScript
Definition: validator.h:67
tesseract::ViramaScript::kTelugu
include_gunit.h
tesseract::Validator::ValidateCleanAndSegment
static bool ValidateCleanAndSegment(GraphemeNormMode g_mode, bool report_errors, const std::vector< char32 > &src, std::vector< std::vector< char32 >> *dest)
Definition: validator.cpp:40
tesseract::GraphemeNormMode::kSingleString
validator.h
tesseract::ViramaScript::kBengali
tesseract::ViramaScript::kDevanagari
tesseract::ViramaScript::kKannada
tesseract
Definition: baseapi.h:65