implement TextLoader tests, add TestSupport module
This commit is contained in:
parent
181d3ad64a
commit
3be19dbf81
@ -33,10 +33,11 @@ public:
|
||||
auto begin() { return m_lines->begin(); }
|
||||
auto end() { return m_lines->end(); }
|
||||
|
||||
protected:
|
||||
typedef std::pair<const uint8_t *, uint32_t> LineIndexPair;
|
||||
typedef std::list<LineIndexPair> LineIndexPairList;
|
||||
typedef std::shared_ptr<LineIndexPairList> LineIndexPairListRef;
|
||||
|
||||
protected:
|
||||
int m_line_endings;
|
||||
LineIndexPairListRef m_lines;
|
||||
};
|
||||
|
1
test/files/line_endings/cr_format.txt
Normal file
1
test/files/line_endings/cr_format.txt
Normal file
@ -0,0 +1 @@
|
||||
Hello.
This file is in CR line ending format.
|
2
test/files/line_endings/crlf_format.txt
Normal file
2
test/files/line_endings/crlf_format.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Hello.
|
||||
This file is in CRLF line ending format.
|
@ -1,2 +0,0 @@
|
||||
Hello.
|
||||
This file is in DOS line ending format.
|
2
test/files/line_endings/lf_format.txt
Normal file
2
test/files/line_endings/lf_format.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Hello.
|
||||
This file is in LF line ending format.
|
@ -1 +0,0 @@
|
||||
Hello.
This file is in MAC line ending format.
|
@ -1,2 +0,0 @@
|
||||
Hello.
|
||||
This file is in UNIX line ending format.
|
24
test/src/TestSupport.cc
Normal file
24
test/src/TestSupport.cc
Normal file
@ -0,0 +1,24 @@
|
||||
#include "TestSupport.h"
|
||||
#include "File.h"
|
||||
#include <stdio.h>
|
||||
|
||||
std::shared_ptr<std::vector<uint8_t>> TestSupport::read_file(const char * filename)
|
||||
{
|
||||
File file;
|
||||
if (!file.open(filename))
|
||||
{
|
||||
fprintf(stderr, "Could not open %s\n", filename);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t size = file.get_size();
|
||||
auto buffer = std::make_shared<std::vector<uint8_t>>(size, 0u);
|
||||
|
||||
if (!file.read(&(*buffer)[0], size))
|
||||
{
|
||||
fprintf(stderr, "Failed to read file %s\n", filename);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
14
test/src/TestSupport.h
Normal file
14
test/src/TestSupport.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef TESTSUPPORT_H
|
||||
#define TESTSUPPORT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
class TestSupport
|
||||
{
|
||||
public:
|
||||
static std::shared_ptr<std::vector<uint8_t>> read_file(const char * filename);
|
||||
};
|
||||
|
||||
#endif
|
@ -1,53 +0,0 @@
|
||||
#if 0
|
||||
#include "gtest/gtest.h"
|
||||
#include "FileLoader.h"
|
||||
|
||||
TEST(FileLoaderTest, num_lines_defaults_to_0)
|
||||
{
|
||||
FileLoader fr;
|
||||
EXPECT_EQ(0u, fr.num_lines());
|
||||
}
|
||||
|
||||
TEST(FileLoaderTest, load_returns_false_for_nonexistent_file)
|
||||
{
|
||||
FileLoader fr;
|
||||
EXPECT_FALSE(fr.load("this/file/does/not/exist.txt"));
|
||||
}
|
||||
|
||||
TEST(FileLoaderTest, loading_empty_file)
|
||||
{
|
||||
FileLoader fr;
|
||||
EXPECT_TRUE(fr.load("test/files/empty.txt"));
|
||||
EXPECT_EQ(0u, fr.num_lines());
|
||||
}
|
||||
|
||||
TEST(FileLoaderTest, reads_lf_format_file)
|
||||
{
|
||||
FileLoader fr;
|
||||
EXPECT_TRUE(fr.load("test/files/line_endings/unix_format.txt"));
|
||||
EXPECT_EQ(FileLoader::LINE_ENDING_LF, fr.get_line_endings());
|
||||
EXPECT_EQ(2u, fr.num_lines());
|
||||
// EXPECT_EQ("Hello.", fr.get_line(0)->to_s());
|
||||
// EXPECT_EQ("This file is in UNIX line ending format.", fr.get_line(1)->to_s());
|
||||
}
|
||||
|
||||
TEST(FileLoaderTest, reads_cr_format_file)
|
||||
{
|
||||
FileLoader fr;
|
||||
EXPECT_TRUE(fr.load("test/files/line_endings/mac_format.txt"));
|
||||
EXPECT_EQ(FileLoader::LINE_ENDING_CR, fr.get_line_endings());
|
||||
EXPECT_EQ(2u, fr.num_lines());
|
||||
// EXPECT_EQ("Hello.", fr.get_line(0)->to_s());
|
||||
// EXPECT_EQ("This file is in MAC line ending format.", fr.get_line(1)->to_s());
|
||||
}
|
||||
|
||||
TEST(FileLoaderTest, reads_crlf_format_file)
|
||||
{
|
||||
FileLoader fr;
|
||||
EXPECT_TRUE(fr.load("test/files/line_endings/dos_format.txt"));
|
||||
EXPECT_EQ(FileLoader::LINE_ENDING_CRLF, fr.get_line_endings());
|
||||
EXPECT_EQ(2u, fr.num_lines());
|
||||
// EXPECT_EQ("Hello.", fr.get_line(0)->to_s());
|
||||
// EXPECT_EQ("This file is in DOS line ending format.", fr.get_line(1)->to_s());
|
||||
}
|
||||
#endif
|
65
test/src/test_TextLoader.cc
Normal file
65
test/src/test_TextLoader.cc
Normal file
@ -0,0 +1,65 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "TextLoader.h"
|
||||
#include "TestSupport.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
static string line_to_string(const TextLoader::LineIndexPairList::iterator & it)
|
||||
{
|
||||
return string((char *)it->first, it->second);
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, num_lines_defaults_to_0)
|
||||
{
|
||||
TextLoader tl;
|
||||
EXPECT_EQ(0u, tl.num_lines());
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, loading_empty_file)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/empty.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
EXPECT_EQ(0u, tl.num_lines());
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, detects_lf_line_endings)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/line_endings/lf_format.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
EXPECT_EQ(TextLoader::LINE_ENDING_LF, tl.get_line_endings());
|
||||
EXPECT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Hello.", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("This file is in LF line ending format.", line_to_string(it));
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, detects_cr_line_endings)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/line_endings/cr_format.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
EXPECT_EQ(TextLoader::LINE_ENDING_CR, tl.get_line_endings());
|
||||
EXPECT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Hello.", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("This file is in CR line ending format.", line_to_string(it));
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, detects_crlf_line_endings)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/line_endings/crlf_format.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
EXPECT_EQ(TextLoader::LINE_ENDING_CRLF, tl.get_line_endings());
|
||||
EXPECT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Hello.", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("This file is in CRLF line ending format.", line_to_string(it));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user