Skip to content

Commit

Permalink
test: add
Browse files Browse the repository at this point in the history
  • Loading branch information
flatcap committed Jun 18, 2020
1 parent 89af578 commit 3bb2c4e
Showing 20 changed files with 358 additions and 7 deletions.
8 changes: 4 additions & 4 deletions mutt/hash.c
Original file line number Diff line number Diff line change
@@ -132,7 +132,7 @@ static struct HashElem *union_hash_insert(struct HashTable *table,
union HashKey key, int type, void *data)
{
if (!table)
return NULL;
return NULL; // LCOV_EXCL_LINE

struct HashElem *he = mutt_mem_calloc(1, sizeof(struct HashElem));
size_t hash = table->gen_hash(key, table->num_elems);
@@ -178,7 +178,7 @@ static struct HashElem *union_hash_insert(struct HashTable *table,
static struct HashElem *union_hash_find_elem(const struct HashTable *table, union HashKey key)
{
if (!table)
return NULL;
return NULL; // LCOV_EXCL_LINE

size_t hash = table->gen_hash(key, table->num_elems);
struct HashElem *he = table->table[hash];
@@ -199,7 +199,7 @@ static struct HashElem *union_hash_find_elem(const struct HashTable *table, unio
static void *union_hash_find(const struct HashTable *table, union HashKey key)
{
if (!table)
return NULL;
return NULL; // LCOV_EXCL_LINE
struct HashElem *he = union_hash_find_elem(table, key);
if (he)
return he->data;
@@ -215,7 +215,7 @@ static void *union_hash_find(const struct HashTable *table, union HashKey key)
static void union_hash_delete(struct HashTable *table, union HashKey key, const void *data)
{
if (!table)
return;
return; // LCOV_EXCL_LINE

size_t hash = table->gen_hash(key, table->num_elems);
struct HashElem *he = table->table[hash];
10 changes: 8 additions & 2 deletions test/Makefile.autosetup
Original file line number Diff line number Diff line change
@@ -29,7 +29,8 @@ ADDRESS_OBJS = test/address/mutt_addr_cat.o \
test/address/mutt_addrlist_search.o \
test/address/mutt_addrlist_to_intl.o \
test/address/mutt_addrlist_to_local.o \
test/address/mutt_addrlist_write.o
test/address/mutt_addrlist_write.o \
test/address/mutt_addrlist_write_list.o

ATTACH_OBJS = test/attach/mutt_actx_add_attach.o \
test/attach/mutt_actx_add_body.o \
@@ -295,7 +296,8 @@ MAILBOX_OBJS = test/mailbox/mailbox_changed.o \
test/mailbox/mailbox_update.o

MAPPING_OBJS = test/mapping/mutt_map_get_name.o \
test/mapping/mutt_map_get_value.o
test/mapping/mutt_map_get_value.o \
test/mapping/mutt_map_get_value_n.o

MBYTE_OBJS = test/mbyte/mutt_mb_charlen.o \
test/mbyte/mutt_mb_filter_unprintable.o \
@@ -387,6 +389,9 @@ POOL_OBJS = test/pool/mutt_buffer_pool_free.o \
test/pool/mutt_buffer_pool_get.o \
test/pool/mutt_buffer_pool_release.o

PREX_OBJS = test/prex/mutt_prex_capture.o \
test/prex/mutt_prex_free.o

REGEX_OBJS = test/regex/mutt_regex_capture.o \
test/regex/mutt_regex_compile.o \
test/regex/mutt_regex_free.o \
@@ -554,6 +559,7 @@ TEST_OBJS = test/main.o test/common.o \
$(PATH_OBJS) \
$(PATTERN_OBJS) \
$(POOL_OBJS) \
$(PREX_OBJS) \
$(REGEX_OBJS) \
$(RFC2047_OBJS) \
$(RFC2231_OBJS) \
30 changes: 30 additions & 0 deletions test/address/mutt_addrlist_write_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @file
* Test code for mutt_addrlist_write_list()
*
* @authors
* Copyright (C) 2020 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define TEST_NO_MAIN
#include "config.h"
#include "acutest.h"

void test_mutt_addrlist_write_list(void)
{
// size_t mutt_addrlist_write_list(const struct AddressList *al, struct ListHead *list);
}
15 changes: 15 additions & 0 deletions test/buffer/mutt_buffer_concatn_path.c
Original file line number Diff line number Diff line change
@@ -32,4 +32,19 @@ void test_mutt_buffer_concatn_path(void)
{
TEST_CHECK(mutt_buffer_concatn_path(NULL, NULL, 0, NULL, 0) == 0);
}

{
struct Buffer buf = mutt_buffer_make(0);

const char *dir = "/home/jim/work";
const char *file = "file.txt";
const char *result = "/home/jim/file";

size_t len = mutt_buffer_concatn_path(&buf, dir, 9, file, 4);

TEST_CHECK(len == 14);
TEST_CHECK(mutt_str_strcmp(mutt_b2s(&buf), result) == 0);

mutt_buffer_dealloc(&buf);
}
}
30 changes: 30 additions & 0 deletions test/buffer/mutt_buffer_copy.c
Original file line number Diff line number Diff line change
@@ -32,4 +32,34 @@ void test_mutt_buffer_copy(void)
{
TEST_CHECK(mutt_buffer_copy(NULL, NULL) == 0);
}

{
struct Buffer buf1 = mutt_buffer_make(0);
struct Buffer buf2 = mutt_buffer_make(0);

size_t len = mutt_buffer_copy(&buf2, &buf1);

TEST_CHECK(len == 0);
TEST_CHECK(mutt_buffer_is_empty(&buf2) == true);

mutt_buffer_dealloc(&buf1);
mutt_buffer_dealloc(&buf2);
}

{
char *src = "abcdefghij";

struct Buffer buf1 = mutt_buffer_make(32);
struct Buffer buf2 = mutt_buffer_make(0);

mutt_buffer_strcpy(&buf1, src);

size_t len = mutt_buffer_copy(&buf2, &buf1);

TEST_CHECK(len == 10);
TEST_CHECK(mutt_str_strcmp(mutt_b2s(&buf1), mutt_b2s(&buf2)) == 0);

mutt_buffer_dealloc(&buf1);
mutt_buffer_dealloc(&buf2);
}
}
18 changes: 18 additions & 0 deletions test/buffer/mutt_buffer_strdup.c
Original file line number Diff line number Diff line change
@@ -33,4 +33,22 @@ void test_mutt_buffer_strdup(void)
char *str = mutt_buffer_strdup(NULL);
TEST_CHECK(str == NULL);
}

{
char *src = "abcdefghij";
char *result = NULL;

struct Buffer buf = mutt_buffer_make(32);

mutt_buffer_strcpy(&buf, src);

result = mutt_buffer_strdup(&buf);

TEST_CHECK(result != NULL);
TEST_CHECK(strcmp(result, src) == 0);

FREE(&result);

mutt_buffer_dealloc(&buf);
}
}
14 changes: 14 additions & 0 deletions test/buffer/mutt_buffer_substrcpy.c
Original file line number Diff line number Diff line change
@@ -32,4 +32,18 @@ void test_mutt_buffer_substrcpy(void)
{
TEST_CHECK(mutt_buffer_substrcpy(NULL, NULL, NULL) == 0);
}

{
char *src = "abcdefghijklmnopqrstuvwxyz";
char *result = "jklmnopqr";

struct Buffer buf = mutt_buffer_make(32);

size_t len = mutt_buffer_substrcpy(&buf, src + 9, src + 18);

TEST_CHECK(len == 9);
TEST_CHECK(mutt_str_strcmp(mutt_b2s(&buf), result) == 0);

mutt_buffer_dealloc(&buf);
}
}
1 change: 1 addition & 0 deletions test/common.c
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ void test_init(void)
const char *path = get_test_dir();
bool success = false;

TEST_CASE("Common setup");
if (!TEST_CHECK(path != NULL))
{
TEST_MSG("Environment variable '%s' isn't set\n", TEST_DIR);
3 changes: 2 additions & 1 deletion test/date/mutt_date_make_time.c
Original file line number Diff line number Diff line change
@@ -53,7 +53,8 @@ void test_mutt_date_make_time(void)
{ { 0, 0, 24, 1, 0, 100, 0 }, TIME_T_MIN },
{ { 0, 0, 0, 0, 0, 100, 0 }, TIME_T_MIN },
{ { 0, 0, 0, 32, 0, 100, 0 }, TIME_T_MIN },
{ { 0, 0, 0, 1, 0, 10000, 0 }, TIME_T_MAX },
{ { 0, 0, 0, 1, 0, 10001, 0 }, TIME_T_MAX },
{ { 0, 0, 0, 1, 0, -10001, 0 }, TIME_T_MIN },
};
// clang-format on

13 changes: 13 additions & 0 deletions test/hash/mutt_hash_delete.c
Original file line number Diff line number Diff line change
@@ -29,6 +29,10 @@ void test_mutt_hash_delete(void)
{
// void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data);

int dummy1 = 42;
int dummy2 = 13;
int dummy3 = 99;

{
mutt_hash_delete(NULL, "apple", "banana");
TEST_CHECK_(1, "mutt_hash_delete(NULL, \"apple\", \"banana\")");
@@ -47,4 +51,13 @@ void test_mutt_hash_delete(void)
TEST_CHECK_(1, "mutt_hash_delete(table, \"apple\", NULL)");
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_new(128, MUTT_HASH_STRDUP_KEYS);
mutt_hash_insert(table, "apple", &dummy1);
mutt_hash_insert(table, "banana", &dummy2);
mutt_hash_insert(table, "cherry", &dummy3);
mutt_hash_delete(table, "banana", NULL);
mutt_hash_free(&table);
}
}
13 changes: 13 additions & 0 deletions test/hash/mutt_hash_find.c
Original file line number Diff line number Diff line change
@@ -29,6 +29,10 @@ void test_mutt_hash_find(void)
{
// void *mutt_hash_find(const struct HashTable *table, const char *strkey);

int dummy1 = 42;
int dummy2 = 13;
int dummy3 = 99;

{
TEST_CHECK(!mutt_hash_find(NULL, "apple"));
}
@@ -38,4 +42,13 @@ void test_mutt_hash_find(void)
TEST_CHECK(!mutt_hash_find(table, "apple"));
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_new(10, MUTT_HASH_NO_FLAGS);
mutt_hash_insert(table, "apple", &dummy1);
mutt_hash_insert(table, "banana", &dummy2);
mutt_hash_insert(table, "cherry", &dummy3);
TEST_CHECK(mutt_hash_find(table, "apple") != NULL);
mutt_hash_free(&table);
}
}
15 changes: 15 additions & 0 deletions test/hash/mutt_hash_find_bucket.c
Original file line number Diff line number Diff line change
@@ -29,6 +29,10 @@ void test_mutt_hash_find_bucket(void)
{
// struct HashElem *mutt_hash_find_bucket(const struct HashTable *table, const char *strkey);

int dummy1 = 42;
int dummy2 = 13;
int dummy3 = 99;

{
TEST_CHECK(!mutt_hash_find_bucket(NULL, "apple"));
}
@@ -37,4 +41,15 @@ void test_mutt_hash_find_bucket(void)
struct HashTable table = { 0 };
TEST_CHECK(!mutt_hash_find_bucket(&table, NULL));
}

{
struct HashTable *table = mutt_hash_new(128, MUTT_HASH_ALLOW_DUPS);
mutt_hash_insert(table, "apple", &dummy1);
mutt_hash_insert(table, "banana", &dummy1);
mutt_hash_insert(table, "banana", &dummy2);
mutt_hash_insert(table, "banana", &dummy3);
mutt_hash_insert(table, "cherry", &dummy3);
mutt_hash_find_bucket(table, "banana");
mutt_hash_free(&table);
}
}
11 changes: 11 additions & 0 deletions test/hash/mutt_hash_int_find.c
Original file line number Diff line number Diff line change
@@ -33,4 +33,15 @@ void test_mutt_hash_int_find(void)
mutt_hash_int_find(NULL, 0);
TEST_CHECK_(1, "mutt_hash_int_find(NULL, 0)");
}

{
struct HashTable *table = mutt_hash_int_new(128, MUTT_HASH_NO_FLAGS);
mutt_hash_int_insert(table, 42, "apple");
mutt_hash_int_insert(table, 42, "banana");
mutt_hash_int_insert(table, 42 + 128, "cherry");
mutt_hash_int_insert(table, 20 + 128, "damson");
mutt_hash_int_insert(table, 20, "endive");
mutt_hash_int_find(table, 42);
mutt_hash_free(&table);
}
}
23 changes: 23 additions & 0 deletions test/hash/mutt_hash_int_new.c
Original file line number Diff line number Diff line change
@@ -28,4 +28,27 @@
void test_mutt_hash_int_new(void)
{
// struct HashTable *mutt_hash_int_new(size_t num_elems, HashFlags flags);

{
struct HashTable *table = mutt_hash_int_new(0, MUTT_HASH_NO_FLAGS);
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_int_new(128, MUTT_HASH_NO_FLAGS);
mutt_hash_int_insert(table, 42, "apple");
mutt_hash_int_insert(table, 42, "banana");
mutt_hash_int_insert(table, 42 + 128, "cherry");
mutt_hash_int_insert(table, 20 + 128, "damson");
mutt_hash_int_insert(table, 20, "endive");
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_int_new(128, MUTT_HASH_ALLOW_DUPS);
mutt_hash_int_insert(table, 42, "apple");
mutt_hash_int_insert(table, 42, "banana");
mutt_hash_int_insert(table, 42 + 128, "cherry");
mutt_hash_free(&table);
}
}
36 changes: 36 additions & 0 deletions test/hash/mutt_hash_new.c
Original file line number Diff line number Diff line change
@@ -28,4 +28,40 @@
void test_mutt_hash_new(void)
{
// struct HashTable *mutt_hash_new(size_t num_elems, HashFlags flags);

int dummy1 = 42;
int dummy2 = 13;
int dummy3 = 99;

{
struct HashTable *table = mutt_hash_new(0, MUTT_HASH_NO_FLAGS);
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_new(32, MUTT_HASH_STRCASECMP);
char buf[32];
for (size_t i = 0; i < 50; i++)
{
snprintf(buf, sizeof(buf), "apple%ld", i);
mutt_hash_insert(table, strdup(buf), &dummy1);
}
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_new(128, MUTT_HASH_STRDUP_KEYS);
mutt_hash_insert(table, "apple", &dummy1);
mutt_hash_insert(table, "banana", &dummy2);
mutt_hash_insert(table, "cherry", &dummy3);
mutt_hash_free(&table);
}

{
struct HashTable *table = mutt_hash_new(128, MUTT_HASH_ALLOW_DUPS);
mutt_hash_insert(table, "apple", &dummy1);
mutt_hash_insert(table, "apple", &dummy2);
mutt_hash_insert(table, "apple", &dummy3);
mutt_hash_free(&table);
}
}
Loading
Oops, something went wrong.

0 comments on commit 3bb2c4e

Please sign in to comment.