tesseract  5.0.0
normstrngs_test.h
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 #ifndef TESSERACT_UNITTEST_NORMSTRNGS_TEST_H_
13 #define TESSERACT_UNITTEST_NORMSTRNGS_TEST_H_
14 
15 #include <tesseract/unichar.h>
16 #include <sstream> // for std::stringstream
17 #include <string>
18 #include <vector>
19 
20 namespace tesseract {
21 
22 inline std::string CodepointList(const std::vector<char32> &str32) {
23  std::stringstream result;
24  int total_chars = str32.size();
25  result << std::hex;
26  for (int i = 0; i < total_chars; ++i) {
27  result << "[" << str32[i] << "]";
28  }
29  return result.str();
30 }
31 
32 inline std::string PrintString32WithUnicodes(const std::string &str) {
33  std::vector<char32> str32 = UNICHAR::UTF8ToUTF32(str.c_str());
34  std::string s = "\"";
35  s += "\" " + CodepointList(str32);
36  return s;
37 }
38 
39 inline std::string PrintStringVectorWithUnicodes(const std::vector<std::string> &glyphs) {
40  std::string result;
41  for (const auto &s : glyphs) {
42  result += "Glyph:";
43  result += PrintString32WithUnicodes(s) + "\n";
44  }
45  return result;
46 }
47 
48 inline void ExpectGraphemeModeResults(const std::string &str, UnicodeNormMode u_mode,
49  int unicode_count, int glyph_count, int grapheme_count,
50  const std::string &target_str) {
51  std::vector<std::string> glyphs;
52  std::string s;
53  EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(
54  u_mode, OCRNorm::kNone, GraphemeNormMode::kIndividualUnicodes, true, str.c_str(), &glyphs));
55  EXPECT_EQ(glyphs.size(), unicode_count) << PrintStringVectorWithUnicodes(glyphs);
56  for (auto &glyph : glyphs) {
57  s += glyph;
58  }
59  EXPECT_EQ(target_str, s);
61  true, str.c_str(), &glyphs));
62  EXPECT_EQ(glyphs.size(), glyph_count) << PrintStringVectorWithUnicodes(glyphs);
63  s.clear();
64  for (auto &glyph : glyphs) {
65  s += glyph;
66  }
67  EXPECT_EQ(target_str, s);
69  true, str.c_str(), &glyphs));
70  EXPECT_EQ(glyphs.size(), grapheme_count) << PrintStringVectorWithUnicodes(glyphs);
71  s.clear();
72  for (auto &glyph : glyphs) {
73  s += glyph;
74  }
75  EXPECT_EQ(target_str, s);
77  true, str.c_str(), &glyphs));
78  EXPECT_EQ(glyphs.size(), 1) << PrintStringVectorWithUnicodes(glyphs);
79  EXPECT_EQ(target_str, glyphs[0]);
80  std::string result;
81  EXPECT_TRUE(
82  NormalizeUTF8String(u_mode, OCRNorm::kNone, GraphemeNorm::kNormalize, str.c_str(), &result));
83  EXPECT_EQ(target_str, result);
84 }
85 
86 } // namespace tesseract
87 
88 #endif // TESSERACT_UNITTEST_NORMSTRNGS_TEST_H_
std::string PrintString32WithUnicodes(const std::string &str)
void ExpectGraphemeModeResults(const std::string &str, UnicodeNormMode u_mode, int unicode_count, int glyph_count, int grapheme_count, const std::string &target_str)
std::string PrintStringVectorWithUnicodes(const std::vector< std::string > &glyphs)
bool NormalizeCleanAndSegmentUTF8(UnicodeNormMode u_mode, OCRNorm ocr_normalize, GraphemeNormMode g_mode, bool report_errors, const char *str8, std::vector< std::string > *graphemes)
Definition: normstrngs.cpp:179
bool NormalizeUTF8String(UnicodeNormMode u_mode, OCRNorm ocr_normalize, GraphemeNorm grapheme_normalize, const char *str8, std::string *normalized)
Definition: normstrngs.cpp:152
std::string CodepointList(const std::vector< char32 > &str32)
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
Definition: unichar.cpp:220