1
0
mirror of https://gitlab.xiph.org/xiph/icecast-server.git synced 2024-12-04 14:46:30 -05:00

Feature: Added tests for refobject

This commit is contained in:
Philipp Schafft 2018-07-27 14:35:26 +00:00
parent df9af991c5
commit f82df09d75
2 changed files with 155 additions and 2 deletions

View File

@ -7,7 +7,8 @@ TESTS = \
startup.test \
admin.test \
ctest_suite.test \
ctest_resourcematch.test
ctest_resourcematch.test \
ctest_refobject.test
EXTRA_DIST = $(TESTS)
@ -15,8 +16,11 @@ EXTRA_DIST += \
icecast.xml \
on-connect.sh
check_PROGRAMS = ctest_suite.test ctest_resourcematch.test
check_PROGRAMS = ctest_suite.test ctest_resourcematch.test ctest_refobject.test
noinst_HEADERS = ctest_lib.h
ctest_suite_test_SOURCES=ctest_suite.c ctest_lib.c
ctest_resourcematch_test_SOURCES=ctest_resourcematch.c ctest_lib.c ../src/resourcematch.c
ctest_refobject_test_SOURCES=ctest_refobject.c ctest_lib.c ../src/refobject.c
ctest_refobject_test_DEPENDENCIES = ../src/common/thread/libicethread.la ../src/common/avl/libiceavl.la
ctest_refobject_test_LDADD = $(ctest_refobject_test_DEPENDENCIES)

149
tests/ctest_refobject.c Normal file
View File

@ -0,0 +1,149 @@
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "ctest_lib.h"
#include "../src/refobject.h"
static void test_ptr(void)
{
refobject_t a;
a = REFOBJECT_NULL;
ctest_test("NULL is NULL", REFOBJECT_IS_NULL(a));
if (!REFOBJECT_IS_NULL(a))
ctest_bailed_out();
}
static void test_create_ref_unref(void)
{
refobject_t a;
a = refobject_new(sizeof(refobject_base_t), NULL, NULL, NULL, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
ctest_test("referenced", refobject_ref(a) == 0);
ctest_test("un-referenced (1 of 2)", refobject_unref(a) == 0);
ctest_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
}
static void test_name(void)
{
refobject_t a;
const char *name = "test object name";
const char *ret;
a = refobject_new(sizeof(refobject_base_t), NULL, NULL, name, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
ret = refobject_get_name(a);
ctest_test("get name", ret != NULL);
ctest_test("name match", strcmp(name, ret) == 0);
ctest_test("un-referenced", refobject_unref(a) == 0);
}
static void test_userdata(void)
{
refobject_t a;
int tmp = 0;
void *userdata = &tmp;
void *ret;
a = refobject_new(sizeof(refobject_base_t), NULL, NULL, NULL, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
ret = refobject_get_userdata(a);
ctest_test("get userdata", ret == NULL);
ctest_test("set userdata", refobject_set_userdata(a, userdata) == 0);
ret = refobject_get_userdata(a);
ctest_test("get userdata", ret == userdata);
ctest_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
ret = refobject_get_userdata(a);
ctest_test("get userdata", ret == NULL);
ctest_test("un-referenced", refobject_unref(a) == 0);
a = refobject_new(sizeof(refobject_base_t), NULL, userdata, NULL, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
ret = refobject_get_userdata(a);
ctest_test("get userdata", ret == userdata);
ctest_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
ret = refobject_get_userdata(a);
ctest_test("get userdata", ret == NULL);
ctest_test("un-referenced", refobject_unref(a) == 0);
}
static void test_associated(void)
{
refobject_t a, b;
a = refobject_new(sizeof(refobject_base_t), NULL, NULL, NULL, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
b = refobject_new(sizeof(refobject_base_t), NULL, NULL, NULL, a);
ctest_test("refobject created with associated", !REFOBJECT_IS_NULL(b));
ctest_test("un-referenced (1 of 2)", refobject_unref(b) == 0);
ctest_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
}
static void test_freecb(void)
{
refobject_t a;
size_t called = 0;
void freecb(refobject_t self, void **userdata)
{
called++;
}
a = refobject_new(sizeof(refobject_base_t), freecb, NULL, NULL, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
ctest_test("un-referenced", refobject_unref(a) == 0);
ctest_test("freecb called", called == 1);
called = 0;
a = refobject_new(sizeof(refobject_base_t), freecb, NULL, NULL, REFOBJECT_NULL);
ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
ctest_test("referenced", refobject_ref(a) == 0);
ctest_test("freecb uncalled", called == 0);
ctest_test("un-referenced (1 of 2)", refobject_unref(a) == 0);
ctest_test("freecb uncalled", called == 0);
ctest_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
ctest_test("freecb called", called == 1);
}
int main (void)
{
ctest_init();
test_ptr();
if (ctest_bailed_out()) {
ctest_fin();
return 1;
}
test_create_ref_unref();
test_name();
test_userdata();
test_associated();
test_freecb();
ctest_fin();
return 0;
}