Merge lp:~smspillaz/compiz-libcompizconfig/ccs-object into lp:~compiz-team/compiz-libcompizconfig/0.9.8

Proposed by Sam Spilsbury
Status: Superseded
Proposed branch: lp:~smspillaz/compiz-libcompizconfig/ccs-object
Merge into: lp:~compiz-team/compiz-libcompizconfig/0.9.8
Diff against target: 685 lines (+608/-1)
6 files modified
CMakeLists.txt (+1/-0)
include/ccs.h (+98/-1)
src/CMakeLists.txt (+3/-0)
src/main.c (+177/-0)
tests/CMakeLists.txt (+56/-0)
tests/test-ccs-object.cpp (+273/-0)
To merge this branch: bzr merge lp:~smspillaz/compiz-libcompizconfig/ccs-object
Reviewer Review Type Date Requested Status
Alan Griffiths Needs Fixing
Review via email: mp+104468@code.launchpad.net

This proposal supersedes a proposal from 2012-04-29.

This proposal has been superseded by a proposal from 2012-05-04.

Description of the change

This is all about bug 990690.

!! - It probably isn't a good idea to test this branch in isolation, as it is part of a pipeline to get compiz-libcompizconfig under test. If you want to test the result of this work, you should probably look at testing

lp:~smspillaz/compiz-libcompizconfig/setting-mock
lp:~smspillaz/compiz-compizconfig-python/compiz-compizconfig-python.setting-api
lp:~smspillaz/compiz-compizconfig-gconf/compiz-compizconfig-gconf.adapt-to-new-interfaces

.. that's all !!

This branch introduces some preliminary work in a series of branches to get libcompizconfig under test. In order to test the objects properly, we need to abstract away their interfaces into replacable structs so you can test the interaction between the various classes.

This would be awkward to do correctly if we didn't have a suitable object system to handle interface implementation, referencing, private storage etc.

As such, a new struct CCSObject is introduced. It is similar in design to GObject, but with a much smaller feature set centered mostly around the handling of interfaces and composition / indirection. A type registrar is also included (also very simple).

Tests are included.

To post a comment you must log in.
Revision history for this message
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

I know it is common (and may be a project style) but there is no advantage to having different names to refer to the same thing in different namespaces. That is not:

    typedef struct _CCSObject CCSObject;

    struct _CCSObject
    {
    ...
    };

but:

    typedef struct CCSObject
    {
    ...
    } CCSObject;

Revision history for this message
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

> I know it is common (and may be a project style) but there is no advantage to
> having different names to refer to the same thing in different namespaces.
> That is not:
>
> typedef struct _CCSObject CCSObject;
>
> struct _CCSObject
> {
> ...
> };
>
> but:
>
> typedef struct CCSObject
> {
> ...
> } CCSObject;

Actually, it doesn't seem that client code should be interested in the members of [_]CCSObject, so why not a completely opaque type? Vis:

    typedef struct CCSObject CCSObject;

in the header. And move the definition to the .c file.

Revision history for this message
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

I don't like the style of coding in ccsObjectAddInterface() - if either realloc fails then the object is hosed. Admittedly FALSE is returned to inform the caller, but I'd prefer the object to remain viable and just the call to fail. Anything else can cause nasty surprises in client code. (Admittedly, if realloc fails there are probably worse problems to deal with.)

review: Needs Fixing
Revision history for this message
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

> I don't like the style of coding in ccsObjectAddInterface() - if either
> realloc fails then the object is hosed. Admittedly FALSE is returned to
> inform the caller, but I'd prefer the object to remain viable and just the
> call to fail. Anything else can cause nasty surprises in client code.
> (Admittedly, if realloc fails there are probably worse problems to deal with.)

Forgot to mention: the cost of calling realloc in ccsObjectRemoveInterface probably outweighs any saving of memory that results.

Revision history for this message
Sam Spilsbury (smspillaz) wrote : Posted in a previous version of this proposal

> > I know it is common (and may be a project style) but there is no advantage
> to
> > having different names to refer to the same thing in different namespaces.
> > That is not:
> >
> > typedef struct _CCSObject CCSObject;
> >
> > struct _CCSObject
> > {
> > ...
> > };
> >
> > but:
> >
> > typedef struct CCSObject
> > {
> > ...
> > } CCSObject;
>
> Actually, it doesn't seem that client code should be interested in the members
> of [_]CCSObject, so why not a completely opaque type? Vis:
>
> typedef struct CCSObject CCSObject;
>
> in the header. And move the definition to the .c file.

I think the struct can't be opaque because of the way that we actually access the object data (eg, casting it to a CCSObject * and accessing the data in the CCSObject in the first part of the struct).

As for realloc - I agree and had similar concerns, and will change that.

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

ccsObjectInit is only used by the test harness which ignores the return code, so the intended use is unclear. Surely a better return value would be a pointer to the initialised object?

ccsObjectRemoveInterface assigns pointers "interfaces" and "interface_types" to NULL without first freeing the memory referenced.

ccsObjectAddInterface assigns pointers "interfaces" and "interface_types" without first freeing the memory referenced.

struct _CCSObjectAllocationInterface (again, no need for the "_") has members realloc, malloc, calloc - these are reserved names.

Actually, talking of reserved names: names that begin with an underscore and a capital are reserved, which is another reason for not using these names in the struct namespace. (I still see no point in having different names (e.g. _CCSObject) in the struct namespace.)

The tests imply that the intended usage is to cast a pointer to some other structure (represented by "TestingObjectWrapper") to CCSObject* before passing that to ccsObjectXXX. If this is not the intended usage the tests are inappropriate. If it is intended there's negative value in these taking a pointer to the CCSObject as a parameter, they may as well take void* in the same way as ccsObjectInit does. It's a waste of the type-safety features of C.

review: Needs Fixing
Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

Let me suggest a way that the client code could be eg "ccsObjectRef(to);"...
...
Bool ccsObjectRef_(CCSObject *object);
#define ccsObjectRef(obj) ccsObjectRef_(&obj->object)
...
No nasty casts, and type safe. At the cost of requiring a member named "object" of CCSObject type.

435. By Sam Spilsbury

Make the CCSObject API a bit more typesafe

Revision history for this message
Sam Spilsbury (smspillaz) wrote :

> ccsObjectInit is only used by the test harness which ignores the return code,
> so the intended use is unclear. Surely a better return value would be a
> pointer to the initialised object?

Indeed, future merges will introduce proper constructors (but this is not trivial right now).

>
> ccsObjectRemoveInterface assigns pointers "interfaces" and "interface_types"
> to NULL without first freeing the memory referenced.
>
> ccsObjectAddInterface assigns pointers "interfaces" and "interface_types"
> without first freeing the memory referenced.

They use realloc, so I don't see why this is a problem ?

>
> struct _CCSObjectAllocationInterface (again, no need for the "_") has members
> realloc, malloc, calloc - these are reserved names.

ack.

>
> Actually, talking of reserved names: names that begin with an underscore and a
> capital are reserved, which is another reason for not using these names in the
> struct namespace. (I still see no point in having different names (e.g.
> _CCSObject) in the struct namespace.)

Right, we should evaluate this elsewhere

>
> The tests imply that the intended usage is to cast a pointer to some other
> structure (represented by "TestingObjectWrapper") to CCSObject* before passing
> that to ccsObjectXXX. If this is not the intended usage the tests are
> inappropriate. If it is intended there's negative value in these taking a
> pointer to the CCSObject as a parameter, they may as well take void* in the
> same way as ccsObjectInit does. It's a waste of the type-safety features of
> C.

Right, this sucks, but we're introspecting the state of the object. Mabye we can get rid of the casts and just do to->object.foo

436. By Sam Spilsbury

Don't use reserved name

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

ccsObjectRemoveInterface_ doesn't use realloc, so it just overwrites the pointer. I think that will leak.

ccsObjectAddInterface does use realloc (I was wrongly assuming that you'd addressed the earlier discussion of invalidating the object on failed operation). Invalidating the object and returning FALSE will cause nasty surprises in client code.

Is there really value in CCSObjectAllocationInterface?

review: Needs Fixing
Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

These macros can lead to surprising results in user code:

86 +#define ccsObjectRef(o) \
87 + ((o)->object).refcnt++;
88 +
89 +#define ccsObjectUnref(o, freeFunc) \
90 + ((o)->object).refcnt--; \
91 + if (!((o)->object).refcnt) \
92 + freeFunc (o)

Example of problem:

  if (false) ccsObjectUnref(o, f);

will call f(o).

Rewrite as:
86 +#define ccsObjectRef(o) \
87 + do { ((o)->object).refcnt++ } while(false)
88 +
89 +#define ccsObjectUnref(o, freeFunc) \
90 + do { ((o)->object).refcnt--; \
91 + if (!((o)->object).refcnt) \
92 + freeFunc (o) } while(false)

PS It feels awkward to require freeFunc as a parameter, but I don't have a cleaner alternative suggestion just now.

review: Needs Fixing
Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

Bad example.

  if (false) ccsObjectUnref(o, f);

is OK.

But...

  if (true) ccsObjectUnref(o, f);
  else printf("surprise");

is a good example (i.e. broken).

437. By Sam Spilsbury

Free interfaces once objects are removed

438. By Sam Spilsbury

Fix potential macro problems

439. By Sam Spilsbury

Lose the ;

440. By Sam Spilsbury

Added a free thread

441. By Sam Spilsbury

Also test for free

Unmerged revisions

441. By Sam Spilsbury

Also test for free

440. By Sam Spilsbury

Added a free thread

439. By Sam Spilsbury

Lose the ;

438. By Sam Spilsbury

Fix potential macro problems

437. By Sam Spilsbury

Free interfaces once objects are removed

436. By Sam Spilsbury

Don't use reserved name

435. By Sam Spilsbury

Make the CCSObject API a bit more typesafe

434. By Sam Spilsbury

Don't reallocate all the time. Also handle realloc failures better

433. By Sam Spilsbury

Added a simple type registrar

432. By Sam Spilsbury

Added a ccsObjectFinalize function which frees everything in CCSObject storage

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2011-07-07 12:23:21 +0000
3+++ CMakeLists.txt 2012-05-04 19:29:18 +0000
4@@ -141,6 +141,7 @@
5 add_subdirectory (src)
6 add_subdirectory (include)
7 add_subdirectory (cmake)
8+add_subdirectory (tests)
9
10 compiz_print_configure_header ("CompizConfig Library")
11 compiz_color_message ("\n${_escape}[4mOptional features:${_escape}[0m\n")
12
13=== modified file 'include/ccs.h'
14--- include/ccs.h 2011-08-20 19:03:37 +0000
15+++ include/ccs.h 2012-05-04 19:29:18 +0000
16@@ -19,10 +19,14 @@
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20-
21 #ifndef _CSS_H
22 #define _CSS_H
23
24+#ifdef __cplusplus
25+extern "C"
26+{
27+#endif
28+
29 #define D_NONE 0
30 #define D_NORMAL 1
31 #define D_FULL 2
32@@ -124,6 +128,95 @@
33 CCSLIST_HDR (StrRestriction, CCSStrRestriction)
34 CCSLIST_HDR (StrExtension, CCSStrExtension)
35
36+typedef struct _CCSInterface CCSInterface; /* Dummy typedef */
37+typedef struct _CCSPrivate CCSPrivate; /* Dummy typedef */
38+typedef struct _CCSObject CCSObject;
39+
40+typedef void * (*reallocProc) (void *, size_t);
41+typedef void * (*mallocProc) (size_t);
42+typedef void * (*callocProc) (size_t, size_t);
43+
44+typedef struct _CCSObjectAllocationInterface
45+{
46+ reallocProc realloc_;
47+ mallocProc malloc_;
48+ callocProc calloc_;
49+} CCSObjectAllocationInterface;
50+
51+extern CCSObjectAllocationInterface ccsDefaultObjectAllocator;
52+
53+struct _CCSObject
54+{
55+ CCSPrivate *priv; /* Private pointer for object storage */
56+
57+ const CCSInterface **interfaces; /* An array of interfaces that this object implements */
58+ int *interface_types; /* An array of interface types */
59+ unsigned int n_interfaces;
60+ unsigned int n_allocated_interfaces;
61+
62+ CCSObjectAllocationInterface *object_allocation;
63+
64+ unsigned int refcnt; /* Reference count of this object */
65+};
66+
67+Bool
68+ccsObjectInit_ (CCSObject *object, CCSObjectAllocationInterface *interface);
69+
70+#define ccsObjectInit(o, interface) (ccsObjectInit_) (&(o)->object, interface)
71+
72+Bool
73+ccsObjectAddInterface_ (CCSObject *object, const CCSInterface *interface, int interface_type);
74+
75+#define ccsObjectAddInterface(o, interface, type) (ccsObjectAddInterface_) (&(o)->object, interface, type);
76+
77+Bool
78+ccsObjectRemoveInterface_ (CCSObject *object, int interface_type);
79+
80+#define ccsObjectRemoveInterface(o, interface_type) (ccsObjectRemoveInterface_) (&(o)->object, interface_type);
81+
82+const CCSInterface * ccsObjectGetInterface_ (CCSObject *object, int interface_type);
83+
84+#define ccsObjectGetInterface(o, interface_type) (ccsObjectGetInterface_) (&(o)->object, interface_type)
85+
86+#define ccsObjectRef(o) \
87+ ((o)->object).refcnt++;
88+
89+#define ccsObjectUnref(o, freeFunc) \
90+ ((o)->object).refcnt--; \
91+ if (!((o)->object).refcnt) \
92+ freeFunc (o)
93+
94+CCSPrivate *
95+ccsObjectGetPrivate_ (CCSObject *object);
96+
97+#define ccsObjectGetPrivate(o) (ccsObjectGetPrivate_) (&(o)->object)
98+
99+void
100+ccsObjectSetPrivate_ (CCSObject *object, CCSPrivate *priv);
101+
102+#define ccsObjectSetPrivate(o, priv) (ccsObjectSetPrivate_) (&(o)->object, priv)
103+
104+void
105+ccsObjectFinalize_ (CCSObject *object);
106+
107+#define ccsObjectFinalize(o) (ccsObjectFinalize_) (&(o)->object)
108+
109+unsigned int
110+ccsAllocateType ();
111+
112+#define GET_INTERFACE_TYPE(Interface) \
113+ ccs##Interface##GetType ();
114+
115+#define INTERFACE_TYPE(Interface) \
116+ inline unsigned int ccs##Interface##GetType () \
117+ { \
118+ static unsigned int type_id = 0; \
119+ if (!type_id) \
120+ type_id = ccsAllocateType (); \
121+ \
122+ return type_id; \
123+ }
124+
125 /**
126 * reference counting
127 *
128@@ -913,4 +1006,8 @@
129 /* Checks if settings need to be constrained */
130 Bool ccsContextNeedsConstraining (CCSContext *context);
131
132+#ifdef __cplusplus
133+}
134+#endif
135+
136 #endif
137
138=== modified file 'src/CMakeLists.txt'
139--- src/CMakeLists.txt 2011-10-15 07:28:45 +0000
140+++ src/CMakeLists.txt 2012-05-04 19:29:18 +0000
141@@ -35,6 +35,9 @@
142 ${LIBCOMPIZCONFIG_FILES}
143 compizconfig.pb.cc
144 )
145+ set (LIBCOMPIZCONFIG_LIBRARIES
146+ ${LIBCOMPIZCONFIG_LIBRARIES}
147+ protobuf)
148 add_definitions (
149 -DUSE_PROTOBUF
150 )
151
152=== modified file 'src/main.c'
153--- src/main.c 2011-11-10 00:58:18 +0000
154+++ src/main.c 2012-05-04 19:29:18 +0000
155@@ -39,6 +39,171 @@
156 #include "ccs-private.h"
157 #include "iniparser.h"
158
159+CCSObjectAllocationInterface ccsDefaultObjectAllocator =
160+{
161+ realloc,
162+ malloc,
163+ calloc
164+};
165+
166+/* CCSObject stuff */
167+Bool
168+ccsObjectInit_(CCSObject *object, CCSObjectAllocationInterface *object_allocation)
169+{
170+ object->priv = NULL;
171+ object->n_interfaces = 0;
172+ object->n_allocated_interfaces = 0;
173+ object->interfaces = NULL;
174+ object->interface_types = NULL;
175+ object->object_allocation = object_allocation;
176+ object->refcnt = 0;
177+
178+ return TRUE;
179+}
180+
181+Bool
182+ccsObjectAddInterface_(CCSObject *object, const CCSInterface *interface, int interface_type)
183+{
184+ object->n_interfaces++;
185+
186+ if (object->n_allocated_interfaces < object->n_interfaces)
187+ {
188+ unsigned int old_allocated_interfaces = object->n_allocated_interfaces;
189+ object->n_allocated_interfaces = object->n_interfaces;
190+ CCSInterface **ifaces = (*object->object_allocation->realloc_) (object->interfaces, object->n_allocated_interfaces * sizeof (CCSInterface *));
191+ int *iface_types = (*object->object_allocation->realloc_) (object->interface_types, object->n_allocated_interfaces * sizeof (int));
192+
193+ if (!ifaces || !iface_types)
194+ {
195+ if (ifaces)
196+ free (ifaces);
197+
198+ if (iface_types)
199+ free (iface_types);
200+
201+ object->n_interfaces--;
202+ object->n_allocated_interfaces = old_allocated_interfaces;
203+ return FALSE;
204+ }
205+ else
206+ {
207+ object->interfaces = (const CCSInterface **) ifaces;
208+ object->interface_types = iface_types;
209+ }
210+ }
211+
212+ object->interfaces[object->n_interfaces - 1] = interface;
213+ object->interface_types[object->n_interfaces - 1] = interface_type;
214+
215+ return TRUE;
216+}
217+
218+Bool
219+ccsObjectRemoveInterface_(CCSObject *object, int interface_type)
220+{
221+ unsigned int i = 0;
222+
223+ if (!object->n_interfaces)
224+ return FALSE;
225+
226+ const CCSInterface **o = object->interfaces;
227+ int *type = object->interface_types;
228+
229+ for (; i < object->n_interfaces; i++, o++, type++)
230+ {
231+ if (object->interface_types[i] == interface_type)
232+ break;
233+ }
234+
235+ if (i >= object->n_interfaces)
236+ return FALSE;
237+
238+ /* Now clear this section and move everything back */
239+ object->interfaces[i] = NULL;
240+
241+ i++;
242+
243+ const CCSInterface **oLast = o;
244+ int *typeLast = type;
245+
246+ o++;
247+ type++;
248+
249+ memmove ((void *) oLast, (void *)o, (object->n_interfaces - i) * sizeof (CCSInterface *));
250+ memmove ((void *) typeLast, (void *) type, (object->n_interfaces - i) * sizeof (int));
251+
252+ object->n_interfaces--;
253+
254+ if (!object->n_interfaces)
255+ {
256+ object->interfaces = NULL;
257+ object->interface_types = NULL;
258+ object->n_allocated_interfaces = 0;
259+ }
260+
261+ return TRUE;
262+}
263+
264+const CCSInterface *
265+ccsObjectGetInterface_(CCSObject *object, int interface_type)
266+{
267+ unsigned int i = 0;
268+
269+ for (; i < object->n_interfaces; i++)
270+ {
271+ if (object->interface_types[i] == interface_type)
272+ return object->interfaces[i];
273+ }
274+
275+ return NULL;
276+}
277+
278+CCSPrivate *
279+ccsObjectGetPrivate_(CCSObject *object)
280+{
281+ return object->priv;
282+}
283+
284+void
285+ccsObjectSetPrivate_(CCSObject *object, CCSPrivate *priv)
286+{
287+ object->priv = priv;
288+}
289+
290+void
291+ccsObjectFinalize_(CCSObject *object)
292+{
293+ if (object->priv)
294+ {
295+ free (object->priv);
296+ object->priv = NULL;
297+ }
298+
299+ if (object->interfaces)
300+ {
301+ free (object->interfaces);
302+ object->interfaces = NULL;
303+ }
304+
305+ if (object->interface_types)
306+ {
307+ free (object->interface_types);
308+ object->interface_types = NULL;
309+ }
310+
311+ object->n_interfaces = 0;
312+}
313+
314+unsigned int
315+ccsAllocateType ()
316+{
317+ static unsigned int start = 0;
318+
319+ start++;
320+
321+ return start;
322+}
323+
324 Bool basicMetadata = FALSE;
325
326 void
327@@ -592,6 +757,18 @@
328 CCSREF (StrRestriction, CCSStrRestriction)
329 CCSREF (StrExtension, CCSStrExtension)
330
331+#define CCSREF_OBJ(type,dtype) \
332+ void ccs##type##Ref (dtype *d) \
333+ { \
334+ ((CCSObject *) d)->refcnt++; \
335+ } \
336+ void ccs##type##Unref (dtype *d) \
337+ { \
338+ ((CCSObject *) d)->refcnt--; \
339+ if (((CCSObject *) d)->refcnt == 0) \
340+ ccsFree##type (d); \
341+ } \
342+
343 static void *
344 openBackend (char *backend)
345 {
346
347=== added directory 'tests'
348=== added file 'tests/CMakeLists.txt'
349--- tests/CMakeLists.txt 1970-01-01 00:00:00 +0000
350+++ tests/CMakeLists.txt 2012-05-04 19:29:18 +0000
351@@ -0,0 +1,56 @@
352+# Build Google Test and make its headers known
353+find_package (GTest)
354+
355+if (NOT GTEST_FOUND)
356+
357+ # Check for google test and build it locally
358+ set (GTEST_ROOT_DIR
359+ "/usr/src/gtest" # Default value, adjustable by user with e.g., ccmake
360+ CACHE
361+ PATH
362+ "Path to Google test srcs"
363+ )
364+
365+ find_path (GTEST_INCLUDE_DIR gtest/gtest.h)
366+
367+ if (GTEST_INCLUDE_DIR)
368+ #FIXME - hardcoded is bad!
369+ add_subdirectory (${GTEST_ROOT_DIR}
370+ gtest)
371+ endif(GTEST_INCLUDE_DIR)
372+
373+ set (GTEST_BOTH_LIBRARIES gtest gtest_main)
374+ set (GTEST_FOUND TRUE)
375+
376+endif (NOT GTEST_FOUND)
377+
378+find_library (GMOCK_LIBRARY gmock)
379+find_library (GMOCK_MAIN_LIBRARY gmock_main)
380+
381+if (NOT GMOCK_LIBRARY OR NOT GMOCK_MAIN_LIBRARY OR NOT GTEST_FOUND)
382+ message ("Google Mock and Google Test not found - cannot build tests!")
383+ set (COMPIZ_BUILD_TESTING OFF)
384+endif (NOT GMOCK_LIBRARY OR NOT GMOCK_MAIN_LIBRARY OR NOT GTEST_FOUND)
385+
386+include_directories (${GTEST_INCLUDE_DIRS})
387+include_directories (${CMAKE_SOURCE_DIR}/include)
388+link_directories (${CMAKE_INSTALL_PREFIX}/lib)
389+
390+add_executable (test-ccs-object
391+ ${CMAKE_CURRENT_SOURCE_DIR}/test-ccs-object.cpp)
392+
393+if (HAVE_PROTOBUF)
394+ set (LIBCOMPIZCONFIG_LIBRARIES
395+ ${LIBCOMPIZCONFIG_LIBRARIES}
396+ protobuf)
397+endif (HAVE_PROTOBUF)
398+
399+target_link_libraries (test-ccs-object
400+ ${GTEST_BOTH_LIBRARIES}
401+ ${GMOCK_LIBRARY}
402+ ${GMOCK_MAIN_LIBRARY}
403+ ${CMAKE_THREAD_LIBS_INIT}
404+ ${LIBCOMPIZCONFIG_LIBRARIES}
405+ compizconfig)
406+
407+gtest_add_tests (test-ccs-object "" ${CMAKE_CURRENT_SOURCE_DIR}/test-ccs-object.cpp)
408
409=== added file 'tests/test-ccs-object.cpp'
410--- tests/test-ccs-object.cpp 1970-01-01 00:00:00 +0000
411+++ tests/test-ccs-object.cpp 2012-05-04 19:29:18 +0000
412@@ -0,0 +1,273 @@
413+#include <gtest/gtest.h>
414+#include <gmock/gmock.h>
415+#include <ccs.h>
416+#include <cstdio>
417+
418+using ::testing::_;
419+using ::testing::Return;
420+
421+class CCSObjectTest :
422+ public ::testing::Test
423+{
424+};
425+
426+struct TestingObjectWrapper
427+{
428+ CCSObject object;
429+};
430+
431+typedef void (*dummyFunc) (void);
432+
433+struct DummyInterface
434+{
435+ dummyFunc dummy;
436+};
437+
438+struct Dummy2Interface
439+{
440+ dummyFunc dummy;
441+};
442+
443+class GoogleMockDummyInterface
444+{
445+ public:
446+
447+ virtual ~GoogleMockDummyInterface () {};
448+
449+ virtual void dummyFunc () = 0;
450+ virtual void freeTestingObjectWrapper (TestingObjectWrapper *) = 0;
451+};
452+
453+class GoogleMockDummy :
454+ public GoogleMockDummyInterface
455+{
456+ public:
457+
458+ MOCK_METHOD0 (dummyFunc, void ());
459+ MOCK_METHOD1 (freeTestingObjectWrapper, void (TestingObjectWrapper *));
460+ public:
461+
462+ static void thunkDummyFunc () { return _mockDummy.dummyFunc (); }
463+ static GoogleMockDummy _mockDummy;
464+};
465+
466+GoogleMockDummy GoogleMockDummy::_mockDummy;
467+
468+const struct DummyInterface SomeDummyInterface =
469+{
470+ GoogleMockDummy::thunkDummyFunc
471+};
472+
473+#define CCS_INTERFACE_TYPE_DUMMY GET_INTERFACE_TYPE (DummyInterface)
474+#define CCS_INTERFACE_TYPE_DUMMY2 GET_INTERFACE_TYPE (Dummy2Interface)
475+
476+INTERFACE_TYPE (DummyInterface)
477+INTERFACE_TYPE (Dummy2Interface)
478+
479+TEST(CCSObjectTest, TestTypeAllocation)
480+{
481+ unsigned int i = CCS_INTERFACE_TYPE_DUMMY;
482+ unsigned int j = CCS_INTERFACE_TYPE_DUMMY;
483+ unsigned int k = CCS_INTERFACE_TYPE_DUMMY2;
484+
485+ EXPECT_EQ (i, 1);
486+ EXPECT_EQ (j ,1);
487+ EXPECT_EQ (k, 2);
488+}
489+
490+TEST(CCSObjectTest, InterfaceAdd)
491+{
492+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
493+
494+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
495+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 1);
496+
497+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
498+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 1);
499+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 1);
500+
501+ free (to);
502+}
503+
504+TEST(CCSObjectTest, InterfaceRemove)
505+{
506+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
507+
508+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
509+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 1);
510+
511+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
512+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 1);
513+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 1);
514+
515+ ccsObjectRemoveInterface (to, 1);
516+
517+ EXPECT_EQ (NULL, ((CCSObject *) to)->interfaces);
518+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 0);
519+ EXPECT_EQ (NULL, ((CCSObject *) to)->interface_types);
520+
521+ free (to);
522+}
523+
524+TEST(CCSObjectTest, InterfaceFetchCall)
525+{
526+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
527+
528+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
529+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 1);
530+
531+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
532+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 1);
533+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 1);
534+
535+ const DummyInterface *myDummyInterface = (const DummyInterface *) ccsObjectGetInterface (to, 1);
536+
537+ EXPECT_CALL (GoogleMockDummy::_mockDummy, dummyFunc ());
538+
539+ (*myDummyInterface->dummy) ();
540+
541+ ccsObjectRemoveInterface (to, 1);
542+
543+ EXPECT_EQ (NULL, ((CCSObject *) to)->interfaces);
544+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 0);
545+ EXPECT_EQ (NULL, ((CCSObject *) to)->interface_types);
546+
547+ free (to);
548+}
549+
550+TEST(CCSObjectTest, SetPrivateGetPrivate)
551+{
552+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
553+
554+ int i = 1;
555+
556+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
557+ ccsObjectSetPrivate (to, (CCSPrivate *) &i);
558+
559+ CCSPrivate *p = ccsObjectGetPrivate (to);
560+
561+ EXPECT_EQ (&i, (int *) p);
562+ EXPECT_EQ (i, (*((int *) p)));
563+}
564+
565+void ccsFreeTestingObjectWrapper (TestingObjectWrapper *wrapper)
566+{
567+ GoogleMockDummy::_mockDummy.freeTestingObjectWrapper (wrapper);
568+}
569+
570+#define CCSREF_OBJ(type,dtype) \
571+ void ccs##type##Ref (dtype *d) \
572+ { \
573+ ccsObjectRef (d); \
574+ } \
575+ \
576+ void ccs##type##Unref (dtype *d) \
577+ { \
578+ ccsObjectUnref (d, ccsFree##type); \
579+ } \
580+
581+CCSREF_HDR(TestingObjectWrapper, TestingObjectWrapper);
582+CCSREF_OBJ(TestingObjectWrapper, TestingObjectWrapper);
583+
584+TEST(CCSObjectTest, TestRefUnrefFreesObject)
585+{
586+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
587+
588+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
589+ ccsTestingObjectWrapperRef (to);
590+
591+ EXPECT_CALL (GoogleMockDummy::_mockDummy, freeTestingObjectWrapper (to));
592+
593+ ccsTestingObjectWrapperUnref (to);
594+}
595+
596+TEST(CCSObjectTest, TestFinalizeFreesEverything)
597+{
598+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
599+
600+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
601+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 1);
602+
603+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
604+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 1);
605+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 1);
606+
607+ int *i = (int *) malloc (sizeof (int));
608+
609+ *i = 1;
610+
611+ ccsObjectSetPrivate (to, (CCSPrivate *) i);
612+
613+ CCSPrivate *p = ccsObjectGetPrivate (to);
614+
615+ EXPECT_EQ (i, (int *) p);
616+ EXPECT_EQ (*i, (*((int *) p)));
617+
618+ ccsObjectFinalize (to);
619+
620+ EXPECT_EQ (NULL, ((CCSObject *) to)->priv);
621+ EXPECT_EQ (NULL, ((CCSObject *) to)->interfaces);
622+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 0);
623+ EXPECT_EQ (NULL, ((CCSObject *) to)->interface_types);
624+}
625+
626+void *
627+failingRealloc (void *p, size_t n)
628+{
629+ return NULL;
630+}
631+
632+CCSObjectAllocationInterface failingAllocator =
633+{
634+ failingRealloc,
635+ malloc,
636+ calloc
637+};
638+
639+TEST(CCSObjectTest, TestReallocFailures)
640+{
641+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
642+
643+ ccsObjectInit (to, &failingAllocator);
644+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 1);
645+
646+ EXPECT_EQ (NULL, ((CCSObject *) to)->interfaces);
647+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 0);
648+ EXPECT_EQ (NULL, ((CCSObject *) to)->interface_types);
649+
650+ free (to);
651+}
652+
653+TEST(CCSObjectTest, TestAllocatedMemoryOnRemove)
654+{
655+ TestingObjectWrapper *to = (TestingObjectWrapper *) calloc (1, sizeof (TestingObjectWrapper));
656+
657+ ccsObjectInit (to, &ccsDefaultObjectAllocator);
658+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 1);
659+
660+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
661+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 1);
662+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 1);
663+
664+ ccsObjectAddInterface (to, (const CCSInterface *) &SomeDummyInterface, 2);
665+
666+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
667+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 2);
668+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 1);
669+
670+ ccsObjectRemoveInterface (to, 1);
671+
672+ EXPECT_EQ (*((CCSObject *) to)->interfaces, (const CCSInterface *) (&SomeDummyInterface));
673+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 1);
674+ EXPECT_EQ (((CCSObject *) to)->n_allocated_interfaces, 2);
675+ EXPECT_EQ (*((CCSObject *) to)->interface_types, 2);
676+
677+ ccsObjectRemoveInterface (to, 2);
678+
679+ EXPECT_EQ (NULL, ((CCSObject *) to)->interfaces);
680+ EXPECT_EQ (((CCSObject *) to)->n_interfaces, 0);
681+ EXPECT_EQ (((CCSObject *) to)->n_allocated_interfaces, 0);
682+ EXPECT_EQ (NULL, ((CCSObject *) to)->interface_types);
683+
684+ free (to);
685+}

Subscribers

People subscribed via source and target branches