591c3c1e673147742b6b0bd8a1c969b464e47d32
[morpheus.git] / pkg / tests / testthat / test-hungarianAlgorithm.R
1 context("hungarianAlgorithm")
2
3 test_that("HungarianAlgorithm provides the correct answer on various inputs",
4 {
5 for (n in c(2,3,10,50))
6 {
7 for (repetition in 1:3)
8 {
9 # Generate a random vector, and permute it:
10 # we expect the algorithm to retrieve the permutation
11 v = runif(n, min=-1, max=1)
12 permutation = sample(1:n)
13 v_p = v[permutation]
14
15 # v is reference, v_p is v after permutation
16 # distances[i,j] = distance between i-th component of v and j-th component of v_p
17 # "in rows : reference; in columns: after permutation"
18 # "permutation" contains order on v to match v_p as closely as possible
19 distances = sapply(v_p, function(elt) ( abs(elt - v) ) )
20 assignment = .hungarianAlgorithm(distances)
21 expect_equal(assignment, permutation)
22 }
23 }
24 })