0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TestData.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #ifndef HYPERTABLE_TESTDATA_H
23 #define HYPERTABLE_TESTDATA_H
24 
25 #include <boost/shared_array.hpp>
26 
27 #include "Common/FileUtils.h"
28 #include "Common/Logger.h"
29 #include "Common/TestHarness.h"
30 
31 namespace Hypertable {
32 
33  typedef boost::shared_array<const char> CharPtr;
34 
35  class TestData {
36  public:
37  bool load(const std::string &datadir) {
38  struct stat statbuf;
39  char *contentdata, *worddata, *urldata;
40  char *base, *ptr, *last, *str;
41  off_t len;
42  std::string shakespearefile = datadir + "/shakespeare.txt";
43  std::string shakespearegz = datadir + "/shakespeare.txt.gz";
44  std::string wordsfile = datadir + "/words";
45  std::string wordsgz = datadir + "/words.gz";
46  std::string urlsfile = datadir + "/urls.txt";
47  std::string urlsgz = datadir + "/urls.txt.gz";
48  std::string syscmd;
49 
53  if (stat(shakespearefile.c_str(), &statbuf) != 0) {
54  if (stat(shakespearegz.c_str(), &statbuf) != 0) {
55  HT_ERRORF("Unable to stat file 'shakespeare.txt.gz' : %s",
56  strerror(errno));
57  return false;
58  }
59  syscmd = "zcat " + shakespearegz + " > " + shakespearefile;
60  if (system(syscmd.c_str())) {
61  HT_ERRORF("Unable to decompress file '%s'", shakespearegz.c_str());
62  return false;
63  }
64  if (stat(shakespearefile.c_str(), &statbuf) != 0) {
65  HT_ERRORF("Unable to stat file '%s' : %s", shakespearefile.c_str(),
66  strerror(errno));
67  return false;
68  }
69  }
70  if ((contentdata = FileUtils::file_to_buffer(shakespearefile.c_str(),
71  &len)) == 0)
72  return false;
73  base = contentdata;
74  while ((ptr = strstr(base, "\n\n")) != 0) {
75  *ptr++ = 0;
76  str = new char [strlen(base) + 1];
77  strcpy(str, base);
78  content.push_back(CharPtr(str));
79  base = ptr + 2;
80  }
81 
82  delete [] contentdata;
83 
87  if (stat(wordsfile.c_str(), &statbuf) != 0) {
88  if (stat(wordsgz.c_str(), &statbuf) != 0) {
89  HT_ERRORF("Unable to stat file '%s' : %s", wordsgz.c_str(),
90  strerror(errno));
91  return false;
92  }
93  syscmd = "zcat " + wordsgz + " > " + wordsfile;
94  if (system(syscmd.c_str())) {
95  HT_ERRORF("Unable to decompress file '%s'", wordsgz.c_str());
96  return false;
97  }
98  if (stat(wordsfile.c_str(), &statbuf) != 0) {
99  HT_ERRORF("Unable to stat file '%s' : %s", wordsfile.c_str(),
100  strerror(errno));
101  return false;
102  }
103  }
104  if ((worddata = FileUtils::file_to_buffer(wordsfile.c_str(), &len)) == 0)
105  return false;
106  base = strtok_r(worddata, "\n\r", &last);
107  while (base) {
108  str = new char [strlen(base) + 1];
109  strcpy(str, base);
110  words.push_back(CharPtr(str));
111  base = strtok_r(0, "\n\r", &last);
112  }
113  delete [] worddata;
114 
115 
119  if (stat(urlsfile.c_str(), &statbuf) != 0) {
120  if (stat(urlsgz.c_str(), &statbuf) != 0) {
121  HT_ERRORF("Unable to stat file 'urls.txt.gz' : %s", strerror(errno));
122  return false;
123  }
124  syscmd = "zcat " + urlsgz + " > " + urlsfile;
125  if (system(syscmd.c_str())) {
126  HT_ERRORF("Unable to decompress file '%s'", urlsgz.c_str());
127  return false;
128  }
129  if (stat(urlsfile.c_str(), &statbuf) != 0) {
130  HT_ERRORF("Unable to stat file '%s' : %s", urlsfile.c_str(),
131  strerror(errno));
132  return false;
133  }
134  }
135  if ((urldata = FileUtils::file_to_buffer(urlsfile.c_str(), &len)) == 0)
136  return false;
137  base = strtok_r(urldata, "\n\r", &last);
138  while (base) {
139  str = new char [strlen(base) + 1];
140  strcpy(str, base);
141  urls.push_back(CharPtr(str));
142  base = strtok_r(0, "\n\r", &last);
143  }
144 
145  return true;
146  }
147 
148  std::vector<CharPtr> content;
149  std::vector<CharPtr> words;
150  std::vector<CharPtr> urls;
151  };
152 
153 } // namespace Hypertable
154 
155 #endif // HYPERTABLE_TESTDATA_H
static char * file_to_buffer(const String &fname, off_t *lenp)
Reads a full file into a new buffer; the buffer is allocated with operator new[], and the caller has ...
Definition: FileUtils.cc:282
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
File system utility functions.
boost::shared_array< const char > CharPtr
Definition: TestData.h:33
Logging routines and macros.
A simple testing framework with some helpers for dealing with golden files (comparing files...
Hypertable definitions
std::vector< CharPtr > urls
Definition: TestData.h:150
std::vector< CharPtr > words
Definition: TestData.h:149
#define HT_ERRORF(msg,...)
Definition: Logger.h:300
std::vector< CharPtr > content
Definition: TestData.h:148
bool load(const std::string &datadir)
Definition: TestData.h:37