Transform `check_uint_eq` into generic `check_uint` (libtest)

This commit is contained in:
Mike Gelfand 2017-05-29 23:39:51 +03:00
parent 9c3c6f11e0
commit 437904198e
10 changed files with 96 additions and 98 deletions

View File

@ -236,28 +236,28 @@ static int test_base64(void)
char* out;
out = tr_base64_encode_str("YOYO!", &len);
check_uint_eq(strlen(out), len);
check_uint(len, ==, strlen(out));
check(base64_eq("WU9ZTyE=", out));
in = tr_base64_decode_str(out, &len);
check_uint_eq(5, len);
check_uint(len, ==, 5);
check_str(in, ==, "YOYO!");
tr_free(in);
tr_free(out);
out = tr_base64_encode("", 0, &len);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check_str(out, ==, "");
tr_free(out);
out = tr_base64_decode("", 0, &len);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check_str(out, ==, "");
tr_free(out);
out = tr_base64_encode(NULL, 0, &len);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check(out == NULL);
out = tr_base64_decode(NULL, 0, &len);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check(out == NULL);
#define MAX_BUF_SIZE 1024
@ -272,9 +272,9 @@ static int test_base64(void)
}
out = tr_base64_encode(buf, i, &len);
check_uint_eq(strlen(out), len);
check_uint(len, ==, strlen(out));
in = tr_base64_decode(out, len, &len);
check_uint_eq(i, len);
check_uint(len, ==, i);
check(memcmp(in, buf, len) == 0);
tr_free(in);
tr_free(out);
@ -287,9 +287,9 @@ static int test_base64(void)
buf[i] = '\0';
out = tr_base64_encode_str(buf, &len);
check_uint_eq(strlen(out), len);
check_uint(len, ==, strlen(out));
in = tr_base64_decode_str(out, &len);
check_uint_eq(i, len);
check_uint(len, ==, i);
check_str(buf, ==, in);
tr_free(in);
tr_free(out);

View File

@ -183,7 +183,7 @@ static int test_get_info(void)
check(tr_sys_path_get_info(path1, 0, &info, &err));
check(err == NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint_eq(4, info.size);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
/* Good file info (by handle) */
@ -192,7 +192,7 @@ static int test_get_info(void)
check(tr_sys_file_get_info(fd, &info, &err));
check(err == NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint_eq(4, info.size);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
tr_sys_file_close(fd, NULL);
@ -224,7 +224,7 @@ static int test_get_info(void)
check(tr_sys_path_get_info(path1, 0, &info, &err));
check(err == NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint_eq(4, info.size);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
/* Good file info (by handle) */
@ -233,7 +233,7 @@ static int test_get_info(void)
check(tr_sys_file_get_info(fd, &info, &err));
check(err == NULL);
check_int(info.type, ==, TR_SYS_PATH_IS_FILE);
check_uint_eq(4, info.size);
check_uint(info.size, ==, 4);
check(info.last_modified_at >= t && info.last_modified_at <= time(NULL));
tr_sys_file_close(fd, NULL);
@ -1017,30 +1017,30 @@ static int test_file_open(void)
check(err != NULL);
tr_error_clear(&err);
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint_eq(4, info.size);
check_uint(info.size, ==, 4);
/* Pointer is at the end of file */
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint_eq(4, info.size);
check_uint(info.size, ==, 4);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_APPEND, 0600, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
tr_sys_file_write(fd, "s", 1, NULL, NULL); /* On *NIX, pointer is positioned on each write but not initially */
tr_sys_file_seek(fd, 0, TR_SEEK_CUR, &n, NULL);
check_uint_eq(5, n);
check_uint(n, ==, 5);
tr_sys_file_close(fd, NULL);
/* File gets truncated */
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint_eq(5, info.size);
check_uint(info.size, ==, 5);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_TRUNCATE, 0600, &err);
check(fd != TR_BAD_SYS_FILE);
check(err == NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint_eq(0, info.size);
check_uint(info.size, ==, 0);
tr_sys_file_close(fd, NULL);
tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &info, NULL);
check_uint_eq(0, info.size);
check_uint(info.size, ==, 0);
/* TODO: symlink and hardlink tests */
@ -1067,69 +1067,69 @@ static int test_file_read_write_seek(void)
check(tr_sys_file_seek(fd, 0, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_uint_eq(0, n);
check_uint(n, ==, 0);
check(tr_sys_file_write(fd, "test", 4, &n, &err));
check(err == NULL);
check_uint_eq(4, n);
check_uint(n, ==, 4);
check(tr_sys_file_seek(fd, 0, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_uint_eq(4, n);
check_uint(n, ==, 4);
check(tr_sys_file_seek(fd, 0, TR_SEEK_SET, &n, &err));
check(err == NULL);
check_uint_eq(0, n);
check_uint(n, ==, 0);
check(tr_sys_file_read(fd, buf, sizeof(buf), &n, &err));
check(err == NULL);
check_uint_eq(4, n);
check_uint(n, ==, 4);
check_int(memcmp(buf, "test", 4), ==, 0);
check(tr_sys_file_seek(fd, -3, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_uint_eq(1, n);
check_uint(n, ==, 1);
check(tr_sys_file_write(fd, "E", 1, &n, &err));
check(err == NULL);
check_uint_eq(1, n);
check_uint(n, ==, 1);
check(tr_sys_file_seek(fd, -2, TR_SEEK_CUR, &n, &err));
check(err == NULL);
check_uint_eq(0, n);
check_uint(n, ==, 0);
check(tr_sys_file_read(fd, buf, sizeof(buf), &n, &err));
check(err == NULL);
check_uint_eq(4, n);
check_uint(n, ==, 4);
check_int(memcmp(buf, "tEst", 4), ==, 0);
check(tr_sys_file_seek(fd, 0, TR_SEEK_END, &n, &err));
check(err == NULL);
check_uint_eq(4, n);
check_uint(n, ==, 4);
check(tr_sys_file_write(fd, " ok", 3, &n, &err));
check(err == NULL);
check_uint_eq(3, n);
check_uint(n, ==, 3);
check(tr_sys_file_seek(fd, 0, TR_SEEK_SET, &n, &err));
check(err == NULL);
check_uint_eq(0, n);
check_uint(n, ==, 0);
check(tr_sys_file_read(fd, buf, sizeof(buf), &n, &err));
check(err == NULL);
check_uint_eq(7, n);
check_uint(n, ==, 7);
check_int(memcmp(buf, "tEst ok", 7), ==, 0);
check(tr_sys_file_write_at(fd, "-", 1, 4, &n, &err));
check(err == NULL);
check_uint_eq(1, n);
check_uint(n, ==, 1);
check(tr_sys_file_read_at(fd, buf, 5, 2, &n, &err));
check(err == NULL);
check_uint_eq(5, n);
check_uint(n, ==, 5);
check_int(memcmp(buf, "st-ok", 5), ==, 0);
@ -1158,17 +1158,17 @@ static int test_file_truncate(void)
check(tr_sys_file_truncate(fd, 10, &err));
check(err == NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint_eq(10, info.size);
check_uint(info.size, ==, 10);
check(tr_sys_file_truncate(fd, 20, &err));
check(err == NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint_eq(20, info.size);
check_uint(info.size, ==, 20);
check(tr_sys_file_truncate(fd, 0, &err));
check(err == NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint_eq(0, info.size);
check_uint(info.size, ==, 0);
check(tr_sys_file_truncate(fd, 50, &err));
check(err == NULL);
@ -1176,7 +1176,7 @@ static int test_file_truncate(void)
tr_sys_file_close(fd, NULL);
tr_sys_path_get_info(path1, 0, &info, NULL);
check_uint_eq(50, info.size);
check_uint(info.size, ==, 50);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600, NULL);
@ -1186,7 +1186,7 @@ static int test_file_truncate(void)
tr_sys_file_close(fd, NULL);
tr_sys_path_get_info(path1, 0, &info, NULL);
check_uint_eq(25, info.size);
check_uint(info.size, ==, 25);
tr_sys_path_remove(path1, NULL);
@ -1212,7 +1212,7 @@ static int test_file_preallocate(void)
{
check(err == NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint_eq(50, info.size);
check_uint(info.size, ==, 50);
}
else
{
@ -1231,7 +1231,7 @@ static int test_file_preallocate(void)
{
check(err == NULL);
tr_sys_file_get_info(fd, &info, NULL);
check_uint_eq(500 * 1024 * 1024, info.size);
check_uint(info.size, ==, 500 * 1024 * 1024);
}
else
{

View File

@ -179,7 +179,7 @@ static int test1(void)
check(tr_variantIsDict(args));
check((ids = tr_variantDictFind(args, TR_KEY_ids)) != NULL);
check(tr_variantIsList(ids));
check_uint_eq(2, tr_variantListSize(ids));
check_uint(tr_variantListSize(ids), ==, 2);
check(tr_variantGetInt(tr_variantListChild(ids, 0), &i));
check_int(i, ==, 7);
check(tr_variantGetInt(tr_variantListChild(ids, 1), &i));

View File

@ -89,20 +89,13 @@ bool libtest_check_int(char const* file, int line, bool pass, intmax_t lhs, intm
return pass;
}
bool check_uint_eq_impl(char const* file, int line, uintmax_t expected, uintmax_t actual)
bool libtest_check_uint(char const* file, int line, bool pass, uintmax_t lhs, uintmax_t rhs, char const* lhs_str,
char const* op_str, char const* rhs_str)
{
bool const pass = expected == actual;
if (should_print(pass))
{
if (pass)
{
fprintf(stderr, "PASS %s:%d\n", file, line);
}
else
{
fprintf(stderr, "FAIL %s:%d, expected \"%ju\", got \"%ju\"\n", file, line, expected, actual);
}
fprintf(stderr, "%s %s:%d: %s %s %s (%ju %s %ju)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str, lhs,
op_str, rhs);
}
return pass;

View File

@ -23,13 +23,14 @@ extern bool verbose;
bool should_print(bool pass);
bool check_condition_impl(char const* file, int line, bool condition);
bool check_uint_eq_impl(char const* file, int line, uintmax_t expected, uintmax_t actual);
bool check_ptr_eq_impl(char const* file, int line, void const* expected, void const* actual);
bool libtest_check_str(char const* file, int line, bool pass, char const* lhs, char const* rhs, char const* lhs_str,
char const* op_str, char const* rhs_str);
bool libtest_check_int(char const* file, int line, bool pass, intmax_t lhs, intmax_t rhs, char const* lhs_str,
char const* op_str, char const* rhs_str);
bool libtest_check_uint(char const* file, int line, bool pass, uintmax_t lhs, uintmax_t rhs, char const* lhs_str,
char const* op_str, char const* rhs_str);
/***
****
@ -79,12 +80,16 @@ bool libtest_check_int(char const* file, int line, bool pass, intmax_t lhs, intm
} \
while (0)
#define check_uint_eq(expected, actual) \
#define check_uint(lhs, op, rhs) \
do \
{ \
++current_test; \
\
if (!check_uint_eq_impl(__FILE__, __LINE__, (expected), (actual))) \
uintmax_t const check_uint_lhs = (lhs); \
uintmax_t const check_uint_rhs = (rhs); \
\
if (!libtest_check_uint(__FILE__, __LINE__, check_uint_lhs op check_uint_rhs, check_uint_lhs, check_uint_rhs, #lhs, \
#op, #rhs)) \
{ \
return current_test; \
} \

View File

@ -88,7 +88,7 @@ static int test_incomplete_dir_impl(char const* incomplete_dir, char const* down
check(tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
check_file_location(tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
check_file_location(tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL));
check_uint_eq(tor->info.pieceSize, tr_torrentStat(tor)->leftUntilDone);
check_uint(tr_torrentStat(tor)->leftUntilDone, ==, tor->info.pieceSize);
completeness = completeness_unset;
tr_torrentSetCompletenessCallback(tor, zeroes_completeness_func, &completeness);
@ -127,7 +127,7 @@ static int test_incomplete_dir_impl(char const* incomplete_dir, char const* down
}
libttest_blockingTorrentVerify(tor);
check_uint_eq(0, tr_torrentStat(tor)->leftUntilDone);
check_uint(tr_torrentStat(tor)->leftUntilDone, ==, 0);
while (completeness == completeness_unset && time(NULL) <= deadline)
{
@ -193,7 +193,7 @@ static int test_set_location(void)
tor = libttest_zero_torrent_init(session);
libttest_zero_torrent_populate(tor, true);
libttest_blockingTorrentVerify(tor);
check_uint_eq(0, tr_torrentStat(tor)->leftUntilDone);
check_uint(tr_torrentStat(tor)->leftUntilDone, ==, 0);
/* now move it */
state = -1;
@ -208,7 +208,7 @@ static int test_set_location(void)
/* confirm the torrent is still complete after being moved */
libttest_blockingTorrentVerify(tor);
check_uint_eq(0, tr_torrentStat(tor)->leftUntilDone);
check_uint(tr_torrentStat(tor)->leftUntilDone, ==, 0);
/* confirm the filest really got moved */
libttest_sync();

View File

@ -21,7 +21,7 @@ static int test_static_quarks(void)
char const* str;
str = tr_quark_get_string((tr_quark)i, &len);
check_uint_eq(strlen(str), len);
check_uint(len, ==, strlen(str));
check(tr_quark_lookup(str, len, &q));
check_int((int)q, ==, i);
}

View File

@ -32,10 +32,10 @@ static tr_session* session = NULL;
tr_stat const* tst = tr_torrentStat(tor); \
check_int(tst->activity, ==, TR_STATUS_STOPPED); \
check_int(tst->error, ==, TR_STAT_OK); \
check_uint_eq(totalSize, tst->sizeWhenDone); \
check_uint_eq(totalSize, tst->leftUntilDone); \
check_uint_eq(totalSize, tor->info.totalSize); \
check_uint_eq(0, tst->haveValid); \
check_uint(tst->sizeWhenDone, ==, totalSize); \
check_uint(tst->leftUntilDone, ==, totalSize); \
check_uint(tor->info.totalSize, ==, totalSize); \
check_uint(tst->haveValid, ==, 0); \
} \
while (0)
@ -164,11 +164,11 @@ static int test_single_filename_torrent(void)
st = tr_torrentStat(tor);
check_int(st->activity, ==, TR_STATUS_STOPPED);
check_int(st->error, ==, TR_STAT_OK);
check_uint_eq(0, st->leftUntilDone);
check_uint_eq(0, st->haveUnchecked);
check_uint_eq(0, st->desiredAvailable);
check_uint_eq(totalSize, st->sizeWhenDone);
check_uint_eq(totalSize, st->haveValid);
check_uint(st->leftUntilDone, ==, 0);
check_uint(st->haveUnchecked, ==, 0);
check_uint(st->desiredAvailable, ==, 0);
check_uint(st->sizeWhenDone, ==, totalSize);
check_uint(st->haveValid, ==, totalSize);
/**
*** okay! we've finally put together all the scaffolding to test
@ -303,8 +303,8 @@ static int test_multifile_torrent(void)
/* sanity check the info */
check_str(tor->info.name, ==, "Felidae");
check_uint_eq(totalSize, tor->info.totalSize);
check_uint_eq(4, tor->info.fileCount);
check_uint(tor->info.totalSize, ==, totalSize);
check_uint(tor->info.fileCount, ==, 4);
for (tr_file_index_t i = 0; i < 4; ++i)
{
@ -323,11 +323,11 @@ static int test_multifile_torrent(void)
st = tr_torrentStat(tor);
check_int(st->activity, ==, TR_STATUS_STOPPED);
check_int(st->error, ==, TR_STAT_OK);
check_uint_eq(0, st->leftUntilDone);
check_uint_eq(0, st->haveUnchecked);
check_uint_eq(0, st->desiredAvailable);
check_uint_eq(totalSize, st->sizeWhenDone);
check_uint_eq(totalSize, st->haveValid);
check_uint(st->leftUntilDone, ==, 0);
check_uint(st->haveUnchecked, ==, 0);
check_uint(st->desiredAvailable, ==, 0);
check_uint(st->sizeWhenDone, ==, totalSize);
check_uint(st->haveValid, ==, totalSize);
/**
*** okay! let's test renaming.
@ -519,22 +519,22 @@ static int test_partial_file(void)
***/
tor = libttest_zero_torrent_init(session);
check_uint_eq(totalSize, tor->info.totalSize);
check_uint_eq(pieceSize, tor->info.pieceSize);
check_uint_eq(pieceCount, tor->info.pieceCount);
check_uint(tor->info.totalSize, ==, totalSize);
check_uint(tor->info.pieceSize, ==, pieceSize);
check_uint(tor->info.pieceCount, ==, pieceCount);
check_str(tor->info.files[0].name, ==, "files-filled-with-zeroes/1048576");
check_str(tor->info.files[1].name, ==, "files-filled-with-zeroes/4096");
check_str(tor->info.files[2].name, ==, "files-filled-with-zeroes/512");
libttest_zero_torrent_populate(tor, false);
fst = tr_torrentFiles(tor, NULL);
check_uint_eq(length[0] - pieceSize, fst[0].bytesCompleted);
check_uint_eq(length[1], fst[1].bytesCompleted);
check_uint_eq(length[2], fst[2].bytesCompleted);
check_uint(fst[0].bytesCompleted, ==, length[0] - pieceSize);
check_uint(fst[1].bytesCompleted, ==, length[1]);
check_uint(fst[2].bytesCompleted, ==, length[2]);
tr_torrentFilesFree(fst, tor->info.fileCount);
st = tr_torrentStat(tor);
check_uint_eq(totalSize, st->sizeWhenDone);
check_uint_eq(pieceSize, st->leftUntilDone);
check_uint(st->sizeWhenDone, ==, totalSize);
check_uint(st->leftUntilDone, ==, pieceSize);
/***
****

View File

@ -44,7 +44,7 @@ static int test_list(void)
tr_rpc_parse_list_str(&top, "asdf", TR_BAD_SIZE);
check(tr_variantIsString(&top));
check(tr_variantGetStr(&top, &str, &len));
check_uint_eq(4, len);
check_uint(len, ==, 4);
check_str(str, ==, "asdf");
tr_variantFree(&top);

View File

@ -100,7 +100,7 @@ static int testStr(void)
n = tr_snprintf((char*)buf, sizeof(buf), "%zu:boat", (size_t)(SIZE_MAX - 2));
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
check_int(err, ==, EILSEQ);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check(str == NULL);
check(end == NULL);
check(len == 0);
@ -109,7 +109,7 @@ static int testStr(void)
n = tr_snprintf((char*)buf, sizeof(buf), "4:boat");
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
check_int(err, ==, 0);
check_uint_eq(4, len);
check_uint(len, ==, 4);
check(strncmp((char const*)str, "boat", len) == 0);
check(end == buf + 6);
str = NULL;
@ -119,7 +119,7 @@ static int testStr(void)
/* string goes past end of buffer */
err = tr_bencParseStr(buf, buf + (n - 1), &end, &str, &len);
check_int(err, ==, EILSEQ);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check(str == NULL);
check(end == NULL);
check(len == 0);
@ -128,7 +128,7 @@ static int testStr(void)
n = tr_snprintf((char*)buf, sizeof(buf), "0:");
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
check_int(err, ==, 0);
check_uint_eq(0, len);
check_uint(len, ==, 0);
check(*str == '\0');
check(end == buf + 2);
str = NULL;
@ -139,7 +139,7 @@ static int testStr(void)
n = tr_snprintf((char*)buf, sizeof(buf), "3:boat");
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
check_int(err, ==, 0);
check_uint_eq(3, len);
check_uint(len, ==, 3);
check(strncmp((char const*)str, "boa", len) == 0);
check(end == buf + 5);
str = NULL;
@ -174,7 +174,7 @@ static int testString(char const* str, bool isGood)
check(end == str + len);
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &savedLen);
check_str(saved, ==, str);
check_uint_eq(savedLen, len);
check_uint(savedLen, ==, len);
tr_free(saved);
tr_variantFree(&val);
}
@ -450,16 +450,16 @@ static int testMerge(void)
check(tr_variantDictFindInt(&dest, i4, &i));
check_int(i, ==, -35);
check(tr_variantDictFindStr(&dest, s5, &s, &len));
check_uint_eq(3, len);
check_uint(len, ==, 3);
check_str(s, ==, "abc");
check(tr_variantDictFindStr(&dest, s6, &s, &len));
check_uint_eq(3, len);
check_uint(len, ==, 3);
check_str(s, ==, "xyz");
check(tr_variantDictFindStr(&dest, s7, &s, &len));
check_uint_eq(9, len);
check_uint(len, ==, 9);
check_str(s, ==, "127.0.0.1");
check(tr_variantDictFindStr(&dest, s8, &s, &len));
check_uint_eq(3, len);
check_uint(len, ==, 3);
check_str(s, ==, "ghi");
tr_variantFree(&dest);
@ -569,7 +569,7 @@ static int testParse2(void)
check(tr_variantDictFindBool(&top, key_bool, &boolVal));
check(boolVal == true);
check(tr_variantDictFindStr(&top, key_str, &strVal, &strLen));
check_uint_eq(16, strLen);
check_uint(strLen, ==, 16);
check_str(strVal, ==, "this-is-a-string");
check(tr_variantDictFindReal(&top, key_real, &realVal));
check_int((int)(realVal * 100), ==, 50);