1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

Implement a CUnit-based runner for unit tests. Copy and adapt (actual unit)

tests from unit-tests/*/*_t.c (now under test/unit). The valgrind/pool test is
missing, since it's not really a unit test and probably not too valuable
either. Available via "make unit" (and if --enable-testing was passed to
configure, also executed by make check).
This commit is contained in:
Petr Rockai 2011-11-20 21:43:20 +00:00
parent bf75c30493
commit d4fed28523
8 changed files with 1293 additions and 1 deletions

View File

@ -79,7 +79,7 @@ all: cscope.out
endif
DISTCLEAN_TARGETS += cscope.out
check check_cluster check_local: all
check check_cluster check_local unit: all
$(MAKE) -C test $(@)
install_system_dirs:

View File

@ -1450,6 +1450,7 @@ scripts/lvm2_monitoring_systemd_red_hat.service
scripts/Makefile
test/Makefile
test/api/Makefile
test/unit/Makefile
tools/Makefile
udev/Makefile
unit-tests/datastruct/Makefile

View File

@ -43,7 +43,12 @@ endif
all: check
check: .tests-stamp
@echo Running API (liblvm2app) tests
make -C api tests
ifeq ("$(TESTING)", "yes")
@echo Running unit tests
make unit
endif
@echo Testing with locking_type 1
VERBOSE=$(VERBOSE) ./lib/harness $(RUN_BASE)
@echo Testing with locking_type 3
@ -111,6 +116,11 @@ clean:
make -C api clean
test "$(srcdir)" != . && rm -f $(RUN_BASE) lvm2app.sh
.PHONY: unit
unit:
make -C unit $(@)
CLEAN_TARGETS += .lib-dir-stamp .tests-stamp $(LIB) $(addprefix lib/,$(CMDS)) \
lib/clvmd lib/dmeventd lib/dmsetup lib/lvmetad lib/fsadm lib/vgimportclone

25
test/unit/Makefile.in Normal file
View File

@ -0,0 +1,25 @@
# Copyright (C) 2011 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
VPATH = @srcdir@
SOURCES = bitset_t.c matcher_t.c run.c
TARGETS = run
LDFLAGS = -ldevmapper -lcunit
include $(top_builddir)/make.tmpl
unit: run
LD_LIBRARY_PATH=$(top_builddir)/libdm ./run

134
test/unit/bitset_t.c Normal file
View File

@ -0,0 +1,134 @@
/*
* Copyright (C) 2010 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License v.2.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libdevmapper.h"
#include <CUnit/CUnit.h>
int bitset_init(void);
int bitset_fini(void);
enum {
NR_BITS = 137
};
static struct dm_pool *mem;
int bitset_init() {
mem = dm_pool_create("bitset test", 1024);
return mem == NULL;
}
int bitset_fini() {
dm_pool_destroy(mem);
return 0;
}
static void test_get_next()
{
int i, j, last, first;
dm_bitset_t bs = dm_bitset_create(mem, NR_BITS);
for (i = 0; i < NR_BITS; i++)
CU_ASSERT(!dm_bit(bs, i));
for (i = 0, j = 1; i < NR_BITS; i += j, j++)
dm_bit_set(bs, i);
first = 1;
for (i = 0, j = 1; i < NR_BITS; i += j, j++) {
if (first) {
last = dm_bit_get_first(bs);
first = 0;
} else
last = dm_bit_get_next(bs, last);
CU_ASSERT(last == i);
}
CU_ASSERT(dm_bit_get_next(bs, last) == -1);
}
static void bit_flip(dm_bitset_t bs, int bit)
{
int old = dm_bit(bs, bit);
if (old)
dm_bit_clear(bs, bit);
else
dm_bit_set(bs, bit);
}
static void test_equal()
{
dm_bitset_t bs1 = dm_bitset_create(mem, NR_BITS);
dm_bitset_t bs2 = dm_bitset_create(mem, NR_BITS);
int i, j;
for (i = 0, j = 1; i < NR_BITS; i += j, j++) {
dm_bit_set(bs1, i);
dm_bit_set(bs2, i);
}
CU_ASSERT(dm_bitset_equal(bs1, bs2));
CU_ASSERT(dm_bitset_equal(bs2, bs1));
for (i = 0; i < NR_BITS; i++) {
bit_flip(bs1, i);
CU_ASSERT(!dm_bitset_equal(bs1, bs2));
CU_ASSERT(!dm_bitset_equal(bs2, bs1));
CU_ASSERT(dm_bitset_equal(bs1, bs1)); /* comparing with self */
bit_flip(bs1, i);
}
}
static void test_and()
{
dm_bitset_t bs1 = dm_bitset_create(mem, NR_BITS);
dm_bitset_t bs2 = dm_bitset_create(mem, NR_BITS);
dm_bitset_t bs3 = dm_bitset_create(mem, NR_BITS);
int i, j;
for (i = 0, j = 1; i < NR_BITS; i += j, j++) {
dm_bit_set(bs1, i);
dm_bit_set(bs2, i);
}
dm_bit_and(bs3, bs1, bs2);
CU_ASSERT(dm_bitset_equal(bs1, bs2));
CU_ASSERT(dm_bitset_equal(bs1, bs3));
CU_ASSERT(dm_bitset_equal(bs2, bs3));
dm_bit_clear_all(bs1);
dm_bit_clear_all(bs2);
for (i = 0; i < NR_BITS; i++) {
if (i % 2)
dm_bit_set(bs1, i);
else
dm_bit_set(bs2, i);
}
dm_bit_and(bs3, bs1, bs2);
for (i = 0; i < NR_BITS; i++)
CU_ASSERT(!dm_bit(bs3, i));
}
CU_TestInfo bitset_list[] = {
{ (char*)"get_next", test_get_next },
{ (char*)"equal", test_equal },
{ (char*)"and", test_and },
CU_TEST_INFO_NULL
};

1013
test/unit/matcher_data.h Normal file

File diff suppressed because it is too large Load Diff

84
test/unit/matcher_t.c Normal file
View File

@ -0,0 +1,84 @@
/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License v.2.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libdevmapper.h"
#include "log.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <CUnit/CUnit.h>
#include "matcher_data.h"
int regex_init(void);
int regex_fini(void);
static struct dm_pool *mem = NULL;
int regex_init() {
mem = dm_pool_create("bitset test", 1024);
return mem == NULL;
}
int regex_fini() {
dm_pool_destroy(mem);
return 0;
}
static struct dm_regex *make_scanner(const char **rx)
{
int nrx = 0;
for (; rx[nrx]; ++nrx);
struct dm_regex *scanner = dm_regex_create(mem, rx, nrx);
CU_ASSERT_FATAL(scanner != NULL);
return scanner;
}
static void test_fingerprints() {
struct dm_regex *scanner;
scanner = make_scanner(dev_patterns);
CU_ASSERT_EQUAL(dm_regex_fingerprint(scanner), 0x352b6c4f);
scanner = make_scanner(random_patterns);
CU_ASSERT_EQUAL(dm_regex_fingerprint(scanner), 0xeed8ceb8);
}
static void test_matching() {
struct dm_regex *scanner;
int i;
scanner = make_scanner(dev_patterns);
for (i = 0; devices[i].str; ++i)
CU_ASSERT_EQUAL(dm_regex_match(scanner, devices[i].str), devices[i].expected - 1);
scanner = make_scanner(nonprint_patterns);
for (i = 0; nonprint[i].str; ++i)
CU_ASSERT_EQUAL(dm_regex_match(scanner, nonprint[i].str), nonprint[i].expected - 1);
}
CU_TestInfo regex_list[] = {
{ (char*)"fingerprints", test_fingerprints },
{ (char*)"matching", test_matching },
CU_TEST_INFO_NULL
};

25
test/unit/run.c Normal file
View File

@ -0,0 +1,25 @@
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#define DECL(n) \
extern CU_TestInfo n ## _list[]; \
int n ## _init(); \
int n ## _fini();
#define USE(n) { (char*) #n, n##_init, n##_fini, n##_list }
DECL(bitset);
DECL(regex);
CU_SuiteInfo suites[] = {
USE(bitset),
USE(regex),
CU_SUITE_INFO_NULL
};
int main() {
CU_initialize_registry();
CU_register_suites(suites);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
return CU_get_number_of_failures() != 0;
}