This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from MathieuBordere/flags
Flags
- Loading branch information
Showing
13 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |