<commit_before>#include <xzero/testing.h>
#include <xzero/BufferUtil.h>
#include <xzero/raft/Generator.h>

using namespace xzero;
using namespace xzero::raft;

TEST(raft_Generator, VoteRequest) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateVoteRequest(VoteRequest{ .term = 0x11,
                                     .candidateId = 0x22,
                                     .lastLogIndex = 0x33,
                                     .lastLogTerm = 0x44 });
  //logf("VoteRequest: $0", BufferUtil::hexPrint(&out));
  EXPECT_EQ("\x01\x11\x22\x33\x44", out);
}

TEST(raft_Generator, VoteResponse) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateVoteResponse(VoteResponse{ .term = 0x11,
                                       .voteGranted = true });
  //logf("VoteResponse: $0", BufferUtil::hexPrint(&out));
  EXPECT_EQ("\x02\x11\x01", out);
}

TEST(raft_Generator, AppendEntriesRequest) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateAppendEntriesRequest(AppendEntriesRequest{
      .term = 0x11,
      .leaderId = 0x12,
      .prevLogIndex = 0x13,
      .prevLogTerm = 0x14,
      .leaderCommit = 0x15,
      .entries = { LogEntry{0x11, Command{1, 2, 3, 4}},
                   LogEntry{0x11, Command{5, 6, 7, 8}} }});
  logf("AppendEntriesRequest: <$0>", BufferUtil::hexPrint(&out));
  //EXPECT_EQ("\x02\x11\x01", out);
}

TEST(raft_Generator, AppendEntriesResponse) {
}

TEST(raft_Generator, InstallSnapshotRequest) {
}

TEST(raft_Generator, InstallSnapshotResponse) {
}<commit_msg>[xzero] raft: finishes unit tests for Generator impl<commit_after>#include <xzero/testing.h>
#include <xzero/BufferUtil.h>
#include <xzero/raft/Generator.h>

using namespace xzero;
using namespace xzero::raft;

TEST(raft_Generator, VoteRequest) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateVoteRequest(VoteRequest{ .term = 0x11,
                                     .candidateId = 0x22,
                                     .lastLogIndex = 0x33,
                                     .lastLogTerm = 0x44 });
  EXPECT_EQ("\x01\x11\x22\x33\x44", out);
}

TEST(raft_Generator, VoteResponse) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateVoteResponse(VoteResponse{ .term = 0x11,
                                       .voteGranted = true });
  EXPECT_EQ("\x02\x11\x01", out);
}

TEST(raft_Generator, AppendEntriesRequest) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateAppendEntriesRequest(AppendEntriesRequest{
      .term = 0x11,
      .leaderId = 0x12,
      .prevLogIndex = 0x13,
      .prevLogTerm = 0x14,
      .leaderCommit = 0x15,
      .entries = { LogEntry{0x11, Command{1, 2, 3, 4}},
                   LogEntry{0x11, Command{5, 6, 7, 8}} }});
  EXPECT_EQ("\x03\x11\x12\x13\x14\x15\x02\x11\x01\x04\x01\x02\x03\x04\x11\x01\x04\x05\x06\x07\x08", out);
}

TEST(raft_Generator, AppendEntriesResponse) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateAppendEntriesResponse(AppendEntriesResponse{
      .term = 2,
      .success = true });

  EXPECT_EQ("\x04\x02\x01", out);
}

TEST(raft_Generator, InstallSnapshotRequest) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateInstallSnapshotRequest(InstallSnapshotRequest{
      .term = 1,
      .leaderId = 2,
      .lastIncludedIndex = 3,
      .lastIncludedTerm = 4,
      .offset = 0,
      .data = {0x42, 0x13, 0x37},
      .done = true });

  EXPECT_EQ("\x05\x01\x02\x03\x04\x00\x03\x42\x13\x37\x01", out);
}

TEST(raft_Generator, InstallSnapshotResponse) {
  Buffer out;
  raft::Generator g(BufferUtil::writer(&out));
  g.generateInstallSnapshotResponse(InstallSnapshotResponse{ .term = 1 });

  EXPECT_EQ("\x06\x01", out);
}
{'fraction_non_alphanumeric': 0.1464446206714248, 'fraction_numerical': 0.05789056304520222, 'mean_word_length': 2.9375650364203953, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5810039', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1305, 'n_words': 215}