library(srsbench)

## log_loss
expect_equal(log_loss(0.5, 1), -log(0.5), tolerance = 1e-12)
expect_equal(log_loss(c(0.9, 0.1), c(1, 0)), -log(0.9), tolerance = 1e-12)
expect_equal(log_loss(c(0.9, 0.5), c(1, 1), weights = c(1, 3)),
             (-log(0.9) - 3 * log(0.5)) / 4, tolerance = 1e-12)

## srs_auc
expect_equal(srs_auc(c(0.9, 0.8, 0.2, 0.1), c(1, 1, 0, 0)), 1)
expect_equal(srs_auc(c(0.1, 0.2, 0.8, 0.9), c(1, 1, 0, 0)), 0)
expect_equal(srs_auc(c(0.5, 0.5), c(1, 0)), 0.5)
expect_true(is.na(srs_auc(c(0.4, 0.6), c(1, 1))))

## rmse_bins
expect_equal(rmse_bins(c(0.5, 0.5), c(1, 0), c(5, 5), c(2, 2), c(0, 0)), 0)
expect_equal(rmse_bins(c(0.9, 0.9), c(1, 1), c(5, 5), c(2, 2), c(0, 0)), 0.1,
             tolerance = 1e-12)
expect_equal(rmse_bins(c(0.9, 0.2), c(1, 0), c(2, 100), c(2, 5), c(0, 1)),
             sqrt(0.05 / 2), tolerance = 1e-12)
expect_equal(rmse_bins(c(0.9, 0.2), c(1, 0), c(2, 100), c(2, 5), c(0, 1),
                       weights = c(3, 1)),
             sqrt((3 * 0.01 + 0.04) / 4), tolerance = 1e-12)

## calibration_bins
cal <- calibration_bins(c(0.1, 0.2, 0.8, 0.9), c(0, 0, 1, 1), n = 5)
expect_equal(nrow(cal), 5L)
expect_equal(sum(cal$n), 4L)
expect_equal(cal$obs_y[c(1, 5)], c(0, 1))
cal2 <- calibration_bins(c(0.85, 0.95), c(1, 1), n = 5)
expect_equal(cal2$n, c(0L, 0L, 0L, 0L, 2L))
expect_true(is.na(cal2$mean_p[1]))

## input validation
expect_error(log_loss(c(0.5, 0.5), 1), "equal length")
expect_error(srs_auc(c(1.2, 0.5), c(1, 0)), "\\[0, 1\\]")
expect_error(rmse_bins(0.5, 2, 5, 2, 0), "0 .* or 1|recalled")
expect_error(rmse_bins(c(0.5, 0.5), c(1, 0), 5, c(2, 2), c(0, 0)), "match")
expect_error(log_loss(numeric(0), numeric(0)), "empty")
expect_error(log_loss(0.5, 1, weights = c(1, 2)), "match")
expect_error(log_loss(0.5, 1, weights = -1), "non-negative")
expect_error(log_loss(0.5, 1, weights = NA_real_), "non-negative")
expect_error(log_loss(c(0.5, 0.5), c(1, 0), weights = c(0, 0)), "non-negative")
expect_error(rmse_bins(c(0.5, 0.5), c(1, 0), c(NA, 5), c(2, 2), c(0, 0)),
             "must not contain NA")
expect_error(rmse_bins(c(0.5, 0.5), c(1, 0), c(5, 5), c(2, 2), c(0, 0),
                       weights = c(1, -1)), "non-negative")
