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 // Portability include to match the Google test environment.
12 
13 #ifndef TESSERACT_UNITTEST_INCLUDE_GUNIT_H_
14 #define TESSERACT_UNITTEST_INCLUDE_GUNIT_H_
15 
16 #include "errcode.h" // for ASSERT_HOST
17 #include "fileio.h"  // for tesseract::File
18 #include "gtest/gtest.h"
19 #include "log.h" // for LOG
20 
21 static const char *FLAGS_test_tmpdir = "./tmp";
22 
23 namespace tesseract {
24 
trim(std::string & s)25 static inline void trim(std::string &s) {
26   s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
27     return !std::isspace(ch);
28   }));
29   s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
30     return !std::isspace(ch);
31   }).base(), s.end());
32 }
33 
34 } // namespace tesseract
35 
36 class file : public tesseract::File {
37 public:
MakeTmpdir()38   static void MakeTmpdir() {
39 #if defined(_WIN32)
40     _mkdir(FLAGS_test_tmpdir);
41 #else
42     mkdir(FLAGS_test_tmpdir, S_IRWXU | S_IRWXG);
43 #endif
44   }
45 
46   // Create a file and write a string to it.
WriteStringToFile(const std::string & contents,const std::string & filename)47   static bool WriteStringToFile(const std::string &contents, const std::string &filename) {
48     File::WriteStringToFileOrDie(contents, filename);
49     return true;
50   }
51 
GetContents(const std::string & filename,std::string * out,int)52   static bool GetContents(const std::string &filename, std::string *out, int) {
53     return File::ReadFileToString(filename, out);
54   }
55 
SetContents(const std::string & name,const std::string & contents,bool)56   static bool SetContents(const std::string &name, const std::string &contents,
57                           bool /*is_default*/) {
58     return WriteStringToFile(contents, name);
59   }
60 
Defaults()61   static int Defaults() {
62     return 0;
63   }
64 
JoinPath(const std::string & s1,const std::string & s2)65   static std::string JoinPath(const std::string &s1, const std::string &s2) {
66     return tesseract::File::JoinPath(s1, s2);
67   }
68 
JoinPath(const std::string & s1,const std::string & s2,const std::string & s3)69   static std::string JoinPath(const std::string &s1, const std::string &s2, const std::string &s3) {
70     return JoinPath(JoinPath(s1, s2), s3);
71   }
72 };
73 
74 // /usr/include/tensorflow/core/platform/default/logging.h defines the CHECK* macros.
75 #if !defined(CHECK)
76 #  define CHECK(condition) \
77     if (!(condition))      \
78     LOG(FATAL) << "Check failed: " #condition " "
79 #  define CHECK_EQ(test, value) CHECK((test) == (value))
80 #  define CHECK_GE(test, value) CHECK((test) >= (value))
81 #  define CHECK_GT(test, value) CHECK((test) > (value))
82 #  define CHECK_LT(test, value) CHECK((test) < (value))
83 #  define CHECK_LE(test, value) CHECK((test) <= (value))
84 #  define CHECK_OK(test) CHECK(test)
85 #endif
86 
87 #endif // TESSERACT_UNITTEST_INCLUDE_GUNIT_H_
88