1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-23 06:30:38 +00:00

Reimplement quarks order check as a static assert (#1836)

This commit is contained in:
Mike Gelfand 2021-09-25 16:56:34 +03:00 committed by GitHub
parent 4ac1c601f5
commit c646a26c43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 11 deletions

View file

@ -9,6 +9,7 @@
#include <algorithm> #include <algorithm>
#include <cstdlib> /* bsearch() */ #include <cstdlib> /* bsearch() */
#include <cstring> /* memcmp() */ #include <cstring> /* memcmp() */
#include <iterator>
#include "transmission.h" #include "transmission.h"
#include "ptrarray.h" #include "ptrarray.h"
@ -27,7 +28,7 @@ struct tr_key_struct
"" name "", sizeof("" name "") - 1, \ "" name "", sizeof("" name "") - 1, \
} }
static struct tr_key_struct const my_static[] = { static struct tr_key_struct constexpr my_static[] = {
Q(""), Q(""),
Q("activeTorrentCount"), Q("activeTorrentCount"),
Q("activity-date"), Q("activity-date"),
@ -418,6 +419,36 @@ static struct tr_key_struct const my_static[] = {
Q("webseedsSendingToUs"), Q("webseedsSendingToUs"),
}; };
static size_t constexpr quarks_are_sorted = ( //
[]() constexpr
{
for (size_t i = 1; i < std::size(my_static); ++i)
{
char const* lhs = my_static[i - 1].str;
char const* rhs = my_static[i].str;
while (true)
{
if (*lhs > *rhs)
{
return false;
}
if (*lhs < *rhs || *lhs == '\0' || *rhs == '\0')
{
break;
}
++lhs;
++rhs;
}
}
return true;
})();
static_assert(quarks_are_sorted, "Predefined quarks must be sorted by their string value");
#undef Q #undef Q
static int compareKeys(void const* va, void const* vb) static int compareKeys(void const* va, void const* vb)

View file

@ -39,16 +39,6 @@ TEST_F(QuarkTest, allPredefinedKeysCanBeLookedUp)
} }
} }
TEST_F(QuarkTest, allPredefinedKeysAreSorted)
{
for (int i = 0; i + 1 < TR_N_KEYS; i++)
{
auto const str1 = quarkGetString(i);
auto const str2 = quarkGetString(i + 1);
EXPECT_LT(str1, str2);
}
}
TEST_F(QuarkTest, newEmptyQuarkReturnsNone) TEST_F(QuarkTest, newEmptyQuarkReturnsNone)
{ {
auto const q = tr_quark_new(nullptr, TR_BAD_SIZE); auto const q = tr_quark_new(nullptr, TR_BAD_SIZE);