-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_histogram.py
More file actions
314 lines (230 loc) · 10.3 KB
/
test_histogram.py
File metadata and controls
314 lines (230 loc) · 10.3 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Comprehensive histogram tests"""
from __future__ import division
import unittest
import numpy as np
import gc
import tttrlib
# Centralized test settings
from test_settings import settings, DATA_AVAILABLE # type: ignore
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestHistogramBasic(unittest.TestCase):
"""Basic histogram functionality tests"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_histogram_returns_two_arrays(self):
"""Test that histogram returns two arrays"""
h, t = self.data.get_microtime_histogram(64)
self.assertIsInstance(h, np.ndarray)
self.assertIsInstance(t, np.ndarray)
def test_histogram_arrays_same_length(self):
"""Test that histogram and time arrays have same length"""
h, t = self.data.get_microtime_histogram(64)
self.assertEqual(len(h), len(t))
def test_histogram_arrays_not_empty(self):
"""Test that histogram arrays are not empty"""
h, t = self.data.get_microtime_histogram(64)
self.assertGreater(len(h), 0)
self.assertGreater(len(t), 0)
def test_histogram_values_non_negative(self):
"""Test that histogram values are non-negative"""
h, t = self.data.get_microtime_histogram(64)
self.assertTrue(np.all(h >= 0))
def test_histogram_time_axis_monotonic(self):
"""Test that time axis is monotonically increasing"""
h, t = self.data.get_microtime_histogram(64)
if len(t) > 1:
diffs = np.diff(t)
self.assertTrue(np.all(diffs > 0))
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestHistogramBinCounts(unittest.TestCase):
"""Histogram bin count tests"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_histogram_sum_equals_event_count(self):
"""Test that histogram sum equals total event count"""
h, t = self.data.get_microtime_histogram(64)
histogram_sum = np.sum(h)
self.assertEqual(histogram_sum, len(self.data))
def test_histogram_sum_equals_event_count_various_bins(self):
"""Test histogram sum for various bin counts"""
for n_bins in [8, 16, 32, 64, 128]:
h, t = self.data.get_microtime_histogram(n_bins)
histogram_sum = np.sum(h)
self.assertEqual(histogram_sum, len(self.data))
def test_histogram_no_negative_bins(self):
"""Test that no histogram bins are negative"""
h, t = self.data.get_microtime_histogram(64)
self.assertTrue(np.all(h >= 0))
self.assertFalse(np.any(h < 0))
def test_histogram_bins_are_integers(self):
"""Test that histogram bins contain integer counts"""
h, t = self.data.get_microtime_histogram(64)
# Histogram values should be integers (or very close to integers)
self.assertTrue(np.allclose(h, np.round(h)))
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestHistogramDataRange(unittest.TestCase):
"""Histogram data range tests"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_histogram_covers_data_range(self):
"""Test that histogram has valid range"""
h, t = self.data.get_microtime_histogram(64)
# Time axis should be monotonically increasing
self.assertGreater(len(t), 0)
if len(t) > 1:
diffs = np.diff(t)
self.assertTrue(np.all(diffs > 0))
def test_histogram_time_axis_reasonable(self):
"""Test that time axis values are reasonable"""
h, t = self.data.get_microtime_histogram(64)
# Time values should be positive or zero
self.assertTrue(np.all(t >= 0))
# Time values should be within reasonable range (16-bit)
self.assertTrue(np.all(t < 2**16))
def test_histogram_bin_width_consistent(self):
"""Test that bin widths are reasonably consistent"""
h, t = self.data.get_microtime_histogram(64)
if len(t) > 1:
bin_widths = np.diff(t)
# Check that bin widths don't vary too much
mean_width = np.mean(bin_widths)
max_deviation = np.max(np.abs(bin_widths - mean_width))
# Allow up to 50% deviation
self.assertLess(max_deviation, mean_width * 0.5)
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestHistogramVariousBins(unittest.TestCase):
"""Histogram tests with various bin counts"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_histogram_8_bins(self):
"""Test histogram with 8 bins"""
h, t = self.data.get_microtime_histogram(8)
self.assertEqual(np.sum(h), len(self.data))
def test_histogram_16_bins(self):
"""Test histogram with 16 bins"""
h, t = self.data.get_microtime_histogram(16)
self.assertEqual(np.sum(h), len(self.data))
def test_histogram_32_bins(self):
"""Test histogram with 32 bins"""
h, t = self.data.get_microtime_histogram(32)
self.assertEqual(np.sum(h), len(self.data))
def test_histogram_64_bins(self):
"""Test histogram with 64 bins"""
h, t = self.data.get_microtime_histogram(64)
self.assertEqual(np.sum(h), len(self.data))
def test_histogram_128_bins(self):
"""Test histogram with 128 bins"""
h, t = self.data.get_microtime_histogram(128)
self.assertEqual(np.sum(h), len(self.data))
def test_histogram_256_bins(self):
"""Test histogram with 256 bins"""
h, t = self.data.get_microtime_histogram(256)
self.assertEqual(np.sum(h), len(self.data))
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestHistogramSubsets(unittest.TestCase):
"""Histogram tests on data subsets"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_histogram_full_data(self):
"""Test histogram on full dataset"""
h, t = self.data.get_microtime_histogram(64)
self.assertEqual(np.sum(h), len(self.data))
def test_histogram_first_half(self):
"""Test histogram on first half of data"""
if len(self.data) > 100:
subset = self.data[:len(self.data)//2]
h, t = subset.get_microtime_histogram(64)
self.assertEqual(np.sum(h), len(subset))
def test_histogram_second_half(self):
"""Test histogram on second half of data"""
if len(self.data) > 100:
subset = self.data[len(self.data)//2:]
h, t = subset.get_microtime_histogram(64)
self.assertEqual(np.sum(h), len(subset))
def test_histogram_small_subset(self):
"""Test histogram on small subset"""
if len(self.data) > 50:
subset = self.data[:50]
h, t = subset.get_microtime_histogram(64)
self.assertEqual(np.sum(h), len(subset))
def test_histogram_large_subset(self):
"""Test histogram on large subset"""
if len(self.data) > 1000:
subset = self.data[:1000]
h, t = subset.get_microtime_histogram(64)
self.assertEqual(np.sum(h), len(subset))
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestHistogramConsistency(unittest.TestCase):
"""Histogram consistency tests"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_histogram_repeated_calls_same_result(self):
"""Test that repeated calls give same result"""
h1, t1 = self.data.get_microtime_histogram(64)
h2, t2 = self.data.get_microtime_histogram(64)
np.testing.assert_array_equal(h1, h2)
np.testing.assert_array_equal(t1, t2)
def test_histogram_different_bin_counts_same_sum(self):
"""Test that different bin counts give same total sum"""
sums = []
for n_bins in [8, 16, 32, 64, 128]:
h, t = self.data.get_microtime_histogram(n_bins)
sums.append(np.sum(h))
# All sums should be equal
self.assertTrue(all(s == sums[0] for s in sums))
def test_histogram_copy_data_same_histogram(self):
"""Test that copy of data gives same histogram"""
data_copy = tttrlib.TTTR(self.data)
h1, t1 = self.data.get_microtime_histogram(64)
h2, t2 = data_copy.get_microtime_histogram(64)
np.testing.assert_array_equal(h1, h2)
@unittest.skipIf(not DATA_AVAILABLE, "Data directory not found")
class TestMeanMicrotime(unittest.TestCase):
"""Mean microtime tests"""
def setUp(self):
self.data = tttrlib.TTTR(settings["spc132_filename"], 'SPC-130')
def tearDown(self):
self.data = None
gc.collect()
def test_mean_microtime_valid(self):
"""Test that mean microtime is valid"""
mean_mt = self.data.get_mean_microtime()
self.assertGreater(mean_mt, 0)
self.assertLess(mean_mt, 2**16)
def test_mean_microtime_within_range(self):
"""Test that mean microtime is positive"""
mean_mt = self.data.get_mean_microtime()
# Mean should be positive
self.assertGreater(mean_mt, 0)
def test_mean_microtime_subset(self):
"""Test mean microtime on subset"""
if len(self.data) > 100:
subset = self.data[:100]
mean_mt = subset.get_mean_microtime()
self.assertGreater(mean_mt, 0)
self.assertLess(mean_mt, 2**16)
def test_mean_microtime_repeated_calls(self):
"""Test that repeated calls give same result"""
mean1 = self.data.get_mean_microtime()
mean2 = self.data.get_mean_microtime()
self.assertEqual(mean1, mean2)
if __name__ == '__main__':
unittest.main()