示例#1
0
static void a_filter_test(void) {
	printsln((String)__func__);
	Array ac, ac2, ex;
	IntPair ip;

	ac = a_create(3, sizeof(IntPair));
	ip = make_int_pair(1, 10);
	a_set(ac, 0, &ip);
	ip = make_int_pair(2, 20);
	a_set(ac, 1, &ip);
	ip = make_int_pair(3, 30);
	a_set(ac, 2, &ip);
	
	ac2 = a_filter(ac, a_eq2, NULL);
	
	ex = a_create(1, sizeof(IntPair));
	ip = make_int_pair(2, 20);
	a_set(ex, 0, &ip);
	
	a_check_expect(ac2, ex);
	
	a_free(ac);
	a_free(ac2);
	a_free(ex);
}
static void a_copy_test(void) {
	printsln((String)__func__);
	Array array, copy;

	array = sa_of_string("-1, -2, -3");
	copy = a_copy(array);
	sa_check_expect(copy, array);
	sa_free(array);
	a_free(copy);

	array = sa_of_string("10");
	copy = a_copy(array);
	sa_check_expect(copy, array);
	sa_free(array);
	a_free(copy);

	array = sa_of_string("100, -200");
	copy = a_copy(array);
	sa_check_expect(copy, array);
	sa_free(array);
	a_free(copy);

	array = sa_of_string("");
	copy = a_copy(array);
	sa_check_expect(copy, array);
	sa_free(array);
	a_free(copy);
}
static void sa_reverse_test(void) {
	printsln((String)__func__);
	Array ac, ex;

	ac = sa_of_string("1, 2, 3");
	ex = sa_of_string("3, 2, 1");
	a_reverse(ac);
	sa_check_expect(ac, ex);
	sa_free(ac);
	sa_free(ex);

	ac = sa_of_string("1, 2, 3, 4");
	ex = sa_of_string("4, 3, 2, 1");
	a_reverse(ac);
	sa_check_expect(ac, ex);
	sa_free(ac);
	sa_free(ex);

	ac = sa_create(0, "");
	ex = sa_create(0, "");
	a_reverse(ac);
	sa_check_expect(ac, ex);
	a_free(ac);
	a_free(ex);

	ac = sa_create(0, "x");
	ex = sa_create(0, "x");
	a_reverse(ac);
	sa_check_expect(ac, ex);
	a_free(ac);
	a_free(ex);
}
示例#4
0
static void a_map2_test(void) {
	printsln((String)__func__);
	Array a1, a2, ac, ex;
	IntPair ip;
	
	a1 = ia_of_string("1, 2, 3");
	a2 = ia_of_string("10, 20, 30");
	
	ac = a_map2(a1, a2, pair_i_j, sizeof(IntPair), NULL);
	a_free(a1);
	a_free(a2);

	ex = a_create(3, sizeof(IntPair));
	ip = make_int_pair(1, 10);
	a_set(ex, 0, &ip);
	ip = make_int_pair(2, 20);
	a_set(ex, 1, &ip);
	ip = make_int_pair(3, 30);
	a_set(ex, 2, &ip);

	a_check_expect(ac, ex);
	
	a_free(ac);
	a_free(ex);
}
示例#5
0
static void a_each_test(void) {
	printsln((String)__func__);
	Array ac, ex;
	IntPair ip;

	ac = a_create(3, sizeof(IntPair));
	ip = make_int_pair(1, 10);
	a_set(ac, 0, &ip);
	ip = make_int_pair(2, 20);
	a_set(ac, 1, &ip);
	ip = make_int_pair(3, 30);
	a_set(ac, 2, &ip);
	// a_println(ac, print_int_pair);
	
	a_each(ac, a_inc_in_place, NULL);
	// a_println(acm, print_int_pair);

	ex = a_create(3, sizeof(IntPair));
	ip = make_int_pair(2, 11);
	a_set(ex, 0, &ip);
	ip = make_int_pair(3, 21);
	a_set(ex, 1, &ip);
	ip = make_int_pair(4, 31);
	a_set(ex, 2, &ip);
	
	a_check_expect(ac, ex);
	
	a_free(ac);
	a_free(ex);
}
示例#6
0
void free_gc()
{
	gc_node* gc_p= alex_gc.gc_head;
	gc_node* gc_b = gc_p;

	while(gc_p)
	{
		gc_b = gc_p->next;
		switch(gc_p->gc_value.sg_t)
		{
		case sym_type_string:
			gc_del_str(gc_p->gc_value.sg_v.str.s_ptr);
			free_string(&gc_p->gc_value.sg_v.str);
			a_free(gc_p);
			break;
		case sym_type_al:
			gc_del_al(gc_p->gc_value.sg_v.al, 1);
			a_free(gc_p);
			break;
		}
		gc_p = gc_b;
	}
	
	memset(&alex_gc, 0, sizeof(alex_gc));
}
示例#7
0
void ia_test_all(void) {
	ia_create_test();
	ia_range_test();
	ia_of_string_test();
	ia_fn_test();
	ia_of_da_test();
	ia_contains_test();
	ia_fill_test();
	ia_fill_from_to_test();
	ia_index_test();
	ia_index_from_test();
	ia_index_fn_test();
	ia_last_index_test();
	ia_last_index_from_test();
	ia_last_index_fn_test();
	ia_sort_test();
	ia_sort_dec_test();
	ia_insert_test();
	ia_remove_test();
	ia_each_test();
	ia_each_state_test();
	ia_foldl_test();
	ia_foldr_test();
	ia_filter_test();
	// ia_filter_state_test();
	ia_choose_test();
	ia_exists_test();
	ia_forall_test();
	
#if 0
	Array a = ia_fn(20, ia_rnd, 5);
	ia_println(a);

	// get number of different numbers in array
	ia_sort(a);
	ia_println(a);
	int n = a_length(a);
	int k = (n > 0) ? 1 : 0;
	for (int i = 1; i < n; i++) {
		if (ia_get(a, i-1) != ia_get(a, i)) {
			k++;
		}
	}
	printiln(k);
	a_free(a);
#endif

#if 0
	// timing
	int n = 2000000000;
	Array a = ia_create(n, 0);
	time_function(   ia_fill_fn(a, fn_id)   );
	a_free(a);
	a = ia_create(n, 0);
	time_function(   ia_id(a)   );
	a_free(a);
#endif
	
}
示例#8
0
文件: vstring.c 项目: saucjedi/nagi
void vstring_free(VSTRING *vs)
{
	if (vs != 0)
	{
		if (vs->data != 0)
			a_free(vs->data);
		a_free(vs);
	}
}
示例#9
0
int _gc_back_()
{
	gc_node* gc_p= alex_gc.gc_head;
	gc_node* gc_b = NULL;


	while(gc_p)
	{
		if(gc_p->gc_level==GC_DEAD || gc_p->gc_count > 0)
		{
			if(gc_b)
				gc_b->next = gc_p;
			gc_b = gc_p;
			gc_p = gc_p->next;
			gc_b->next = NULL;
			continue;
		}
		
		switch(gc_p->gc_value.sg_t)
		{
		case sym_type_string:
			{
				gc_node* now_n = gc_p->next;

				gc_del_str(gc_p->gc_value.sg_v.str.s_ptr);
				free_string(&gc_p->gc_value.sg_v.str);
				a_free(gc_p);
				if(gc_p==alex_gc.gc_head)
					alex_gc.gc_head = now_n;
				gc_p = now_n;
			}
			break;
		case sym_type_al:
			{
				gc_node* now_n = gc_p->next;
				
				gc_del_al(gc_p->gc_value.sg_v.al, 0);
				a_free(gc_p);
				if(gc_p==alex_gc.gc_head)
					alex_gc.gc_head = now_n;
				gc_p = now_n;
			}
			break;
		default:
			print("gc[error], not konw gc_value!\n");
			return 1;
		}
		
		alex_gc.gc_size--;
	}

	alex_gc.c_size = 0;
	return 0;
}
示例#10
0
static void da_foldr_test(void) {
    printsln((String)__func__);
    Array a = da_of_string("");
    check_within_d(da_foldr(a, double_plus, 100), 100, EPSILON);
    check_within_d(da_foldr(a, double_minus, 100), 100, EPSILON);
    a_free(a);

    a = da_of_string("1, 2, 3, 4");
    check_within_d(da_foldr(a, double_plus, 100), 1 + (2 + (3 + (4 + 100))), EPSILON);
    check_within_d(da_foldr(a, double_minus, 0), 1 - (2 - (3 - (4 - 0))), EPSILON);
    a_free(a);
}
示例#11
0
static void ia_of_da_test(void) {
	printsln((String)__func__);
	Array da;
	Array ex, ac;
	da = da_of_string("0.0 0.49999 0.5 1.0 -0.49999 -0.50001 -1.0");
	ex = ia_of_string(" 0   0       1   1    0       -1       -1 ");
	ac = ia_of_da(da);
	ia_check_expect(ac, ex);
	a_free(da);
	a_free(ex);
	a_free(ac);
}
示例#12
0
static void ia_foldr_test(void) {
	printsln((String)__func__);
	Array a = ia_of_string("");	
	check_expect_i(ia_foldr(a, int_plus, 100), 100);
	check_expect_i(ia_foldr(a, int_minus, 100), 100);
	a_free(a);
	
	a = ia_of_string("1, 2, 3, 4");	
	check_expect_i(ia_foldr(a, int_plus, 100), 1 + (2 + (3 + (4 + 100))));
	check_expect_i(ia_foldr(a, int_minus, 0), 1 - (2 - (3 - (4 - 0))));
	a_free(a);
}
示例#13
0
static void ia_foldl_test(void) {
	printsln((String)__func__);
	Array a = ia_of_string("");	
	check_expect_i(ia_foldl(a, int_plus, 100), 100);
	check_expect_i(ia_foldl(a, int_minus, 100), 100);
	a_free(a);
	
	a = ia_of_string("1, 2, 3, 4");	
	check_expect_i(ia_foldl(a, int_plus, 0),    (((0 + 1) + 2) + 3) + 4);
	check_expect_i(ia_foldl(a, int_minus, 100), (((100 - 1) - 2) - 3) - 4);
	a_free(a);
}
示例#14
0
static void da_of_ia_test(void) {
    printsln((String)__func__);
    Array ia;
    Array ex, ac;
    ia = ia_of_string("-1 2 -3 44");
    ex = da_of_string("-1 2 -3 44");
    ac = da_of_ia(ia);
    da_check_within(ac, ex);
    a_free(ia);
    a_free(ex);
    a_free(ac);
}
示例#15
0
static void da_foldl_test(void) {
    printsln((String)__func__);
    Array a = da_of_string("");
    check_within_d(da_foldl(a, double_plus, 100), 100, EPSILON);
    check_within_d(da_foldl(a, double_minus, 100), 100, EPSILON);
    a_free(a);

    a = da_of_string("1, 2, 3, 4");
    check_within_d(da_foldl(a, double_plus, 0),    (((0 + 1) + 2) + 3) + 4, EPSILON);
    check_within_d(da_foldl(a, double_minus, 100), (((100 - 1) - 2) - 3) - 4, EPSILON);
    a_free(a);
}
示例#16
0
static void ia_choose_test(void) {
	printsln((String)__func__);
	Array a, ac, ex;

	a = ia_of_string("1, 2, 3, 4, 5, 6");
	ac = ia_choose(a, evens_times_x, 3);
	ex = ia_of_string("6, 12, 18");
	ia_check_expect(ac, ex);

	a_free(ac);
	a_free(ex);
	a_free(a);
}
示例#17
0
static void ia_sort_dec_test(void) {
	printsln((String)__func__);
	Array ac, ex;

	ac = ia_of_string("1, 2, 3, 4, 5");
	ex = ia_of_string("5, 4, 3, 2, 1");
	a_shuffle(ac);
	ia_sort_dec(ac);
	ia_check_expect(ac, ex);
	a_free(ac);
	a_free(ex);

	ac = ia_of_string("1, 2, 1, 3, 2");
	ex = ia_of_string("3, 2, 2, 1, 1");
	ia_sort_dec(ac);
	ia_check_expect(ac, ex);
	a_free(ac);
	a_free(ex);

	ac = ia_of_string("");
	ex = ia_of_string("");
	ia_sort_dec(ac);
	ia_check_expect(ac, ex);
	a_free(ac);
	a_free(ex);

	ac = ia_of_string("-3, -2, -1");
	ex = ia_of_string("-1, -2, -3");
	ia_sort_dec(ac);
	ia_check_expect(ac, ex);
	a_free(ac);
	a_free(ex);
}
示例#18
0
static void da_sort_dec_test(void) {
    printsln((String)__func__);
    Array ac, ex;

    ac = da_of_string("1, 2, 3, 4, 5, 6, 7, 8, 9, 10");
    ex = da_of_string("10, 9, 8, 7, 6, 5, 4, 3, 2, 1");
    da_sort_dec(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);

    ac = da_of_string("4.5, 4.5, 4.51, 2, 3, 9");
    ex = da_of_string("9 4.51 4.5 4.5 3 2");
    da_sort_dec(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);

    ac = da_of_string("");
    ex = da_of_string("");
    da_sort_dec(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);

    ac = da_of_string("3, 2, 1, 0.9, -0.1");
    ex = da_of_string("3, 2, 1, 0.9, -0.1");
    da_sort_dec(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);
}
示例#19
0
static void da_sort_test(void) {
    printsln((String)__func__);
    Array ac, ex;

    ac = da_of_string("1, 2, 3, 4, 5, 6, 7, 8, 9, 10");
    ex = da_of_string("1, 2, 3, 4, 5, 6, 7, 8, 9, 10");
    da_sort(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);

    ac = da_of_string("4.5, 4.5, 4.5, 4.51, 2, 3, 4, 5, 6, 7, 8, 9, 10");
    ex = da_of_string("2 3 4 4.5 4.5 4.5 4.51 5 6 7 8 9 10");
    da_sort(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);

    ac = da_of_string("");
    ex = da_of_string("");
    da_sort(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);

    ac = da_of_string("3, 2, 1, 0.9, -0.1");
    ex = da_of_string("-0.1 0.9 1 2 3");
    da_sort(ac);
    da_check_within(ac, ex);
    a_free(ac);
    a_free(ex);
}
示例#20
0
文件: mem.c 项目: Arkitecht/LibAleph
void a_gc_free(a_pool pool)
{
    a_str *e = a_p_data(pool);
    while (pool->count--)
        a_free(e[pool->count]);
    free(pool);
}
示例#21
0
int gc_del_al(alex_al* al_p, ubyte is_free)
{
	int i=0;
	if(al_p==NULL)
		return 0;
	
	if(is_free==0)
	{
		for(i=0; i<al_p->al_len; i++)
			check_l_gc(&(al_p->al_v[i]));
	}
	a_free(al_p->al_v);
	a_free(al_p);
	
	return 1;
}
示例#22
0
文件: server.c 项目: ptrrkssn/pftpd
static void *
client_thread(void *vp)
{
    CLIENT *cp = (CLIENT *) vp;
    SERVER *sp = cp->sp;
    
    sp->handler(cp);
    
    s_close(cp->fd);
    
#ifdef HAVE_THREADS
    if (sp->clients_max > 0)
    {
	pthread_mutex_lock(&sp->clients_mtx);
	if (sp->clients_cur == sp->clients_max)
	    pthread_cond_signal(&sp->clients_cv);
	
	sp->clients_cur--;
	pthread_mutex_unlock(&sp->clients_mtx);
    }
#endif

    a_free(cp);
    return NULL;
}
示例#23
0
static void da_last_index_fn_test(void) {
    printsln((String)__func__);
    Array a = da_of_string("1 2 2 3 -3 5");
    check_expect_i(da_last_index_fn(a, da_gt, 0), 5);
    check_expect_i(da_last_index_fn(a, da_lt, 0), 4);
    a_free(a);
}
示例#24
0
static void da_index_option_test(void) {
    printsln((String)__func__);
    Array array;

    array = da_of_string("1, 2");
    DoubleOption op = da_index_option(array, 0, EPSILON);
#if 0
    if (op.none) {
        printsln("value not found");
    } else {
        printf("value found at index %f\n", op.some);
    }
#endif
    check_expect_b(op.none, true);

    op = da_index_option(array, 1, EPSILON);
    check_expect_b(op.none, false);
    check_expect_i(op.some, 0);

    op = da_index_option(array, 2, EPSILON);
    check_expect_b(op.none, false);
    check_expect_i(op.some, 1);

    a_free(array);
}
示例#25
0
// 表达式
tree_node* syn_exp_def(token_list* t_lt)
{
	tree_node* op_l = syn_logic_exp(t_lt);	// 左操作数
	tree_node* rt_n = op_l;

	if(op_l == NULL)
		return NULL;

	while(type_token(t_lt) == token_type_ass)		// 赋值
	{
		rt_n = new_tree_node(get_line(t_lt), bnf_type_ass);
		rt_n->childs_p[0] = op_l;
		rt_n->b_v.op_t = token_type_ass;

		next_token(t_lt);
		rt_n->childs_p[1] = syn_logic_exp(t_lt);
		if(rt_n->childs_p[1] == NULL)
		{
			a_free(rt_n);
			return NULL;
		}
		op_l=rt_n;					// 迭代解析
	}
	
	return rt_n;
}
示例#26
0
static void ia_last_index_fn_test(void) {
	printsln((String)__func__);
	Array a = ia_of_string("1 2 2 3 -3 5");
	check_expect_i(ia_last_index_fn(a, ia_even, 0), 2);
	check_expect_i(ia_last_index_fn(a, ia_lt, 0), 4);
	a_free(a);
}
示例#27
0
int gc_del_str(char* str)
{
	unsigned int index = 0;
	str_node* str_b = NULL;
	str_node* str_p = NULL;
	
	if(str==NULL)
		return 0;
	
	index = gc_hash(str);
	str_p = alex_gc.gc_str_table.str_ptr[index];
	str_b = str_p;

	while(str_p)
	{
		if(alex_strcmp(str_p->str, str)==0)
		{
			str_b->next = str_p->next;
			if(str_b==str_p)
				alex_gc.gc_str_table.str_ptr[index] = str_p->next;
			a_free(str_p);
			return 1;
		}

		str_b = str_p;
		str_p = str_p->next;
	}

	return 0;
}
示例#28
0
alex_al* relloc_al(alex_al* al)
{
	if(al==NULL)
		return NULL;

	if(al->al_v==NULL)
	{
		al->al_v = (r_value*)a_malloc(sizeof(r_value)*DEF_AL_SIZE);
		memset(al->al_v, 0, sizeof(r_value)*DEF_AL_SIZE);
		al->al_size = DEF_AL_SIZE;
		al->al_len=0;
	}
	else
	{
		if(al->al_size <= al->al_len)
		{
			int size = sizeof(r_value)*(al->al_size+DEF_AL_SIZE);
			r_value* n_al_v = (r_value*)a_malloc(size);
			memset(n_al_v, 0, size);
			memcpy(n_al_v, al->al_v, sizeof(r_value)*al->al_size);
			a_free(al->al_v);
			al->al_v = n_al_v;
			al->al_size +=DEF_AL_SIZE; 
		}
	}

	return al;
}
示例#29
0
文件: gmcmd.c 项目: TomBraun/server
static int read_permissions(attrib * a, void *owner, struct storage *store)
{
  attrib *attr = NULL;
  a_read(store, &attr, NULL);
  a_free(attr);
  return AT_READ_OK;
}
示例#30
0
void
irc_status_busy(connection_t *c, int on, char *msg)
{
	c->status &= ~STATUS_MASK;

	if (on) {
		c->status |= STATUS_BUSY;

		if (msg != NULL) {
			IRC(c)->away = a_strdup(c->area, msg);

			PDEBUG("vado in away: %s", IRC(c)->away);
		}
	} else {
		c->status |= STATUS_AVAILABLE;

		if (IRC(c)->away != NULL) {
			a_free(c->area, IRC(c)->away);
			IRC(c)->away = NULL;
		}
	}

	EMIT(c, "send status");

	IRC_SEND_SRVMODE(c, c->nick, c->nick, (on) ? "+a" : "-a");
}