Skip to content

Commit

Permalink
Migrate util.c unit tests to new test framework (valkey-io#448)
Browse files Browse the repository at this point in the history
This PR migrates all tests related to util into new test framework as
part of the parent issue valkey-io#428.

---------

Signed-off-by: Karthick Ariyaratnam <karthyuom@gmail.com>

Signed-off-by: Karthick Ariyaratnam <karthyuom@gmail.com>
  • Loading branch information
karthyuom committed May 13, 2024
1 parent cee504a commit c33f89b
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 285 deletions.
1 change: 0 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6906,7 +6906,6 @@ struct serverTest {
{"quicklist", quicklistTest},
{"zipmap", zipmapTest},
{"sha1test", sha1Test},
{"util", utilTest},
{"endianconv", endianconvTest},
{"sds", sdsTest},
{"dict", dictTest},
Expand Down
8 changes: 8 additions & 0 deletions src/unit/test_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ int test_intsetUpgradeFromint16Toint64(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_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);
int test_zmallocInitialUsedMemory(int argc, char **argv, int flags);
int test_zmallocAllocReallocCallocAndFree(int argc, char **argv, int flags);
int test_zmallocAllocZeroBytesAndFree(int argc, char **argv, int flags);

unitTest __test_crc64_c[] = {{"test_crc64", test_crc64}, {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_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}};
unitTest __test_zmalloc_c[] = {{"test_zmallocInitialUsedMemory", test_zmallocInitialUsedMemory}, {"test_zmallocAllocReallocCallocAndFree", test_zmallocAllocReallocCallocAndFree}, {"test_zmallocAllocZeroBytesAndFree", test_zmallocAllocZeroBytesAndFree}, {NULL, NULL}};

struct unitTestSuite {
Expand All @@ -32,5 +39,6 @@ struct unitTestSuite {
{"test_crc64.c", __test_crc64_c},
{"test_crc64combine.c", __test_crc64combine_c},
{"test_intset.c", __test_intset_c},
{"test_util.c", __test_util_c},
{"test_zmalloc.c", __test_zmalloc_c},
};
299 changes: 299 additions & 0 deletions src/unit/test_util.c
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit c33f89b

Please sign in to comment.