diff options
-rw-r--r-- | src/RunStyles.cxx | 36 | ||||
-rw-r--r-- | src/RunStyles.h | 2 | ||||
-rw-r--r-- | test/unit/testRunStyles.cxx | 18 |
3 files changed, 56 insertions, 0 deletions
diff --git a/src/RunStyles.cxx b/src/RunStyles.cxx index 9c4e90a66..b74053e75 100644 --- a/src/RunStyles.cxx +++ b/src/RunStyles.cxx @@ -9,6 +9,8 @@ #include <stdlib.h> #include <stdarg.h> +#include <stdexcept> + #include "Platform.h" #include "Scintilla.h" @@ -113,7 +115,13 @@ int RunStyles::EndRun(int position) { } bool RunStyles::FillRange(int &position, int value, int &fillLength) { + if (fillLength <= 0) { + throw std::invalid_argument("RunStyles::FillRange <= 0 length."); + } int end = position + fillLength; + if (end > Length()) { + throw std::invalid_argument("RunStyles::FillRange length goes past end."); + } int runEnd = RunFromPosition(end); if (styles->ValueAt(runEnd) == value) { // End already has value so trim range. @@ -249,3 +257,31 @@ int RunStyles::Find(int value, int start) const { } return -1; } + +void RunStyles::Check() { + if (Length() < 0) { + throw std::runtime_error("RunStyles: Length can not be negative."); + } + if (starts->Partitions() < 1) { + throw std::runtime_error("RunStyles: Must always have 1 or more partitions."); + } + if (starts->Partitions() != styles->Length()-1) { + throw std::runtime_error("RunStyles: Partitions and styles different lengths."); + } + int start=0; + while (start < Length()) { + int end = EndRun(start); + if (start >= end) { + throw std::runtime_error("RunStyles: Partition is 0 length."); + } + start = end; + } + if (styles->ValueAt(styles->Length()-1) != 0) { + throw std::runtime_error("RunStyles: Unused style at end changed."); + } + for (int j=1; j<styles->Length()-1; j++) { + if (styles->ValueAt(j) == styles->ValueAt(j-1)) { + throw std::runtime_error("RunStyles: Style of a partition same as previous."); + } + } +} diff --git a/src/RunStyles.h b/src/RunStyles.h index 4e90969a2..521c701fa 100644 --- a/src/RunStyles.h +++ b/src/RunStyles.h @@ -43,6 +43,8 @@ public: bool AllSame() const; bool AllSameAs(int value) const; int Find(int value, int start) const; + + void Check(); }; #ifdef SCI_NAMESPACE diff --git a/test/unit/testRunStyles.cxx b/test/unit/testRunStyles.cxx index 8fec25b56..3c09dd87a 100644 --- a/test/unit/testRunStyles.cxx +++ b/test/unit/testRunStyles.cxx @@ -2,6 +2,8 @@ #include <string.h> +#include <stdexcept> + #include "Platform.h" #include "SplitVector.h" @@ -311,4 +313,20 @@ TEST_F(RunStylesTest, DeleteEndRun) { EXPECT_EQ(1, prs->EndRun(0)); EXPECT_EQ(0, prs->StartRun(1)); EXPECT_EQ(1, prs->EndRun(1)); + prs->Check(); +} + +TEST_F(RunStylesTest, OutsideBounds) { + prs->InsertSpace(0, 1); + int startFill = 1; + int lengthFill = 1; + try { + prs->FillRange(startFill, 99, lengthFill); + } catch (std::invalid_argument &) { + // Exception is supposed to occur so ignore. + } + EXPECT_EQ(1, prs->Length()); + EXPECT_EQ(1, prs->Runs()); + EXPECT_EQ(0, prs->StartRun(0)); + EXPECT_EQ(1, prs->EndRun(0)); } |