tesseract v5.3.3.20231005
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 <tesseract/unichar.h>
13#include "gmock/gmock.h" // for testing::ElementsAreArray
14#include "include_gunit.h"
15
16namespace tesseract {
17
18TEST(UnicharTest, Conversion) {
19 // This test verifies that Unichar::UTF8ToUTF32 and Unichar::UTF32ToUTF8
20 // show the required conversion properties.
21 // Test for round-trip utf8-32-8 for 1, 2, 3 and 4 byte codes.
22 const char *kUTF8Src = "a\u05d0\u0ca4\U0002a714";
23 const std::vector<char32> kUTF32Src = {'a', 0x5d0, 0xca4, 0x2a714};
24 // Check for round-trip conversion.
25 std::vector<char32> utf32 = UNICHAR::UTF8ToUTF32(kUTF8Src);
26 EXPECT_THAT(utf32, testing::ElementsAreArray(kUTF32Src));
27 std::string utf8 = UNICHAR::UTF32ToUTF8(utf32);
28 EXPECT_STREQ(kUTF8Src, utf8.c_str());
29}
30
31TEST(UnicharTest, InvalidText) {
32 // This test verifies that Unichar correctly deals with invalid text.
33 const char *kInvalidUTF8 = "a b\200d string";
34 const std::vector<char32> kInvalidUTF32 = {'a', ' ', 0x200000, 'x'};
35 // Invalid utf8 produces an empty vector.
36 std::vector<char32> utf32 = UNICHAR::UTF8ToUTF32(kInvalidUTF8);
37 EXPECT_TRUE(utf32.empty());
38 // Invalid utf32 produces an empty string.
39 std::string utf8 = UNICHAR::UTF32ToUTF8(kInvalidUTF32);
40 EXPECT_TRUE(utf8.empty());
41}
42
43} // namespace tesseract
#define EXPECT_THAT(value, matcher)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2112
TEST(TesseractInstanceTest, TestMultipleTessInstances)
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
Definition: unichar.cpp:220
static std::string UTF32ToUTF8(const std::vector< char32 > &str32)
Definition: unichar.cpp:237