test_utils.R 5.05 KB
Newer Older
1
2
test_that(".params2str() works as expected for empty lists", {
    out_str <- .params2str(
3
4
        params = list()
    )
5
6
    expect_identical(class(out_str), "character")
    expect_equal(out_str, "")
7
8
})

9
test_that(".params2str() works as expected for a key in params with multiple different-length elements", {
10
11
12
13
14
15
16
    metrics <- c("a", "ab", "abc", "abcdefg")
    params <- list(
        objective = "magic"
        , metric = metrics
        , nrounds = 10L
        , learning_rate = 0.0000001
    )
17
    out_str <- .params2str(
18
19
        params = params
    )
20
    expect_identical(class(out_str), "character")
21
    expect_identical(
22
        out_str
23
24
25
        , "objective=magic metric=a,ab,abc,abcdefg nrounds=10 learning_rate=0.0000001"
    )
})
26

27
28
test_that(".params2str() passes through duplicated params", {
    out_str <- .params2str(
29
30
31
        params = list(
            objective = "regression"
            , bagging_fraction = 0.8
32
            , bagging_fraction = 0.5  # nolint: duplicate_argument
33
34
35
36
37
        )
    )
    expect_equal(out_str, "objective=regression bagging_fraction=0.8 bagging_fraction=0.5")
})

38
39
test_that(".check_eval works as expected with no metric", {
    params <- .check_eval(
40
41
42
43
44
45
46
        params = list(device = "cpu")
        , eval = "binary_error"
    )
    expect_named(params, c("device", "metric"))
    expect_identical(params[["metric"]], list("binary_error"))
})

47
48
test_that(".check_eval adds eval to metric in params", {
    params <- .check_eval(
49
50
51
52
53
54
55
        params = list(metric = "auc")
        , eval = "binary_error"
    )
    expect_named(params, "metric")
    expect_identical(params[["metric"]], list("auc", "binary_error"))
})

56
57
test_that(".check_eval adds eval to metric in params if two evaluation names are provided", {
    params <- .check_eval(
58
59
60
61
62
63
64
        params = list(metric = "auc")
        , eval = c("binary_error", "binary_logloss")
    )
    expect_named(params, "metric")
    expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss"))
})

65
66
test_that(".check_eval adds eval to metric in params if a list is provided", {
    params <- .check_eval(
67
68
69
70
71
72
        params = list(metric = "auc")
        , eval = list("binary_error", "binary_logloss")
    )
    expect_named(params, "metric")
    expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss"))
})
73

74
75
test_that(".check_eval drops duplicate metrics and preserves order", {
    params <- .check_eval(
76
77
78
79
80
81
        params = list(metric = "l1")
        , eval = list("l2", "rmse", "l1", "rmse")
    )
    expect_named(params, "metric")
    expect_identical(params[["metric"]], list("l1", "l2", "rmse"))
})
82

83
test_that(".check_wrapper_param() uses passed-in keyword arg if no alias found in params", {
84
    kwarg_val <- sample(seq_len(100L), size = 1L)
85
    params <- .check_wrapper_param(
86
87
88
89
90
91
92
        main_param_name = "num_iterations"
        , params = list()
        , alternative_kwarg_value = kwarg_val
    )
    expect_equal(params[["num_iterations"]], kwarg_val)
})

93
test_that(".check_wrapper_param() prefers main parameter to alias and keyword arg", {
94
95
    num_iterations <- sample(seq_len(100L), size = 1L)
    kwarg_val <- sample(seq_len(100L), size = 1L)
96
    params <- .check_wrapper_param(
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        main_param_name = "num_iterations"
        , params = list(
            num_iterations = num_iterations
            , num_tree = sample(seq_len(100L), size = 1L)
            , n_estimators = sample(seq_len(100L), size = 1L)
        )
        , alternative_kwarg_value = kwarg_val
    )
    expect_equal(params[["num_iterations"]], num_iterations)

    # aliases should be removed
    expect_identical(params, list(num_iterations = num_iterations))
})

111
test_that(".check_wrapper_param() prefers alias to keyword arg", {
112
113
114
    n_estimators <- sample(seq_len(100L), size = 1L)
    num_tree <- sample(seq_len(100L), size = 1L)
    kwarg_val <- sample(seq_len(100L), size = 1L)
115
    params <- .check_wrapper_param(
116
117
118
119
120
121
122
123
124
125
        main_param_name = "num_iterations"
        , params = list(
            num_tree = num_tree
            , n_estimators = n_estimators
        )
        , alternative_kwarg_value = kwarg_val
    )
    expect_equal(params[["num_iterations"]], num_tree)
    expect_identical(params, list(num_iterations = num_tree))

126
    # switching the order shouldn't switch which one is chosen
127
    params2 <- .check_wrapper_param(
128
129
130
131
132
133
134
        main_param_name = "num_iterations"
        , params = list(
            n_estimators = n_estimators
            , num_tree = num_tree
        )
        , alternative_kwarg_value = kwarg_val
    )
135
136
    expect_equal(params2[["num_iterations"]], num_tree)
    expect_identical(params2, list(num_iterations = num_tree))
137
})
138

139
140
141
142
143
144
145
146
147
148
test_that(".equal_or_both_null produces expected results", {
    expect_true(.equal_or_both_null(NULL, NULL))
    expect_false(.equal_or_both_null(1.0, NULL))
    expect_false(.equal_or_both_null(NULL, 1.0))
    expect_true(.equal_or_both_null(1.0, 1.0))
    expect_true(.equal_or_both_null(1.0, 1L))
    expect_false(.equal_or_both_null(NA, NULL))
    expect_false(.equal_or_both_null(NULL, NA))
    expect_false(.equal_or_both_null(10.0, 1L))
    expect_true(.equal_or_both_null(0L, 0L))
149
})