Package v.1.0 ready to be sent to CRAN
[morpheus.git] / pkg / tests / testthat / test-hungarianAlgorithm.R
CommitLineData
cbd88fe5
BA
1context("hungarianAlgorithm")
2
3test_that("HungarianAlgorithm provides the correct answer on various inputs",
4{
6dd5c2ac
BA
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
2b3a6af5
BA
11 v <- runif(n, min=-1, max=1)
12 permutation <- sample(1:n)
13 v_p <- v[permutation]
cbd88fe5 14
6dd5c2ac 15 # v is reference, v_p is v after permutation
2b3a6af5
BA
16 # distances[i,j] = distance between i-th component of v
17 # and j-th component of v_p
6dd5c2ac
BA
18 # "in rows : reference; in columns: after permutation"
19 # "permutation" contains order on v to match v_p as closely as possible
2b3a6af5
BA
20 distances <- sapply(v_p, function(elt) ( abs(elt - v) ) )
21 assignment <- .hungarianAlgorithm(distances)
6dd5c2ac
BA
22 expect_equal(assignment, permutation)
23 }
24 }
cbd88fe5 25})