Add unit tests for GapBuffer and fix an insert() bug.
This commit is contained in:
parent
595d74c8c4
commit
6f244e5c8f
@ -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;
|
||||
}
|
||||
}
|
||||
|
39
test/src/test_GapBuffer.cc
Normal file
39
test/src/test_GapBuffer.cc
Normal file
@ -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());
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user