From 6f244e5c8faa2d4994f52210b19e11d793024154 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 7 Dec 2016 23:37:52 -0500 Subject: [PATCH] Add unit tests for GapBuffer and fix an insert() bug. --- src/core/GapBuffer.cc | 1 + test/src/test_GapBuffer.cc | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/src/test_GapBuffer.cc diff --git a/src/core/GapBuffer.cc b/src/core/GapBuffer.cc index 0c17ca3..cb7fc04 100644 --- a/src/core/GapBuffer.cc +++ b/src/core/GapBuffer.cc @@ -70,5 +70,6 @@ void GapBuffer::move_gap(size_t position) { memmove(&m_buffer[m_gap_position], &m_buffer[m_gap_position + gap_size()], position - m_gap_position); } + m_gap_position = position; } } diff --git a/test/src/test_GapBuffer.cc b/test/src/test_GapBuffer.cc new file mode 100644 index 0000000..2d18e58 --- /dev/null +++ b/test/src/test_GapBuffer.cc @@ -0,0 +1,39 @@ +#include "gtest/gtest.h" +#include "GapBuffer.h" +#include "System.h" + +TEST(GapBufferTest, creates_an_empty_buffer) +{ + GapBuffer gp; + EXPECT_EQ(0u, gp.size()); +} + +TEST(GapBufferTest, creates_a_buffer_from_preallocated_memory) +{ + uint8_t * b = (uint8_t *)System::alloc_pages(2u); + strcpy((char *)b, "hi\n"); + GapBuffer gp(b, System::page_size * 2u, 3u); + + EXPECT_EQ(3u, gp.size()); +} + +TEST(GapBufferTest, allows_inserting) +{ + GapBuffer gp; + + gp.insert(0u, (const uint8_t *)"test", 4u); + EXPECT_EQ("test", gp.get_string()); + EXPECT_EQ(4u, gp.size()); + + gp.insert(2u, (const uint8_t *)"12", 2u); + EXPECT_EQ("te12st", gp.get_string()); + EXPECT_EQ(6u, gp.size()); + + gp.insert(0u, (const uint8_t *)"-", 1u); + EXPECT_EQ("-te12st", gp.get_string()); + EXPECT_EQ(7u, gp.size()); + + gp.insert(7u, (const uint8_t *)"!", 1u); + EXPECT_EQ("-te12st!", gp.get_string()); + EXPECT_EQ(8u, gp.size()); +}