From 7d0cf59286b9969e97d062ce98adf93610712e39 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Fri, 8 Sep 2017 20:47:10 -0400 Subject: [PATCH] correct off-by-one error in range dimension calculation, closes #213 --- source/worksheet/cell_vector.cpp | 2 +- source/worksheet/range_reference.cpp | 6 +++--- tests/worksheet/worksheet_test_suite.hpp | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/source/worksheet/cell_vector.cpp b/source/worksheet/cell_vector.cpp index a6468661..6698d3ca 100644 --- a/source/worksheet/cell_vector.cpp +++ b/source/worksheet/cell_vector.cpp @@ -109,7 +109,7 @@ const cell cell_vector::back() const std::size_t cell_vector::length() const { - return order_ == major_order::row ? bounds_.width() + 1 : bounds_.height() + 1; + return order_ == major_order::row ? bounds_.width() : bounds_.height(); } cell_vector::const_iterator cell_vector::begin() const diff --git a/source/worksheet/range_reference.cpp b/source/worksheet/range_reference.cpp index 8137c708..8fe9eb99 100644 --- a/source/worksheet/range_reference.cpp +++ b/source/worksheet/range_reference.cpp @@ -90,17 +90,17 @@ range_reference range_reference::make_offset(int column_offset, int row_offset) std::size_t range_reference::height() const { - return bottom_right_.row() - top_left_.row(); + return 1 + bottom_right_.row() - top_left_.row(); } std::size_t range_reference::width() const { - return (bottom_right_.column() - top_left_.column()).index; + return 1 + (bottom_right_.column() - top_left_.column()).index; } bool range_reference::is_single_cell() const { - return width() == 0 && height() == 0; + return width() == 1 && height() == 1; } std::string range_reference::to_string() const diff --git a/tests/worksheet/worksheet_test_suite.hpp b/tests/worksheet/worksheet_test_suite.hpp index 9998ba3d..e915d357 100644 --- a/tests/worksheet/worksheet_test_suite.hpp +++ b/tests/worksheet/worksheet_test_suite.hpp @@ -93,6 +93,7 @@ public: register_test(test_get_point_pos); register_test(test_named_range_named_cell_reference); register_test(test_iteration_skip_empty); + register_test(test_dimensions); } void test_new_worksheet() @@ -1088,4 +1089,17 @@ public: xlnt_assert_equals(cells[1].value(), "F6"); } } + + void test_dimensions() + { + xlnt::workbook workbook; + workbook.load(path_helper::test_file("4_every_style.xlsx")); + + auto active_sheet = workbook.active_sheet(); + auto sheet_range = active_sheet.calculate_dimension(); + + xlnt_assert(!sheet_range.is_single_cell()); + xlnt_assert_equals(sheet_range.width(), 4); + xlnt_assert_equals(sheet_range.height(), 35); + } };