test_everything.py 22.7 KB
Newer Older
Tomeu Vizoso's avatar
Tomeu Vizoso committed
1
# -*- Mode: Python; py-indent-offset: 4 -*-
2
# coding=utf-8
Tomeu Vizoso's avatar
Tomeu Vizoso committed
3
4
5
6
7
8
# vim: tabstop=4 shiftwidth=4 expandtab

import unittest

import sys
sys.path.insert(0, "../")
9
from sys import getrefcount
Tomeu Vizoso's avatar
Tomeu Vizoso committed
10

11
import copy
12
13
14
15
16
try:
    import cairo
    has_cairo = True
except ImportError:
    has_cairo = False
Tomeu Vizoso's avatar
Tomeu Vizoso committed
17

18
from gi.repository import GObject
19
from gi.repository import GLib
20
from gi.repository import Gio
21
from gi.repository import Regress as Everything
Tomeu Vizoso's avatar
Tomeu Vizoso committed
22

23
24
25
26
27
28
29
if sys.version_info < (3, 0):
    UNICHAR = "\xe2\x99\xa5"
    PY2_UNICODE_UNICHAR = unicode(UNICHAR, 'UTF-8')
else:
    UNICHAR = "鈾"


Tomeu Vizoso's avatar
Tomeu Vizoso committed
30
31
class TestEverything(unittest.TestCase):

32
    @unittest.skipUnless(has_cairo, 'built without cairo support')
Tomeu Vizoso's avatar
Tomeu Vizoso committed
33
34
35
36
37
38
39
40
    def test_cairo_context(self):
        context = Everything.test_cairo_context_full_return()
        self.assertTrue(isinstance(context, cairo.Context))

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        Everything.test_cairo_context_none_in(context)

41
    @unittest.skipUnless(has_cairo, 'built without cairo support')
Tomeu Vizoso's avatar
Tomeu Vizoso committed
42
43
44
45
    def test_cairo_surface(self):
        surface = Everything.test_cairo_surface_none_return()
        self.assertTrue(isinstance(surface, cairo.ImageSurface))
        self.assertTrue(isinstance(surface, cairo.Surface))
46
47
48
        self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
        self.assertEqual(surface.get_width(), 10)
        self.assertEqual(surface.get_height(), 10)
Tomeu Vizoso's avatar
Tomeu Vizoso committed
49
50
51
52

        surface = Everything.test_cairo_surface_full_return()
        self.assertTrue(isinstance(surface, cairo.ImageSurface))
        self.assertTrue(isinstance(surface, cairo.Surface))
53
54
55
        self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
        self.assertEqual(surface.get_width(), 10)
        self.assertEqual(surface.get_height(), 10)
Tomeu Vizoso's avatar
Tomeu Vizoso committed
56
57
58
59
60
61
62

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        Everything.test_cairo_surface_none_in(surface)

        surface = Everything.test_cairo_surface_full_out()
        self.assertTrue(isinstance(surface, cairo.ImageSurface))
        self.assertTrue(isinstance(surface, cairo.Surface))
63
64
65
        self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
        self.assertEqual(surface.get_width(), 10)
        self.assertEqual(surface.get_height(), 10)
Tomeu Vizoso's avatar
Tomeu Vizoso committed
66

Paolo Borelli's avatar
Paolo Borelli committed
67
    def test_unichar(self):
68
        self.assertEqual("c", Everything.test_unichar("c"))
69
70

        if sys.version_info < (3, 0):
71
72
            self.assertEqual(UNICHAR, Everything.test_unichar(PY2_UNICODE_UNICHAR))
        self.assertEqual(UNICHAR, Everything.test_unichar(UNICHAR))
73
74
        self.assertRaises(TypeError, Everything.test_unichar, "")
        self.assertRaises(TypeError, Everything.test_unichar, "morethanonechar")
75

76
    def test_floating(self):
77
        e = Everything.TestFloating()
78
        self.assertEqual(e.__grefcount__, 1)
79
80

        e = GObject.new(Everything.TestFloating)
81
        self.assertEqual(e.__grefcount__, 1)
82
83

        e = Everything.TestFloating.new()
84
        self.assertEqual(e.__grefcount__, 1)
85

86
87
88
89
90
91
92
93
94
    def test_caller_allocates(self):
        struct_a = Everything.TestStructA()
        struct_a.some_int = 10
        struct_a.some_int8 = 21
        struct_a.some_double = 3.14
        struct_a.some_enum = Everything.TestEnum.VALUE3

        struct_a_clone = struct_a.clone()
        self.assertTrue(struct_a != struct_a_clone)
95
96
97
98
        self.assertEqual(struct_a.some_int, struct_a_clone.some_int)
        self.assertEqual(struct_a.some_int8, struct_a_clone.some_int8)
        self.assertEqual(struct_a.some_double, struct_a_clone.some_double)
        self.assertEqual(struct_a.some_enum, struct_a_clone.some_enum)
99
100
101
102
103
104
105
106
107
108

        struct_b = Everything.TestStructB()
        struct_b.some_int8 = 8
        struct_b.nested_a.some_int = 20
        struct_b.nested_a.some_int8 = 12
        struct_b.nested_a.some_double = 333.3333
        struct_b.nested_a.some_enum = Everything.TestEnum.VALUE2

        struct_b_clone = struct_b.clone()
        self.assertTrue(struct_b != struct_b_clone)
109
110
111
112
113
        self.assertEqual(struct_b.some_int8, struct_b_clone.some_int8)
        self.assertEqual(struct_b.nested_a.some_int, struct_b_clone.nested_a.some_int)
        self.assertEqual(struct_b.nested_a.some_int8, struct_b_clone.nested_a.some_int8)
        self.assertEqual(struct_b.nested_a.some_double, struct_b_clone.nested_a.some_double)
        self.assertEqual(struct_b.nested_a.some_enum, struct_b_clone.nested_a.some_enum)
114

115
116
117
    def test_wrong_type_of_arguments(self):
        try:
            Everything.test_int8()
118
119
        except TypeError:
            (e_type, e) = sys.exc_info()[:2]
120
            self.assertEqual(e.args, ("test_int8() takes exactly 1 argument (0 given)",))
121

122
123
124
    def test_gtypes(self):
        gchararray_gtype = GObject.type_from_name('gchararray')
        gtype = Everything.test_gtype(str)
125
        self.assertEqual(gchararray_gtype, gtype)
126
        gtype = Everything.test_gtype('gchararray')
127
        self.assertEqual(gchararray_gtype, gtype)
128
129
        gobject_gtype = GObject.GObject.__gtype__
        gtype = Everything.test_gtype(GObject.GObject)
130
        self.assertEqual(gobject_gtype, gtype)
131
        gtype = Everything.test_gtype('GObject')
132
        self.assertEqual(gobject_gtype, gtype)
133
134
135
136
137
138
139
140
141
142
143
        self.assertRaises(TypeError, Everything.test_gtype, 'invalidgtype')

        class NotARegisteredClass(object):
            pass

        self.assertRaises(TypeError, Everything.test_gtype, NotARegisteredClass)

        class ARegisteredClass(GObject.GObject):
            __gtype_name__ = 'EverythingTestsARegisteredClass'

        gtype = Everything.test_gtype('EverythingTestsARegisteredClass')
144
        self.assertEqual(ARegisteredClass.__gtype__, gtype)
145
        gtype = Everything.test_gtype(ARegisteredClass)
146
        self.assertEqual(ARegisteredClass.__gtype__, gtype)
147
        self.assertRaises(TypeError, Everything.test_gtype, 'ARegisteredClass')
148

149
150
    def test_dir(self):
        attr_list = dir(Everything)
151

152
153
        # test that typelib attributes are listed
        self.assertTrue('TestStructA' in attr_list)
154

155
156
157
158
        # test that class attributes and methods are listed
        self.assertTrue('__class__' in attr_list)
        self.assertTrue('__dir__' in attr_list)
        self.assertTrue('__repr__' in attr_list)
159

160
161
        # test that instance members are listed
        self.assertTrue('_namespace' in attr_list)
162
        self.assertTrue('_version' in attr_list)
163

164
165
        # test that there are no duplicates returned
        self.assertEqual(len(attr_list), len(set(attr_list)))
166

Xavier Claessens's avatar
Xavier Claessens committed
167
    def test_ptrarray(self):
168
169
        # transfer container
        result = Everything.test_garray_container_return()
170
        self.assertEqual(result, ['regress'])
171
172
173
174
        result = None

        # transfer full
        result = Everything.test_garray_full_return()
175
        self.assertEqual(result, ['regress'])
176
        result = None
Xavier Claessens's avatar
Xavier Claessens committed
177

178
179
    def test_hash_return(self):
        result = Everything.test_ghash_gvalue_return()
180
181
182
        self.assertEqual(result['integer'], 12)
        self.assertEqual(result['boolean'], True)
        self.assertEqual(result['string'], 'some text')
183
        self.assertEqual(result['strings'], ['first', 'second', 'third'])
184
185
        self.assertEqual(result['flags'], Everything.TestFlags.FLAG1 | Everything.TestFlags.FLAG3)
        self.assertEqual(result['enum'], Everything.TestEnum.VALUE2)
186
187
        result = None

188
    def test_hash_in(self):
189
190
191
        # specifying a simple string array for "strings" does not work due to
        # https://bugzilla.gnome.org/show_bug.cgi?id=666636
        # workaround by explicitly building a GStrv object
192
193
194
        class GStrv(list):
            __gtype__ = GObject.type_from_name('GStrv')

195
196
197
198
199
200
201
        data = {'integer': 12,
                'boolean': True,
                'string': 'some text',
                'strings': GStrv(['first', 'second', 'third']),
                'flags': Everything.TestFlags.FLAG1 | Everything.TestFlags.FLAG3,
                'enum': Everything.TestEnum.VALUE2,
               }
202
203
        Everything.test_ghash_gvalue_in(data)
        data = None
204

205
206
207
208
209
210
211
212
213
214
    def test_struct_gpointer(self):
        l1 = GLib.List()
        self.assertEqual(l1.data, None)
        init_refcount = getrefcount(l1)

        l1.data = 'foo'
        self.assertEqual(l1.data, 'foo')

        l2 = l1
        self.assertEqual(l1.data, l2.data)
215
        self.assertEqual(getrefcount(l1), init_refcount + 1)
216
217
218
219
220
221

        l3 = copy.copy(l1)
        l3.data = 'bar'
        self.assertEqual(l1.data, 'foo')
        self.assertEqual(l2.data, 'foo')
        self.assertEqual(l3.data, 'bar')
222
223
        self.assertEqual(getrefcount(l1), init_refcount + 1)
        self.assertEqual(getrefcount(l3), init_refcount)
224

225

226
227
228
229
230
231
232
class TestNullableArgs(unittest.TestCase):
    def test_in_nullable_hash(self):
        Everything.test_ghash_null_in(None)

    def test_in_nullable_list(self):
        Everything.test_gslist_null_in(None)
        Everything.test_glist_null_in(None)
233
234
        Everything.test_gslist_null_in([])
        Everything.test_glist_null_in([])
235
236

    def test_in_nullable_array(self):
237
        Everything.test_array_int_null_in(None)
238
        Everything.test_array_int_null_in([])
239
240
241
242

    def test_in_nullable_string(self):
        Everything.test_utf8_null_in(None)

243
    def test_in_nullable_object(self):
244
        Everything.func_obj_null_in(None)
245

246
247
248
249
    def test_out_nullable_hash(self):
        self.assertEqual(None, Everything.test_ghash_null_out())

    def test_out_nullable_list(self):
250
251
        self.assertEqual([], Everything.test_gslist_null_out())
        self.assertEqual([], Everything.test_glist_null_out())
252
253

    def test_out_nullable_array(self):
254
        self.assertEqual([], Everything.test_array_int_null_out())
255
256
257
258

    def test_out_nullable_string(self):
        self.assertEqual(None, Everything.test_utf8_null_out())

259
    def test_out_nullable_object(self):
260
        self.assertEqual(None, Everything.TestObj.null_out())
261

262

263
264
class TestCallbacks(unittest.TestCase):
    called = False
265
    main_loop = GObject.MainLoop()
Tomeu Vizoso's avatar
Tomeu Vizoso committed
266

267
    def test_callback(self):
268
        TestCallbacks.called = False
269

270
271
        def callback():
            TestCallbacks.called = True
272

273
274
275
        Everything.test_simple_callback(callback)
        self.assertTrue(TestCallbacks.called)

276
    def test_callback_exception(self):
277
278
279
280
281
282
        """
        This test ensures that we get errors from callbacks correctly
        and in particular that we do not segv when callbacks fail
        """
        def callback():
            x = 1 / 0
283
            self.fail('unexpected surviving zero divsion:' + str(x))
284

285
286
287
288
        # note that we do NOT expect the ZeroDivisionError to be propagated
        # through from the callback, as it crosses the Python<->C boundary
        # twice. (See GNOME #616279)
        Everything.test_simple_callback(callback)
289

290
    def test_double_callback_exception(self):
291
292
293
294
295
296
        """
        This test ensures that we get errors from callbacks correctly
        and in particular that we do not segv when callbacks fail
        """
        def badcallback():
            x = 1 / 0
297
            self.fail('unexpected surviving zero divsion:' + str(x))
298
299
300
301
302
303

        def callback():
            Everything.test_boolean(True)
            Everything.test_boolean(False)
            Everything.test_simple_callback(badcallback())

304
305
306
307
        # note that we do NOT expect the ZeroDivisionError to be propagated
        # through from the callback, as it crosses the Python<->C boundary
        # twice. (See GNOME #616279)
        Everything.test_simple_callback(callback)
308

309
    def test_return_value_callback(self):
310
        TestCallbacks.called = False
311

312
313
314
315
        def callback():
            TestCallbacks.called = True
            return 44

316
        self.assertEqual(Everything.test_callback(callback), 44)
317
        self.assertTrue(TestCallbacks.called)
318

319
    def test_callback_async(self):
320
        TestCallbacks.called = False
321

322
323
324
325
        def callback(foo):
            TestCallbacks.called = True
            return foo

Johan Dahlin's avatar
Johan Dahlin committed
326
327
        Everything.test_callback_async(callback, 44)
        i = Everything.test_callback_thaw_async()
328
        self.assertEqual(44, i)
329
        self.assertTrue(TestCallbacks.called)
330

331
    def test_callback_scope_call(self):
332
        TestCallbacks.called = 0
333

334
335
336
337
338
        def callback():
            TestCallbacks.called += 1
            return 0

        Everything.test_multi_callback(callback)
339
        self.assertEqual(TestCallbacks.called, 2)
340

341
    def test_callback_userdata(self):
342
        TestCallbacks.called = 0
343

344
        def callback(userdata):
345
            self.assertEqual(userdata, "Test%d" % TestCallbacks.called)
346
347
            TestCallbacks.called += 1
            return TestCallbacks.called
348

349
350
        for i in range(100):
            val = Everything.test_callback_user_data(callback, "Test%d" % i)
351
            self.assertEqual(val, i + 1)
352

353
        self.assertEqual(TestCallbacks.called, 100)
Tomeu Vizoso's avatar
Tomeu Vizoso committed
354

355
    def test_callback_userdata_refcount(self):
356
        TestCallbacks.called = False
357

358
359
360
361
362
363
364
365
366
        def callback(userdata):
            TestCallbacks.called = True
            return 1

        ud = "Test User Data"

        start_ref_count = getrefcount(ud)
        for i in range(100):
            Everything.test_callback_destroy_notify(callback, ud)
367

368
369
370
        Everything.test_callback_thaw_notifications()
        end_ref_count = getrefcount(ud)

371
        self.assertEqual(start_ref_count, end_ref_count)
372

373
    def test_async_ready_callback(self):
Tomeu Vizoso's avatar
Tomeu Vizoso committed
374
        TestCallbacks.called = False
375
        TestCallbacks.main_loop = GObject.MainLoop()
Tomeu Vizoso's avatar
Tomeu Vizoso committed
376
377
378
379
380
381
382
383
384
385

        def callback(obj, result, user_data):
            TestCallbacks.main_loop.quit()
            TestCallbacks.called = True

        Everything.test_async_ready_callback(callback)

        TestCallbacks.main_loop.run()

        self.assertTrue(TestCallbacks.called)
Tomeu Vizoso's avatar
Tomeu Vizoso committed
386

387
    def test_callback_destroy_notify(self):
Tomeu Vizoso's avatar
Tomeu Vizoso committed
388
389
390
391
392
        def callback(user_data):
            TestCallbacks.called = True
            return 42

        TestCallbacks.called = False
393
        self.assertEqual(Everything.test_callback_destroy_notify(callback, 42), 42)
Tomeu Vizoso's avatar
Tomeu Vizoso committed
394
        self.assertTrue(TestCallbacks.called)
395
        self.assertEqual(Everything.test_callback_thaw_notifications(), 42)
396

397
    def test_callback_in_methods(self):
398
399
400
401
        object_ = Everything.TestObj()

        def callback():
            TestCallbacks.called = True
402
            return 42
403
404
405
406
407
408
409
410
411
412
413

        TestCallbacks.called = False
        object_.instance_method_callback(callback)
        self.assertTrue(TestCallbacks.called)

        TestCallbacks.called = False
        Everything.TestObj.static_method_callback(callback)
        self.assertTrue(TestCallbacks.called)

        def callbackWithUserData(user_data):
            TestCallbacks.called = True
414
            return 42
415
416

        TestCallbacks.called = False
417
        Everything.TestObj.new_callback(callbackWithUserData, None)
418
        self.assertTrue(TestCallbacks.called)
419

420
    def test_callback_none(self):
421
        # make sure this doesn't assert or crash
422
        Everything.test_simple_callback(None)
423

424
    def test_callback_gerror(self):
425
426
427
428
429
430
431
432
433
434
        def callback(error):
            self.assertEqual(error.message, 'regression test error')
            self.assertTrue('g-io' in error.domain)
            self.assertEqual(error.code, Gio.IOErrorEnum.NOT_SUPPORTED)
            TestCallbacks.called = True

        TestCallbacks.called = False
        Everything.test_gerror_callback(callback)
        self.assertTrue(TestCallbacks.called)

435
436
437
438
439
440
441
442
443
    def test_callback_null_gerror(self):
        def callback(error):
            self.assertEqual(error, None)
            TestCallbacks.called = True

        TestCallbacks.called = False
        Everything.test_null_gerror_callback(callback)
        self.assertTrue(TestCallbacks.called)

444
    def test_callback_owned_gerror(self):
445
446
447
448
449
450
451
452
453
454
        def callback(error):
            self.assertEqual(error.message, 'regression test owned error')
            self.assertTrue('g-io' in error.domain)
            self.assertEqual(error.code, Gio.IOErrorEnum.PERMISSION_DENIED)
            TestCallbacks.called = True

        TestCallbacks.called = False
        Everything.test_owned_gerror_callback(callback)
        self.assertTrue(TestCallbacks.called)

455
    def test_callback_hashtable(self):
456
457
458
459
460
        def callback(data):
            self.assertEqual(data, mydict)
            mydict['new'] = 42
            TestCallbacks.called = True

461
        mydict = {'foo': 1, 'bar': 2}
462
463
464
        TestCallbacks.called = False
        Everything.test_hash_table_callback(mydict, callback)
        self.assertTrue(TestCallbacks.called)
465
466
        self.assertEqual(mydict, {'foo': 1, 'bar': 2, 'new': 42})

467

Martin Pitt's avatar
Martin Pitt committed
468
469
470
471
class TestClosures(unittest.TestCase):
    def test_int_arg(self):
        def callback(num):
            self.called = True
472
            return num + 1
Martin Pitt's avatar
Martin Pitt committed
473
474
475
476
477
478
479

        self.called = False
        result = Everything.test_closure_one_arg(callback, 42)
        self.assertTrue(self.called)
        self.assertEqual(result, 43)

    # https://bugzilla.gnome.org/show_bug.cgi?id=656554
480

Martin Pitt's avatar
Martin Pitt committed
481
482
483
484
485
486
487
488
489
490
491
492
    @unittest.expectedFailure
    def test_variant(self):
        def callback(variant):
            self.assertEqual(variant.get_type_string(), 'i')
            self.called = True
            return GLib.Variant('i', variant.get_int32() + 1)

        self.called = False
        result = Everything.test_closure_variant(callback, GLib.Variant('i', 42))
        self.assertTrue(self.called)
        self.assertEqual(result.get_type_string(), 'i')
        self.assertEqual(result.get_int32(), 43)
493

494

495
496
class TestProperties(unittest.TestCase):

497
498
499
    def test_basic(self):
        object_ = Everything.TestObj()

500
        self.assertEqual(object_.props.int, 0)
501
502
        object_.props.int = 42
        self.assertTrue(isinstance(object_.props.int, int))
503
        self.assertEqual(object_.props.int, 42)
504

505
        self.assertEqual(object_.props.float, 0.0)
506
507
        object_.props.float = 42.42
        self.assertTrue(isinstance(object_.props.float, float))
Martin Pitt's avatar
Martin Pitt committed
508
        self.assertAlmostEqual(object_.props.float, 42.42, places=5)
509

510
        self.assertEqual(object_.props.double, 0.0)
511
512
        object_.props.double = 42.42
        self.assertTrue(isinstance(object_.props.double, float))
Martin Pitt's avatar
Martin Pitt committed
513
        self.assertAlmostEqual(object_.props.double, 42.42, places=5)
514

515
        self.assertEqual(object_.props.string, None)
516
517
        object_.props.string = 'mec'
        self.assertTrue(isinstance(object_.props.string, str))
518
        self.assertEqual(object_.props.string, 'mec')
519

520
        self.assertEqual(object_.props.gtype, GObject.TYPE_INVALID)
521
        object_.props.gtype = int
522
        self.assertEqual(object_.props.gtype, GObject.TYPE_INT)
523

524
525
    def test_hash_table(self):
        object_ = Everything.TestObj()
526
        self.assertEqual(object_.props.hash_table, None)
527
528
529

        object_.props.hash_table = {'mec': 56}
        self.assertTrue(isinstance(object_.props.hash_table, dict))
530
        self.assertEqual(list(object_.props.hash_table.items())[0], ('mec', 56))
531
532
533

    def test_list(self):
        object_ = Everything.TestObj()
534
        self.assertEqual(object_.props.list, [])
535
536
537

        object_.props.list = ['1', '2', '3']
        self.assertTrue(isinstance(object_.props.list, list))
538
        self.assertEqual(object_.props.list, ['1', '2', '3'])
539
540
541

    def test_boxed(self):
        object_ = Everything.TestObj()
542
        self.assertEqual(object_.props.boxed, None)
543
544
545
546
547
548

        boxed = Everything.TestBoxed()
        boxed.some_int8 = 42
        object_.props.boxed = boxed

        self.assertTrue(isinstance(object_.props.boxed, Everything.TestBoxed))
549
        self.assertEqual(object_.props.boxed.some_int8, 42)
550

551
552
    def test_gtype(self):
        object_ = Everything.TestObj()
553
        self.assertEqual(object_.props.gtype, GObject.TYPE_INVALID)
554
        object_.props.gtype = int
555
        self.assertEqual(object_.props.gtype, GObject.TYPE_INT)
556

Martin Pitt's avatar
Martin Pitt committed
557
        object_ = Everything.TestObj(gtype=int)
558
        self.assertEqual(object_.props.gtype, GObject.TYPE_INT)
559
        object_.props.gtype = str
560
        self.assertEqual(object_.props.gtype, GObject.TYPE_STRING)
561

562

563
564
565
566
567
568
569
570
571
572
class TestTortureProfile(unittest.TestCase):
    def test_torture_profile(self):
        import time
        total_time = 0
        print("")
        object_ = Everything.TestObj()
        sys.stdout.write("\ttorture test 1 (10000 iterations): ")

        start_time = time.clock()
        for i in range(10000):
573
574
575
            (y, z, q) = object_.torture_signature_0(5000,
                                                    "Torture Test 1",
                                                    12345)
576
577
578
579
580
581
582
583
584
585

        end_time = time.clock()
        delta_time = end_time - start_time
        total_time += delta_time
        print("%f secs" % delta_time)

        sys.stdout.write("\ttorture test 2 (10000 iterations): ")

        start_time = time.clock()
        for i in range(10000):
586
587
            (y, z, q) = Everything.TestObj().torture_signature_0(
                5000, "Torture Test 2", 12345)
588
589
590
591
592
593
594
595
596
597

        end_time = time.clock()
        delta_time = end_time - start_time
        total_time += delta_time
        print("%f secs" % delta_time)

        sys.stdout.write("\ttorture test 3 (10000 iterations): ")
        start_time = time.clock()
        for i in range(10000):
            try:
598
599
                (y, z, q) = object_.torture_signature_1(
                    5000, "Torture Test 3", 12345)
600
601
602
603
604
605
606
607
            except:
                pass
        end_time = time.clock()
        delta_time = end_time - start_time
        total_time += delta_time
        print("%f secs" % delta_time)

        sys.stdout.write("\ttorture test 4 (10000 iterations): ")
608

609
610
611
        def callback(userdata):
            pass

612
        userdata = [1, 2, 3, 4]
613
614
        start_time = time.clock()
        for i in range(10000):
615
616
            (y, z, q) = Everything.test_torture_signature_2(
                5000, callback, userdata, "Torture Test 4", 12345)
617
618
619
620
621
622
623
        end_time = time.clock()
        delta_time = end_time - start_time
        total_time += delta_time
        print("%f secs" % delta_time)
        print("\t====")
        print("\tTotal: %f sec" % total_time)

624

625
626
627
628
629
630
class TestAdvancedInterfaces(unittest.TestCase):
    def test_array_objs(self):
        obj1, obj2 = Everything.test_array_fixed_out_objects()
        self.assertTrue(isinstance(obj1, Everything.TestObj))
        self.assertTrue(isinstance(obj2, Everything.TestObj))
        self.assertNotEqual(obj1, obj2)
631
632

    def test_obj_skip_return_val(self):
Johan Dahlin's avatar
Johan Dahlin committed
633
634
        obj = Everything.TestObj()
        ret = obj.skip_return_val(50, 42.0, 60, 2, 3)
635
636
637
638
        self.assertEqual(len(ret), 3)
        self.assertEqual(ret[0], 51)
        self.assertEqual(ret[1], 61)
        self.assertEqual(ret[2], 32)
639

640
    def test_obj_skip_return_val_no_out(self):
Johan Dahlin's avatar
Johan Dahlin committed
641
        obj = Everything.TestObj()
642
643
644
645
        # raises an error for 0, succeeds for any other value
        self.assertRaises(GLib.GError, obj.skip_return_val_no_out, 0)

        ret = obj.skip_return_val_no_out(1)
646
        self.assertEqual(ret, None)
647

648

649
650
class TestSignals(unittest.TestCase):
    def test_object_param_signal(self):
Johan Dahlin's avatar
Johan Dahlin committed
651
        obj = Everything.TestObj()
652

653
        def callback(obj, obj_param):
654
            self.assertEqual(obj_param.props.int, 3)
655
            self.assertGreater(obj_param.__grefcount__, 1)
656
657
658

        obj.connect('sig-with-obj', callback)
        obj.emit_sig_with_obj()