Skip to content

Commit

Permalink
code style update & C99 compatibility for unit tests (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolkykim authored Oct 28, 2022
1 parent eb693d4 commit 9c0042c
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 259 deletions.
20 changes: 10 additions & 10 deletions tests/test_qhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ QUNIT_START("Test qhash.c");

TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 0)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 0, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 0, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -43,7 +43,7 @@ TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 0)") {

TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 1)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 1, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 1, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -53,7 +53,7 @@ TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 1)") {

TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 2)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 2, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 2, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -63,7 +63,7 @@ TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 2)") {

TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 3)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 3, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_1.bin", 0, 3, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -73,7 +73,7 @@ TEST("qhashmd5_file(test_qhash_data_1.bin, 0, 3)") {

TEST("qhashmd5_file(test_qhash_data_1.bin, 1, 2)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_1.bin", 1, 2, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_1.bin", 1, 2, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -83,7 +83,7 @@ TEST("qhashmd5_file(test_qhash_data_1.bin, 1, 2)") {

TEST("qhashmd5_file(test_qhash_data_1.bin, 2, 3)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_1.bin", 2, 3, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_1.bin", 2, 3, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -93,18 +93,18 @@ TEST("qhashmd5_file(test_qhash_data_1.bin, 2, 3)") {

TEST("qhashmd5_file(test_qhash_data_2.bin, 0, 0)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_2.bin", 0, 0, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_2.bin", 0, 0, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
ASSERT_EQUAL_STR(hash, "8d03ad1bae270828874995868fa74476");
ASSERT_EQUAL_MEM(hash, "8d03ad1bae270828874995868fa74476", 16);
free(hash);
}

/* test_qhash_data_3.bin is (32*1024) bytes to test for loop edge case */
TEST("qhashmd5_file(test_qhash_data_3.bin, 0, 0)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_3.bin", 0, 0, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_3.bin", 0, 0, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand All @@ -115,7 +115,7 @@ TEST("qhashmd5_file(test_qhash_data_3.bin, 0, 0)") {
/* test_qhash_data_4.bin is (32*1024)+1 bytes to test for loop edge case */
TEST("qhashmd5_file(test_qhash_data_4.bin, 0, 0)") {
unsigned char digest[16];
bool success = qhashmd5_file("test_qhash_data_4.bin", 0, 0, &digest[0]);
bool success = qhashmd5_file("test_qhash_data_4.bin", 0, 0, digest);
ASSERT_EQUAL_BOOL(true, success);
char *hash = qhex_encode(digest, 16);
ASSERT_NOT_NULL(hash);
Expand Down
1 change: 0 additions & 1 deletion tests/test_qhasharr.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ TEST("Test remove_by_idx()") {

QUNIT_END();


void test_thousands_of_keys(size_t memsize, int num_keys, char *key_postfix, char *value_postfix) {
char memory[memsize];
qhasharr_t *tbl = qhasharr(memory, sizeof(memory));
Expand Down
18 changes: 6 additions & 12 deletions tests/test_qhasharr_darkdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ QUNIT_START("Test qhasharr.c by darkdh");
qhasharr_t *tbl;
char *memory;

TEST("qhasharr()")
{
TEST("qhasharr()") {
int memsize = qhasharr_calculate_memsize(5);
memory = (char*) malloc(memsize * sizeof(char));
tbl = qhasharr(memory, memsize);
Expand Down Expand Up @@ -69,8 +68,7 @@ for (i = 0; i < EXTRA_NUM; i++) {
extra_valuesize[i] = strlen(extra_value[i]);
}

TEST("put() to full")
{
TEST("put() to full") {
size_t size;
for (i = 0; i < TARGET_NUM; i++) {
ASSERT_TRUE(tbl->put(tbl, key[i], value[i], valuesize[i]));
Expand All @@ -86,15 +84,13 @@ TEST("put() to full")
ASSERT_EQUAL_INT(tbl->size(tbl, NULL, NULL), TARGET_NUM);
}

TEST("get() non-exist key")
{
TEST("get() non-exist key") {
size_t size;
char *target_got = (char*) tbl->get(tbl, extra_key[0], &size);
ASSERT_NULL(target_got);
}

TEST("remove() to empty")
{
TEST("remove() to empty") {
for (i = 0; i < TARGET_NUM; i++) {
ASSERT_TRUE(tbl->remove(tbl, key[i]));
}
Expand All @@ -103,8 +99,7 @@ TEST("remove() to empty")
ASSERT_EQUAL_INT(tbl->size(tbl, NULL, NULL), 0);
}

TEST("add() same key")
{
TEST("add() same key") {
size_t size;
ASSERT_TRUE(tbl->put(tbl, key[0], value[0], valuesize[0]));
ASSERT_TRUE(tbl->put(tbl, key[0], value[1], valuesize[1]));
Expand All @@ -115,8 +110,7 @@ TEST("add() same key")
ASSERT_TRUE(tbl->remove(tbl, key[0]));
}

TEST("getnext()")
{
TEST("getnext()") {
size_t size;
for (i = 0; i < TARGET_NUM; i++) {
ASSERT_TRUE(tbl->put(tbl, key[i], value[i], valuesize[i]));
Expand Down
39 changes: 20 additions & 19 deletions tests/test_qhashtbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "qunit.h"
#include "qlibc.h"

void test_thousands_of_keys(int num_keys, char *key_postfix, char *value_postfix);

QUNIT_START("Test qhashtbl.c");

TEST("Test basic but complete") {
Expand Down Expand Up @@ -63,6 +65,24 @@ TEST("Test basic but complete") {
tbl->free(tbl);
}

TEST("Test thousands of keys insertion and removal: short key + short value") {
test_thousands_of_keys(10000, "", "");
}

TEST("Test thousands of keys insertion and removal: short key + long value") {
test_thousands_of_keys(10000, "", "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

TEST("Test thousands of keys insertion and removal: long key + short value") {
test_thousands_of_keys(10000, "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866", "");
}

TEST("Test thousands of keys insertion and removal: long key + long value") {
test_thousands_of_keys(10000, "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866", "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

QUNIT_END();

void test_thousands_of_keys(int num_keys, char *key_postfix, char *value_postfix) {
qhashtbl_t *tbl = qhashtbl(0, 0);
ASSERT_EQUAL_INT(0, tbl->size(tbl));
Expand Down Expand Up @@ -91,22 +111,3 @@ void test_thousands_of_keys(int num_keys, char *key_postfix, char *value_postfix
ASSERT_EQUAL_INT(0, tbl->size(tbl));
tbl->free(tbl);
}


TEST("Test thousands of keys insertion and removal: short key + short value") {
test_thousands_of_keys(10000, "", "");
}

TEST("Test thousands of keys insertion and removal: short key + long value") {
test_thousands_of_keys(10000, "", "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

TEST("Test thousands of keys insertion and removal: long key + short value") {
test_thousands_of_keys(10000, "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866", "");
}

TEST("Test thousands of keys insertion and removal: long key + long value") {
test_thousands_of_keys(10000, "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866", "1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

QUNIT_END();
77 changes: 36 additions & 41 deletions tests/test_qlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
#include "qunit.h"
#include "qlibc.h"

void test_thousands_of_values(int num_values, char *prefix, char *postfix);

QUNIT_START("Test qlist.c");

TEST("Test basic features")
{
TEST("Test basic features") {
const char *values[] =
{ "value1",
"value2_long_value-fef6bd00f77aef990a6d62969fee0cb904d052665a1dcf10492156124fafc59769e91d1a06ec1215e435e29ef43de177f6f2a5e035860e702c82e08084950313",
Expand Down Expand Up @@ -80,8 +81,7 @@ TEST("Test basic features")
free(data);
}

TEST("Test boundary conditions")
{
TEST("Test boundary conditions") {
const char *values[] = { "value0" };

/*test when list is empty*/
Expand Down Expand Up @@ -179,6 +179,33 @@ TEST("Test boundary conditions")
list->free(list);
}

TEST("Test thousands of values: without prefix and postfix") {
test_thousands_of_values(10000, "", "");
}

TEST("Test thousands of values: with prefix and without postfix") {
test_thousands_of_values(
10000,
"1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866",
"");
}

TEST("Test thousands of values: without prefix and with postfix") {
test_thousands_of_values(
10000,
"",
"1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

TEST("Test thousands of values: with prefix and postfix") {
test_thousands_of_values(
10000,
"1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866",
"1a087a6982371bbfc9d4e14ae 76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

QUNIT_END();

void test_thousands_of_values(int num_values, char *prefix, char *postfix) {
qlist_t *list = qlist(0);

Expand All @@ -194,7 +221,7 @@ void test_thousands_of_values(int num_values, char *prefix, char *postfix) {

list->addlast(list, value, dsize);
ASSERT_EQUAL_INT(i + 1, list->size(list));

datasize += dsize;
strsize += ssize;
free(value);
Expand All @@ -213,8 +240,8 @@ void test_thousands_of_values(int num_values, char *prefix, char *postfix) {
free(obj.data);
i++;
}
/*test reverse()*/

/* test reverse() */
list->reverse(list);
i = num_values - 1;
memset((void *) &obj, 0, sizeof(obj));
Expand All @@ -226,7 +253,7 @@ void test_thousands_of_values(int num_values, char *prefix, char *postfix) {
i--;
}

/*test toarray() and tostring()*/
/* test toarray() and tostring() */
list->reverse(list);
void *all_data = malloc(datasize);
char *all_str = malloc(strsize + 1);
Expand All @@ -247,7 +274,7 @@ void test_thousands_of_values(int num_values, char *prefix, char *postfix) {
free(value);
i++;
}

void *to_array = list->toarray(list, NULL);
char *to_string = list->tostring(list);

Expand All @@ -262,35 +289,3 @@ void test_thousands_of_values(int num_values, char *prefix, char *postfix) {
list->clear(list);
list->free(list);
}

TEST("Test thousands of values: without prefix and postfix")
{
test_thousands_of_values(10000, "", "");
}

TEST("Test thousands of values: with prefix and without postfix")
{
test_thousands_of_values(
10000,
"1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866",
"");
}

TEST("Test thousands of values: without prefix and with postfix")
{
test_thousands_of_values(
10000,
"",
"1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

TEST("Test thousands of values: with prefix and postfix")
{
test_thousands_of_values(
10000,
"1a087a6982371bbfc9d4e14ae76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866",
"1a087a6982371bbfc9d4e14ae 76e05ddd784a5d9c6b0fc9e6cd715baab66b90987b2ee054764e58fc04e449dfa060a68398601b64cf470cb6f0a260ec6539866");
}

QUNIT_END();

Loading

0 comments on commit 9c0042c

Please sign in to comment.