#![allow(
    clippy::assertions_on_constants,
    clippy::assertions_on_result_states,
    clippy::clone_on_copy,
    clippy::decimal_literal_representation,
    clippy::doc_markdown,
    clippy::empty_line_after_doc_comments,
    clippy::field_reassign_with_default,
    clippy::get_unwrap,
    clippy::identity_op,
    clippy::inconsistent_digit_grouping,
    clippy::indexing_slicing,
    clippy::integer_division,
    clippy::len_zero,
    clippy::let_underscore_must_use,
    clippy::manual_div_ceil,
    clippy::manual_let_else,
    clippy::manual_range_contains,
    clippy::modulo_arithmetic,
    clippy::needless_range_loop,
    clippy::non_ascii_literal,
    clippy::redundant_clone,
    clippy::shadow_reuse,
    clippy::shadow_same,
    clippy::shadow_unrelated,
    clippy::single_match_else,
    clippy::str_to_string,
    clippy::string_slice,
    clippy::tests_outside_test_module,
    clippy::too_many_lines,
    clippy::unnecessary_wraps,
    clippy::unseparated_literal_suffix,
    clippy::use_debug,
    clippy::useless_vec,
    clippy::wildcard_enum_match_arm,
    clippy::else_if_without_else,
    clippy::expect_used,
    clippy::missing_const_for_fn,
    clippy::similar_names,
    clippy::type_complexity,
    clippy::collapsible_else_if,
    clippy::doc_lazy_continuation,
    clippy::items_after_test_module,
    clippy::map_clone,
    clippy::multiple_unsafe_ops_per_block,
    clippy::unwrap_or_default,
    clippy::assign_op_pattern,
    clippy::needless_borrow,
    clippy::println_empty_string,
    clippy::unnecessary_cast,
    clippy::used_underscore_binding,
    clippy::create_dir,
    clippy::implicit_saturating_sub,
    clippy::exit,
    clippy::expect_fun_call,
    clippy::too_many_arguments,
    clippy::unnecessary_map_or,
    clippy::unwrap_used,
    dead_code,
    unused_imports,
    unused_variables,
    clippy::cloned_ref_to_slice_refs,
    clippy::neg_multiply,
    clippy::while_let_loop,
    clippy::bool_assert_comparison,
    clippy::excessive_precision,
    clippy::trivially_copy_pass_by_ref,
    clippy::op_ref,
    clippy::redundant_closure,
    clippy::unnecessary_lazy_evaluations,
    clippy::if_then_some_else_none,
    clippy::unnecessary_to_owned,
    clippy::single_component_path_imports,
)]
/// Test to validate Huber loss implementation in continuous PPO.
///
/// Verifies:
/// 1. Gradients are non-zero in both quadratic and linear regions
/// 2. Quadratic region: L(x) = 0.5 * x^2 for |x| <= delta
/// 3. Linear region: L(x) = delta * (|x| - 0.5*delta) for |x| > delta
/// 4. Gradient magnitude is bounded by delta
/// 5. No gradient vanishing (unlike clamp)

// candle eliminated — test uses host-side f32 arithmetic directly

/// Compute element-wise Huber loss on host.
fn huber_loss(value_diff: &[f32], delta: f32) -> Vec<f32> {
    value_diff
        .iter()
        .map(|&x| {
            let abs_x = x.abs();
            if abs_x <= delta {
                0.5 * x * x
            } else {
                delta * (abs_x - 0.5 * delta)
            }
        })
        .collect()
}

#[test]
fn test_huber_loss_quadratic_region() {
    // Test quadratic region: |x| <= delta
    let delta = 10.0f32;

    // Create value differences in quadratic region: [-5.0, 5.0]
    let value_diff = vec![-5.0f32, -2.5, 0.0, 2.5, 5.0];

    // Expected: 0.5 * x^2
    let expected = vec![
        0.5 * 25.0,   // -5.0^2
        0.5 * 6.25,   // -2.5^2
        0.0,          // 0.0^2
        0.5 * 6.25,   // 2.5^2
        0.5 * 25.0,   // 5.0^2
    ];

    // Compute Huber loss
    let actual = huber_loss(&value_diff, delta);

    // Verify results
    for (a, e) in actual.iter().zip(expected.iter()) {
        assert!((a - e).abs() < 1e-5, "Expected {}, got {}", e, a);
    }
}

#[test]
fn test_huber_loss_linear_region() {
    // Test linear region: |x| > delta
    let delta = 10.0f32;

    // Create value differences in linear region: [-20.0, -15.0, 15.0, 20.0]
    let value_diff = vec![-20.0f32, -15.0, 15.0, 20.0];

    // Expected: delta * (|x| - 0.5*delta)
    let expected = vec![
        delta * (20.0 - 0.5 * delta),  // |-20.0|
        delta * (15.0 - 0.5 * delta),  // |-15.0|
        delta * (15.0 - 0.5 * delta),  // |15.0|
        delta * (20.0 - 0.5 * delta),  // |20.0|
    ];

    // Compute Huber loss
    let actual = huber_loss(&value_diff, delta);

    // Verify results
    for (a, e) in actual.iter().zip(expected.iter()) {
        assert!((a - e).abs() < 1e-5, "Expected {}, got {}", e, a);
    }
}

#[test]
fn test_huber_loss_gradient_nonzero() {
    // Verify gradients are non-zero (unlike clamp)
    let delta = 10.0f32;

    // Create value differences spanning both regions
    let value_diff = vec![-20.0f32, -5.0, 0.0, 5.0, 20.0];

    // Compute Huber loss
    let losses = huber_loss(&value_diff, delta);

    // Compute mean loss
    let loss_val: f32 = losses.iter().sum::<f32>() / losses.len() as f32;

    // Verify loss is positive (non-zero gradient region)
    assert!(loss_val > 0.0, "Loss should be positive: {}", loss_val);

    // Expected loss:
    // Quadratic: 0.5 * (5^2 + 0^2 + 5^2) = 25
    // Linear: 10 * (20 - 5) + 10 * (20 - 5) = 150 + 150 = 300
    // Mean: (25 + 300) / 5 = 65
    let expected_loss = 65.0;
    assert!(
        (loss_val - expected_loss).abs() < 1.0,
        "Expected ~{}, got {}",
        expected_loss,
        loss_val
    );
}

#[test]
fn test_huber_loss_boundary_continuity() {
    // Verify continuity at boundary |x| = delta
    let delta = 10.0f32;

    // Test values just below, at, and just above delta
    let values = vec![
        delta - 0.1,
        delta,
        delta + 0.1,
    ];

    let losses: Vec<f32> = values
        .iter()
        .map(|&val| huber_loss(&[val], delta)[0])
        .collect();

    // Verify continuity: L(delta - 0.1) ~ L(delta) ~ L(delta + 0.1)
    let tolerance = 0.5; // Allow small discontinuity due to discrete switch
    for i in 0..losses.len() - 1 {
        let diff = (losses[i] - losses[i + 1]).abs();
        assert!(
            diff < tolerance,
            "Discontinuity at boundary: L({:.1}) = {:.2}, L({:.1}) = {:.2}, diff = {:.2}",
            values[i],
            losses[i],
            values[i + 1],
            losses[i + 1],
            diff
        );
    }
}
