Skip to content

Commit 0f4d52a

Browse files
authored
[R] add tests on print.xgb.DMatrix() (#8704)
1 parent 9fb12b2 commit 0f4d52a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

R-package/tests/testthat/test_dmatrix.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,56 @@ test_that("xgb.DMatrix: nrow is correct for a very sparse matrix", {
162162
dtest <- xgb.DMatrix(x)
163163
expect_equal(dim(dtest), dim(x))
164164
})
165+
166+
test_that("xgb.DMatrix: print", {
167+
data(agaricus.train, package = 'xgboost')
168+
169+
# core DMatrix with just data and labels
170+
dtrain <- xgb.DMatrix(
171+
data = agaricus.train$data
172+
, label = agaricus.train$label
173+
)
174+
txt <- capture.output({
175+
print(dtrain)
176+
})
177+
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: label colnames: yes")
178+
179+
# verbose=TRUE prints feature names
180+
txt <- capture.output({
181+
print(dtrain, verbose = TRUE)
182+
})
183+
expect_equal(txt[[1L]], "xgb.DMatrix dim: 6513 x 126 info: label colnames:")
184+
expect_equal(txt[[2L]], sprintf("'%s'", paste(colnames(dtrain), collapse = "','")))
185+
186+
# DMatrix with weights and base_margin
187+
dtrain <- xgb.DMatrix(
188+
data = agaricus.train$data
189+
, label = agaricus.train$label
190+
, weight = seq_along(agaricus.train$label)
191+
, base_margin = agaricus.train$label
192+
)
193+
txt <- capture.output({
194+
print(dtrain)
195+
})
196+
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: label weight base_margin colnames: yes")
197+
198+
# DMatrix with just features
199+
dtrain <- xgb.DMatrix(
200+
data = agaricus.train$data
201+
)
202+
txt <- capture.output({
203+
print(dtrain)
204+
})
205+
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: NA colnames: yes")
206+
207+
# DMatrix with no column names
208+
data_no_colnames <- agaricus.train$data
209+
colnames(data_no_colnames) <- NULL
210+
dtrain <- xgb.DMatrix(
211+
data = data_no_colnames
212+
)
213+
txt <- capture.output({
214+
print(dtrain)
215+
})
216+
expect_equal(txt, "xgb.DMatrix dim: 6513 x 126 info: NA colnames: no")
217+
})

0 commit comments

Comments
 (0)