Skip to content

Commit

Permalink
add killring resize test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed Dec 9, 2023
1 parent bb1f741 commit 98908c7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/misc/ring_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RingBuffer {
Storage storage_;

public:
static constexpr unsigned int alignCapacity(unsigned int cap) {
static constexpr unsigned int alignAllocSize(unsigned int cap) {
if (cap == 0) {
cap = 1;
}
Expand All @@ -50,7 +50,7 @@ class RingBuffer {
return 1 << std::min(count, 31u);
}

explicit RingBuffer(unsigned int cap) : storage_(alignCapacity(cap)) {}
explicit RingBuffer(unsigned int allocSize) : storage_(alignAllocSize(allocSize)) {}

RingBuffer() : RingBuffer(0) {}

Expand All @@ -76,6 +76,8 @@ class RingBuffer {
return *this;
}

unsigned int allocSize() const { return static_cast<unsigned int>(this->storage_.size); }

unsigned int capacity() const { return this->allocSize() - 1; }

unsigned int size() const { return this->backIndex_ - this->frontIndex_; }
Expand Down Expand Up @@ -124,8 +126,6 @@ class RingBuffer {

const T *values() const { return this->storage_.ptr; }

size_t allocSize() const { return this->storage_.size; }

unsigned int actualIndex(unsigned int index) const { return index & this->capacity(); }
};

Expand Down
2 changes: 1 addition & 1 deletion src/rotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bool HistRotator::save(ssize_t index, StringRef curBuf) {
// ######################

void KillRing::expand(unsigned int afterCap) {
if (this->buf.capacity() == RingBuffer<std::string>::alignCapacity(afterCap)) {
if (this->buf.allocSize() == RingBuffer<std::string>::alignAllocSize(afterCap)) {
return; // do nothing
}

Expand Down
4 changes: 2 additions & 2 deletions test/buffer/buffer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ TEST(BufferTest, case1) {
msg += std::to_string(buffer.size());
msg += ", but index is: ";
msg += std::to_string(buffer.size() + 2);
ASSERT_EXIT({ buffer.at(buffer.size() + 2) = 999; }, ::testing::KilledBySignal(SIGABRT),
msg.c_str());
ASSERT_EXIT(
{ buffer.at(buffer.size() + 2) = 999; }, ::testing::KilledBySignal(SIGABRT), msg.c_str());
#endif
}

Expand Down
51 changes: 50 additions & 1 deletion test/interactive/interactive_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ TEST_F(InteractiveTest, keybind) {
ASSERT_NO_FATAL_FAILURE(this->waitAndExpect(0, WaitStatus::EXITED, "\n"));
}

TEST_F(InteractiveTest, killRing) {
TEST_F(InteractiveTest, killRing1) {
this->invoke("--quiet", "--norc");

ASSERT_NO_FATAL_FAILURE(this->expect(PROMPT));
Expand Down Expand Up @@ -410,6 +410,55 @@ TEST_F(InteractiveTest, killRing) {
ASSERT_NO_FATAL_FAILURE(this->waitAndExpect(0, WaitStatus::EXITED, "\n"));
}

TEST_F(InteractiveTest, killRing2) { // resize kill-ring
this->invoke("--quiet", "--norc");

ASSERT_NO_FATAL_FAILURE(this->expect(PROMPT));

ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("var killed : [String]"));
ASSERT_NO_FATAL_FAILURE(
this->sendLineAndExpect("$LINE_EDIT.action('action1', 'kill-ring-select', function(q, l) => "
"{ $killed = $l!.copy(); '12'; })"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$LINE_EDIT.bind('^R', 'action1')"));

// kill-ring-select (not work)
this->send(CTRL_R "34\r");
ASSERT_NO_FATAL_FAILURE(this->expect(PROMPT + "34\n: Int = 34\n" + PROMPT));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("assert $killed.size() == 0"));

// set size (to 7)
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$LINE_EDIT.config('killring-size', 4)"));
ASSERT_NO_FATAL_FAILURE(
this->sendLineAndExpect("$LINE_EDIT.configs()['killring-size'] as Int", ": Int = 7"));

this->send("12345" CTRL_U "abcde" CTRL_U "ABCDE" CTRL_U "67890" CTRL_U "FGHIJ" CTRL_U);
this->send(CTRL_R);
ASSERT_NO_FATAL_FAILURE(this->expect(PROMPT + "12"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("", ": Int = 12"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed.size()", ": Int = 5"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[0]", ": String = 12345"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[1]", ": String = abcde"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[2]", ": String = ABCDE"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[3]", ": String = 67890"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[4]", ": String = FGHIJ"));

// resize (shrink to 3)
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$LINE_EDIT.config('killring-size', 2)"));
ASSERT_NO_FATAL_FAILURE(
this->sendLineAndExpect("$LINE_EDIT.configs()['killring-size'] as Int", ": Int = 3"));

this->send(CTRL_R);
ASSERT_NO_FATAL_FAILURE(this->expect(PROMPT + "12"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("", ": Int = 12"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed.size()", ": Int = 3"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[0]", ": String = ABCDE"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[1]", ": String = 67890"));
ASSERT_NO_FATAL_FAILURE(this->sendLineAndExpect("$killed[2]", ": String = FGHIJ"));

this->send(CTRL_D);
ASSERT_NO_FATAL_FAILURE(this->waitAndExpect(0, WaitStatus::EXITED, "\n"));
}

TEST_F(InteractiveTest, history1) {
#ifdef CODE_COVERAGE
this->timeout = 500;
Expand Down

0 comments on commit 98908c7

Please sign in to comment.