@@ -220,9 +220,10 @@ class TestPReLU
220220 assert (fabs (data (1 , 2 ) - 0 .0f ) < 1e-6 ); // 0.0 (unchanged)
221221 }
222222
223- static void test_wrong_number_of_channels ()
223+ static void test_wrong_number_of_channels_matrix ()
224224 {
225- // Test that we fail when we have more channels than slopes
225+ // Test that we fail when matrix has more channels than slopes
226+ // Note: This validation only runs in debug builds (#ifndef NDEBUG)
226227 Eigen::MatrixXf data (3 , 2 ); // 3 channels, 2 time steps
227228
228229 // Initialize with test data
@@ -232,21 +233,69 @@ class TestPReLU
232233 std::vector<float > slopes = {0 .01f , 0 .05f };
233234 nam::activations::ActivationPReLU prelu (slopes);
234235
235- // Apply the activation
236+ #ifndef NDEBUG
237+ // In debug mode, this should throw std::invalid_argument
236238 bool caught = false ;
237239 try
238240 {
239241 prelu.apply (data);
240242 }
241- catch (const std::runtime_error & e)
243+ catch (const std::invalid_argument & e)
242244 {
243245 caught = true ;
244246 }
245- catch (...)
247+ assert (caught && " Expected std::invalid_argument for channel count mismatch" );
248+ #endif
249+ }
250+
251+ static void test_wrong_size_array ()
252+ {
253+ // Test that we fail when array size doesn't divide evenly by channel count
254+ // Note: This validation only runs in debug builds (#ifndef NDEBUG)
255+
256+ // Create PReLU with 2 channels
257+ std::vector<float > slopes = {0 .01f , 0 .05f };
258+ nam::activations::ActivationPReLU prelu (slopes);
259+
260+ // Array of size 5 doesn't divide evenly by 2 channels
261+ std::vector<float > data = {-1 .0f , -2 .0f , 0 .5f , 1 .0f , -0 .5f };
262+
263+ #ifndef NDEBUG
264+ // In debug mode, this should throw std::invalid_argument
265+ bool caught = false ;
266+ try
267+ {
268+ prelu.apply (data.data (), (long )data.size ());
269+ }
270+ catch (const std::invalid_argument& e)
246271 {
272+ caught = true ;
247273 }
274+ assert (caught && " Expected std::invalid_argument for array size mismatch" );
275+ #endif
276+ }
277+
278+ static void test_valid_array_size ()
279+ {
280+ // Test that valid array sizes work correctly
281+
282+ // Create PReLU with 2 channels
283+ std::vector<float > slopes = {0 .1f , 0 .2f };
284+ nam::activations::ActivationPReLU prelu (slopes);
285+
286+ // Array of size 6 divides evenly by 2 channels (3 time steps per channel)
287+ std::vector<float > data = {-1 .0f , -1 .0f , -1 .0f , -1 .0f , -1 .0f , -1 .0f };
288+
289+ // Should not throw
290+ prelu.apply (data.data (), (long )data.size ());
248291
249- assert (caught);
292+ // Verify results: alternating between slope 0.1 and 0.2
293+ assert (fabs (data[0 ] - (-0 .1f )) < 1e-6 ); // channel 0, slope 0.1
294+ assert (fabs (data[1 ] - (-0 .2f )) < 1e-6 ); // channel 1, slope 0.2
295+ assert (fabs (data[2 ] - (-0 .1f )) < 1e-6 ); // channel 0, slope 0.1
296+ assert (fabs (data[3 ] - (-0 .2f )) < 1e-6 ); // channel 1, slope 0.2
297+ assert (fabs (data[4 ] - (-0 .1f )) < 1e-6 ); // channel 0, slope 0.1
298+ assert (fabs (data[5 ] - (-0 .2f )) < 1e-6 ); // channel 1, slope 0.2
250299 }
251300};
252301
0 commit comments