tests/testthat/test-r-bodyfat.R

context("test-r-bodyfat.R")

test_that("calculate_bfp errors are produces when needed.", {
  # Test for error when sex is not recognized
  expect_error(
    calculate_bfp(
      weight = 80,
      height = 180,
      sex = "unknown",
      waist = 55,
      neck = 10,
      equation = "us-navy"
    )
  )
  # Test for error when one of waist or neck is missing with sex = male
  expect_error(calculate_bfp(
    weight = 80,
    height = 180,
    sex = "male",
    neck = 10,
    equation = "us-navy"
  ))
  # Test for error when one of waist or neck or hip is missing with sex = female
  expect_error(
    calculate_bfp(
      weight = 80,
      height = 180,
      sex = "female",
      waist = 55,
      neck = 10,
      equation = "us-navy"
    )
  )
  # Expect error if equation = bmi and weight or height or *age* is missing
  expect_error(calculate_bfp(
    weight = 80,
    height = 180,
    sex = "male",
    equation = "bmi"
  ))

  expect_error(calculate_bfp(
    weight = "80",
    height = 190,
    sex = "male",
    age = 30,
    equation = "cunbae"
  ))
})

test_that("calculate_bfp calculates body fat percentage correctly", {
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      waist = 90,
      neck = 36,
      hip = 94,
      sex = "female",
      equation = "us-navy"
    ),
    28.48,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      waist = 90,
      neck = 36,
      hip = 94,
      sex = "male",
      equation = "us-navy"
    ),
    21.21,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      age = 27,
      sex = "male",
      equation = "bmi"
    ),
    19.64,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      age = 27,
      sex = "female",
      equation = "bmi"
    ),
    30.44,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      sex = "male",
      waist = 80,
      equation
      = "woolcott"
    ),
    23.75,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      sex = "female",
      waist = 80,
      equation
      = "woolcott"
    ),
    38.75,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      sex = "male",
      squat = 100,
      bench = 75,
      equation
      = "strongur"
    ),
    17.47,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 180,
      sex = "female",
      squat = 100,
      bench = 75,
      equation
      = "strongur"
    ),
    22.94,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      sex = "male",
      waist = 90,
      equation = "ymca"
    ),
    19.43,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      sex = "female",
      waist = 90,
      equation = "ymca"
    ),
    31.74,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 190,
      sex = "male",
      age = 30,
      equation = "cunbae"
    ),
    17.43,
    tolerance = 0.01
  )
  expect_equal(
    calculate_bfp(
      weight = 80,
      height = 190,
      sex = "female",
      age = 30,
      equation = "cunbae"
    ),
    29.67,
    tolerance = 0.01
  )
})

test_that("calculate_fat_mass is correctly calculating fat weight", {
  expect_equal(calculate_fat_mass(weight = 80,
                                  bfp = 15),
               12)
  expect_error(calculate_fat_mass(weight = 80,
                                  bfp = 101))
  expect_error(calculate_fat_mass(weight = "80",
                                  bfp = 15))
})

test_that("calculate_bai", {
  expect_equal(calculate_bai(180, 90), 19.27, tolerance = 0.01)
  expect_error(calculate_bai("180", 90))
  expect_error(calculate_bai(180, "90"))
})
MarijnJABoer/befitteR documentation built on April 24, 2020, 5:43 a.m.