MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / require_max_abs_diff_below

Function require_max_abs_diff_below

tests/unittests/test_encoder_modules.cpp:45–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43 oss << label << " mismatch at " << i << ": expected " << expected[i] << ", got " << actual[i]
44 << ", diff=" << diff;
45 throw std::runtime_error(oss.str());
46 }
47 }
48}
49
50void require_max_abs_diff_below(
51 const std::vector<float> & actual,
52 const std::vector<float> & expected,
53 float max_allowed,
54 double mean_allowed,
55 const std::string & label) {
56 if (actual.size() != expected.size()) {
57 throw std::runtime_error(label + " size mismatch");
58 }
59 float max_diff = 0.0f;
60 size_t max_index = 0;
61 double mean_diff = 0.0;
62 for (size_t i = 0; i < actual.size(); ++i) {
63 const float diff = std::fabs(actual[i] - expected[i]);
64 mean_diff += diff;
65 if (diff > max_diff) {
66 max_diff = diff;
67 max_index = i;
68 }
69 }
70 mean_diff /= static_cast<double>(actual.size());
71 if (max_diff > max_allowed || mean_diff > mean_allowed) {
72 std::ostringstream oss;
73 oss << label << " drift exceeds bounds: max diff " << max_diff << " (limit " << max_allowed << ")"
74 << ", mean diff=" << mean_diff << " (limit " << mean_allowed << ")"
75 << " at " << max_index << " (expected " << expected[max_index]
76 << ", got " << actual[max_index] << ")";
77 throw std::runtime_error(oss.str());

Calls 2

strMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected