bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_packet.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
f0a42b33 20import utils
838a5a52 21from utils import run_in_component_init
f0a42b33
FD
22from bt2 import stream as bt2_stream
23from bt2 import field as bt2_field
9cf643d1
PP
24
25
26class PacketTestCase(unittest.TestCase):
838a5a52
SM
27 @staticmethod
28 def _create_packet(with_pc):
29 def create_tc_cc(comp_self):
30 cc = comp_self._create_clock_class(frequency=1000, name='my_cc')
31 tc = comp_self._create_trace_class()
32 return cc, tc
9cf643d1 33
838a5a52 34 clock_class, tc = run_in_component_init(create_tc_cc)
9cf643d1
PP
35
36 # stream event context
838a5a52 37 sec = tc.create_structure_field_class()
45c51519 38 sec += [
838a5a52
SM
39 ('cpu_id', tc.create_signed_integer_field_class(8)),
40 ('stuff', tc.create_real_field_class()),
45c51519 41 ]
9cf643d1
PP
42
43 # packet context
838a5a52 44 pc = None
9cf643d1 45 if with_pc:
838a5a52 46 pc = tc.create_structure_field_class()
45c51519 47 pc += [
838a5a52
SM
48 ('something', tc.create_signed_integer_field_class(8)),
49 ('something_else', tc.create_real_field_class()),
50 ('events_discarded', tc.create_unsigned_integer_field_class(64)),
51 ('packet_seq_num', tc.create_unsigned_integer_field_class(64)),
45c51519 52 ]
9cf643d1
PP
53
54 # stream class
cfbd7cf3
FD
55 sc = tc.create_stream_class(
56 default_clock_class=clock_class,
57 event_common_context_field_class=sec,
58 packet_context_field_class=pc,
59 supports_packets=True,
60 )
9cf643d1
PP
61
62 # event context
838a5a52 63 ec = tc.create_structure_field_class()
45c51519 64 ec += [
838a5a52
SM
65 ('ant', tc.create_signed_integer_field_class(16)),
66 ('msg', tc.create_string_field_class()),
45c51519 67 ]
9cf643d1
PP
68
69 # event payload
838a5a52 70 ep = tc.create_structure_field_class()
45c51519 71 ep += [
838a5a52
SM
72 ('giraffe', tc.create_signed_integer_field_class(32)),
73 ('gnu', tc.create_signed_integer_field_class(8)),
74 ('mosquito', tc.create_signed_integer_field_class(8)),
45c51519 75 ]
9cf643d1
PP
76
77 # event class
838a5a52
SM
78 event_class = sc.create_event_class(name='ec', payload_field_class=ep)
79 event_class.common_context_field_class = ec
9cf643d1 80
838a5a52
SM
81 # trace
82 trace = tc()
9cf643d1
PP
83
84 # stream
838a5a52 85 stream = trace.create_stream(sc)
9cf643d1
PP
86
87 # packet
838a5a52 88 return stream.create_packet(), stream, pc
9cf643d1
PP
89
90 def test_attr_stream(self):
838a5a52
SM
91 packet, stream, _ = self._create_packet(with_pc=True)
92 self.assertEqual(packet.stream.addr, stream.addr)
f0a42b33
FD
93 self.assertIs(type(packet.stream), bt2_stream._Stream)
94
95 def test_const_attr_stream(self):
96 packet = utils.get_const_packet_beginning_message().packet
97 self.assertIs(type(packet.stream), bt2_stream._StreamConst)
9cf643d1 98
838a5a52
SM
99 def test_context_field(self):
100 packet, stream, pc_fc = self._create_packet(with_pc=True)
d8e2073c 101 self.assertEqual(packet.context_field.cls.addr, pc_fc.addr)
f0a42b33
FD
102 self.assertIs(type(packet.context_field), bt2_field._StructureField)
103
104 def test_const_context_field(self):
105 packet = utils.get_const_packet_beginning_message().packet
106 self.assertIs(type(packet.context_field), bt2_field._StructureFieldConst)
9cf643d1
PP
107
108 def test_no_context_field(self):
838a5a52 109 packet, _, _ = self._create_packet(with_pc=False)
9cf643d1 110 self.assertIsNone(packet.context_field)
This page took 0.050258 seconds and 4 git commands to generate.