Commit 3a34fbdf authored by Paul Asmuth's avatar Paul Asmuth
Browse files

more tests, exception fix

parent 7e9150f2
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -34,6 +34,17 @@ TEST_CASE(CSVFileTest, TestInvalidFileName, [] () {
  });
});

TEST_CASE(CSVFileTest, TestReadHeaders, [] () {
  auto csv_file = CSVFile::openFile("test/fixtures/gbp_per_country_simple.csv");
  EXPECT(csv_file.get() != nullptr);
  std::vector<std::string> headers;
  csv_file->readNextRow(&headers);
  EXPECT(headers.size() == 2);
  EXPECT(headers[0] == "country");
  EXPECT(headers[1] == "gbp");
});


/*
int main() {
  try {
+15 −2
Original line number Diff line number Diff line
@@ -12,7 +12,11 @@
namespace fnordmetric {
namespace csv_backend {

std::unique_ptr<CSVFile> CSVFile::openFile(const std::string& file_path) {
std::unique_ptr<CSVFile> CSVFile::openFile(
    const std::string& file_path,
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
    char quote_char /* = '"' */) {
  auto fp = file_path.c_str();
  int fd = open(fp, O_RDONLY);

@@ -28,9 +32,18 @@ std::unique_ptr<CSVFile> CSVFile::openFile(const std::string& file_path) {
  return std::unique_ptr<CSVFile>(csv_file);
}

CSVFile::CSVFile(int fd) : fd_(fd) {
CSVFile::CSVFile(
    int fd,
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
    char quote_char /* = '"' */) :
    fd_(fd) {
  assert(fd > 0);
}

void CSVFile::readNextRow(std::vector<std::string>* target) {

}

}
}
+15 −2
Original line number Diff line number Diff line
@@ -28,7 +28,11 @@ public:
   *
   * @param file_path the path to the csv file
   */
  static std::unique_ptr<CSVFile> openFile(const std::string& file_path);
  static std::unique_ptr<CSVFile> openFile(
      const std::string& file_path,
      char column_seperator = ',',
      char row_seperator = '\n',
      char quote_char = '"');

  CSVFile(const CSVFile& other) = delete;
  CSVFile& operator=(const CSVFile& other) = delete;
@@ -39,7 +43,16 @@ public:
   *
   * @param fd a valid an opened fd, transfers ownership and closes on destruct
   */
  explicit CSVFile(int fd);
  explicit CSVFile(
      int fd,
      char column_seperator = ',',
      char row_seperator = '\n',
      char quote_char = '"');

  /**
   * Read the next row from the csv file
   */
  void readNextRow(std::vector<std::string>* target);

protected:
  int fd_;
+2 −2
Original line number Diff line number Diff line
@@ -37,10 +37,10 @@ RuntimeException::RuntimeException(
    pos = 0;
  }

  if (errno > 0) {
  if (posix_errno > 0) {
    snprintf(message_ + pos, sizeof(message_) - pos, ": ");
    pos += 2;
    strerror_r(errno, message_ + pos, sizeof(message_) - pos);
    strerror_r(posix_errno, message_ + pos, sizeof(message_) - pos);
  }
}

+3 −3
Original line number Diff line number Diff line
USA,1,,United States," 16,800,000 ",,,,,
USA,1,,United States," 16,800,000 ",,,,,
CHN,2,,China," 9,240,270 ",,,,,
JPN,3,,Japan," 4,901,530 ",,,,,
DEU,4,,Germany," 3,634,823 ",,,,,
@@ -92,7 +92,7 @@ YEM,91,,"Yemen, Rep."," 35,955 ",,,,,
JOR,92,,Jordan," 33,678 ",,,,,
TZA,93,,Tanzania," 33,225 ",c,,,,
BHR,94,,Bahrain," 32,788 ",,,,,
CIV,95,,Cte d'Ivoire," 30,905 ",,,,,
CIV,95,,Côte d'Ivoire," 30,905 ",,,,,
ZAR,96,,"Congo, Dem. Rep."," 30,629 ",,,,,
BOL,97,,Bolivia," 30,601 ",,,,,
PRY,98,,Paraguay," 29,949 ",,,,,
@@ -184,7 +184,7 @@ COM,183,,Comoros, 657 ,,,,,
DMA,184,,Dominica, 505 ,,,,,
TON,185,,Tonga, 466 ,,,,,
FSM,186,,"Micronesia, Fed. Sts.", 335 ,,,,,
STP,187,,So Tom and Principe, 311 ,,,,,
STP,187,,São Tomé and Principe, 311 ,,,,,
PLW,188,,Palau, 247 ,,,,,
MHL,189,,Marshall Islands, 175 ,,,,,
KIR,190,,Kiribati, 169 ,,,,,
Loading