-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_join_sections.py
More file actions
273 lines (209 loc) · 8.5 KB
/
test_join_sections.py
File metadata and controls
273 lines (209 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python
"""Test script to verify join_sections() functionality."""
from objutils import Section, Image
from objutils.section import join_sections
def test_consecutive_sections():
"""Test joining consecutive sections."""
print("Test 1: Consecutive sections")
print("-" * 80)
sections = [
Section(0x1000, b"Hello"),
Section(0x1005, b" "),
Section(0x1006, b"World"),
]
print("Before join:")
for idx, sec in enumerate(sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
joined = join_sections(sections)
print("\nAfter join:")
for idx, sec in enumerate(joined):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
assert len(joined) == 1, f"Expected 1 section, got {len(joined)}"
assert joined[0].start_address == 0x1000, f"Expected start_address 0x1000, got 0x{joined[0].start_address:04x}"
assert joined[0].length == 11, f"Expected length 11, got {joined[0].length}"
assert joined[0].data == b"Hello World", f"Expected b'Hello World', got {joined[0].data}"
print("✓ PASSED\n")
def test_non_consecutive_sections():
"""Test that non-consecutive sections remain separate."""
print("Test 2: Non-consecutive sections (with gap)")
print("-" * 80)
sections = [
Section(0x1000, b"Hello"),
Section(0x1005, b" "),
Section(0x2000, b"World"), # Gap before this
]
print("Before join:")
for idx, sec in enumerate(sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
joined = join_sections(sections)
print("\nAfter join:")
for idx, sec in enumerate(joined):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
assert len(joined) == 2, f"Expected 2 sections, got {len(joined)}"
assert joined[0].start_address == 0x1000
assert joined[0].length == 6
assert joined[0].data == b"Hello "
assert joined[1].start_address == 0x2000
assert joined[1].length == 5
assert joined[1].data == b"World"
print("✓ PASSED\n")
def test_unsorted_input():
"""Test that unsorted input is sorted correctly."""
print("Test 3: Unsorted input sections")
print("-" * 80)
sections = [
Section(0x2000, b"World"),
Section(0x1000, b"Hello"),
Section(0x1005, b" "),
]
print("Before join (unsorted):")
for idx, sec in enumerate(sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
joined = join_sections(sections)
print("\nAfter join (sorted and joined):")
for idx, sec in enumerate(joined):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
assert len(joined) == 2, f"Expected 2 sections, got {len(joined)}"
assert joined[0].start_address == 0x1000
assert joined[1].start_address == 0x2000
print("✓ PASSED\n")
def test_image_with_join():
"""Test Image class with join=True."""
print("Test 4: Image class with join=True")
print("-" * 80)
sections = [
Section(0x1000, b"AAA"),
Section(0x1003, b"BBB"),
Section(0x1006, b"CCC"),
Section(0x2000, b"DDD"),
]
print("Before join:")
for idx, sec in enumerate(sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
img = Image(sections, join=True)
print("\nAfter Image(join=True):")
for idx, sec in enumerate(img.sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
assert len(img.sections) == 2, f"Expected 2 sections, got {len(img.sections)}"
assert img.sections[0].start_address == 0x1000
assert img.sections[0].length == 9
assert img.sections[0].data == b"AAABBBCCC"
assert img.sections[1].start_address == 0x2000
assert img.sections[1].length == 3
assert img.sections[1].data == b"DDD"
print("✓ PASSED\n")
def test_image_without_join():
"""Test Image class with join=False."""
print("Test 5: Image class with join=False")
print("-" * 80)
sections = [
Section(0x1000, b"AAA"),
Section(0x1003, b"BBB"),
]
print("Before Image:")
for idx, sec in enumerate(sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
img = Image(sections, join=False)
print("\nAfter Image(join=False):")
for idx, sec in enumerate(img.sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
assert len(img.sections) == 2, f"Expected 2 sections, got {len(img.sections)}"
print("✓ PASSED\n")
def test_join_sections_method():
"""Test Image.join_sections() method."""
print("Test 6: Image.join_sections() method")
print("-" * 80)
sections = [
Section(0x1000, b"AAA"),
Section(0x1003, b"BBB"),
Section(0x2000, b"CCC"),
]
img = Image(sections, join=False)
print("Before join_sections():")
for idx, sec in enumerate(img.sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
img.join_sections()
print("\nAfter join_sections():")
for idx, sec in enumerate(img.sections):
print(f" Section {idx}: Address=0x{sec.start_address:04x}, Length={sec.length}, Data={sec.data}")
assert len(img.sections) == 2, f"Expected 2 sections, got {len(img.sections)}"
assert img.sections[0].start_address == 0x1000
assert img.sections[0].length == 6
assert img.sections[0].data == b"AAABBB"
assert img.sections[1].start_address == 0x2000
print("✓ PASSED\n")
def test_empty_list():
"""Test with empty list."""
print("Test 7: Empty section list")
print("-" * 80)
sections = []
joined = join_sections(sections)
print(f"Result: {joined}")
assert joined == [], f"Expected empty list, got {joined}"
print("✓ PASSED\n")
def test_single_section():
"""Test with single section."""
print("Test 8: Single section")
print("-" * 80)
sections = [Section(0x1000, b"Hello")]
joined = join_sections(sections)
print(f"Before: {sections[0]}")
print(f"After: {joined[0]}")
assert len(joined) == 1
assert joined[0].start_address == 0x1000
assert joined[0].data == b"Hello"
print("✓ PASSED\n")
def test_address_and_length_correctness():
"""Test that address and length are calculated correctly."""
print("Test 9: Address and length correctness")
print("-" * 80)
sections = [
Section(0x0000, b"\x01\x02\x03"),
Section(0x0003, b"\x04\x05"),
Section(0x0005, b"\x06\x07\x08\x09"),
Section(0x1000, b"\x0a"),
]
print("Before join:")
for idx, sec in enumerate(sections):
end_addr = sec.start_address + sec.length
print(f" Section {idx}: 0x{sec.start_address:04x}-0x{end_addr:04x}, Length={sec.length}")
joined = join_sections(sections)
print("\nAfter join:")
for idx, sec in enumerate(joined):
end_addr = sec.start_address + sec.length
print(f" Section {idx}: 0x{sec.start_address:04x}-0x{end_addr:04x}, Length={sec.length}, Data={sec.data.hex()}")
assert len(joined) == 2
# First merged section: 0x0000 to 0x0009
assert joined[0].start_address == 0x0000
assert joined[0].length == 9
assert joined[0].data == b"\x01\x02\x03\x04\x05\x06\x07\x08\x09"
# Second section: 0x1000 to 0x1001
assert joined[1].start_address == 0x1000
assert joined[1].length == 1
assert joined[1].data == b"\x0a"
print("✓ PASSED\n")
if __name__ == "__main__":
print("\n" + "=" * 80)
print("TESTING join_sections() FUNCTIONALITY")
print("=" * 80 + "\n")
try:
test_consecutive_sections()
test_non_consecutive_sections()
test_unsorted_input()
test_image_with_join()
test_image_without_join()
test_join_sections_method()
test_empty_list()
test_single_section()
test_address_and_length_correctness()
print("=" * 80)
print("ALL TESTS PASSED ✓")
print("=" * 80)
except AssertionError as e:
print(f"\n❌ TEST FAILED: {e}")
exit(1)
except Exception as e:
print(f"\n❌ ERROR: {e}")
import traceback
traceback.print_exc()
exit(1)