Best Python code snippet using fMBT_python
fgx_encode.py
Source:fgx_encode.py
...215class encoder(object):216 def __init__(self):217 # This is the output generated by calling `encode_gci()`218 self.output = bytearray([0]*0x8c000)219 # `_encode()` must maintain an offset/cursor into the output buffer220 self.offset = 0221 # The total number of iterations we've taken in `_encode()` calls222 self.total_iter = 0223 self.bytes_written = 0224 def _encode(self, count, val):225 result = 0226 num_iter = count227 if (num_iter == 0): return None228 while (count > 0):229 base_offset = (self.total_iter >> 3) & 0x1fffffff230 if (base_offset < 0x8c000):231 mask = self.total_iter & 0x00000007232 if ( val & ( 1 << (num_iter - 1) ) != 0 ):233 self.output[base_offset] = self.output[base_offset] | (1 << mask)234 self.total_iter += 1235 num_iter = num_iter - 1236 count = count - 1237 self.offset += 1238 self.bytes_written += 1239 return self.output[base_offset]240 def encode_gci(self, data):241 """242 Expects a `class replay` filled out by the decoder,243 (or imported from a file, maybe sometime down the road)244 """245 self._encode(8, data.tick)246 # Note that the following loops do not conditionally encode bits247 # based on the value of player_entry_size.248 self._encode(7, data.player_entry_size)249 self._encode(6, data.course_id)250 self._encode(32, data.unk_1)251 self._encode(32, data.unk_2)252 self._encode(5, data.player_array_entries)253 self._encode(3, data.unk_3)254 self._encode(2, data.unk_4)255 self._encode(1, data.unk_5)256 self._encode(7, data.unk_6)257 # Note that the array size is decoupled from player_array_size here258 for entry in data.player_array_dict:259 self._encode(1, entry['is_human'])260 self._encode(6, entry['char_id'])261 self._encode(5, entry['member_0x4'])262 self._encode(7, entry['accel_speed_slider'])263 self._encode(2, entry['member_0x2'])264 self._encode(1, entry['is_custom_ship'])265 if (entry['is_custom_ship'] == 1):266 for i in range(0, 33216):267 self._encode(8, entry['custom_ship_data'][i])268 for entry in data.unk_one_bit_array:269 self._encode(1, entry)270 for entry in data.unk_two_bit_array:271 self._encode(2, entry)272 self._encode(20, data.total_frames)273 self._encode(8, data.unk_array_entries)274 # Note that the array size is decoupled from unk_array_entries here275 for entry in data.unk_array:276 self._encode(14, entry['part_one'][0])277 self._encode(14, entry['part_one'][1])278 self._encode(4, entry['part_one'][2])279 self._encode(5, entry['part_one'][3])280 self._encode(5, entry['part_one'][4])281 self._encode(5, entry['part_one'][5])282 self._encode(5, entry['part_one'][6])283 self._encode(5, entry['part_one'][7])284 for word in entry['part_two']:285 self._encode(32, word)286 for word in entry['part_three']:287 self._encode(32, word)288 self._encode(14, data.replay_array_entries)289 # Note that the array size is decoupled from replay_array_entries here290 for entry in data.replay_array_dict:291 self._encode(8, entry['mask'])292 self._encode(8, entry['strafe'])293 self._encode(7, entry['accel'])294 self._encode(7, entry['brake'])295 self._encode(8, entry['frames'])296 self._encode(8, entry['steer_x'])297 self._encode(8, entry['steer_y'])298 print("Wrote {} bytes of encoded output".format(hex(self.bytes_written)))...
decode_test.py
Source:decode_test.py
...18from pw_build.generated_tests import Context, PyTest, TestGenerator, GroupOrTest19from pw_build.generated_tests import parse_test_generation_args20from pw_hdlc.decode import Frame, FrameDecoder, FrameStatus, NO_ADDRESS21from pw_hdlc.protocol import frame_check_sequence as fcs22def _encode(address: int, control: int, data: bytes) -> bytes:23 frame = bytearray([address, control]) + data24 frame += fcs(frame)25 frame = frame.replace(b'\x7d', b'\x7d\x5d')26 frame = frame.replace(b'\x7e', b'\x7d\x5e')27 return b''.join([b'\x7e', frame, b'\x7e'])28class Expected(NamedTuple):29 address: int30 control: bytes31 data: bytes32 status: FrameStatus = FrameStatus.OK33 @classmethod34 def error(cls, status: FrameStatus):35 assert status is not FrameStatus.OK36 return cls(NO_ADDRESS, b'', b'', status)37 def __eq__(self, other) -> bool:38 """Define == so an Expected and a Frame can be compared."""39 return (self.address == other.address and self.control == other.control40 and self.data == other.data and self.status is other.status)41_PARTIAL = fcs(b'\x0ACmsg\x5e')42_ESCAPED_FLAG_TEST_CASE = (43 b'\x7e\x0ACmsg\x7d\x7e' + _PARTIAL + b'\x7e',44 [45 Expected.error(FrameStatus.FRAMING_ERROR),46 Expected.error(FrameStatus.FRAMING_ERROR),47 ],48)49TEST_CASES: Tuple[GroupOrTest[Tuple[bytes, List[Expected]]], ...] = (50 'Empty payload',51 (_encode(0, 0, b''), [Expected(0, b'\0', b'')]),52 (_encode(55, 0x99, b''), [Expected(55, b'\x99', b'')]),53 (_encode(55, 0x99, b'') * 3, [Expected(55, b'\x99', b'')] * 3),54 'Simple one-byte payload',55 (_encode(0, 0, b'\0'), [Expected(0, b'\0', b'\0')]),56 (_encode(123, 0, b'A'), [Expected(123, b'\0', b'A')]),57 'Simple multi-byte payload',58 (_encode(0, 0, b'Hello, world!'), [Expected(0, b'\0', b'Hello, world!')]),59 (_encode(123, 0, b'\0\0\1\0\0'), [Expected(123, b'\0', b'\0\0\1\0\0')]),60 'Escaped one-byte payload',61 (_encode(1, 2, b'\x7e'), [Expected(1, b'\2', b'\x7e')]),62 (_encode(1, 2, b'\x7d'), [Expected(1, b'\2', b'\x7d')]),63 (_encode(1, 2, b'\x7e') + _encode(1, 2, b'\x7d'),64 [Expected(1, b'\2', b'\x7e'),65 Expected(1, b'\2', b'\x7d')]),66 'Escaped address',67 (_encode(0x7e, 0, b'A'), [Expected(0x7e, b'\0', b'A')]),68 (_encode(0x7d, 0, b'B'), [Expected(0x7d, b'\0', b'B')]),69 'Escaped control',70 (_encode(0, 0x7e, b'C'), [Expected(0, b'\x7e', b'C')]),71 (_encode(0, 0x7d, b'D'), [Expected(0, b'\x7d', b'D')]),72 'Escaped address and control',73 (_encode(0x7e, 0x7d, b'E'), [Expected(0x7e, b'\x7d', b'E')]),74 (_encode(0x7d, 0x7e, b'F'), [Expected(0x7d, b'\x7e', b'F')]),75 (_encode(0x7e, 0x7e, b'\x7e'), [Expected(0x7e, b'\x7e', b'\x7e')]),76 'Multiple frames separated by single flag',77 (_encode(0, 0, b'A')[:-1] + _encode(1, 2, b'123'),78 [Expected(0, b'\0', b'A'),79 Expected(1, b'\2', b'123')]),80 (_encode(0xff, 0, b'Yo')[:-1] * 3 + b'\x7e',81 [Expected(0xff, b'\0', b'Yo')] * 3),82 'Ignore empty frames',83 (b'\x7e\x7e', []),84 (b'\x7e' * 10, []),85 (b'\x7e\x7e' + _encode(1, 2, b'3') + b'\x7e' * 5,86 [Expected(1, b'\2', b'3')]),87 (b'\x7e' * 10 + _encode(1, 2, b':O') + b'\x7e' * 3 + _encode(3, 4, b':P'),88 [Expected(1, b'\2', b':O'),89 Expected(3, b'\4', b':P')]),90 'Cannot escape flag',91 (b'\x7e\xAA\x7d\x7e\xab\x00Hello' + fcs(b'\xab\0Hello') + b'\x7e', [92 Expected.error(FrameStatus.FRAMING_ERROR),93 Expected(0xab, b'\0', b'Hello'),94 ]),95 _ESCAPED_FLAG_TEST_CASE,96 'Frame too short',97 (b'\x7e1\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),98 (b'\x7e12\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),99 (b'\x7e12345\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),100 'Incorrect frame check sequence',101 (b'\x7e123456\x7e', [Expected.error(FrameStatus.FCS_MISMATCH)]),102 (b'\x7e\1\2msg\xff\xff\xff\xff\x7e',103 [Expected.error(FrameStatus.FCS_MISMATCH)]),104 (_encode(0xA, 0xB, b'???')[:-2] + _encode(1, 2, b'def'), [105 Expected.error(FrameStatus.FCS_MISMATCH),106 Expected(1, b'\2', b'def'),107 ]),108 'Invalid escape in address',109 (b'\x7e\x7d\x7d\0' + fcs(b'\x5d\0') + b'\x7e',110 [Expected.error(FrameStatus.FRAMING_ERROR)]),111 'Invalid escape in control',112 (b'\x7e\0\x7d\x7d' + fcs(b'\0\x5d') + b'\x7e',113 [Expected.error(FrameStatus.FRAMING_ERROR)]),114 'Invalid escape in data',115 (b'\x7e\0\1\x7d\x7d' + fcs(b'\0\1\x5d') + b'\x7e',116 [Expected.error(FrameStatus.FRAMING_ERROR)]),117 'Frame ends with escape',118 (b'\x7e\x7d\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),119 (b'\x7e\1\x7d\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),120 (b'\x7e\1\2abc\x7d\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),121 (b'\x7e\1\2abcd\x7d\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),122 (b'\x7e\1\2abcd1234\x7d\x7e', [Expected.error(FrameStatus.FRAMING_ERROR)]),123 'Inter-frame data is only escapes',124 (b'\x7e\x7d\x7e\x7d\x7e', [125 Expected.error(FrameStatus.FRAMING_ERROR),126 Expected.error(FrameStatus.FRAMING_ERROR),127 ]),128 (b'\x7e\x7d\x7d\x7e\x7d\x7d\x7e', [129 Expected.error(FrameStatus.FRAMING_ERROR),130 Expected.error(FrameStatus.FRAMING_ERROR),131 ]),132 'Data before first flag',133 (b'\0\1' + fcs(b'\0\1'), []),134 (b'\0\1' + fcs(b'\0\1') + b'\x7e',135 [Expected.error(FrameStatus.FRAMING_ERROR)]),136 'No frames emitted until flag',137 (_encode(1, 2, b'3')[:-1], []),138 (b'\x7e' + _encode(1, 2, b'3')[1:-1] * 2, []),139 'Only flag and escape characters can be escaped',140 (b'\x7e\x7d\0' + _encode(1, 2, b'3'),141 [Expected.error(FrameStatus.FRAMING_ERROR),142 Expected(1, b'\2', b'3')]),143 (b'\x7e1234\x7da' + _encode(1, 2, b'3'),144 [Expected.error(FrameStatus.FRAMING_ERROR),145 Expected(1, b'\2', b'3')]),146) # yapf: disable147# Formatting for the above tuple is very slow, so disable yapf.148_TESTS = TestGenerator(TEST_CASES)149def _expected(frames: List[Frame]) -> Iterator[str]:150 for i, frame in enumerate(frames, 1):151 if frame.ok():152 yield f' Frame(kDecodedFrame{i:02}),'153 else:154 yield f' Status::DataLoss(), // Frame {i}'155_CPP_HEADER = """\156#include "pw_hdlc/decoder.h"157#include <array>...
test_image_encoder.py
Source:test_image_encoder.py
...3from pycozmo.util import hex_dump, hex_load4from pycozmo.tests.image_encoder_fixtures import FIXTURES5class TestImageEncoder(unittest.TestCase):6 @staticmethod7 def _encode(sim: str) -> str:8 im = str_to_image(sim)9 encoder = ImageEncoder(im)10 buf = encoder.encode()11 res = hex_dump(buf)12 return res13 def assertSameImage(self, sim: str, seq: str) -> None:14 buffer = hex_load(seq)15 decoder = ImageDecoder(buffer)16 decoder.decode()17 actual = image_to_str(decoder.image)18 self.assertEqual(sim.strip(), actual.strip())19 def test_blank(self):20 fixture = FIXTURES["blank"]21 sim = fixture["image"]22 expected = fixture["seq"]23 actual = self._encode(sim)24 self.assertEqual(expected, actual)25 self.assertSameImage(sim, actual)26 def test_fill_screen(self):27 fixture = FIXTURES["fill_screen"]28 sim = fixture["image"]29 expected = fixture["seq"]30 actual = self._encode(sim)31 self.assertEqual(expected, actual)32 self.assertSameImage(sim, actual)33 def test_fill_screen2(self):34 fixture = FIXTURES["fill_screen2"]35 sim = fixture["image"]36 expected = fixture["alt_seq"]37 actual = self._encode(sim)38 self.assertEqual(expected, actual)39 self.assertSameImage(sim, actual)40 def test_top_left(self):41 fixture = FIXTURES["top_left"]42 sim = fixture["image"]43 expected = fixture["alt_seq"]44 actual = self._encode(sim)45 self.assertEqual(expected, actual)46 self.assertSameImage(sim, actual)47 def test_top_left_5(self):48 fixture = FIXTURES["top_left_5"]49 sim = fixture["image"]50 expected = fixture["alt_seq"]51 actual = self._encode(sim)52 self.assertEqual(expected, actual)53 self.assertSameImage(sim, actual)54 def test_top_left_1_8(self):55 fixture = FIXTURES["top_left_1_8"]56 sim = fixture["image"]57 expected = fixture["alt_seq"]58 actual = self._encode(sim)59 self.assertEqual(expected, actual)60 self.assertSameImage(sim, actual)61 def test_top_left_line(self):62 fixture = FIXTURES["top_left_line"]63 sim = fixture["image"]64 expected = fixture["alt_seq"]65 actual = self._encode(sim)66 self.assertEqual(expected, actual)67 self.assertSameImage(sim, actual)68 def test_top_line(self):69 fixture = FIXTURES["top_line"]70 sim = fixture["image"]71 expected = fixture["alt_seq"]72 actual = self._encode(sim)73 self.assertEqual(expected, actual)74 self.assertSameImage(sim, actual)75 def test_bottom_line(self):76 fixture = FIXTURES["bottom_line"]77 sim = fixture["image"]78 expected = fixture["seq"]79 actual = self._encode(sim)80 self.assertEqual(expected, actual)81 self.assertSameImage(sim, actual)82 def test_left_line(self):83 fixture = FIXTURES["left_line"]84 sim = fixture["image"]85 expected = fixture["seq"]86 actual = self._encode(sim)87 self.assertEqual(expected, actual)88 self.assertSameImage(sim, actual)89 def test_right_line(self):90 fixture = FIXTURES["right_line"]91 sim = fixture["image"]92 expected = fixture["seq"]93 actual = self._encode(sim)94 self.assertEqual(expected, actual)95 self.assertSameImage(sim, actual)96 def test_columns(self):97 fixture = FIXTURES["columns"]98 sim = fixture["image"]99 expected = fixture["seq"]100 actual = self._encode(sim)101 self.assertEqual(expected, actual)102 self.assertSameImage(sim, actual)103 def test_rect(self):104 fixture = FIXTURES["rect"]105 sim = fixture["image"]106 expected = fixture["alt_seq"]107 actual = self._encode(sim)108 self.assertEqual(expected, actual)109 self.assertSameImage(sim, actual)110 def test_rect2(self):111 fixture = FIXTURES["rect2"]112 sim = fixture["image"]113 expected = fixture["alt_seq"]114 actual = self._encode(sim)115 self.assertEqual(expected, actual)116 self.assertSameImage(sim, actual)117 def test_rect3(self):118 fixture = FIXTURES["rect3"]119 sim = fixture["image"]120 expected = fixture["alt_seq"]121 actual = self._encode(sim)122 self.assertEqual(expected, actual)123 self.assertSameImage(sim, actual)124 def test_rect4(self):125 fixture = FIXTURES["rect4"]126 sim = fixture["image"]127 expected = fixture["alt_seq"]128 actual = self._encode(sim)129 self.assertEqual(expected, actual)130 self.assertSameImage(sim, actual)131 def test_diagonal(self):132 fixture = FIXTURES["diagonal"]133 sim = fixture["image"]134 expected = fixture["alt_seq"]135 actual = self._encode(sim)136 self.assertEqual(expected, actual)137 self.assertSameImage(sim, actual)138 def test_diagonal2(self):139 fixture = FIXTURES["diagonal2"]140 sim = fixture["image"]141 expected = fixture["alt_seq"]142 actual = self._encode(sim)143 self.assertEqual(expected, actual)144 self.assertSameImage(sim, actual)145 def test_blocks(self):146 fixture = FIXTURES["blocks"]147 sim = fixture["image"]148 expected = fixture["seq"]149 actual = self._encode(sim)150 self.assertEqual(expected, actual)151 self.assertSameImage(sim, actual)152 def test_pycozmo(self):153 fixture = FIXTURES["pycozmo"]154 sim = fixture["image"]155 expected = fixture["seq"]156 actual = self._encode(sim)157 self.assertEqual(expected, actual)158 self.assertSameImage(sim, actual)159 def test_chessboard_tl(self):160 fixture = FIXTURES["chessboard_tl"]161 sim = fixture["image"]162 expected = fixture["seq"]163 actual = self._encode(sim)164 self.assertEqual(expected, actual)165 self.assertSameImage(sim, actual)166 def test_chessboard_bl(self):167 fixture = FIXTURES["chessboard_bl"]168 sim = fixture["image"]169 expected = fixture["alt_seq"]170 actual = self._encode(sim)171 self.assertEqual(expected, actual)172 self.assertSameImage(sim, actual)173 def test_chessboard_tr(self):174 fixture = FIXTURES["chessboard_tr"]175 sim = fixture["image"]176 expected = fixture["seq"]177 actual = self._encode(sim)178 self.assertEqual(expected, actual)179 self.assertSameImage(sim, actual)180 def test_chessboard_br(self):181 fixture = FIXTURES["chessboard_br"]182 sim = fixture["image"]183 expected = fixture["alt_seq"]184 actual = self._encode(sim)185 self.assertEqual(expected, actual)186 self.assertSameImage(sim, actual)187 def test_chessboard2_tl(self):188 fixture = FIXTURES["chessboard2_tl"]189 sim = fixture["image"]190 expected = fixture["seq"]191 actual = self._encode(sim)192 self.assertEqual(expected, actual)193 self.assertSameImage(sim, actual)194 def test_chessboard2_bl(self):195 fixture = FIXTURES["chessboard2_bl"]196 sim = fixture["image"]197 expected = fixture["alt_seq"]198 actual = self._encode(sim)199 self.assertEqual(expected, actual)200 self.assertSameImage(sim, actual)201 def test_chessboard2_tr(self):202 fixture = FIXTURES["chessboard2_tr"]203 sim = fixture["image"]204 expected = fixture["seq"]205 actual = self._encode(sim)206 self.assertEqual(expected, actual)207 self.assertSameImage(sim, actual)208 def test_chessboard2_br(self):209 fixture = FIXTURES["chessboard2_br"]210 sim = fixture["image"]211 expected = fixture["alt_seq"]212 actual = self._encode(sim)213 self.assertEqual(expected, actual)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!