import numpy as np from src.hdc_encoder.encoder import encode, DIM class TestHDCEncoder: def setup_method(self): """Initialize base parameters before each test to guarantee state isolation.""" self.prosody = {"pitch": 140.0, "energy": 0.25, "tempo": 115.0, "pause_ratio": 0.15} self.frames = 40 self.mfcc = np.random.rand(13, self.frames).astype(np.float32) def test_encoder_dimensionality(self): """Verify the output vector matches the strict dimensional parameters.""" hv = encode(self.mfcc, self.prosody) assert hv.shape == (DIM,), f"Dimensionality failure: Expected {DIM}, got {hv.shape[0]}" assert hv.dtype == np.uint8, f"Type architecture failure: Expected uint8, got {hv.dtype}" def test_temporal_sequence_asymmetry(self): """ Verify that reversing the audio sequence produces a fundamentally different hypervector. This proves the encoder captures the temporal direction of speech. """ mfcc_reversed = self.mfcc[:, ::-1] hv_forward = encode(self.mfcc, self.prosody) hv_reversed = encode(mfcc_reversed, self.prosody) similarity = np.mean(hv_forward == hv_reversed) # In an orthogonal 10k dimensional space, random vectors share ~50% similarity. # We enforce a strict threshold to ensure the temporal shift breaks vector alignment. assert similarity < 0.60, f"Commutativity failure: Temporal sequence not isolated. Similarity: {similarity}" def test_prosody_modulation(self): """Verify that altering emotional/prosodic intent alters the final memory vector.""" angry_prosody = {"pitch": 240.0, "energy": 0.85, "tempo": 160.0, "pause_ratio": 0.05} hv_base = encode(self.mfcc, self.prosody) hv_angry = encode(self.mfcc, angry_prosody) similarity = np.mean(hv_base == hv_angry) assert similarity < 0.95, "Modulation failure: Prosody shifts did not impact the spatial vector."