Skip to content

Commit

Permalink
Re-enable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoturri1966 committed Aug 2, 2023
1 parent a4758d1 commit f449b47
Show file tree
Hide file tree
Showing 5 changed files with 23,143 additions and 12,413 deletions.
53 changes: 49 additions & 4 deletions src/convert/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ using namespace core;
using namespace base;
namespace convert {

static std::string_view trim(const std::string_view& str)
{
if (str.empty())
return std::string_view();
size_t start = 0;
while (start < str.size() - 1 && (str[start] == ' ' || str[start] == '\t')) { ++start; }
size_t end = str.size() - 1;
while (end > 0 && (str[end] == ' ' || str[end] == '\t')) { --end; }
if ((start == end && (str[end] == ' ' || str[end] == '\t')) || (start > end))
return std::string_view();
else
return std::string_view(&str[start], end - start + 1);
}

static std::string_view uncomment(const std::string_view& str)
{
return (!str.empty() && str[0] == ';') ? trim(str.substr(1)) : str;
}

BGCODE_CONVERT_EXPORT EResult from_ascii_to_binary(FILE& src_file, FILE& dst_file)
{
return EResult::Success;
Expand Down Expand Up @@ -110,7 +129,7 @@ BGCODE_CONVERT_EXPORT EResult from_binary_to_ascii(FILE& src_file, FILE& dst_fil
case EThumbnailFormat::JPG: { format = "thumbnail_JPG"; break; }
case EThumbnailFormat::QOI: { format = "thumbnail_QOI"; break; }
}
if (!write_line(";\n; " + format + " begin " + std::to_string(thumbnail_block.width) + "x" + std::to_string(thumbnail_block.height) +
if (!write_line("\n;\n; " + format + " begin " + std::to_string(thumbnail_block.width) + "x" + std::to_string(thumbnail_block.height) +
" " + std::to_string(encoded.length()) + "\n"))
return EResult::WriteError;
while (encoded.size() > max_row_length) {
Expand All @@ -122,7 +141,7 @@ BGCODE_CONVERT_EXPORT EResult from_binary_to_ascii(FILE& src_file, FILE& dst_fil
if (!write_line("; " + encoded + "\n"))
return EResult::WriteError;
}
if (!write_line("; " + format + " end\n;\n\n"))
if (!write_line("; " + format + " end\n;\n"))
return EResult::WriteError;

restore_position = ftell(&src_file);
Expand All @@ -135,6 +154,29 @@ BGCODE_CONVERT_EXPORT EResult from_binary_to_ascii(FILE& src_file, FILE& dst_fil
//
// convert gcode blocks
//
auto remove_empty_lines = [](const std::string& data) {
std::string ret;
auto begin_it = data.begin();
auto end_it = data.begin();
while (end_it != data.end()) {
while (end_it != data.end() && *end_it != '\n') {
++end_it;
}

const size_t pos = std::distance(data.begin(), begin_it);
const size_t line_length = std::distance(begin_it, end_it);
const std::string_view original_line(&data[pos], line_length);
const std::string_view reduced_line = uncomment(trim(original_line));
if (!reduced_line.empty())
ret += std::string(original_line) + "\n";
begin_it = ++end_it;
}

return ret;
};

if (!write_line("\n"))
return EResult::WriteError;
res = skip_block_content(src_file, file_header, block_header);
if (res != EResult::Success)
// propagate error
Expand All @@ -149,8 +191,11 @@ BGCODE_CONVERT_EXPORT EResult from_binary_to_ascii(FILE& src_file, FILE& dst_fil
if (res != EResult::Success)
// propagate error
return res;
if (!write_line(block.raw_data))
return EResult::WriteError;
const std::string out_str = remove_empty_lines(block.raw_data);
if (!out_str.empty()) {
if (!write_line(out_str))
return EResult::WriteError;
}
if (ftell(&src_file) == file_size)
break;
res = read_next_block_header(src_file, file_header, block_header, verify_checksum);
Expand Down
104 changes: 60 additions & 44 deletions tests/convert/convert_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,73 @@ class ScopedFile
FILE* m_file{ nullptr };
};

// Does not build on Linux
//TEST_CASE("Convert from binary to ascii", "[Convert]")
//{
// std::cout << "\nTEST: Convert from binary to ascii\n";
void binary_to_ascii(const std::string& src_filename, const std::string& dst_filename)
{
// Open source file
FILE* src_file;
errno_t err = fopen_s(&src_file, src_filename.c_str(), "rb");
REQUIRE(err == 0);
ScopedFile scoped_src_file(src_file);

// Open destination file
FILE* dst_file;
err = fopen_s(&dst_file, dst_filename.c_str(), "wb");
REQUIRE(err == 0);
ScopedFile scoped_dst_file(dst_file);

// Perform conversion
EResult res = from_binary_to_ascii(*src_file, *dst_file, true);
REQUIRE(res == EResult::Success);
}

// const std::string src_filename = std::string(TEST_DATA_DIR) + "/mini_cube_binary.gcode";
// const std::string dst_filename = std::string(TEST_DATA_DIR) + "/mini_cube_binary_converted.gcode";
// const std::string check_filename = std::string(TEST_DATA_DIR) + "/mini_cube_binary_ascii.gcode";
void compare_files(const std::string& filename1, const std::string& filename2)
{
// Open file 1
FILE* file1;
errno_t err = fopen_s(&file1, filename1.c_str(), "rb");
REQUIRE(err == 0);
ScopedFile scoped_file1(file1);

// // Open source file
// FILE* src_file;
// errno_t err = fopen_s(&src_file, src_filename.c_str(), "rb");
// REQUIRE(err == 0);
// ScopedFile scoped_src_file(src_file);
// Open file 2
FILE* file2;
err = fopen_s(&file2, filename2.c_str(), "rb");
REQUIRE(err == 0);
ScopedFile scoped_file2(file2);

// // Open destination file
// FILE* dst_file;
// err = fopen_s(&dst_file, dst_filename.c_str(), "w+b");
// REQUIRE(err == 0);
// ScopedFile scoped_dst_file(dst_file);
// Compare file sizes
fseek(file1, 0, SEEK_END);
const long file1_size = ftell(file1);
rewind(file1);
fseek(file2, 0, SEEK_END);
const long file2_size = ftell(file2);
rewind(file2);
REQUIRE(file1_size == file2_size);

// // Perform conversion
// EResult res = from_binary_to_ascii(*src_file, *dst_file, true);
// REQUIRE(res == EResult::Success);
// Compare file contents
static const size_t buf_size = 4096;
std::vector<uint8_t> buf1(buf_size);
std::vector<uint8_t> buf2(buf_size);
do {
const size_t r1 = fread(buf1.data(), 1, buf_size, file1);
const size_t r2 = fread(buf2.data(), 1, buf_size, file2);
REQUIRE(r1 == r2);
REQUIRE(buf1 == buf2);
} while (!feof(file1) || !feof(file2));
}

// // Open check file
// FILE* check_file;
// err = fopen_s(&check_file, check_filename.c_str(), "rb");
// REQUIRE(err == 0);
// ScopedFile scoped_check_file(check_file);
TEST_CASE("Convert from binary to ascii", "[Convert]")
{
std::cout << "\nTEST: Convert from binary to ascii\n";

// // Compare file sizes
// fseek(dst_file, 0, SEEK_END);
// const long dst_file_size = ftell(dst_file);
// rewind(dst_file);
// fseek(check_file, 0, SEEK_END);
// const long check_file_size = ftell(check_file);
// rewind(check_file);
// REQUIRE(dst_file_size == check_file_size);
const std::string src_filename = std::string(TEST_DATA_DIR) + "/mini_cube_binary.gcode";
const std::string dst_filename = std::string(TEST_DATA_DIR) + "/mini_cube_binary_converted.gcode";
const std::string check_filename = std::string(TEST_DATA_DIR) + "/mini_cube_binary_ascii.gcode";

// // Compare file contents
// static const size_t buf_size = 4096;
// std::vector<uint8_t> dst_buf(buf_size);
// std::vector<uint8_t> check_buf(buf_size);
// do {
// const size_t dst_r = fread(dst_buf.data(), 1, buf_size, dst_file);
// const size_t check_r = fread(check_buf.data(), 1, buf_size, check_file);
// REQUIRE(dst_r == check_r);
// REQUIRE(dst_buf == check_buf);
// } while (!feof(dst_file) || !feof(check_file));
//}
// convert from binary to ascii
binary_to_ascii(src_filename, dst_filename);
// compare results
compare_files(dst_filename, check_filename);
}

TEST_CASE("Convert from ascii to binary", "[Convert]")
{
Expand Down
Loading

0 comments on commit f449b47

Please sign in to comment.