Migrate util.c unit tests to new test framework (#448)
This PR migrates all tests related to util into new test framework as part of the parent issue https://github.com/valkey-io/valkey/issues/428. --------- Signed-off-by: Karthick Ariyaratnam <karthyuom@gmail.com>
This commit is contained in:
parent
cde8ec1b41
commit
2ed71de8e1
@ -6906,7 +6906,6 @@ struct serverTest {
|
|||||||
{"quicklist", quicklistTest},
|
{"quicklist", quicklistTest},
|
||||||
{"zipmap", zipmapTest},
|
{"zipmap", zipmapTest},
|
||||||
{"sha1test", sha1Test},
|
{"sha1test", sha1Test},
|
||||||
{"util", utilTest},
|
|
||||||
{"endianconv", endianconvTest},
|
{"endianconv", endianconvTest},
|
||||||
{"zmalloc", zmalloc_test},
|
{"zmalloc", zmalloc_test},
|
||||||
{"sds", sdsTest},
|
{"sds", sdsTest},
|
||||||
|
@ -16,10 +16,17 @@ int test_intsetUpgradeFromint16Toint64(int argc, char **argv, int flags);
|
|||||||
int test_intsetUpgradeFromint32Toint64(int argc, char **argv, int flags);
|
int test_intsetUpgradeFromint32Toint64(int argc, char **argv, int flags);
|
||||||
int test_intsetStressLookups(int argc, char **argv, int flags);
|
int test_intsetStressLookups(int argc, char **argv, int flags);
|
||||||
int test_intsetStressAddDelete(int argc, char **argv, int flags);
|
int test_intsetStressAddDelete(int argc, char **argv, int flags);
|
||||||
|
int test_string2ll(int argc, char **argv, int flags);
|
||||||
|
int test_string2l(int argc, char **argv, int flags);
|
||||||
|
int test_ll2string(int argc, char **argv, int flags);
|
||||||
|
int test_ld2string(int argc, char **argv, int flags);
|
||||||
|
int test_fixedpoint_d2string(int argc, char **argv, int flags);
|
||||||
|
int test_reclaimFilePageCache(int argc, char **argv, int flags);
|
||||||
|
|
||||||
unitTest __test_crc64_c[] = {{"test_crc64", test_crc64}, {NULL, NULL}};
|
unitTest __test_crc64_c[] = {{"test_crc64", test_crc64}, {NULL, NULL}};
|
||||||
unitTest __test_crc64combine_c[] = {{"test_crc64combine", test_crc64combine}, {NULL, NULL}};
|
unitTest __test_crc64combine_c[] = {{"test_crc64combine", test_crc64combine}, {NULL, NULL}};
|
||||||
unitTest __test_intset_c[] = {{"test_intsetValueEncodings", test_intsetValueEncodings}, {"test_intsetBasicAdding", test_intsetBasicAdding}, {"test_intsetLargeNumberRandomAdd", test_intsetLargeNumberRandomAdd}, {"test_intsetUpgradeFromint16Toint32", test_intsetUpgradeFromint16Toint32}, {"test_intsetUpgradeFromint16Toint64", test_intsetUpgradeFromint16Toint64}, {"test_intsetUpgradeFromint32Toint64", test_intsetUpgradeFromint32Toint64}, {"test_intsetStressLookups", test_intsetStressLookups}, {"test_intsetStressAddDelete", test_intsetStressAddDelete}, {NULL, NULL}};
|
unitTest __test_intset_c[] = {{"test_intsetValueEncodings", test_intsetValueEncodings}, {"test_intsetBasicAdding", test_intsetBasicAdding}, {"test_intsetLargeNumberRandomAdd", test_intsetLargeNumberRandomAdd}, {"test_intsetUpgradeFromint16Toint32", test_intsetUpgradeFromint16Toint32}, {"test_intsetUpgradeFromint16Toint64", test_intsetUpgradeFromint16Toint64}, {"test_intsetUpgradeFromint32Toint64", test_intsetUpgradeFromint32Toint64}, {"test_intsetStressLookups", test_intsetStressLookups}, {"test_intsetStressAddDelete", test_intsetStressAddDelete}, {NULL, NULL}};
|
||||||
|
unitTest __test_util_c[] = {{"test_string2ll", test_string2ll}, {"test_string2l", test_string2l}, {"test_ll2string", test_ll2string}, {"test_ld2string", test_ld2string}, {"test_fixedpoint_d2string", test_fixedpoint_d2string}, {"test_reclaimFilePageCache", test_reclaimFilePageCache}, {NULL, NULL}};
|
||||||
|
|
||||||
struct unitTestSuite {
|
struct unitTestSuite {
|
||||||
char *filename;
|
char *filename;
|
||||||
@ -28,4 +35,5 @@ struct unitTestSuite {
|
|||||||
{"test_crc64.c", __test_crc64_c},
|
{"test_crc64.c", __test_crc64_c},
|
||||||
{"test_crc64combine.c", __test_crc64combine_c},
|
{"test_crc64combine.c", __test_crc64combine_c},
|
||||||
{"test_intset.c", __test_intset_c},
|
{"test_intset.c", __test_intset_c},
|
||||||
|
{"test_util.c", __test_util_c},
|
||||||
};
|
};
|
||||||
|
299
src/unit/test_util.c
Normal file
299
src/unit/test_util.c
Normal file
@ -0,0 +1,299 @@
|
|||||||
|
#include <sys/mman.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../config.h"
|
||||||
|
#include "../util.h"
|
||||||
|
#undef UNUSED
|
||||||
|
#include "test_help.h"
|
||||||
|
|
||||||
|
int test_string2ll(int argc, char **argv, int flags) {
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
UNUSED(flags);
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
long long v;
|
||||||
|
|
||||||
|
/* May not start with +. */
|
||||||
|
valkey_strlcpy(buf,"+1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
/* Leading space. */
|
||||||
|
valkey_strlcpy(buf," 1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
/* Trailing space. */
|
||||||
|
valkey_strlcpy(buf,"1 ",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
/* May not start with 0. */
|
||||||
|
valkey_strlcpy(buf,"01",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == -1);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"0",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == 0);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == 1);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"99",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == 99);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-99",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == -99);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-9223372036854775808",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == LLONG_MIN);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-9223372036854775809",sizeof(buf)); /* overflow */
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"9223372036854775807",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == LLONG_MAX);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"9223372036854775808",sizeof(buf)); /* overflow */
|
||||||
|
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_string2l(int argc, char **argv, int flags) {
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
UNUSED(flags);
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
long v;
|
||||||
|
|
||||||
|
/* May not start with +. */
|
||||||
|
valkey_strlcpy(buf,"+1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
/* May not start with 0. */
|
||||||
|
valkey_strlcpy(buf,"01",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == -1);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"0",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == 0);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"1",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == 1);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"99",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == 99);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-99",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == -99);
|
||||||
|
|
||||||
|
#if LONG_MAX != LLONG_MAX
|
||||||
|
valkey_strlcpy(buf,"-2147483648",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == LONG_MIN);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"-2147483649",sizeof(buf)); /* overflow */
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"2147483647",sizeof(buf));
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
|
||||||
|
TEST_ASSERT(v == LONG_MAX);
|
||||||
|
|
||||||
|
valkey_strlcpy(buf,"2147483648",sizeof(buf)); /* overflow */
|
||||||
|
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_ll2string(int argc, char **argv, int flags) {
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
UNUSED(flags);
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
long long v;
|
||||||
|
int sz;
|
||||||
|
|
||||||
|
v = 0;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 1);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0"));
|
||||||
|
|
||||||
|
v = -1;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 2);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "-1"));
|
||||||
|
|
||||||
|
v = 99;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 2);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "99"));
|
||||||
|
|
||||||
|
v = -99;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 3);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "-99"));
|
||||||
|
|
||||||
|
v = -2147483648;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 11);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "-2147483648"));
|
||||||
|
|
||||||
|
v = LLONG_MIN;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 20);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "-9223372036854775808"));
|
||||||
|
|
||||||
|
v = LLONG_MAX;
|
||||||
|
sz = ll2string(buf, sizeof buf, v);
|
||||||
|
TEST_ASSERT(sz == 19);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "9223372036854775807"));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_ld2string(int argc, char **argv, int flags) {
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
UNUSED(flags);
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
long double v;
|
||||||
|
int sz;
|
||||||
|
|
||||||
|
v = 0.0 / 0.0;
|
||||||
|
sz = ld2string(buf, sizeof(buf), v, LD_STR_AUTO);
|
||||||
|
TEST_ASSERT(sz == 3);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "nan"));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_fixedpoint_d2string(int argc, char **argv, int flags) {
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
UNUSED(flags);
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
double v;
|
||||||
|
int sz;
|
||||||
|
v = 0.0;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
||||||
|
TEST_ASSERT(sz == 6);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.0000"));
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
||||||
|
TEST_ASSERT(sz == 3);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.0"));
|
||||||
|
/* set junk in buffer */
|
||||||
|
memset(buf,'A',32);
|
||||||
|
v = 0.0001;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
||||||
|
TEST_ASSERT(sz == 6);
|
||||||
|
TEST_ASSERT(buf[sz] == '\0');
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.0001"));
|
||||||
|
/* set junk in buffer */
|
||||||
|
memset(buf,'A',32);
|
||||||
|
v = 6.0642951598391699e-05;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
||||||
|
TEST_ASSERT(sz == 6);
|
||||||
|
TEST_ASSERT(buf[sz] == '\0');
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.0001"));
|
||||||
|
v = 0.01;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
||||||
|
TEST_ASSERT(sz == 6);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.0100"));
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
||||||
|
TEST_ASSERT(sz == 3);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.0"));
|
||||||
|
v = -0.01;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
||||||
|
TEST_ASSERT(sz == 7);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "-0.0100"));
|
||||||
|
v = -0.1;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
||||||
|
TEST_ASSERT(sz == 4);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "-0.1"));
|
||||||
|
v = 0.1;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
||||||
|
TEST_ASSERT(sz == 3);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.1"));
|
||||||
|
v = 0.01;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 17);
|
||||||
|
TEST_ASSERT(sz == 19);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "0.01000000000000000"));
|
||||||
|
v = 10.01;
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
||||||
|
TEST_ASSERT(sz == 7);
|
||||||
|
TEST_ASSERT(!strcmp(buf, "10.0100"));
|
||||||
|
/* negative tests */
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 18);
|
||||||
|
TEST_ASSERT(sz == 0);
|
||||||
|
sz = fixedpoint_d2string(buf, sizeof buf, v, 0);
|
||||||
|
TEST_ASSERT(sz == 0);
|
||||||
|
sz = fixedpoint_d2string(buf, 1, v, 1);
|
||||||
|
TEST_ASSERT(sz == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__linux__)
|
||||||
|
/* Since fadvise and mincore is only supported in specific platforms like
|
||||||
|
* Linux, we only verify the fadvise mechanism works in Linux */
|
||||||
|
static int cache_exist(int fd) {
|
||||||
|
unsigned char flag;
|
||||||
|
void *m = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
TEST_ASSERT(m);
|
||||||
|
TEST_ASSERT(mincore(m, 4096, &flag) == 0);
|
||||||
|
munmap(m, 4096);
|
||||||
|
/* the least significant bit of the byte will be set if the corresponding
|
||||||
|
* page is currently resident in memory */
|
||||||
|
return flag&1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_reclaimFilePageCache(int argc, char **argv, int flags) {
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
UNUSED(flags);
|
||||||
|
|
||||||
|
char *tmpfile = "/tmp/redis-reclaim-cache-test";
|
||||||
|
int fd = open(tmpfile, O_RDWR|O_CREAT, 0644);
|
||||||
|
TEST_ASSERT(fd >= 0);
|
||||||
|
|
||||||
|
/* test write file */
|
||||||
|
char buf[4] = "foo";
|
||||||
|
TEST_ASSERT(write(fd, buf, sizeof(buf)) > 0);
|
||||||
|
TEST_ASSERT(cache_exist(fd));
|
||||||
|
TEST_ASSERT(valkey_fsync(fd) == 0);
|
||||||
|
TEST_ASSERT(reclaimFilePageCache(fd, 0, 0) == 0);
|
||||||
|
TEST_ASSERT(!cache_exist(fd));
|
||||||
|
|
||||||
|
/* test read file */
|
||||||
|
TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) > 0);
|
||||||
|
TEST_ASSERT(cache_exist(fd));
|
||||||
|
TEST_ASSERT(reclaimFilePageCache(fd, 0, 0) == 0);
|
||||||
|
TEST_ASSERT(!cache_exist(fd));
|
||||||
|
|
||||||
|
unlink(tmpfile);
|
||||||
|
printf("reclaimFilePageCache test is ok\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
280
src/util.c
280
src/util.c
@ -1377,283 +1377,3 @@ int snprintf_async_signal_safe(char *to, size_t n, const char *fmt, ...) {
|
|||||||
va_end(args);
|
va_end(args);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SERVER_TEST
|
|
||||||
#include <assert.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include "testhelp.h"
|
|
||||||
|
|
||||||
static void test_string2ll(void) {
|
|
||||||
char buf[32];
|
|
||||||
long long v;
|
|
||||||
|
|
||||||
/* May not start with +. */
|
|
||||||
valkey_strlcpy(buf,"+1",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
/* Leading space. */
|
|
||||||
valkey_strlcpy(buf," 1",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
/* Trailing space. */
|
|
||||||
valkey_strlcpy(buf,"1 ",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
/* May not start with 0. */
|
|
||||||
valkey_strlcpy(buf,"01",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-1",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == -1);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"0",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == 0);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"1",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == 1);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"99",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == 99);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-99",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == -99);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-9223372036854775808",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == LLONG_MIN);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-9223372036854775809",sizeof(buf)); /* overflow */
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"9223372036854775807",sizeof(buf));
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == LLONG_MAX);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"9223372036854775808",sizeof(buf)); /* overflow */
|
|
||||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_string2l(void) {
|
|
||||||
char buf[32];
|
|
||||||
long v;
|
|
||||||
|
|
||||||
/* May not start with +. */
|
|
||||||
valkey_strlcpy(buf,"+1",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
/* May not start with 0. */
|
|
||||||
valkey_strlcpy(buf,"01",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-1",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == -1);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"0",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == 0);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"1",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == 1);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"99",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == 99);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-99",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == -99);
|
|
||||||
|
|
||||||
#if LONG_MAX != LLONG_MAX
|
|
||||||
valkey_strlcpy(buf,"-2147483648",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == LONG_MIN);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"-2147483649",sizeof(buf)); /* overflow */
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"2147483647",sizeof(buf));
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
|
||||||
assert(v == LONG_MAX);
|
|
||||||
|
|
||||||
valkey_strlcpy(buf,"2147483648",sizeof(buf)); /* overflow */
|
|
||||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_ll2string(void) {
|
|
||||||
char buf[32];
|
|
||||||
long long v;
|
|
||||||
int sz;
|
|
||||||
|
|
||||||
v = 0;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 1);
|
|
||||||
assert(!strcmp(buf, "0"));
|
|
||||||
|
|
||||||
v = -1;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 2);
|
|
||||||
assert(!strcmp(buf, "-1"));
|
|
||||||
|
|
||||||
v = 99;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 2);
|
|
||||||
assert(!strcmp(buf, "99"));
|
|
||||||
|
|
||||||
v = -99;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 3);
|
|
||||||
assert(!strcmp(buf, "-99"));
|
|
||||||
|
|
||||||
v = -2147483648;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 11);
|
|
||||||
assert(!strcmp(buf, "-2147483648"));
|
|
||||||
|
|
||||||
v = LLONG_MIN;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 20);
|
|
||||||
assert(!strcmp(buf, "-9223372036854775808"));
|
|
||||||
|
|
||||||
v = LLONG_MAX;
|
|
||||||
sz = ll2string(buf, sizeof buf, v);
|
|
||||||
assert(sz == 19);
|
|
||||||
assert(!strcmp(buf, "9223372036854775807"));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_ld2string(void) {
|
|
||||||
char buf[32];
|
|
||||||
long double v;
|
|
||||||
int sz;
|
|
||||||
|
|
||||||
v = 0.0 / 0.0;
|
|
||||||
sz = ld2string(buf, sizeof(buf), v, LD_STR_AUTO);
|
|
||||||
assert(sz == 3);
|
|
||||||
assert(!strcmp(buf, "nan"));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_fixedpoint_d2string(void) {
|
|
||||||
char buf[32];
|
|
||||||
double v;
|
|
||||||
int sz;
|
|
||||||
v = 0.0;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
|
||||||
assert(sz == 6);
|
|
||||||
assert(!strcmp(buf, "0.0000"));
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
|
||||||
assert(sz == 3);
|
|
||||||
assert(!strcmp(buf, "0.0"));
|
|
||||||
/* set junk in buffer */
|
|
||||||
memset(buf,'A',32);
|
|
||||||
v = 0.0001;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
|
||||||
assert(sz == 6);
|
|
||||||
assert(buf[sz] == '\0');
|
|
||||||
assert(!strcmp(buf, "0.0001"));
|
|
||||||
/* set junk in buffer */
|
|
||||||
memset(buf,'A',32);
|
|
||||||
v = 6.0642951598391699e-05;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
|
||||||
assert(sz == 6);
|
|
||||||
assert(buf[sz] == '\0');
|
|
||||||
assert(!strcmp(buf, "0.0001"));
|
|
||||||
v = 0.01;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
|
||||||
assert(sz == 6);
|
|
||||||
assert(!strcmp(buf, "0.0100"));
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
|
||||||
assert(sz == 3);
|
|
||||||
assert(!strcmp(buf, "0.0"));
|
|
||||||
v = -0.01;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
|
||||||
assert(sz == 7);
|
|
||||||
assert(!strcmp(buf, "-0.0100"));
|
|
||||||
v = -0.1;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
|
||||||
assert(sz == 4);
|
|
||||||
assert(!strcmp(buf, "-0.1"));
|
|
||||||
v = 0.1;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
|
|
||||||
assert(sz == 3);
|
|
||||||
assert(!strcmp(buf, "0.1"));
|
|
||||||
v = 0.01;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 17);
|
|
||||||
assert(sz == 19);
|
|
||||||
assert(!strcmp(buf, "0.01000000000000000"));
|
|
||||||
v = 10.01;
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
|
|
||||||
assert(sz == 7);
|
|
||||||
assert(!strcmp(buf, "10.0100"));
|
|
||||||
/* negative tests */
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 18);
|
|
||||||
assert(sz == 0);
|
|
||||||
sz = fixedpoint_d2string(buf, sizeof buf, v, 0);
|
|
||||||
assert(sz == 0);
|
|
||||||
sz = fixedpoint_d2string(buf, 1, v, 1);
|
|
||||||
assert(sz == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__linux__)
|
|
||||||
/* Since fadvise and mincore is only supported in specific platforms like
|
|
||||||
* Linux, we only verify the fadvise mechanism works in Linux */
|
|
||||||
static int cache_exist(int fd) {
|
|
||||||
unsigned char flag;
|
|
||||||
void *m = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
assert(m);
|
|
||||||
assert(mincore(m, 4096, &flag) == 0);
|
|
||||||
munmap(m, 4096);
|
|
||||||
/* the least significant bit of the byte will be set if the corresponding
|
|
||||||
* page is currently resident in memory */
|
|
||||||
return flag&1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_reclaimFilePageCache(void) {
|
|
||||||
char *tmpfile = "/tmp/redis-reclaim-cache-test";
|
|
||||||
int fd = open(tmpfile, O_RDWR|O_CREAT, 0644);
|
|
||||||
assert(fd >= 0);
|
|
||||||
|
|
||||||
/* test write file */
|
|
||||||
char buf[4] = "foo";
|
|
||||||
assert(write(fd, buf, sizeof(buf)) > 0);
|
|
||||||
assert(cache_exist(fd));
|
|
||||||
assert(valkey_fsync(fd) == 0);
|
|
||||||
assert(reclaimFilePageCache(fd, 0, 0) == 0);
|
|
||||||
assert(!cache_exist(fd));
|
|
||||||
|
|
||||||
/* test read file */
|
|
||||||
assert(pread(fd, buf, sizeof(buf), 0) > 0);
|
|
||||||
assert(cache_exist(fd));
|
|
||||||
assert(reclaimFilePageCache(fd, 0, 0) == 0);
|
|
||||||
assert(!cache_exist(fd));
|
|
||||||
|
|
||||||
unlink(tmpfile);
|
|
||||||
printf("reclaimFilePageCache test is ok\n");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int utilTest(int argc, char **argv, int flags) {
|
|
||||||
UNUSED(argc);
|
|
||||||
UNUSED(argv);
|
|
||||||
UNUSED(flags);
|
|
||||||
|
|
||||||
test_string2ll();
|
|
||||||
test_string2l();
|
|
||||||
test_ll2string();
|
|
||||||
test_ld2string();
|
|
||||||
test_fixedpoint_d2string();
|
|
||||||
#if defined(__linux__)
|
|
||||||
if (!(flags & TEST_VALGRIND)) {
|
|
||||||
test_reclaimFilePageCache();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
printf("Done testing util\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
@ -100,8 +100,4 @@ int snprintf_async_signal_safe(char *to, size_t n, const char *fmt, ...);
|
|||||||
size_t valkey_strlcpy(char *dst, const char *src, size_t dsize);
|
size_t valkey_strlcpy(char *dst, const char *src, size_t dsize);
|
||||||
size_t valkey_strlcat(char *dst, const char *src, size_t dsize);
|
size_t valkey_strlcat(char *dst, const char *src, size_t dsize);
|
||||||
|
|
||||||
#ifdef SERVER_TEST
|
|
||||||
int utilTest(int argc, char **argv, int flags);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user