tests: Add missing copyright headers
[babeltrace.git] / tests / bindings / python / bt2 / test_trace.py
CommitLineData
d2d857a8
MJ
1#
2# Copyright (C) 2019 EfficiOS Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; only version 2
7# of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17#
18
9cf643d1 19import unittest
8c2367b8 20from utils import get_default_trace_class
9cf643d1
PP
21
22
23class TraceTestCase(unittest.TestCase):
24 def setUp(self):
8c2367b8 25 self._tc = get_default_trace_class()
9cf643d1
PP
26
27 def test_create_default(self):
8c2367b8
SM
28 trace = self._tc()
29 self.assertEqual(trace.name, None)
811644b8
PP
30
31 def test_create_full(self):
8c2367b8
SM
32 trace = self._tc(name='my name')
33 self.assertEqual(trace.name, 'my name')
9cf643d1 34
8c2367b8 35 def test_create_invalid_name(self):
9cf643d1 36 with self.assertRaises(TypeError):
8c2367b8 37 self._tc(name=17)
9cf643d1 38
05abc762
PP
39 def test_attr_trace_class(self):
40 trace = self._tc(name='my name')
41 self.assertEqual(trace.cls.addr, self._tc.addr)
42
8c2367b8
SM
43 def test_len(self):
44 trace = self._tc()
45 sc = self._tc.create_stream_class()
46 self.assertEqual(len(trace), 0)
9cf643d1 47
8c2367b8
SM
48 trace.create_stream(sc)
49 self.assertEqual(len(trace), 1)
9cf643d1 50
8c2367b8
SM
51 def _create_trace_with_some_streams(self):
52 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
53 trace = self._tc()
54 trace.create_stream(sc, id=12)
55 trace.create_stream(sc, id=15)
56 trace.create_stream(sc, id=17)
9cf643d1 57
8c2367b8 58 return trace
9cf643d1
PP
59
60 def test_iter(self):
8c2367b8
SM
61 trace = self._create_trace_with_some_streams()
62 stream_ids = set(trace)
63 self.assertEqual(stream_ids, {12, 15, 17})
9cf643d1 64
8c2367b8
SM
65 def test_getitem(self):
66 trace = self._create_trace_with_some_streams()
9cf643d1 67
8c2367b8 68 self.assertEqual(trace[12].id, 12)
9cf643d1 69
8c2367b8
SM
70 def test_getitem_invalid_key(self):
71 trace = self._create_trace_with_some_streams()
9cf643d1 72 with self.assertRaises(KeyError):
8c2367b8 73 trace[18]
9cf643d1 74
8c2367b8
SM
75 def test_destruction_listener(self):
76 def on_trace_class_destruction(trace_class):
77 nonlocal trace_class_destroyed
78 trace_class_destroyed = True
811644b8 79
8c2367b8
SM
80 def on_trace_destruction(trace):
81 nonlocal trace_destroyed
82 trace_destroyed = True
811644b8 83
8c2367b8
SM
84 trace_destroyed = False
85 trace_class_destroyed = False
811644b8 86
8c2367b8
SM
87 trace_class = get_default_trace_class()
88 stream_class = trace_class.create_stream_class()
89 trace = trace_class()
90 stream = trace.create_stream(stream_class)
811644b8 91
8c2367b8
SM
92 trace_class.add_destruction_listener(on_trace_class_destruction)
93 trace.add_destruction_listener(on_trace_destruction)
811644b8 94
8c2367b8
SM
95 self.assertFalse(trace_class_destroyed)
96 self.assertFalse(trace_destroyed)
9cf643d1 97
8c2367b8 98 del trace
9cf643d1 99
8c2367b8
SM
100 self.assertFalse(trace_class_destroyed)
101 self.assertFalse(trace_destroyed)
9cf643d1 102
8c2367b8 103 del stream
9cf643d1 104
8c2367b8
SM
105 self.assertFalse(trace_class_destroyed)
106 self.assertTrue(trace_destroyed)
9cf643d1 107
8c2367b8 108 del trace_class
9cf643d1 109
8c2367b8
SM
110 self.assertFalse(trace_class_destroyed)
111 self.assertTrue(trace_destroyed)
9cf643d1 112
8c2367b8 113 del stream_class
9cf643d1 114
8c2367b8
SM
115 self.assertTrue(trace_class_destroyed)
116 self.assertTrue(trace_destroyed)
This page took 0.047359 seconds and 4 git commands to generate.