diff options
Diffstat (limited to 'test/unit/testCellBuffer.cxx')
-rw-r--r-- | test/unit/testCellBuffer.cxx | 137 |
1 files changed, 70 insertions, 67 deletions
diff --git a/test/unit/testCellBuffer.cxx b/test/unit/testCellBuffer.cxx index b44f47735..eaa5b0f31 100644 --- a/test/unit/testCellBuffer.cxx +++ b/test/unit/testCellBuffer.cxx @@ -30,20 +30,23 @@ using namespace Scintilla; using namespace Scintilla::Internal; // Test CellBuffer. +bool Equal(const char *ptr, std::string_view sv) noexcept { + return memcmp(ptr, sv.data(), sv.length()) == 0; +} TEST_CASE("CellBuffer") { - const char sText[] = "Scintilla"; - const Sci::Position sLength = static_cast<Sci::Position>(strlen(sText)); + constexpr std::string_view sText = "Scintilla"; + constexpr Sci::Position sLength = sText.length(); CellBuffer cb(true, false); SECTION("InsertOneLine") { bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText, sLength, startSequence); + const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(startSequence); REQUIRE(sLength == cb.Length()); - REQUIRE(memcmp(cpChange, sText, sLength) == 0); + REQUIRE(Equal(cpChange, sText)); REQUIRE(1 == cb.Lines()); REQUIRE(0 == cb.LineStart(0)); REQUIRE(0 == cb.LineFromPosition(0)); @@ -54,13 +57,13 @@ TEST_CASE("CellBuffer") { } SECTION("InsertTwoLines") { - const char sText2[] = "Two\nLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); + constexpr std::string_view sText2 = "Two\nLines"; + constexpr Sci::Position sLength2 = sText2.length(); bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText2, sLength2, startSequence); + const char *cpChange = cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(startSequence); REQUIRE(sLength2 == cb.Length()); - REQUIRE(memcmp(cpChange, sText2, sLength2) == 0); + REQUIRE(Equal(cpChange, sText2)); REQUIRE(2 == cb.Lines()); REQUIRE(0 == cb.LineStart(0)); REQUIRE(0 == cb.LineFromPosition(0)); @@ -78,45 +81,45 @@ TEST_CASE("CellBuffer") { bool startSequence = false; { // Unix \n - const char sText2[] = "Two\nLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, strlen(sText2), startSequence); + constexpr std::string_view sText2 = "Two\nLines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Windows \r\n - const char sText2[] = "Two\r\nLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, sLength2, startSequence); + constexpr std::string_view sText2 = "Two\r\nLines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Old macOS \r - const char sText2[] = "Two\rLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, strlen(sText2), startSequence); + constexpr std::string_view sText2 = "Two\rLines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Unicode NEL is U+0085 \xc2\x85 - const char sText2[] = "Two\xc2\x85Lines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, sLength2, startSequence); + constexpr std::string_view sText2 = "Two\xc2\x85Lines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Unicode LS line separator is U+2028 \xe2\x80\xa8 - const char sText2[] = "Two\xe2\x80\xa8Lines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, sLength2, startSequence); + constexpr std::string_view sText2 = "Two\xe2\x80\xa8Lines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); @@ -129,36 +132,36 @@ TEST_CASE("CellBuffer") { cb.SetUndoCollection(false); REQUIRE(!cb.IsCollectingUndo()); bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText, sLength, startSequence); + const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(!startSequence); REQUIRE(sLength == cb.Length()); - REQUIRE(memcmp(cpChange, sText, sLength) == 0); + REQUIRE(Equal(cpChange, sText)); REQUIRE(!cb.CanUndo()); REQUIRE(!cb.CanRedo()); } SECTION("UndoRedo") { - const char sTextDeleted[] = "ci"; - const char sTextAfterDeletion[] = "Sntilla"; + constexpr std::string_view sTextDeleted = "ci"; + constexpr std::string_view sTextAfterDeletion = "Sntilla"; bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText, sLength, startSequence); + const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(startSequence); REQUIRE(sLength == cb.Length()); - REQUIRE(memcmp(cpChange, sText, sLength) == 0); - REQUIRE(memcmp(cb.BufferPointer(), sText, sLength) == 0); + REQUIRE(Equal(cpChange, sText)); + REQUIRE(Equal(cb.BufferPointer(), sText)); REQUIRE(cb.CanUndo()); REQUIRE(!cb.CanRedo()); const char *cpDeletion = cb.DeleteChars(1, 2, startSequence); REQUIRE(startSequence); - REQUIRE(memcmp(cpDeletion, sTextDeleted, strlen(sTextDeleted)) == 0); - REQUIRE(memcmp(cb.BufferPointer(), sTextAfterDeletion, strlen(sTextAfterDeletion)) == 0); + REQUIRE(Equal(cpDeletion, sTextDeleted)); + REQUIRE(Equal(cb.BufferPointer(), sTextAfterDeletion)); REQUIRE(cb.CanUndo()); REQUIRE(!cb.CanRedo()); int steps = cb.StartUndo(); REQUIRE(steps == 1); cb.PerformUndoStep(); - REQUIRE(memcmp(cb.BufferPointer(), sText, sLength) == 0); + REQUIRE(Equal(cb.BufferPointer(), sText)); REQUIRE(cb.CanUndo()); REQUIRE(cb.CanRedo()); @@ -172,14 +175,14 @@ TEST_CASE("CellBuffer") { steps = cb.StartRedo(); REQUIRE(steps == 1); cb.PerformRedoStep(); - REQUIRE(memcmp(cb.BufferPointer(), sText, sLength) == 0); + REQUIRE(Equal(cb.BufferPointer(), sText)); REQUIRE(cb.CanUndo()); REQUIRE(cb.CanRedo()); steps = cb.StartRedo(); REQUIRE(steps == 1); cb.PerformRedoStep(); - REQUIRE(memcmp(cb.BufferPointer(), sTextAfterDeletion, strlen(sTextAfterDeletion)) == 0); + REQUIRE(Equal(cb.BufferPointer(), sTextAfterDeletion)); REQUIRE(cb.CanUndo()); REQUIRE(!cb.CanRedo()); @@ -201,7 +204,7 @@ TEST_CASE("CellBuffer") { cb.SetReadOnly(true); REQUIRE(cb.IsReadOnly()); bool startSequence = false; - cb.InsertString(0, sText, sLength, startSequence); + cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(cb.Length() == 0); } @@ -239,8 +242,8 @@ TEST_CASE("CharacterIndex") { REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 1); - const char *hwair = "\xF0\x90\x8D\x88"; - cb.InsertString(0, hwair, strlen(hwair), startSequence); + constexpr std::string_view hwair = "\xF0\x90\x8D\x88"; + cb.InsertString(0, hwair.data(), hwair.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0); @@ -253,8 +256,8 @@ TEST_CASE("CharacterIndex") { cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32); bool startSequence = false; - const char *hwair = "a\xF0\x90\x8D\x88z"; - cb.InsertString(0, hwair, strlen(hwair), startSequence); + constexpr std::string_view hwair = "a\xF0\x90\x8D\x88z"; + cb.InsertString(0, hwair.data(), hwair.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 4); @@ -283,8 +286,8 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 3 lines of text containing 8 bytes - const char *data = "a\n\xF0\x90\x8D\x88\nz"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz"; + cb.InsertString(0, data.data(), data.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -298,7 +301,7 @@ TEST_CASE("CharacterIndex") { // Insert a new line at end -> "a\n\xF0\x90\x8D\x88\nz\n" 4 lines // Last line empty - cb.InsertString(strlen(data), "\n", 1, startSequence); + cb.InsertString(data.length(), "\n", 1, startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -313,7 +316,7 @@ TEST_CASE("CharacterIndex") { REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf32) == 6); // Insert a new line before end -> "a\n\xF0\x90\x8D\x88\nz\n\n" 5 lines - cb.InsertString(strlen(data), "\n", 1, startSequence); + cb.InsertString(data.length(), "\n", 1, startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -332,8 +335,8 @@ TEST_CASE("CharacterIndex") { // Insert a valid 3-byte UTF-8 character at start -> // "\xE2\x82\xACa\n\xF0\x90\x8D\x88\nz\n\n" 5 lines - const char *euro = "\xE2\x82\xAC"; - cb.InsertString(0, euro, strlen(euro), startSequence); + constexpr std::string_view euro = "\xE2\x82\xAC"; + cb.InsertString(0, euro.data(), euro.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -353,8 +356,8 @@ TEST_CASE("CharacterIndex") { // "\xE2\x82\xACa\n\EF\xF0\x90\x8D\x88\nz\n\n" 5 lines // Should be treated as a single byte character - const char *lead = "\xEF"; - cb.InsertString(5, lead, strlen(lead), startSequence); + constexpr std::string_view lead = "\xEF"; + cb.InsertString(5, lead.data(), lead.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -372,8 +375,8 @@ TEST_CASE("CharacterIndex") { // byte before and the 2 bytes after also be each treated as singles // so 3 more characters on line 0. - const char *ascii = "!"; - cb.InsertString(1, ascii, strlen(ascii), startSequence); + constexpr std::string_view ascii = "!"; + cb.InsertString(1, ascii.data(), ascii.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 6); @@ -386,8 +389,8 @@ TEST_CASE("CharacterIndex") { // Insert a NEL after the '!' to trigger the utf8 line end case -> // "\xE2!\xC2\x85 \x82\xACa\n \EF\xF0\x90\x8D\x88\n z\n\n" 5 lines - const char *nel = "\xC2\x85"; - cb.InsertString(2, nel, strlen(nel), startSequence); + constexpr std::string_view nel = "\xC2\x85"; + cb.InsertString(2, nel.data(), nel.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -406,11 +409,11 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 3 lines of text containing 8 bytes - const char *data = "a\n\xF0\x90\x8D\x88\nz\nc"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz\nc"; + cb.InsertString(0, data.data(), data.length(), startSequence); // Delete first 2 new lines -> "az\nc" - cb.DeleteChars(1, strlen(data) - 4, startSequence); + cb.DeleteChars(1, data.length() - 4, startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -427,8 +430,8 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 3 lines of text containing 8 bytes - const char *data = "a\n\xF0\x90\x8D\x88\nz"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz"; + cb.InsertString(0, data.data(), data.length(), startSequence); // Delete lead byte from character on line 1 -> // "a\n\x90\x8D\x88\nz" @@ -461,8 +464,8 @@ TEST_CASE("CharacterIndex") { // Restore lead byte from character on line 0 making a 4-byte character -> // "a\xF0\x90\x8D\x88\nz" - const char *lead4 = "\xF0"; - cb.InsertString(1, lead4, strlen(lead4), startSequence); + constexpr std::string_view lead4 = "\xF0"; + cb.InsertString(1, lead4.data(), lead4.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 4); @@ -479,13 +482,13 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 2 lines of text containing 4 bytes - const char *data = "a\r\nb"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\r\nb"; + cb.InsertString(0, data.data(), data.length(), startSequence); // 3 lines of text containing 5 bytes -> // "a\r!\nb" - const char *ascii = "!"; - cb.InsertString(2, ascii, strlen(ascii), startSequence); + constexpr std::string_view ascii = "!"; + cb.InsertString(2, ascii.data(), ascii.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -862,9 +865,9 @@ TEST_CASE("CellBufferWithChangeHistory") { SECTION("StraightUndoRedoSaveRevertRedo") { CellBuffer cb(true, false); cb.SetUndoCollection(false); - std::string sInsert = "abcdefghijklmnopqrstuvwxyz"; + constexpr std::string_view sInsert = "abcdefghijklmnopqrstuvwxyz"; bool startSequence = false; - cb.InsertString(0, sInsert.c_str(), sInsert.length(), startSequence); + cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence); cb.SetUndoCollection(true); cb.SetSavePoint(); cb.ChangeHistorySet(true); @@ -1001,9 +1004,9 @@ TEST_CASE("CellBufferWithChangeHistory") { SECTION("Detached") { CellBuffer cb(true, false); cb.SetUndoCollection(false); - std::string sInsert = "abcdefghijklmnopqrstuvwxyz"; + constexpr std::string_view sInsert = "abcdefghijklmnopqrstuvwxyz"; bool startSequence = false; - cb.InsertString(0, sInsert.c_str(), sInsert.length(), startSequence); + cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence); cb.SetUndoCollection(true); cb.SetSavePoint(); cb.ChangeHistorySet(true); |