tests/testthat/test_brute_force_knapsack.R

context("brute_force_knapsack")

set.seed(42)
n <- 2000
knapsack_objects <- data.frame(
    w=sample(1:4000, size = n, replace = TRUE),
    v=runif(n = n, 0, 10000)
)

test_that("Correct object is returned", {
    expect_silent(bfk <- brute_force_knapsack(x = knapsack_objects[1:8,], W = 3500))
    expect_named(bfk, c("value", "elements"))
})


test_that("functions rejects errounous input.", {
    expect_error(brute_force_knapsack("hej", 3500))
    expect_error(brute_force_knapsack(x = knapsack_objects[1:8,], W = -3500))
})

test_that("Function return correct results.", {
    bfk <- brute_force_knapsack(x = knapsack_objects[1:8,], W = 3500)
    expect_equal(round(bfk$value), 16770)
    expect_true(all(round(bfk$elements) %in% c(5, 8)))
    
    bfk <- brute_force_knapsack(x = knapsack_objects[1:12,], W = 3500)
    expect_equal(round(bfk$value), 16770)
    expect_true(all(round(bfk$elements) %in% c(5, 8)))
    
    bfk <- brute_force_knapsack(x = knapsack_objects[1:8,], W = 2000)
    expect_equal(round(bfk$value), 15428)
    expect_true(all(round(bfk$elements) %in% c(3, 8)))
    
    bfk <- brute_force_knapsack(x = knapsack_objects[1:12,], W = 2000)
    expect_equal(round(bfk$value), 15428)
    expect_true(all(round(bfk$elements) %in% c(3, 8)))
    
    st <- system.time(bfk <- brute_force_knapsack(x = knapsack_objects[1:16,], W = 2000))
    expect_true(as.numeric(st)[2] > 0.00)
})
poceviciute/AdvRprogr_lab6 documentation built on Oct. 6, 2017, 3:21 a.m.