Commit | Line | Data |
---|---|---|
9cf643d1 | 1 | from collections import OrderedDict |
9cf643d1 | 2 | import unittest |
838a5a52 | 3 | from utils import run_in_component_init |
9cf643d1 PP |
4 | |
5 | ||
6 | class PacketTestCase(unittest.TestCase): | |
838a5a52 SM |
7 | @staticmethod |
8 | def _create_packet(with_pc): | |
9 | def create_tc_cc(comp_self): | |
10 | cc = comp_self._create_clock_class(frequency=1000, name='my_cc') | |
11 | tc = comp_self._create_trace_class() | |
12 | return cc, tc | |
9cf643d1 | 13 | |
838a5a52 | 14 | clock_class, tc = run_in_component_init(create_tc_cc) |
9cf643d1 PP |
15 | |
16 | # stream event context | |
838a5a52 | 17 | sec = tc.create_structure_field_class() |
9cf643d1 | 18 | sec += OrderedDict(( |
838a5a52 SM |
19 | ('cpu_id', tc.create_signed_integer_field_class(8)), |
20 | ('stuff', tc.create_real_field_class()), | |
9cf643d1 PP |
21 | )) |
22 | ||
23 | # packet context | |
838a5a52 | 24 | pc = None |
9cf643d1 | 25 | if with_pc: |
838a5a52 | 26 | pc = tc.create_structure_field_class() |
9cf643d1 | 27 | pc += OrderedDict(( |
838a5a52 SM |
28 | ('something', tc.create_signed_integer_field_class(8)), |
29 | ('something_else', tc.create_real_field_class()), | |
30 | ('events_discarded', tc.create_unsigned_integer_field_class(64)), | |
31 | ('packet_seq_num', tc.create_unsigned_integer_field_class(64)), | |
9cf643d1 | 32 | )) |
9cf643d1 PP |
33 | |
34 | # stream class | |
838a5a52 SM |
35 | sc = tc.create_stream_class(default_clock_class=clock_class, |
36 | event_common_context_field_class=sec, | |
37 | packet_context_field_class=pc) | |
9cf643d1 PP |
38 | |
39 | # event context | |
838a5a52 | 40 | ec = tc.create_structure_field_class() |
9cf643d1 | 41 | ec += OrderedDict(( |
838a5a52 SM |
42 | ('ant', tc.create_signed_integer_field_class(16)), |
43 | ('msg', tc.create_string_field_class()), | |
9cf643d1 PP |
44 | )) |
45 | ||
46 | # event payload | |
838a5a52 | 47 | ep = tc.create_structure_field_class() |
9cf643d1 | 48 | ep += OrderedDict(( |
838a5a52 SM |
49 | ('giraffe', tc.create_signed_integer_field_class(32)), |
50 | ('gnu', tc.create_signed_integer_field_class(8)), | |
51 | ('mosquito', tc.create_signed_integer_field_class(8)), | |
9cf643d1 PP |
52 | )) |
53 | ||
54 | # event class | |
838a5a52 SM |
55 | event_class = sc.create_event_class(name='ec', payload_field_class=ep) |
56 | event_class.common_context_field_class = ec | |
9cf643d1 | 57 | |
838a5a52 SM |
58 | # trace |
59 | trace = tc() | |
9cf643d1 PP |
60 | |
61 | # stream | |
838a5a52 | 62 | stream = trace.create_stream(sc) |
9cf643d1 PP |
63 | |
64 | # packet | |
838a5a52 | 65 | return stream.create_packet(), stream, pc |
9cf643d1 PP |
66 | |
67 | def test_attr_stream(self): | |
838a5a52 SM |
68 | packet, stream, _ = self._create_packet(with_pc=True) |
69 | self.assertEqual(packet.stream.addr, stream.addr) | |
9cf643d1 | 70 | |
838a5a52 SM |
71 | def test_context_field(self): |
72 | packet, stream, pc_fc = self._create_packet(with_pc=True) | |
73 | self.assertEqual(packet.context_field.field_class.addr, pc_fc.addr) | |
9cf643d1 PP |
74 | |
75 | def test_no_context_field(self): | |
838a5a52 | 76 | packet, _, _ = self._create_packet(with_pc=False) |
9cf643d1 | 77 | self.assertIsNone(packet.context_field) |