tesseract v5.3.3.20231005
fileio_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 <stdio.h>
13#include <memory>
14
15#include "fileio.h"
16#include "include_gunit.h"
17
18namespace tesseract {
19
20TEST(FileTest, JoinPath) {
21 EXPECT_EQ("/abc/def", File::JoinPath("/abc", "def"));
22 EXPECT_EQ("/abc/def", File::JoinPath("/abc/", "def"));
23 EXPECT_EQ("def", File::JoinPath("", "def"));
24}
25
26TEST(OutputBufferTest, WriteString) {
27 const int kMaxBufSize = 128;
28 char buffer[kMaxBufSize];
29 for (char &i : buffer) {
30 i = '\0';
31 }
32 FILE *fp = tmpfile();
33 CHECK(fp != nullptr);
34
35 auto output = std::make_unique<OutputBuffer>(fp);
36 output->WriteString("Hello ");
37 output->WriteString("world!");
38
39 rewind(fp);
40 auto s = "Hello world!";
41 fread(buffer, strlen(s), 1, fp);
42 EXPECT_STREQ(s, buffer);
43}
44
45TEST(InputBufferTest, Read) {
46 const int kMaxBufSize = 128;
47 char buffer[kMaxBufSize];
48 auto s = "Hello\n world!";
49 strncpy(buffer, s, kMaxBufSize);
50 EXPECT_STREQ(s, buffer);
51 FILE *fp = tmpfile();
52 CHECK(fp != nullptr);
53 fwrite(buffer, strlen(s), 1, fp);
54 rewind(fp);
55
56 std::string str;
57 auto input = std::make_unique<InputBuffer>(fp);
58 EXPECT_TRUE(input->Read(&str));
59 std::vector<std::string> lines = split(str, '\n');
60 EXPECT_EQ(2, lines.size());
61 EXPECT_EQ("Hello", lines[0]);
62 EXPECT_EQ(" world!", lines[1]);
63}
64
65} // namespace tesseract
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2112
#define CHECK(condition)
Definition: include_gunit.h:76
const std::vector< std::string > split(const std::string &s, char c)
Definition: helpers.h:43
TEST(TesseractInstanceTest, TestMultipleTessInstances)
int Read(int fd, void *buf, unsigned int count)
Definition: gtest-port.h:2094
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:84