Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
flags: Add raft_flags methods.
Browse files Browse the repository at this point in the history
flags will be used to track node capabilities.

Signed-off-by: Mathieu Borderé <[email protected]>
  • Loading branch information
Mathieu Borderé committed Jun 6, 2023
1 parent 0870ced commit 86eb348
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ libraft_la_SOURCES = \
src/election.c \
src/entry.c \
src/err.c \
src/flags.c \
src/heap.c \
src/lifecycle.c \
src/log.c \
Expand Down Expand Up @@ -65,13 +66,15 @@ test_unit_core_SOURCES = \
src/compress.c \
src/configuration.c \
src/err.c \
src/flags.c \
src/heap.c \
src/log.c \
test/unit/main_core.c \
test/unit/test_byte.c \
test/unit/test_compress.c \
test/unit/test_configuration.c \
test/unit/test_err.c \
test/unit/test_flags.c \
test/unit/test_log.c \
test/unit/test_queue.c
test_unit_core_CFLAGS = $(AM_CFLAGS) -Wno-conversion
Expand Down
5 changes: 5 additions & 0 deletions include/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ typedef unsigned long long raft_index;
*/
typedef unsigned long long raft_time;

/**
* Hold the features a raft node is capable of.
*/
typedef uint64_t raft_flags;

/**
* A data buffer.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "flags.h"

inline raft_flags flagsSet(raft_flags in, raft_flags flags)
{
return in | flags;
}

inline raft_flags flagsClear(raft_flags in, raft_flags flags)
{
return in & (~flags);
}

inline bool flagsIsSet(raft_flags in, raft_flags flag)
{
return (bool)(in & flag);
}
20 changes: 20 additions & 0 deletions src/flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef FLAGS_H_
#define FLAGS_H_

#include "../include/raft.h"

#define RAFT_DEFAULT_FEATURE_FLAGS (0)

/* Adds the flags @flags to @in and returns the new flags. Multiple flags should
* be combined using the `|` operator. */
raft_flags flagsSet(raft_flags in, raft_flags flags);

/* Clears the flags @flags from @in and returns the new flags. Multiple flags
* should be combined using the `|` operator. */
raft_flags flagsClear(raft_flags in, raft_flags flags);

/* Returns `true` if the single flag @flag is set in @in, otherwise returns
* `false`. */
bool flagsIsSet(raft_flags in, raft_flags flag);

#endif /* FLAGS_H */
97 changes: 97 additions & 0 deletions test/unit/test_flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "../../src/flags.h"
#include "../lib/runner.h"

/******************************************************************************
*
* flags
*
*****************************************************************************/

SUITE(flags)

TEST(flags, empty, NULL, NULL, 0, NULL)
{
raft_flags flags = 0;
for (int i = 0; i < 64; i++) {
munit_assert_false(flagsIsSet(flags, ((raft_flags)1) << i));
}
return MUNIT_OK;
}

TEST(flags, setClear, NULL, NULL, 0, NULL)
{
raft_flags flags = 0;
raft_flags flag = 0;
for (int i = 0; i < 64; i++) {
flag = ((raft_flags)1) << i;
flags = flagsSet(flags, flag);
munit_assert_true(flagsIsSet(flags, flag));
flags = flagsClear(flags, flag);
munit_assert_false(flagsIsSet(flags, flag));
munit_assert_true(flags == 0);
}
return MUNIT_OK;
}

TEST(flags, setMultipleClearMultiple, NULL, NULL, 0, NULL)
{
raft_flags in = 0;
raft_flags out;
raft_flags flags = (raft_flags)(1 | 1 << 4 | 1 << 13 | (raft_flags)1 << 40 |
(raft_flags)1 << 63);
out = flagsSet(in, flags);
/* clang-format off */
int positions[64] = {
1, 0, 0, 0, 1, 0, 0, 0, // 0th and 4th
0, 0, 0, 0, 0, 1, 0, 0, // 13th
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, // 40th
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, // 63th
};
/* clang-format on */
for (unsigned i = 0; i < 64; i++) {
if (positions[i]) {
munit_assert_true(flagsIsSet(out, (raft_flags)1 << i));
} else {
munit_assert_false(flagsIsSet(out, (raft_flags)1 << i));
}
}
out = flagsClear(out, flags);
munit_assert_true(out == 0);
return MUNIT_OK;
}

TEST(flags, setMultipleClearSingle, NULL, NULL, 0, NULL)
{
raft_flags in = 0;
raft_flags out;
raft_flags flags = (raft_flags)(1 << 3 | 1 << 5 | 1 << 18 |
(raft_flags)1 << 32 | (raft_flags)1 << 35);
out = flagsSet(in, flags);
/* clang-format off */
int positions[64] = {
0, 0, 0, 1, 0, 1, 0, 0, // 3rd and 5th
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, // 18th
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 1, 0, 0, 0, 0, // 32rd 35th
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
/* clang-format on */
for (unsigned i = 0; i < 64; i++) {
if (positions[i]) {
munit_assert_true(flagsIsSet(out, (raft_flags)1 << i));
} else {
munit_assert_false(flagsIsSet(out, (raft_flags)1 << i));
}
}
out = flagsClear(out, (raft_flags)1 << 32);
munit_assert_true(
out == (raft_flags)(1 << 3 | 1 << 5 | 1 << 18 | (raft_flags)1 << 35));
return MUNIT_OK;
}

0 comments on commit 86eb348

Please sign in to comment.