tesseract  5.0.0-alpha-619-ge9db
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 
13 #include <stdio.h>
14 #include <memory>
15 
16 #include "absl/strings/str_split.h"
17 
18 #include "fileio.h"
19 #include "include_gunit.h"
20 
21 namespace {
22 
23 using tesseract::File;
26 
27 TEST(FileTest, JoinPath) {
28  EXPECT_EQ("/abc/def", File::JoinPath("/abc", "def"));
29  EXPECT_EQ("/abc/def", File::JoinPath("/abc/", "def"));
30  EXPECT_EQ("def", File::JoinPath("", "def"));
31 }
32 
33 TEST(OutputBufferTest, WriteString) {
34  const int kMaxBufSize = 128;
35  char buffer[kMaxBufSize];
36  for (int i = 0; i < kMaxBufSize; ++i) buffer[i] = '\0';
37  FILE* fp = fmemopen(buffer, kMaxBufSize, "w");
38  CHECK(fp != nullptr);
39 
40  {
41  std::unique_ptr<OutputBuffer> output(new OutputBuffer(fp));
42  output->WriteString("Hello ");
43  output->WriteString("world!");
44  }
45  EXPECT_STREQ("Hello world!", buffer);
46 }
47 
48 TEST(InputBufferTest, Read) {
49  const int kMaxBufSize = 128;
50  char buffer[kMaxBufSize];
51  snprintf(buffer, kMaxBufSize, "Hello\n world!");
52  EXPECT_STREQ("Hello\n world!", buffer);
53  FILE* fp = fmemopen(buffer, kMaxBufSize, "r");
54  CHECK(fp != nullptr);
55 
56  std::string str;
57  std::unique_ptr<InputBuffer> input(new InputBuffer(fp));
58  EXPECT_TRUE(input->Read(&str));
59  std::vector<std::string> lines = absl::StrSplit(str, '\n', absl::SkipEmpty());
60  EXPECT_EQ(2, lines.size());
61  EXPECT_EQ("Hello", lines[0]);
62  EXPECT_EQ(" world!", lines[1]);
63 }
64 
65 } // namespace
string
std::string string
Definition: equationdetect_test.cc:21
tesseract::OutputBuffer
Definition: fileio.h:103
tesseract::InputBuffer
Definition: fileio.h:81
include_gunit.h
fileio.h
CHECK
#define CHECK(test)
Definition: include_gunit.h:57
tesseract::File
Definition: fileio.h:55