tests: Add missing copyright headers
[babeltrace.git] / tests / bindings / python / bt2 / test_trace.py
CommitLineData
32d2d479
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
f377a958 20from utils import get_default_trace_class
9cf643d1
PP
21
22
23class TraceTestCase(unittest.TestCase):
24 def setUp(self):
f377a958 25 self._tc = get_default_trace_class()
9cf643d1
PP
26
27 def test_create_default(self):
f377a958
SM
28 trace = self._tc()
29 self.assertEqual(trace.name, None)
f6a5e476
PP
30
31 def test_create_full(self):
f377a958
SM
32 trace = self._tc(name='my name')
33 self.assertEqual(trace.name, 'my name')
9cf643d1 34
f377a958 35 def test_create_invalid_name(self):
9cf643d1 36 with self.assertRaises(TypeError):
f377a958 37 self._tc(name=17)
9cf643d1 38
a7f0580d
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
f377a958
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
f377a958
SM
48 trace.create_stream(sc)
49 self.assertEqual(len(trace), 1)
9cf643d1 50
f377a958
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
f377a958 58 return trace
9cf643d1
PP
59
60 def test_iter(self):
f377a958
SM
61 trace = self._create_trace_with_some_streams()
62 stream_ids = set(trace)
63 self.assertEqual(stream_ids, {12, 15, 17})
9cf643d1 64
f377a958
SM
65 def test_getitem(self):
66 trace = self._create_trace_with_some_streams()
9cf643d1 67
f377a958 68 self.assertEqual(trace[12].id, 12)
9cf643d1 69
f377a958
SM
70 def test_getitem_invalid_key(self):
71 trace = self._create_trace_with_some_streams()
9cf643d1 72 with self.assertRaises(KeyError):
f377a958 73 trace[18]
9cf643d1 74
f377a958
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
f6a5e476 79
f377a958
SM
80 def on_trace_destruction(trace):
81 nonlocal trace_destroyed
82 trace_destroyed = True
f6a5e476 83
f377a958
SM
84 trace_destroyed = False
85 trace_class_destroyed = False
f6a5e476 86
f377a958
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)
f6a5e476 91
f377a958
SM
92 trace_class.add_destruction_listener(on_trace_class_destruction)
93 trace.add_destruction_listener(on_trace_destruction)
f6a5e476 94
f377a958
SM
95 self.assertFalse(trace_class_destroyed)
96 self.assertFalse(trace_destroyed)
9cf643d1 97
f377a958 98 del trace
9cf643d1 99
f377a958
SM
100 self.assertFalse(trace_class_destroyed)
101 self.assertFalse(trace_destroyed)
9cf643d1 102
f377a958 103 del stream
9cf643d1 104
f377a958
SM
105 self.assertFalse(trace_class_destroyed)
106 self.assertTrue(trace_destroyed)
9cf643d1 107
f377a958 108 del trace_class
9cf643d1 109
f377a958
SM
110 self.assertFalse(trace_class_destroyed)
111 self.assertTrue(trace_destroyed)
9cf643d1 112
f377a958 113 del stream_class
9cf643d1 114
f377a958
SM
115 self.assertTrue(trace_class_destroyed)
116 self.assertTrue(trace_destroyed)
This page took 0.045937 seconds and 4 git commands to generate.