Skip to content

Commit

Permalink
unittest: Format code
Browse files Browse the repository at this point in the history
It was formatted with clang-format-7 -i unittest/*.{c*,h}.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Sep 29, 2018
1 parent 0f3206d commit 9e66fb9
Show file tree
Hide file tree
Showing 53 changed files with 1,484 additions and 1,619 deletions.
133 changes: 66 additions & 67 deletions unittest/apiexample_test.cc
Expand Up @@ -2,7 +2,8 @@
// File: apiexample_test.cc
// Description: Api Test for Tesseract using text fixtures and parameters.
// Tests for Devanagari, Latin and Arabic scripts are disabled by default.
// Disabled tests can be run when required by using the --gtest_also_run_disabled_tests argument.
// Disabled tests can be run when required by using the
// --gtest_also_run_disabled_tests argument.
// ./unittest/apiexample_test --gtest_also_run_disabled_tests
//
// Author: ShreeDevi Kumar
Expand All @@ -21,91 +22,89 @@
// expects clone of tessdata_fast repo in ../../tessdata_fast

//#include "log.h"
#include "include_gunit.h"
#include "baseapi.h"
#include "leptonica/allheaders.h"
#include <iostream>
#include <string>
#include <fstream>
#include <locale>
#include <limits.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <locale>
#include <string>
#include "baseapi.h"
#include "include_gunit.h"
#include "leptonica/allheaders.h"

namespace {

class QuickTest : public testing::Test {
protected:
virtual void SetUp() {
start_time_ = time(nullptr);
}
virtual void SetUp() { start_time_ = time(nullptr); }
virtual void TearDown() {
const time_t end_time = time(nullptr);
EXPECT_TRUE(end_time - start_time_ <=55) << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
EXPECT_TRUE(end_time - start_time_ <= 55)
<< "The test took too long - "
<< ::testing::PrintToString(end_time - start_time_);
}
time_t start_time_;
};
};

void OCRTester(const char* imgname, const char* groundtruth, const char* tessdatadir, const char* lang) {
//log.info() << tessdatadir << " for language: " << lang << std::endl;
char *outText;
std::locale loc("C"); // You can also use "" for the default system locale
std::ifstream file(groundtruth);
file.imbue(loc); // Use it for file input
std::string gtText((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
Pix *image = pixRead(imgname);
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
api->SetImage(image);
outText = api->GetUTF8Text();
EXPECT_EQ(gtText,outText) << "Phototest.tif OCR does not match ground truth for " << ::testing::PrintToString(lang);
api->End();
delete [] outText;
pixDestroy(&image);
}
void OCRTester(const char* imgname, const char* groundtruth,
const char* tessdatadir, const char* lang) {
// log.info() << tessdatadir << " for language: " << lang << std::endl;
char* outText;
std::locale loc("C"); // You can also use "" for the default system locale
std::ifstream file(groundtruth);
file.imbue(loc); // Use it for file input
std::string gtText((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
ASSERT_FALSE(api->Init(tessdatadir, lang))
<< "Could not initialize tesseract.";
Pix* image = pixRead(imgname);
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
api->SetImage(image);
outText = api->GetUTF8Text();
EXPECT_EQ(gtText, outText)
<< "Phototest.tif OCR does not match ground truth for "
<< ::testing::PrintToString(lang);
api->End();
delete[] outText;
pixDestroy(&image);
}

class MatchGroundTruth : public QuickTest ,
public ::testing::WithParamInterface<const char*> {
};
class MatchGroundTruth : public QuickTest,
public ::testing::WithParamInterface<const char*> {};

TEST_P(MatchGroundTruth, FastPhototestOCR) {
OCRTester(TESTING_DIR "/phototest.tif",
TESTING_DIR "/phototest.txt",
TESSDATA_DIR "_fast", GetParam());
}
TEST_P(MatchGroundTruth, FastPhototestOCR) {
OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt",
TESSDATA_DIR "_fast", GetParam());
}

TEST_P(MatchGroundTruth, BestPhototestOCR) {
OCRTester(TESTING_DIR "/phototest.tif",
TESTING_DIR "/phototest.txt",
TESSDATA_DIR "_best", GetParam());
}
TEST_P(MatchGroundTruth, BestPhototestOCR) {
OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt",
TESSDATA_DIR "_best", GetParam());
}

TEST_P(MatchGroundTruth, TessPhototestOCR) {
OCRTester(TESTING_DIR "/phototest.tif",
TESTING_DIR "/phototest.txt",
TESSDATA_DIR , GetParam());
}
TEST_P(MatchGroundTruth, TessPhototestOCR) {
OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt",
TESSDATA_DIR, GetParam());
}

INSTANTIATE_TEST_CASE_P( Eng, MatchGroundTruth,
::testing::Values("eng") );
INSTANTIATE_TEST_CASE_P( DISABLED_Latin, MatchGroundTruth,
::testing::Values("script/Latin") );
INSTANTIATE_TEST_CASE_P( DISABLED_Deva, MatchGroundTruth,
::testing::Values("script/Devanagari") );
INSTANTIATE_TEST_CASE_P( DISABLED_Arabic, MatchGroundTruth,
::testing::Values("script/Arabic") );
INSTANTIATE_TEST_CASE_P(Eng, MatchGroundTruth, ::testing::Values("eng"));
INSTANTIATE_TEST_CASE_P(DISABLED_Latin, MatchGroundTruth,
::testing::Values("script/Latin"));
INSTANTIATE_TEST_CASE_P(DISABLED_Deva, MatchGroundTruth,
::testing::Values("script/Devanagari"));
INSTANTIATE_TEST_CASE_P(DISABLED_Arabic, MatchGroundTruth,
::testing::Values("script/Arabic"));

class EuroText : public QuickTest {
};
class EuroText : public QuickTest {};

TEST_F(EuroText, FastLatinOCR) {
OCRTester(TESTING_DIR "/eurotext.tif",
TESTING_DIR "/eurotext.txt",
TESSDATA_DIR "_fast", "script/Latin");
}
TEST_F(EuroText, FastLatinOCR) {
OCRTester(TESTING_DIR "/eurotext.tif", TESTING_DIR "/eurotext.txt",
TESSDATA_DIR "_fast", "script/Latin");
}

// script/Latin for eurotext.tif does not match groundtruth
// for tessdata & tessdata_best.
// so do not test these here.
// script/Latin for eurotext.tif does not match groundtruth
// for tessdata & tessdata_best.
// so do not test these here.

} // namespace
39 changes: 16 additions & 23 deletions unittest/applybox_test.cc
Expand Up @@ -31,19 +31,13 @@ class ApplyBoxTest : public testing::Test {
std::string TestDataNameToPath(const std::string& name) {
return file::JoinPath(TESTING_DIR, name);
}
std::string TessdataPath() {
return TESSDATA_DIR;
}
std::string TessdataPath() { return TESSDATA_DIR; }
std::string OutputNameToPath(const std::string& name) {
return file::JoinPath(FLAGS_test_tmpdir, name);
}

ApplyBoxTest() {
src_pix_ = NULL;
}
~ApplyBoxTest() {
pixDestroy(&src_pix_);
}
ApplyBoxTest() { src_pix_ = NULL; }
~ApplyBoxTest() { pixDestroy(&src_pix_); }

void SetImage(const char* filename) {
pixDestroy(&src_pix_);
Expand All @@ -70,7 +64,7 @@ class ApplyBoxTest : public testing::Test {
api_.Recognize(NULL);
char* ocr_text = api_.GetUTF8Text();
EXPECT_STREQ(truth_str, ocr_text);
delete [] ocr_text;
delete[] ocr_text;
// Test the boxes by reading the target box file in parallel with the
// bounding boxes in the ocr output.
std::string box_filename = TestDataNameToPath(target_box_file);
Expand All @@ -80,22 +74,21 @@ class ApplyBoxTest : public testing::Test {
ResultIterator* it = api_.GetIterator();
do {
int left, top, right, bottom;
EXPECT_TRUE(it->BoundingBox(tesseract::RIL_SYMBOL,
&left, &top, &right, &bottom));
TBOX ocr_box(ICOORD(left, height - bottom),
ICOORD(right, height - top));
EXPECT_TRUE(
it->BoundingBox(tesseract::RIL_SYMBOL, &left, &top, &right, &bottom));
TBOX ocr_box(ICOORD(left, height - bottom), ICOORD(right, height - top));
int line_number;
TBOX truth_box;
STRING box_text;
EXPECT_TRUE(ReadNextBox(0, &line_number, box_file, &box_text,
&truth_box));
EXPECT_TRUE(
ReadNextBox(0, &line_number, box_file, &box_text, &truth_box));
// Testing for major overlap is a bit weak, but if they all
// major overlap successfully, then it has to be fairly close.
EXPECT_TRUE(ocr_box.major_overlap(truth_box));
// Also check that the symbol text matches the box text.
char* symbol_text = it->GetUTF8Text(tesseract::RIL_SYMBOL);
EXPECT_STREQ(box_text.string(), symbol_text);
delete [] symbol_text;
delete[] symbol_text;
} while (it->Next(tesseract::RIL_SYMBOL));
delete it;
}
Expand All @@ -107,14 +100,14 @@ class ApplyBoxTest : public testing::Test {

// Tests character-level applyboxes on normal Times New Roman.
TEST_F(ApplyBoxTest, TimesCharLevel) {
VerifyBoxesAndText("trainingtimes.tif", kTruthTextWords,
"trainingtimes.box", false);
VerifyBoxesAndText("trainingtimes.tif", kTruthTextWords, "trainingtimes.box",
false);
}

// Tests character-level applyboxes on italic Times New Roman.
TEST_F(ApplyBoxTest, ItalicCharLevel) {
VerifyBoxesAndText("trainingital.tif", kTruthTextWords,
"trainingital.box", false);
VerifyBoxesAndText("trainingital.tif", kTruthTextWords, "trainingital.box",
false);
}

// Tests line-level applyboxes on normal Times New Roman.
Expand All @@ -125,8 +118,8 @@ TEST_F(ApplyBoxTest, TimesLineLevel) {

// Tests line-level applyboxes on italic Times New Roman.
TEST_F(ApplyBoxTest, ItalLineLevel) {
VerifyBoxesAndText("trainingitalline.tif", kTruthTextLine,
"trainingital.box", true);
VerifyBoxesAndText("trainingitalline.tif", kTruthTextLine, "trainingital.box",
true);
}

} // namespace

0 comments on commit 9e66fb9

Please sign in to comment.