# Property-based testing demo: detects an edge-case boundary bug in clinical age grouping # using quickcheck::for_all() and shrinks the failing input to the minimal counterexample. # # Run with: # Rscript property-demo.R library(quickcheck) library(testthat) #' Classify age into standard clinical categories (buggy version) #' #' @param age Numeric or integer vector of patient ages. #' @return Character vector with categories "< 18", "18-64", or ">= 65". classify_age_buggy <- function(age) { dplyr::case_when( is.na(age) ~ NA_character_, age < 18 ~ "< 18", age > 18 & age < 65 ~ "18-64", # Planted edge-case bug: strict > drops age == 18 age >= 65 ~ ">= 65" ) } #' Classify age into standard clinical categories (fixed version) #' #' @param age Numeric or integer vector of patient ages. #' @return Character vector with categories "< 18", "18-64", or ">= 65". classify_age_fixed <- function(age) { dplyr::case_when( is.na(age) ~ NA_character_, age < 18 ~ "< 18", age >= 18 & age < 65 ~ "18-64", age >= 65 ~ ">= 65" ) } #' Derive age group in an ADaM-style dataset #' #' @param data A data frame containing patient records. #' @param age_col Name of the numeric age column (default "AGE"). #' @param new_col Name of the derived column to create (default "AGEGR1"). #' @param classify Classification function to use (default classify_age_fixed). #' @return Data frame with the derived age group column appended. derive_age_group <- function( data, age_col = "AGE", new_col = "AGEGR1", classify = classify_age_fixed ) { if (!is.data.frame(data)) { stop("`data` must be a data frame.", call. = FALSE) } if (!age_col %in% names(data)) { stop(sprintf("Column `%s` not found in `data`.", age_col), call. = FALSE) } data[[new_col]] <- classify(data[[age_col]]) data } #' Convenience wrapper: derive age group using buggy classifier #' #' @param data A data frame containing patient records. #' @param age_col Name of the numeric age column. #' @param new_col Name of the derived column to create. #' @return Data frame with derived age group. derive_age_group_buggy <- function(data, age_col = "AGE", new_col = "AGEGR1") { derive_age_group( data, age_col = age_col, new_col = new_col, classify = classify_age_buggy ) } #' Convenience wrapper: derive age group using fixed classifier #' #' @param data A data frame containing patient records. #' @param age_col Name of the numeric age column. #' @param new_col Name of the derived column to create. #' @return Data frame with derived age group. derive_age_group_fixed <- function(data, age_col = "AGE", new_col = "AGEGR1") { derive_age_group( data, age_col = age_col, new_col = new_col, classify = classify_age_fixed ) } #' Run property test for age classification #' #' Invariant: every valid, non-missing integer age between 0 and 100 must produce #' a non-missing classification belonging to the allowed clinical levels. #' #' @param classify_fn Function under test. #' @param tests Number of random iterations (default 100L). #' @param seed Random seed for reproducible generation. #' @return The expectation result from for_all(). test_age_property <- function(classify_fn, tests = 100L, seed = 42L) { set.seed(seed) allowed_levels <- c("< 18", "18-64", ">= 65") quickcheck::for_all( age = quickcheck::integer_bounded(0L, 100L), property = function(age) { res <- classify_fn(age) testthat::expect_true(all(!is.na(res))) testthat::expect_true(all(res %in% allowed_levels)) }, tests = tests ) } #' Run property test for tabular derivation #' #' Invariants: #' 1. Row count is preserved: nrow(result) == nrow(data) #' 2. Completeness: is.na(result$AGEGR1) == is.na(data$AGE) #' 3. Domain: all non-NA derived values belong to the allowed levels #' #' @param derive_fn Derivation function under test. #' @param tests Number of random iterations (default 100L). #' @param seed Random seed for reproducible generation. #' @return The expectation result from for_all(). test_derivation_property <- function(derive_fn, tests = 100L, seed = 42L) { set.seed(seed) allowed_levels <- c("< 18", "18-64", ">= 65") quickcheck::for_all( df = quickcheck::data_frame_( AGE = quickcheck::integer_bounded(0L, 100L, any_na = FALSE), rows = c(0L, 25L) ), property = function(df) { res <- derive_fn(df) testthat::expect_equal(nrow(res), nrow(df)) testthat::expect_equal(is.na(res$AGEGR1), is.na(df$AGE)) valid_vals <- res$AGEGR1[!is.na(res$AGEGR1)] testthat::expect_true(all(valid_vals %in% allowed_levels)) }, tests = tests ) } # Run demo when executed as a standalone script if (sys.nframe() == 0L) { cat("=======================================================\n") cat("Step 1: Testing classify_age_buggy() with quickcheck\n") cat("=======================================================\n") tryCatch( test_age_property(classify_age_buggy, tests = 100L, seed = 42L), error = function(e) { cat("Caught property failure:\n", conditionMessage(e), "\n\n") } ) cat("=======================================================\n") cat("Step 2: Testing classify_age_fixed() with quickcheck\n") cat("=======================================================\n") fixed_res <- test_age_property(classify_age_fixed, tests = 100L, seed = 42L) cat("Result:", class(fixed_res)[1], "(passed all 100 random tests)\n\n") cat("=======================================================\n") cat("Step 3: Testing derive_age_group_fixed() on random data frames\n") cat("=======================================================\n") df_res <- test_derivation_property( derive_age_group_fixed, tests = 100L, seed = 42L ) cat("Result:", class(df_res)[1], "(all tabular invariants hold)\n") }