tesseract  5.0.0-alpha-619-ge9db
unichar_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 "include_gunit.h"
13 #include "gmock/gmock.h" // for testing::ElementsAreArray
14 #include <tesseract/unichar.h>
15 
16 using tesseract::char32;
17 using tesseract::UNICHAR;
18 
19 namespace {
20 
21 TEST(UnicharTest, Conversion) {
22  // This test verifies that Unichar::UTF8ToUTF32 and Unichar::UTF32ToUTF8
23  // show the required conversion properties.
24  // Test for round-trip utf8-32-8 for 1, 2, 3 and 4 byte codes.
25  const char* kUTF8Src = "a\u05d0\u0ca4\U0002a714";
26  const std::vector<char32> kUTF32Src = {'a', 0x5d0, 0xca4, 0x2a714};
27  // Check for round-trip conversion.
28  std::vector<char32> utf32 = UNICHAR::UTF8ToUTF32(kUTF8Src);
29  EXPECT_THAT(utf32, testing::ElementsAreArray(kUTF32Src));
30  std::string utf8 = UNICHAR::UTF32ToUTF8(utf32);
31  EXPECT_STREQ(kUTF8Src, utf8.c_str());
32 }
33 
34 TEST(UnicharTest, InvalidText) {
35  // This test verifies that Unichar correctly deals with invalid text.
36  const char* kInvalidUTF8 = "a b\200d string";
37  const std::vector<char32> kInvalidUTF32 = {'a', ' ', 0x200000, 'x'};
38  // Invalid utf8 produces an empty vector.
39  std::vector<char32> utf32 = UNICHAR::UTF8ToUTF32(kInvalidUTF8);
40  EXPECT_TRUE(utf32.empty());
41  // Invalid utf32 produces an empty string.
42  std::string utf8 = UNICHAR::UTF32ToUTF8(kInvalidUTF32);
43  EXPECT_TRUE(utf8.empty());
44 }
45 
46 } // namespace
string
std::string string
Definition: equationdetect_test.cc:21
include_gunit.h
tesseract::UNICHAR
Definition: unichar.h:59
tesseract::char32
signed int char32
Definition: unichar.h:53
unichar.h