Skip to content

Commit 29242b9

Browse files
committed
support void*
1 parent bf64d08 commit 29242b9

13 files changed

+473
-12
lines changed

build-win/libcstl.def

+6
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,9 @@ EXPORTS
10901090
fun_less_equal_cstr @24230
10911091
fun_equal_cstr @24231
10921092
fun_not_equal_cstr @24232
1093+
fun_equal_pointer @24233
1094+
fun_not_equal_pointer @24234
1095+
fun_greater_pointer @24235
1096+
fun_greater_equal_pointer @24236
1097+
fun_less_pointer @24237
1098+
fun_less_equal_pointer @24238

cstl/cstl_function.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* The interface of function.
3-
* Copyright (C) 2008 - 2012 Wangbo
3+
* Copyright (C) 2008 - 2014 Wangbo
44
*
55
* This library is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU Lesser General Public
@@ -170,6 +170,7 @@ extern void fun_equal_float(const void* cpv_first, const void* cpv_second, void*
170170
extern void fun_equal_double(const void* cpv_first, const void* cpv_second, void* pv_output);
171171
extern void fun_equal_long_double(const void* cpv_first, const void* cpv_second, void* pv_output);
172172
extern void fun_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv_output);
173+
extern void fun_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output);
173174
extern void fun_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output);
174175
extern void fun_equal_deque(const void* cpv_first, const void* cpv_second, void* pv_output);
175176
extern void fun_equal_list(const void* cpv_first, const void* cpv_second, void* pv_output);
@@ -207,6 +208,7 @@ extern void fun_not_equal_float(const void* cpv_first, const void* cpv_second, v
207208
extern void fun_not_equal_double(const void* cpv_first, const void* cpv_second, void* pv_output);
208209
extern void fun_not_equal_long_double(const void* cpv_first, const void* cpv_second, void* pv_output);
209210
extern void fun_not_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv_output);
211+
extern void fun_not_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output);
210212
extern void fun_not_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output);
211213
extern void fun_not_equal_deque(const void* cpv_first, const void* cpv_second, void* pv_output);
212214
extern void fun_not_equal_list(const void* cpv_first, const void* cpv_second, void* pv_output);
@@ -244,6 +246,7 @@ extern void fun_greater_float(const void* cpv_first, const void* cpv_second, voi
244246
extern void fun_greater_double(const void* cpv_first, const void* cpv_second, void* pv_output);
245247
extern void fun_greater_long_double(const void* cpv_first, const void* cpv_second, void* pv_output);
246248
extern void fun_greater_cstr(const void* cpv_first, const void* cpv_second, void* pv_output);
249+
extern void fun_greater_pointer(const void* cpv_first, const void* cpv_second, void* pv_output);
247250
extern void fun_greater_vector(const void* cpv_first, const void* cpv_second, void* pv_output);
248251
extern void fun_greater_deque(const void* cpv_first, const void* cpv_second, void* pv_output);
249252
extern void fun_greater_list(const void* cpv_first, const void* cpv_second, void* pv_output);
@@ -281,6 +284,7 @@ extern void fun_greater_equal_float(const void* cpv_first, const void* cpv_secon
281284
extern void fun_greater_equal_double(const void* cpv_first, const void* cpv_second, void* pv_output);
282285
extern void fun_greater_equal_long_double(const void* cpv_first, const void* cpv_second, void* pv_output);
283286
extern void fun_greater_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv_output);
287+
extern void fun_greater_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output);
284288
extern void fun_greater_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output);
285289
extern void fun_greater_equal_deque(const void* cpv_first, const void* cpv_second, void* pv_output);
286290
extern void fun_greater_equal_list(const void* cpv_first, const void* cpv_second, void* pv_output);
@@ -318,6 +322,7 @@ extern void fun_less_float(const void* cpv_first, const void* cpv_second, void*
318322
extern void fun_less_double(const void* cpv_first, const void* cpv_second, void* pv_output);
319323
extern void fun_less_long_double(const void* cpv_first, const void* cpv_second, void* pv_output);
320324
extern void fun_less_cstr(const void* cpv_first, const void* cpv_second, void* pv_output);
325+
extern void fun_less_pointer(const void* cpv_first, const void* cpv_second, void* pv_output);
321326
extern void fun_less_vector(const void* cpv_first, const void* cpv_second, void* pv_output);
322327
extern void fun_less_deque(const void* cpv_first, const void* cpv_second, void* pv_output);
323328
extern void fun_less_list(const void* cpv_first, const void* cpv_second, void* pv_output);
@@ -355,6 +360,7 @@ extern void fun_less_equal_float(const void* cpv_first, const void* cpv_second,
355360
extern void fun_less_equal_double(const void* cpv_first, const void* cpv_second, void* pv_output);
356361
extern void fun_less_equal_long_double(const void* cpv_first, const void* cpv_second, void* pv_output);
357362
extern void fun_less_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv_output);
363+
extern void fun_less_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output);
358364
extern void fun_less_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output);
359365
extern void fun_less_equal_deque(const void* cpv_first, const void* cpv_second, void* pv_output);
360366
extern void fun_less_equal_list(const void* cpv_first, const void* cpv_second, void* pv_output);

src/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ libcstl_la_SOURCES = \
3333
cstl_types.c cstl_types_builtin.c cstl_types_builtin.h cstl_types_parse.c cstl_types_parse.h cstl_types_aux.c cstl_types_aux.h \
3434
cstl_vector.c cstl_vector_iterator.c cstl_vector_private.c cstl_vector_aux.c cstl_vector_aux.h
3535
libcstl_la_CPPFLAGS = -I$(srcdir)/..
36-
libcstl_la_LDFLAGS = -version-info 4:0:2
36+
libcstl_la_LDFLAGS = -version-info 5:0:3

src/cstl_function.c

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* The implementation of function.
3-
* Copyright (C) 2008 - 2012 Wangbo
3+
* Copyright (C) 2008 - 2014 Wangbo
44
*
55
* This library is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU Lesser General Public
@@ -611,6 +611,13 @@ void fun_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv_outp
611611
*(bool_t*)pv_output = strncmp(s_first, s_second, t_len) == 0 ? true : false;
612612
}
613613

614+
void fun_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output)
615+
{
616+
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
617+
618+
*(bool_t*)pv_output = *(void**)cpv_first == *(void**)cpv_second ? true : false;
619+
}
620+
614621
void fun_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output)
615622
{
616623
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
@@ -828,6 +835,13 @@ void fun_not_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv_
828835
*(bool_t*)pv_output = strncmp(s_first, s_second, t_len) != 0 ? true : false;
829836
}
830837

838+
void fun_not_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output)
839+
{
840+
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
841+
842+
*(bool_t*)pv_output = *(void**)cpv_first != *(void**)cpv_second ? true : false;
843+
}
844+
831845
void fun_not_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output)
832846
{
833847
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
@@ -1039,6 +1053,13 @@ void fun_greater_cstr(const void* cpv_first, const void* cpv_second, void* pv_ou
10391053
*(bool_t*)pv_output = strncmp(s_first, s_second, t_len) > 0 ? true : false;
10401054
}
10411055

1056+
void fun_greater_pointer(const void* cpv_first, const void* cpv_second, void* pv_output)
1057+
{
1058+
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
1059+
1060+
*(bool_t*)pv_output = *(void**)cpv_first > *(void**)cpv_second ? true : false;
1061+
}
1062+
10421063
void fun_greater_vector(const void* cpv_first, const void* cpv_second, void* pv_output)
10431064
{
10441065
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
@@ -1250,6 +1271,13 @@ void fun_greater_equal_cstr(const void* cpv_first, const void* cpv_second, void*
12501271
*(bool_t*)pv_output = strncmp(s_first, s_second, t_len) >= 0 ? true : false;
12511272
}
12521273

1274+
void fun_greater_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output)
1275+
{
1276+
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
1277+
1278+
*(bool_t*)pv_output = *(void**)cpv_first >= *(void**)cpv_second ? true : false;
1279+
}
1280+
12531281
void fun_greater_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output)
12541282
{
12551283
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
@@ -1461,6 +1489,13 @@ void fun_less_cstr(const void* cpv_first, const void* cpv_second, void* pv_outpu
14611489
*(bool_t*)pv_output = strncmp(s_first, s_second, t_len) < 0 ? true : false;
14621490
}
14631491

1492+
void fun_less_pointer(const void* cpv_first, const void* cpv_second, void* pv_output)
1493+
{
1494+
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
1495+
1496+
*(bool_t*)pv_output = *(void**)cpv_first < *(void**)cpv_second ? true : false;
1497+
}
1498+
14641499
void fun_less_vector(const void* cpv_first, const void* cpv_second, void* pv_output)
14651500
{
14661501
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
@@ -1672,6 +1707,13 @@ void fun_less_equal_cstr(const void* cpv_first, const void* cpv_second, void* pv
16721707
*(bool_t*)pv_output = strncmp(s_first, s_second, t_len) <= 0 ? true : false;
16731708
}
16741709

1710+
void fun_less_equal_pointer(const void* cpv_first, const void* cpv_second, void* pv_output)
1711+
{
1712+
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
1713+
1714+
*(bool_t*)pv_output = *(void**)cpv_first <= *(void**)cpv_second ? true : false;
1715+
}
1716+
16751717
void fun_less_equal_vector(const void* cpv_first, const void* cpv_second, void* pv_output)
16761718
{
16771719
assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);

src/cstl_function_private.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,17 @@ bfun_t _fun_get_binary(iterator_t it_iter, fun_type_t ftype_type)
341341
case _GREATER_EQUAL_FUN: return fun_greater_equal_cstr; break;
342342
case _LESS_FUN: return fun_less_cstr; break;
343343
case _LESS_EQUAL_FUN: return fun_less_equal_cstr; break;
344-
default:break;
344+
default: break;
345+
}
346+
} else if (strncmp(s_typename, _POINTER_TYPE, _TYPE_NAME_SIZE) == 0) { /* void* */
347+
switch (ftype_type) {
348+
case _EQUAL_FUN: return fun_equal_pointer; break;
349+
case _NOT_EQUAL_FUN: return fun_not_equal_pointer; break;
350+
case _GREATER_FUN: return fun_greater_pointer; break;
351+
case _GREATER_EQUAL_FUN: return fun_greater_equal_pointer; break;
352+
case _LESS_FUN: return fun_less_pointer; break;
353+
case _LESS_EQUAL_FUN: return fun_less_equal_pointer; break;
354+
default: break;
345355
}
346356
} else if (strncmp(s_typename, _CSTL_BOOL_TYPE, _TYPE_NAME_SIZE) == 0) { /* bool_t */
347357
switch (ftype_type) {

src/cstl_types.c

+4
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ void _type_get_varg_value(_typeinfo_t* pt_typeinfo, va_list val_elemlist, void*
535535
/* bool_t */
536536
assert(pt_typeinfo->_pt_type->_t_typesize == sizeof(bool_t));
537537
*(bool_t*)pv_output = va_arg(val_elemlist, bool_t);
538+
} else if (strncmp(pt_typeinfo->_pt_type->_s_typename, _POINTER_TYPE, _TYPE_NAME_SIZE) == 0) {
539+
/* void* */
540+
assert(pt_typeinfo->_pt_type->_t_typesize == sizeof(void*));
541+
*(void**)pv_output = va_arg(val_elemlist, void*);
538542
} else if (strncmp(pt_typeinfo->_pt_type->_s_typename, _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
539543
/* char* */
540544
char* s_str = va_arg(val_elemlist, char*);

test/ut/ut_cstl_function.c

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include <stddef.h>
2+
#include <stdarg.h>
3+
#include <setjmp.h>
4+
#include <google/cmockery.h>
5+
6+
#include "cstl/cstl_def.h"
7+
#include "cstl/citerator.h"
8+
#include "cstl/chash_map.h"
9+
#include "cstl/cstring.h"
10+
#include "cstl/cvector.h"
11+
#include "cstl/clist.h"
12+
#include "cstl/cdeque.h"
13+
#include "cstl/cslist.h"
14+
#include "cstl/cmap.h"
15+
#include "cstl/cset.h"
16+
#include "cstl/chash_set.h"
17+
#include "cstl/calgorithm.h"
18+
#include "cstl/cfunctional.h"
19+
20+
#include "ut_def.h"
21+
#include "ut_cstl_function.h"
22+
23+
UT_SUIT_DEFINATION(cstl_function, fun_equal_pointer_usage)
24+
25+
/*
26+
* test fun_equal_pointer usage
27+
*/
28+
UT_CASE_DEFINATION(fun_equal_pointer_usage)
29+
void test__fun_equal_pointer_usage__algo_unique_copy(void** state)
30+
{
31+
vector_t* pvec = create_vector(void*);
32+
list_t* plist = create_list(void*);
33+
deque_t* pdeq = create_deque(void*);
34+
35+
void* src[] = {0x1111, 0x1111, 0x1111, 0x2222, 0x2222, 0x3333, 0x4444, 0x4444, 0x8888, 0x8888};
36+
void* result[] = {0x1111, 0x2222, 0x3333, 0x4444, 0x8888, 0x00, 0x00, 0x00, 0x00, 0x00};
37+
38+
vector_init_copy_array(pvec, src, 10);
39+
list_init_copy_array(plist, result, 10);
40+
deque_init_n(pdeq, vector_size(pvec));
41+
42+
algo_unique_copy(vector_begin(pvec), vector_end(pvec), deque_begin(pdeq));
43+
assert_true(algo_equal(deque_begin(pdeq), deque_end(pdeq), list_begin(plist)));
44+
45+
vector_destroy(pvec);
46+
list_destroy(plist);
47+
deque_destroy(pdeq);
48+
}
49+
50+
/*
51+
* test fun_not_equal_pointer usage
52+
*/
53+
UT_CASE_DEFINATION(fun_not_equal_pointer_usage)
54+
void test__fun_not_equal_pointer_usage__algo_unique_copy_if(void** state)
55+
{
56+
vector_t* pvec = create_vector(void*);
57+
list_t* plist = create_list(void*);
58+
deque_t* pdeq = create_deque(void*);
59+
60+
void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888};
61+
void* result[] = {0x1111, 0x1111, 0x1111, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
62+
63+
vector_init_copy_array(pvec, src, 10);
64+
list_init_copy_array(plist, result, 10);
65+
deque_init_n(pdeq, vector_size(pvec));
66+
67+
algo_unique_copy_if(vector_begin(pvec), vector_end(pvec), deque_begin(pdeq), _fun_get_binary(vector_begin(pvec), _NOT_EQUAL_FUN));
68+
assert_true(algo_equal(deque_begin(pdeq), deque_end(pdeq), list_begin(plist)));
69+
70+
vector_destroy(pvec);
71+
list_destroy(plist);
72+
deque_destroy(pdeq);
73+
}
74+
75+
/*
76+
* test fun_greater_pointer usage
77+
*/
78+
UT_CASE_DEFINATION(fun_greater_pointer_usage)
79+
void test__fun_greater_pointer_usage__algo_sort_if(void** state)
80+
{
81+
vector_t* pvec = create_vector(void*);
82+
list_t* plist = create_list(void*);
83+
84+
void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888};
85+
void* result[] = {0x9999, 0x8888, 0x8888, 0x8888, 0x4444, 0x3333, 0x2222, 0x1111, 0x1111, 0x1111};
86+
87+
vector_init_copy_array(pvec, src, 10);
88+
list_init_copy_array(plist, result, 10);
89+
90+
algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _GREATER_FUN));
91+
assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist)));
92+
93+
vector_destroy(pvec);
94+
list_destroy(plist);
95+
}
96+
97+
/*
98+
* test fun_greater_equal_pointer usage
99+
*/
100+
UT_CASE_DEFINATION(fun_greater_equal_pointer_usage)
101+
void test__fun_greater_equal_pointer_usage__algo_sort_if(void** state)
102+
{
103+
vector_t* pvec = create_vector(void*);
104+
list_t* plist = create_list(void*);
105+
106+
void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888};
107+
void* result[] = {0x9999, 0x8888, 0x8888, 0x8888, 0x4444, 0x3333, 0x2222, 0x1111, 0x1111, 0x1111};
108+
109+
vector_init_copy_array(pvec, src, 10);
110+
list_init_copy_array(plist, result, 10);
111+
112+
algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _GREATER_EQUAL_FUN));
113+
assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist)));
114+
115+
vector_destroy(pvec);
116+
list_destroy(plist);
117+
}
118+
119+
/*
120+
* test fun_less_pointer usage
121+
*/
122+
UT_CASE_DEFINATION(fun_less_pointer_usage)
123+
void test__fun_less_pointer_usage__algo_sort_if(void** state)
124+
{
125+
vector_t* pvec = create_vector(void*);
126+
list_t* plist = create_list(void*);
127+
128+
void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888};
129+
void* result[] = {0x1111, 0x1111, 0x1111, 0x2222, 0x3333, 0x4444, 0x8888, 0x8888, 0x8888, 0x9999};
130+
131+
vector_init_copy_array(pvec, src, 10);
132+
list_init_copy_array(plist, result, 10);
133+
134+
algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _LESS_FUN));
135+
assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist)));
136+
137+
vector_destroy(pvec);
138+
list_destroy(plist);
139+
}
140+
141+
/*
142+
* test fun_less_equal_pointer usage
143+
*/
144+
UT_CASE_DEFINATION(fun_less_equal_pointer_usage)
145+
void test__fun_less_equal_pointer_usage__algo_sort_if(void** state)
146+
{
147+
vector_t* pvec = create_vector(void*);
148+
list_t* plist = create_list(void*);
149+
150+
void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888};
151+
void* result[] = {0x1111, 0x1111, 0x1111, 0x2222, 0x3333, 0x4444, 0x8888, 0x8888, 0x8888, 0x9999};
152+
153+
vector_init_copy_array(pvec, src, 10);
154+
list_init_copy_array(plist, result, 10);
155+
156+
algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _LESS_EQUAL_FUN));
157+
assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist)));
158+
159+
vector_destroy(pvec);
160+
list_destroy(plist);
161+
}

0 commit comments

Comments
 (0)