Skip to content

Commit

Permalink
unittest: Add imagedata_test
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 12, 2018
1 parent 30081c5 commit 53f0e76
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
14 changes: 14 additions & 0 deletions unittest/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ libabseil_la_SOURCES += ../abseil/absl/base/internal/throw_delegate.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/unscaledcycleclock.cc
libabseil_la_SOURCES += ../abseil/absl/numeric/int128.cc
libabseil_la_SOURCES += ../abseil/absl/strings/ascii.cc
libabseil_la_SOURCES += ../abseil/absl/strings/charconv.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/charconv_bigint.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/charconv_parse.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/memutil.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/str_format/arg.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/str_format/bind.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/str_format/extension.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/str_format/float_conversion.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/str_format/output.cc
libabseil_la_SOURCES += ../abseil/absl/strings/internal/str_format/parser.cc
libabseil_la_SOURCES += ../abseil/absl/strings/numbers.cc
libabseil_la_SOURCES += ../abseil/absl/strings/str_cat.cc
libabseil_la_SOURCES += ../abseil/absl/strings/str_split.cc
libabseil_la_SOURCES += ../abseil/absl/strings/string_view.cc
Expand Down Expand Up @@ -87,6 +97,7 @@ check_PROGRAMS = \
denorm_test \
fileio_test \
heap_test \
imagedata_test \
indexmapbidi_test \
intfeaturemap_test \
intsimdmatrix_test \
Expand Down Expand Up @@ -148,6 +159,9 @@ fileio_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)
heap_test_SOURCES = heap_test.cc
heap_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

imagedata_test_SOURCES = imagedata_test.cc
imagedata_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)

indexmapbidi_test_SOURCES = indexmapbidi_test.cc
indexmapbidi_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

Expand Down
40 changes: 29 additions & 11 deletions unittest/imagedata_test.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#include "tesseract/ccstruct/imagedata.h"
// (C) Copyright 2017, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string>
#include <vector>

#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"

#include "imagedata.h"
#include "include_gunit.h"
#include "log.h"

using tesseract::DocumentCache;
using tesseract::DocumentData;
using tesseract::ImageData;
Expand All @@ -15,8 +32,8 @@ class ImagedataTest : public ::testing::Test {
ImagedataTest() {}

// Creates a fake DocumentData, writes it to a file, and returns the filename.
string MakeFakeDoc(int num_pages, int doc_id,
std::vector<string>* page_texts) {
std::string MakeFakeDoc(int num_pages, int doc_id,
std::vector<std::string>* page_texts) {
// The size of the fake images that we will use.
const int kImageSize = 1048576;
// Not using a real image here - just an array of zeros! We are just testing
Expand All @@ -26,7 +43,7 @@ class ImagedataTest : public ::testing::Test {
for (int p = 0; p < num_pages; ++p) {
// Make some fake text that is different for each page and save it.
page_texts->push_back(
StringPrintf("Page %d of %d in doc %d", p, num_pages, doc_id));
absl::StrFormat("Page %d of %d in doc %d", p, num_pages, doc_id));
// Make an imagedata and put it in the document.
ImageData* imagedata =
ImageData::Build("noname", p, "eng", fake_image.data(),
Expand All @@ -35,7 +52,7 @@ class ImagedataTest : public ::testing::Test {
write_doc.AddPageToDocument(imagedata);
}
// Write it to a file.
string filename = file::JoinPath(
std::string filename = file::JoinPath(
FLAGS_test_tmpdir, absl::StrCat("documentdata", doc_id, ".lstmf"));
EXPECT_TRUE(write_doc.SaveDocument(filename.c_str(), nullptr));
return filename;
Expand All @@ -52,8 +69,8 @@ TEST_F(ImagedataTest, CachesProperly) {
// Order in which to read the pages, with some sequential and some seeks.
const int kPageReadOrder[] = {0, 1, 2, 3, 8, 4, 5, 6, 7, 11, 10, 9, -1};

std::vector<string> page_texts;
string filename = MakeFakeDoc(kNumPages, 0, &page_texts);
std::vector<std::string> page_texts;
std::string filename = MakeFakeDoc(kNumPages, 0, &page_texts);
// Now try getting it back with different memory allowances and check that
// the pages can still be read.
for (int m = 0; kMemoryAllowances[m] > 0; ++m) {
Expand All @@ -65,7 +82,8 @@ TEST_F(ImagedataTest, CachesProperly) {
for (int p = 0; kPageReadOrder[p] >= 0; ++p) {
int page = kPageReadOrder[p];
const ImageData* imagedata = read_doc.GetPage(page);
EXPECT_NE(reinterpret_cast<const ImageData*>(nullptr), imagedata);
EXPECT_NE(nullptr, imagedata);
//EXPECT_NE(reinterpret_cast<ImageData*>(nullptr), imagedata);
// Check that this is the right page.
EXPECT_STREQ(page_texts[page].c_str(),
imagedata->transcription().string());
Expand All @@ -78,11 +96,11 @@ TEST_F(ImagedataTest, CachesMultiDocs) {
// and the two caching strategies read images in the right order.
// Number of pages in each document.
const std::vector<int> kNumPages = {6, 5, 7};
std::vector<std::vector<string>> page_texts;
std::vector<std::vector<std::string>> page_texts;
GenericVector<STRING> filenames;
for (int d = 0; d < kNumPages.size(); ++d) {
page_texts.emplace_back(std::vector<string>());
string filename = MakeFakeDoc(kNumPages[d], d, &page_texts.back());
page_texts.emplace_back(std::vector<std::string>());
std::string filename = MakeFakeDoc(kNumPages[d], d, &page_texts.back());
filenames.push_back(STRING(filename.c_str()));
}
// Now try getting them back with different cache strategies and check that
Expand Down

0 comments on commit 53f0e76

Please sign in to comment.