📄 su_alloc_test.c
字号:
/* * This file is part of the Sofia-SIP package * * Copyright (C) 2005 Nokia Corporation. * * Contact: Pekka Pessi <pekka.pessi@nokia.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * *//**@ingroup su_alloc * * @file su_alloc_test.c * * Testing functions for su_alloc functions. * * @author Pekka Pessi <Pekka.Pessi@nokia.com> * * @date Created: Thu May 2 18:17:46 2002 ppessi */#include "config.h"#include <stdio.h>#include <string.h>#include <sofia-sip/su_alloc.h>#include <sofia-sip/su_strlst.h>#include <sofia-sip/su_alloc_stat.h>#define TSTFLAGS tstflags#include <sofia-sip/tstdef.h>int tstflags;char const *name = "su_alloc_test";static int test_alloc(void);static int test_strdupcat(void);static int test_strlst(void);static int test_vectors(void);static int test_auto(void);/* Type derived from home */typedef struct { su_home_t home[1]; int *p; } exhome_t;void exdestructor(void *arg){ exhome_t *ex = arg; (*ex->p)++;}/** Test basic memory home operations */int test_alloc(void){ exhome_t *h0, *h1, *h2, *h3; su_home_t home[1] = { SU_HOME_INIT(home) }; enum { N = 40 }; void *m0[N], *m1[N], *m; char *c, *c0, *p0, *p1; int i; int d0, d1, d2, d3; BEGIN(); TEST_1(h0 = su_home_new(sizeof(*h0))); TEST_1(h1 = su_home_clone(h0->home, sizeof(*h1))); d0 = d1 = d2 = d3 = 0; h0->p = &d0; h1->p = &d1; TEST(su_home_desctructor(h0->home, exdestructor), 0); TEST(su_home_desctructor(h1->home, exdestructor), 0); TEST_1(h2 = su_home_ref(h0->home)); su_home_unref(h0->home); TEST(d0, 0); su_home_unref(h2->home); /* Should call destructor of cloned home, too */ TEST(d0, 1); TEST(d1, 1); TEST_1(h0 = su_home_new(sizeof(*h0))); TEST_1(h1 = su_home_clone(h0->home, sizeof(*h1))); TEST_1(h2 = su_home_clone(h1->home, sizeof(*h2))); TEST_1(h3 = su_home_clone(h2->home, sizeof(*h3))); TEST(su_home_threadsafe(h0->home), 0); for (i = 0; i < N; i++) { TEST_1(m0[i] = su_zalloc(h3->home, 20)); TEST_1(m1[i] = su_zalloc(h2->home, 20)); } TEST_1(m = su_zalloc(h2->home, 20)); TEST(su_home_move(home, NULL), 0); TEST(su_home_move(NULL, home), 0); TEST(su_home_move(home, h3->home), 0); TEST(su_home_move(h2->home, h3->home), 0); TEST(su_home_move(h1->home, h2->home), 0); su_home_preload(home, 1, 1024 + 2 * 8); TEST_1(c = su_zalloc(home, 64)); p0 = c; p1 = c + 1024; TEST(c = su_realloc(home, c0 = c, 127), c0); TEST_1(c = c0 = su_zalloc(home, 1024 - 128)); TEST_1(p0 <= c); TEST_1(c < p1); TEST(c = su_realloc(home, c, 128), c0); TEST(c = su_realloc(home, c, 1023 - 128), c0); TEST(c = su_realloc(home, c, 1024 - 128), c0); TEST_1(c = su_realloc(home, c, 1024)); TEST_1(c = su_realloc(home, c, 2 * 1024)); TEST(c = su_realloc(home, p0, 126), p0); TEST_1(c = su_realloc(home, p0, 1024)); TEST(c = su_realloc(home, c, 0), NULL); su_home_check(home); su_home_deinit(home); su_home_check(h2->home); su_home_zap(h2->home); su_home_check(h0->home); su_home_zap(h0->home); END();}static int test_strdupcat(void){ su_home_t home[1] = { SU_HOME_INIT(home) }; BEGIN(); TEST_S(su_strdup(home, "foo"), "foo"); TEST_S(su_strcat(home, "foo", "bar"), "foobar"); TEST_S(su_strndup(home, "foobar", 3), "foo"); su_home_deinit(home); END();}#include <stdarg.h>static int test_sprintf(char const *fmt, ...){ BEGIN(); su_home_t home[1] = { SU_HOME_INIT(home) }; va_list va; TEST_S(su_sprintf(home, "foo%s", "bar"), "foobar"); va_start(va, fmt); TEST_S(su_vsprintf(home, fmt, va), "foo.bar"); TEST_S(su_sprintf(home, "foo%200s", "bar"), "foo " " " " " " bar"); su_home_deinit(home); END();}static int test_strlst(void){ su_home_t home[1] = { SU_HOME_INIT(home) }; su_strlst_t *l, *l1, *l2; char *s; char foo[] = "foo"; char bar[] = "bar"; char baz[] = "baz"; su_home_stat_t parent[1], kids[2]; BEGIN(); parent->hs_size = (sizeof parent); kids[0].hs_size = (sizeof kids[0]); kids[1].hs_size = (sizeof kids[1]); su_home_init_stats(home); /* Test API for invalid arguments */ TEST_1(l = su_strlst_create(NULL)); TEST_1(l2 = su_strlst_dup(home, l)); TEST_VOID(su_strlst_destroy(l2)); TEST_1(!su_strlst_dup(home, NULL)); TEST_1(l1 = su_strlst_copy(home, l)); TEST_VOID(su_strlst_destroy(l1)); TEST_1(!su_strlst_copy(home, NULL)); TEST_VOID(su_strlst_destroy(NULL)); TEST_VOID(su_strlst_destroy(l)); TEST_1(!su_strlst_dup_append(NULL, "aa")); TEST_1(!su_strlst_append(NULL, "bee")); TEST_1(!su_strlst_item(NULL, 1)); TEST_1(!su_strlst_set_item(NULL, 1, "cee")); TEST_1(!su_strlst_remove(NULL, 1)); TEST_S(s = su_strlst_join(NULL, home, "a"), ""); TEST_VOID(su_free(home, s)); TEST_1(!su_strlst_split(home, NULL, ".")); TEST_1(s = su_strdup(home, "aaa")); TEST_1(l = su_strlst_split(home, s, NULL)); TEST_S(su_strlst_item(l, 0), "aaa"); TEST_VOID(su_strlst_destroy(l)); TEST_VOID(su_free(home, s)); TEST_1(!su_strlst_dup_split(home, NULL, ".")); TEST_1(l1 = su_strlst_dup_split(home, "aaa", "")); TEST_S(su_strlst_item(l1, 0), "aaa"); TEST_VOID(su_strlst_destroy(l1)); TEST(su_strlst_len(NULL), 0); TEST_1(!su_strlst_get_array(NULL)); TEST_VOID(su_strlst_free_array(NULL, NULL)); TEST_1(l = su_strlst_create(home)); TEST_VOID(su_strlst_free_array(l, NULL)); TEST_S(su_strlst_dup_append(l, "oh"), "oh"); TEST_VOID(su_strlst_free_array(l, NULL)); TEST_VOID(su_strlst_destroy(l)); /* Test functionality */ TEST_1(l = su_strlst_create(home)); su_home_init_stats(su_strlst_home(l)); TEST_S(su_strlst_join(l, home, "bar"), ""); TEST_S(su_strlst_append(l, foo), "foo"); TEST_S(su_strlst_dup_append(l, bar), "bar"); TEST_S(su_strlst_append(l, baz), "baz"); TEST_S((s = su_strlst_join(l, home, "!")), "foo!bar!baz"); TEST_S(su_strlst_item(l, 0), foo); TEST_S(su_strlst_item(l, 1), bar); TEST_S(su_strlst_item(l, 2), baz); TEST(su_strlst_item(l, 3), NULL); TEST(su_strlst_item(l, (unsigned)-1), NULL); TEST_1(l1 = su_strlst_copy(su_strlst_home(l), l)); TEST_1(l2 = su_strlst_dup(su_strlst_home(l), l)); strcpy(foo, "hum"); strcpy(bar, "pah"); strcpy(baz, "hah"); TEST_S(su_strlst_dup_append(l1, "kuik"), "kuik"); TEST_S(su_strlst_dup_append(l2, "uik"), "uik"); TEST_S((s = su_strlst_join(l, home, ".")), "hum.bar.hah"); TEST_S((su_strlst_join(l1, home, ".")), "hum.bar.hah.kuik"); TEST_S((su_strlst_join(l2, home, ".")), "foo.bar.baz.uik"); su_strlst_destroy(l2); su_home_get_stats(su_strlst_home(l), 0, kids, sizeof kids); TEST(kids->hs_clones, 2); TEST(kids->hs_allocs.hsa_number, 3); TEST(kids->hs_frees.hsf_number, 1); su_strlst_destroy(l); TEST_S(s, "hum.bar.hah"); TEST_1(l = su_strlst_create(home)); su_home_init_stats(su_strlst_home(l)); TEST_S(su_strlst_join(l, home, "bar"), ""); TEST_S(su_strlst_append(l, "a"), "a"); TEST_S(su_strlst_append(l, "b"), "b"); TEST_S(su_strlst_append(l, "c"), "c"); TEST_S(su_strlst_append(l, "d"), "d"); TEST_S(su_strlst_append(l, "e"), "e"); TEST_S(su_strlst_append(l, "f"), "f"); TEST_S(su_strlst_append(l, "g"), "g"); TEST_S(su_strlst_append(l, "h"), "h"); TEST_S(su_strlst_append(l, "i"), "i"); TEST_S(su_strlst_append(l, "j"), "j");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -