Skip to content

Commit d9e134b

Browse files
committed
Add doc test to cargo test in config.rs
1 parent a63167c commit d9e134b

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

src/config.rs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,167 @@ impl Config {
406406
s
407407
}
408408
}
409+
410+
#[cfg(test)]
411+
mod tests {
412+
use crate::config::{loss2string, string2loss, Config, Loss};
413+
414+
const STRINGLOSS: [(&'static str, Loss); 11] = [
415+
("LogLikelyhood", Loss::LogLikelyhood),
416+
("SquaredError", Loss::SquaredError),
417+
("LAD", Loss::LAD),
418+
("reg:linear", Loss::RegLinear),
419+
("binary:logistic", Loss::BinaryLogistic),
420+
("reg:logistic", Loss::RegLogistic),
421+
("binary:logitraw", Loss::BinaryLogitraw),
422+
("multi:softprob", Loss::MultiSoftprob),
423+
("multi:softmax", Loss::MultiSoftmax),
424+
("rank:pairwise", Loss::RankPairwise),
425+
("unknown", Loss::SquaredError),
426+
];
427+
428+
#[test]
429+
fn doc_test_config_head() {
430+
let mut cfg = Config::new();
431+
cfg.set_feature_size(4);
432+
cfg.set_max_depth(3);
433+
cfg.set_iterations(3);
434+
cfg.set_loss("LAD");
435+
436+
assert_eq!(cfg.feature_size, 4);
437+
assert_eq!(cfg.max_depth, 3);
438+
assert_eq!(cfg.iterations, 3);
439+
assert_eq!(cfg.loss, Loss::LAD);
440+
}
441+
442+
#[test]
443+
fn doc_test_string2loss() {
444+
for (s, l) in &STRINGLOSS {
445+
assert_eq!(string2loss(s), *l);
446+
}
447+
}
448+
449+
#[test]
450+
fn doc_test_loss2string() {
451+
for (s, l) in &STRINGLOSS[..10] {
452+
assert_eq!(loss2string(l), *s);
453+
}
454+
}
455+
456+
#[test]
457+
fn doc_test_config_new() {
458+
let cfg = Config::new();
459+
assert_eq!(cfg.feature_size, 1);
460+
assert_eq!(cfg.max_depth, 2);
461+
assert_eq!(cfg.iterations, 2);
462+
assert_eq!(cfg.shrinkage, 1.0);
463+
assert_eq!(cfg.feature_sample_ratio, 1.0);
464+
assert_eq!(cfg.data_sample_ratio, 1.0);
465+
assert_eq!(cfg.min_leaf_size, 1);
466+
assert_eq!(cfg.loss, Loss::SquaredError);
467+
assert_eq!(cfg.debug, false);
468+
assert_eq!(cfg.initial_guess_enabled, false);
469+
assert_eq!(cfg.training_optimization_level, 2);
470+
}
471+
472+
#[test]
473+
fn doc_test_set_feature_size() {
474+
let mut cfg = Config::new();
475+
cfg.set_feature_size(10);
476+
assert_eq!(cfg.feature_size, 10);
477+
cfg.set_feature_size(20);
478+
assert_eq!(cfg.feature_size, 20);
479+
}
480+
481+
#[test]
482+
fn doc_test_set_shrinkage() {
483+
let mut cfg = Config::new();
484+
cfg.set_shrinkage(3.0);
485+
assert_eq!(cfg.shrinkage, 3.0);
486+
cfg.set_shrinkage(5.0);
487+
assert_eq!(cfg.shrinkage, 5.0);
488+
}
489+
490+
#[test]
491+
fn doc_test_set_training_optimization_level() {
492+
let mut cfg = Config::new();
493+
cfg.set_training_optimization_level(0);
494+
assert_eq!(cfg.training_optimization_level, 0);
495+
cfg.set_training_optimization_level(1);
496+
assert_eq!(cfg.training_optimization_level, 1);
497+
cfg.set_training_optimization_level(2);
498+
assert_eq!(cfg.training_optimization_level, 2);
499+
cfg.set_training_optimization_level(3);
500+
assert_eq!(cfg.training_optimization_level, 2);
501+
cfg.set_training_optimization_level(100);
502+
assert_eq!(cfg.training_optimization_level, 2);
503+
}
504+
505+
#[test]
506+
fn doc_test_set_iterations() {
507+
let mut cfg = Config::new();
508+
cfg.set_iterations(1);
509+
assert_eq!(cfg.iterations, 1);
510+
cfg.set_iterations(10);
511+
assert_eq!(cfg.iterations, 10);
512+
cfg.set_iterations(100);
513+
assert_eq!(cfg.iterations, 100);
514+
}
515+
516+
#[test]
517+
fn doc_test_set_feature_sample_ratio() {
518+
let mut cfg = Config::new();
519+
cfg.set_feature_sample_ratio(1.0);
520+
assert_eq!(cfg.feature_sample_ratio, 1.0);
521+
cfg.set_feature_sample_ratio(0.9);
522+
assert_eq!(cfg.feature_sample_ratio, 0.9);
523+
cfg.set_feature_sample_ratio(1.8);
524+
assert_eq!(cfg.feature_sample_ratio, 1.8);
525+
}
526+
527+
#[test]
528+
fn doc_test_set_data_sample_ratio() {
529+
let mut cfg = Config::new();
530+
cfg.set_data_sample_ratio(1.0);
531+
assert_eq!(cfg.data_sample_ratio, 1.0);
532+
cfg.set_data_sample_ratio(0.9);
533+
assert_eq!(cfg.data_sample_ratio, 0.9);
534+
cfg.set_data_sample_ratio(1.8);
535+
assert_eq!(cfg.data_sample_ratio, 1.8);
536+
}
537+
538+
#[test]
539+
fn doc_test_min_leaf_size() {
540+
let mut cfg = Config::new();
541+
cfg.set_min_leaf_size(1);
542+
assert_eq!(cfg.min_leaf_size, 1);
543+
cfg.set_min_leaf_size(10);
544+
assert_eq!(cfg.min_leaf_size, 10);
545+
cfg.set_min_leaf_size(100);
546+
assert_eq!(cfg.min_leaf_size, 100);
547+
}
548+
549+
#[test]
550+
fn doc_test_set_loss() {
551+
let mut cfg = Config::new();
552+
for (s, l) in &STRINGLOSS {
553+
cfg.set_loss(s);
554+
assert_eq!(cfg.loss, *l);
555+
}
556+
}
557+
558+
#[test]
559+
fn doc_test_set_debug() {
560+
let mut cfg = Config::new();
561+
cfg.set_debug(true);
562+
assert_eq!(cfg.debug, true);
563+
cfg.set_debug(false);
564+
assert_eq!(cfg.debug, false);
565+
}
566+
567+
#[test]
568+
fn doc_test_to_string() {
569+
let cfg = Config::new();
570+
assert_eq!(cfg.to_string(), "number of features = 1\nmin leaf size = 1\nmaximum depth = 2\niterations = 2\nshrinkage = 1\nfeature sample ratio = 1\ndata sample ratio = 1\ndebug enabled = false\nloss type = SquaredError\ninitial guess enabled = false\n");
571+
}
572+
}

0 commit comments

Comments
 (0)