15 #include "absl/strings/str_cat.h"
16 #include "absl/strings/str_format.h"
30 class ImagedataTest :
public ::testing::Test {
33 std::locale::global(std::locale(
""));
39 std::string MakeFakeDoc(
int num_pages,
unsigned doc_id,
40 std::vector<std::string>* page_texts) {
42 const int kImageSize = 1048576;
45 std::vector<char> fake_image(kImageSize, 0);
47 for (
int p = 0; p < num_pages; ++p) {
49 page_texts->push_back(
50 absl::StrFormat(
"Page %d of %d in doc %u", p, num_pages, doc_id));
53 ImageData::Build(
"noname", p,
"eng", fake_image.data(),
54 fake_image.size(), (*page_texts)[p].c_str(),
nullptr);
55 EXPECT_EQ(kImageSize, imagedata->
MemoryUsed());
56 write_doc.AddPageToDocument(imagedata);
61 EXPECT_TRUE(write_doc.SaveDocument(filename.c_str(),
nullptr));
66 TEST_F(ImagedataTest, CachesProperly) {
70 const int kNumPages = 12;
72 const int kMemoryAllowances[] = {2000000, 4000000, 1000000, 100000000, 0};
74 const int kPageReadOrder[] = {0, 1, 2, 3, 8, 4, 5, 6, 7, 11, 10, 9, -1};
76 std::vector<std::string> page_texts;
77 std::string filename = MakeFakeDoc(kNumPages, 0, &page_texts);
80 for (
int m = 0; kMemoryAllowances[m] > 0; ++m) {
83 read_doc.LoadDocument(filename.c_str(), 0, kMemoryAllowances[m],
nullptr));
84 LOG(
ERROR) <<
"Allowance = " << kMemoryAllowances[m];
86 for (
int p = 0; kPageReadOrder[p] >= 0; ++p) {
87 int page = kPageReadOrder[p];
88 const ImageData* imagedata = read_doc.GetPage(page);
89 EXPECT_NE(
nullptr, imagedata);
92 EXPECT_STREQ(page_texts[page].c_str(),
98 TEST_F(ImagedataTest, CachesMultiDocs) {
102 const std::vector<int> kNumPages = {6, 5, 7};
103 std::vector<std::vector<std::string>> page_texts;
105 for (
size_t d = 0; d < kNumPages.size(); ++d) {
106 page_texts.emplace_back(std::vector<std::string>());
107 std::string filename = MakeFakeDoc(kNumPages[d], d, &page_texts.back());
116 for (
int p = 0; p <= 21; ++p) {
118 const ImageData* robin_data = robin_cache.GetPageBySerial(p);
119 const ImageData* serial_data = serial_cache.GetPageBySerial(p);
120 CHECK(robin_data !=
nullptr);
121 CHECK(serial_data !=
nullptr);
122 int robin_doc = p % kNumPages.size();
123 int robin_page = p / kNumPages.size() % kNumPages[robin_doc];
125 EXPECT_STREQ(page_texts[robin_doc][robin_page].c_str(),
127 int serial_doc = p / kNumPages[0] % kNumPages.size();
128 int serial_page = p % kNumPages[0] % kNumPages[serial_doc];
129 EXPECT_STREQ(page_texts[serial_doc][serial_page].c_str(),