test_that("the CPU weighted-Euclidean backend matches stats::dist", { x = matrix( c( 0, 1, 2, 3, 1, 1, 4, 2, 2, 0, 1, 5 ), nrow = 4, ncol = 3 ) w = c(1, 2, 0.5) reference = as.matrix(stats::dist(sweep(x, 2, sqrt(w), "*"))) dimnames(reference) = NULL cpu_matrix = EuclideanGPU_Distance( x, w, backend = "cpu", OutputType = "mat" ) expect_equal(cpu_matrix, reference) cpu_dist = EuclideanGPU_Distance( x, w, backend = "cpu", OutputType = "dist" ) expect_equal(unname(as.matrix(cpu_dist)), reference) cpu_vector = EuclideanGPU_Distance( x, w, backend = "cpu", OutputType = "vec" ) expect_length(cpu_vector, length(reference)) expect_equal(cpu_vector, as.vector(reference)) }) test_that("DistanceMatrix delegates weighted Euclidean work to the selected backend", { x = matrix(seq_len(20), nrow = 5, ncol = 4) w = c(1, 2, 0.5, 3) reference = as.matrix(stats::dist(sweep(x, 2, sqrt(w), "*"))) dimnames(reference) = NULL observed = DistanceMatrix( x, method = "euclidean", GPU = TRUE, Weights = w, backend = "cpu" ) squared = DistanceMatrix( x, method = "sqeuclidean", GPU = TRUE, Weights = w, backend = "cpu" ) expect_equal(observed, reference) expect_equal(squared, reference^2) }) test_that("the GPU memory planner covers each execution regime", { plan0 = calculateMemoryDemandGPU(100, 10, 2) expect_identical(plan0$Version, 0L) expect_equal(sum(plan0$batchSizes), 100) plan1 = calculateMemoryDemandGPU(20000, 10, 1) expect_identical(plan1$Version, 1L) expect_equal(sum(plan1$batchSizes), 20000) plan2 = calculateMemoryDemandGPU(10000, 5000, 0.01) expect_identical(plan2$Version, 2L) expect_equal(sum(plan2$batchSizes), 10000) }) test_that("weighted Euclidean edge cases are handled", { single = matrix(3, nrow = 1L, ncol = 1L) expect_identical( EuclideanGPU_Distance(single, backend = "cpu"), matrix(0, nrow = 1L, ncol = 1L) ) x = matrix(seq_len(12), nrow = 4, ncol = 3) expect_equal( EuclideanGPU_Distance(x, rep.int(0, ncol(x)), backend = "cpu"), matrix(0, nrow(x), nrow(x)) ) })