Supports truncated test data in zippy benchmark.

R=sesse


git-svn-id: https://snappy.googlecode.com/svn/trunk@74 03e5f5b5-db94-4691-08a0-1a8bf15f6143
diff --git a/snappy-test.cc b/snappy-test.cc
index 3f4003d..4619410 100644
--- a/snappy-test.cc
+++ b/snappy-test.cc
@@ -42,21 +42,25 @@
 
 namespace snappy {
 
-string ReadTestDataFile(const string& base) {
+string ReadTestDataFile(const string& base, size_t size_limit) {
   string contents;
   const char* srcdir = getenv("srcdir");  // This is set by Automake.
+  string prefix;
   if (srcdir) {
-    file::ReadFileToString(string(srcdir) + "/testdata/" + base,
-                           &contents,
-                           file::Defaults()).CheckSuccess();
-  } else {
-    file::ReadFileToString("testdata/" + base,
-                           &contents,
-                           file::Defaults()).CheckSuccess();
+    prefix = string(srcdir) + "/";
+  }
+  file::GetContents(prefix + "testdata/" + base, &contents, file::Defaults()
+      ).CheckSuccess();
+  if (size_limit > 0) {
+    contents = contents.substr(0, size_limit);
   }
   return contents;
 }
 
+string ReadTestDataFile(const string& base) {
+  return ReadTestDataFile(base, 0);
+}
+
 string StringPrintf(const char* format, ...) {
   char buf[4096];
   va_list ap;
diff --git a/snappy-test.h b/snappy-test.h
index bec9020..f7ba79e 100644
--- a/snappy-test.h
+++ b/snappy-test.h
@@ -139,10 +139,10 @@
     void CheckSuccess() { }
   };
 
-  DummyStatus ReadFileToString(const char* filename, string* data, int unused) {
-    FILE* fp = fopen(filename, "rb");
+  DummyStatus GetContents(const string& filename, string* data, int unused) {
+    FILE* fp = fopen(filename.c_str(), "rb");
     if (fp == NULL) {
-      perror(filename);
+      perror(filename.c_str());
       exit(1);
     }
 
@@ -160,15 +160,9 @@
     fclose(fp);
   }
 
-  DummyStatus ReadFileToString(const string& filename,
-                               string* data,
-                               int unused) {
-    ReadFileToString(filename.c_str(), data, unused);
-  }
-
-  DummyStatus WriteStringToFile(const string& str,
-                                const string& filename,
-                                int unused) {
+  DummyStatus SetContents(const string& filename,
+                          const string& str,
+                          int unused) {
     FILE* fp = fopen(filename.c_str(), "wb");
     if (fp == NULL) {
       perror(filename.c_str());
@@ -203,6 +197,8 @@
 void Test_Snappy_FindMatchLength();
 void Test_Snappy_FindMatchLengthRandom();
 
+string ReadTestDataFile(const string& base, size_t size_limit);
+
 string ReadTestDataFile(const string& base);
 
 // A sprintf() variant that returns a std::string.
diff --git a/snappy_unittest.cc b/snappy_unittest.cc
index f345dc3..59c108f 100644
--- a/snappy_unittest.cc
+++ b/snappy_unittest.cc
@@ -611,7 +611,8 @@
 
   // try reading stuff in from a bad file.
   for (int i = 1; i <= 3; ++i) {
-    string data = ReadTestDataFile(StringPrintf("baddata%d.snappy", i).c_str());
+    string data = ReadTestDataFile(StringPrintf("baddata%d.snappy", i).c_str(),
+                                   0);
     string uncmp;
     // check that we don't return a crazy length
     size_t ulen;
@@ -768,7 +769,8 @@
 
   string uncompressed;
   CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
-  CHECK(snappy::Uncompress(compressed.data(), compressed.size(), &uncompressed));
+  CHECK(snappy::Uncompress(compressed.data(), compressed.size(),
+                           &uncompressed));
   CHECK_EQ(uncompressed, src);
 }
 
@@ -971,19 +973,18 @@
 
 static void CompressFile(const char* fname) {
   string fullinput;
-  file::ReadFileToString(fname, &fullinput, file::Defaults()).CheckSuccess();
+  file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess();
 
   string compressed;
   Compress(fullinput.data(), fullinput.size(), SNAPPY, &compressed, false);
 
-  file::WriteStringToFile(
-      string(fname).append(".comp").c_str(), compressed,
-      file::Defaults()).CheckSuccess();
+  file::SetContents(string(fname).append(".comp"), compressed, file::Defaults())
+      .CheckSuccess();
 }
 
 static void UncompressFile(const char* fname) {
   string fullinput;
-  file::ReadFileToString(fname, &fullinput, file::Defaults()).CheckSuccess();
+  file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess();
 
   size_t uncompLength;
   CHECK(CheckUncompressedLength(fullinput, &uncompLength));
@@ -992,14 +993,13 @@
   uncompressed.resize(uncompLength);
   CHECK(snappy::Uncompress(fullinput.data(), fullinput.size(), &uncompressed));
 
-  file::WriteStringToFile(
-      string(fname).append(".uncomp").c_str(), uncompressed,
-      file::Defaults()).CheckSuccess();
+  file::SetContents(string(fname).append(".uncomp"), uncompressed,
+                    file::Defaults()).CheckSuccess();
 }
 
 static void MeasureFile(const char* fname) {
   string fullinput;
-  file::ReadFileToString(fname, &fullinput, file::Defaults()).CheckSuccess();
+  file::GetContents(fname, &fullinput, file::Defaults()).CheckSuccess();
   printf("%-40s :\n", fname);
 
   int start_len = (FLAGS_start_len < 0) ? fullinput.size() : FLAGS_start_len;
@@ -1032,25 +1032,29 @@
 static struct {
   const char* label;
   const char* filename;
+  size_t size_limit;
 } files[] = {
-  { "html", "html" },
-  { "urls", "urls.10K" },
-  { "jpg", "house.jpg" },
-  { "pdf", "mapreduce-osdi-1.pdf" },
-  { "html4", "html_x_4" },
-  { "cp", "cp.html" },
-  { "c", "fields.c" },
-  { "lsp", "grammar.lsp" },
-  { "xls", "kennedy.xls" },
-  { "txt1", "alice29.txt" },
-  { "txt2", "asyoulik.txt" },
-  { "txt3", "lcet10.txt" },
-  { "txt4", "plrabn12.txt" },
-  { "bin", "ptt5" },
-  { "sum", "sum" },
-  { "man", "xargs.1" },
-  { "pb", "geo.protodata" },
-  { "gaviota", "kppkn.gtb" },
+  { "html", "html", 0 },
+  { "urls", "urls.10K", 0 },
+  { "jpg", "house.jpg", 0 },
+  { "jpg_200", "house.jpg", 200 },
+  { "pdf", "mapreduce-osdi-1.pdf", 0 },
+  { "html4", "html_x_4", 0 },
+  { "cp", "cp.html", 0 },
+  { "c", "fields.c", 0 },
+  { "lsp", "grammar.lsp", 0 },
+  { "xls", "kennedy.xls", 0 },
+  { "xls_200", "kennedy.xls", 200 },
+  { "txt1", "alice29.txt", 0 },
+  { "txt2", "asyoulik.txt", 0 },
+  { "txt3", "lcet10.txt", 0 },
+  { "txt4", "plrabn12.txt", 0 },
+  { "bin", "ptt5", 0 },
+  { "bin_200", "ptt5", 200 },
+  { "sum", "sum", 0 },
+  { "man", "xargs.1", 0 },
+  { "pb", "geo.protodata", 0 },
+  { "gaviota", "kppkn.gtb", 0 },
 };
 
 static void BM_UFlat(int iters, int arg) {
@@ -1059,7 +1063,8 @@
   // Pick file to process based on "arg"
   CHECK_GE(arg, 0);
   CHECK_LT(arg, ARRAYSIZE(files));
-  string contents = ReadTestDataFile(files[arg].filename);
+  string contents = ReadTestDataFile(files[arg].filename,
+                                     files[arg].size_limit);
 
   string zcontents;
   snappy::Compress(contents.data(), contents.size(), &zcontents);
@@ -1076,7 +1081,7 @@
 
   delete[] dst;
 }
-BENCHMARK(BM_UFlat)->DenseRange(0, 17);
+BENCHMARK(BM_UFlat)->DenseRange(0, ARRAYSIZE(files) - 1);
 
 static void BM_UValidate(int iters, int arg) {
   StopBenchmarkTiming();
@@ -1084,7 +1089,8 @@
   // Pick file to process based on "arg"
   CHECK_GE(arg, 0);
   CHECK_LT(arg, ARRAYSIZE(files));
-  string contents = ReadTestDataFile(files[arg].filename);
+  string contents = ReadTestDataFile(files[arg].filename,
+                                     files[arg].size_limit);
 
   string zcontents;
   snappy::Compress(contents.data(), contents.size(), &zcontents);
@@ -1107,7 +1113,8 @@
   // Pick file to process based on "arg"
   CHECK_GE(arg, 0);
   CHECK_LT(arg, ARRAYSIZE(files));
-  string contents = ReadTestDataFile(files[arg].filename);
+  string contents = ReadTestDataFile(files[arg].filename,
+                                     files[arg].size_limit);
 
   char* dst = new char[snappy::MaxCompressedLength(contents.size())];
 
@@ -1128,7 +1135,7 @@
                           files[arg].label, contents.size(), zsize);
   delete[] dst;
 }
-BENCHMARK(BM_ZFlat)->DenseRange(0, 17);
+BENCHMARK(BM_ZFlat)->DenseRange(0, ARRAYSIZE(files) - 1);
 
 
 }  // namespace snappy