"""Tests for the JSON output-formatting helpers.""" import math import numpy as np from mcltspice.output_format import compact_list, downsample_indices, round_sig class TestRoundSig: def test_significant_figures(self): assert round_sig(123456.789, 3) == 123000.0 assert round_sig(0.00123456, 3) == 0.00123 def test_zero_and_nonfinite_pass_through(self): assert round_sig(0.0) == 0.0 assert math.isinf(round_sig(float("inf"))) assert math.isnan(round_sig(float("nan"))) class TestCompactList: def test_decimal_places(self): assert compact_list([1.23456, 2.98765], decimal_places=2) == [1.23, 2.99] def test_sig_figs(self): assert compact_list([123456.0], sig_figs=3) == [123000.0] def test_no_rounding_returns_floats(self): out = compact_list([1, 2, 3]) assert out == [1.0, 2.0, 3.0] assert all(isinstance(v, float) for v in out) class TestDownsampleIndices: def test_no_downsample_when_under_limit(self): idx = downsample_indices(50, 100) assert np.array_equal(idx, np.arange(50)) def test_stride_for_transient(self): idx = downsample_indices(1000, 100) assert len(idx) <= 100 # plain stride: evenly spaced assert idx[0] == 0 assert idx[1] - idx[0] == idx[2] - idx[1] def test_peak_preserving_keeps_a_narrow_spike(self): # A flat magnitude with one sharp spike: peak-preserving must keep it. n = 1000 mag = np.ones(n, dtype=complex) mag[497] = 1000.0 # narrow resonance peak idx = downsample_indices(n, 50, signals=[mag]) assert 497 in idx # spike survived downsampling