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

Proposed by Sam Spilsbury on 2012-05-07
Status: Superseded
Proposed branch: lp:~smspillaz/compiz-libcompizconfig/ccs-object
Merge into: lp:~compiz-team/compiz-libcompizconfig/0.9.8
Diff against target: 690 lines (+613/-1)
6 files modified
CMakeLists.txt (+1/-0)
include/ccs.h (+101/-1)
src/CMakeLists.txt (+3/-0)
src/main.c (+179/-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
Sam Spilsbury Needs Fixing on 2012-05-07
Alan Griffiths 2012-05-07 Pending
Review via email: mp+104864@code.launchpad.net

This proposal supersedes 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.
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;

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.

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
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.

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.

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

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
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

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.

Sam Spilsbury (smspillaz) wrote : Posted in a previous version of this proposal

> 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

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

> > 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 ?

Ah, I wrongly assumed you were using a function with different semantics to avoid the problems discussed earlier (i.e. invalidating the object on failed realloc).

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

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
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

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
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

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 on 2012-05-07

Free interfaces once objects are removed

438. By Sam Spilsbury on 2012-05-07

Fix potential macro problems

Sam Spilsbury (smspillaz) wrote :

Keeping old status

review: Needs Fixing
Sam Spilsbury (smspillaz) wrote :

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

Fixed as discussed

> 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.

Not an issue as discussed on irc. (realloc (foo, sizeof (whatever)); doesn't invalidate foo if realloc fails)

> Is there really value in CCSObjectAllocationInterface?

Yes, for mocking allocation failures. Feel crap having to inject it, but there isn't much of an alternative

> 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).

Fixed as discussed

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

If / when we add proper constructor / destructor semantics this will go away :) (That's a larger change though that should be done later)

439. By Sam Spilsbury on 2012-05-07

Lose the ;

440. By Sam Spilsbury on 2012-05-07

Added a free thread

441. By Sam Spilsbury on 2012-05-07

Also test for free

Unmerged revisions

441. By Sam Spilsbury on 2012-05-07

Also test for free

440. By Sam Spilsbury on 2012-05-07

Added a free thread

439. By Sam Spilsbury on 2012-05-07

Lose the ;

438. By Sam Spilsbury on 2012-05-07

Fix potential macro problems

437. By Sam Spilsbury on 2012-05-07

Free interfaces once objects are removed

436. By Sam Spilsbury on 2012-05-04

Don't use reserved name

435. By Sam Spilsbury on 2012-05-04

Make the CCSObject API a bit more typesafe

434. By Sam Spilsbury on 2012-05-02

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

433. By Sam Spilsbury on 2012-04-28

Added a simple type registrar

432. By Sam Spilsbury on 2012-04-28

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

Subscribers

People subscribed via source and target branches