From 48861721ad555da5bc8db698aebddb0d0faa61cb Mon Sep 17 00:00:00 2001 From: wlandau Date: Sun, 10 Nov 2024 08:52:39 -0500 Subject: [PATCH] remove memory class --- R/class_memory.R | 45 -------------- tests/testthat/test-class_memory.R | 97 ------------------------------ 2 files changed, 142 deletions(-) delete mode 100644 R/class_memory.R delete mode 100644 tests/testthat/test-class_memory.R diff --git a/R/class_memory.R b/R/class_memory.R deleted file mode 100644 index cfa735b5..00000000 --- a/R/class_memory.R +++ /dev/null @@ -1,45 +0,0 @@ -memory_init <- function(envir = new.env(parent = emptyenv())) { - names <- names(envir) - memory_new(envir, names, length(names)) -} - -memory_new <- function(envir = NULL, names = NULL, count = NULL) { - out <- new.env(parent = emptyenv(), hash = FALSE) - out$envir <- envir - out$names <- names - out$count <- count - out -} - -memory_exists_object <- function(memory, name) { - exists(name, envir = memory$envir, inherits = FALSE) -} - -memory_get_object <- function(memory, name) { - base::get(x = name, envir = memory$envir, inherits = FALSE) -} - -memory_set_object <- function(memory, name, object) { - if (!exists(name, envir = memory$envir, inherits = FALSE)) { - memory$names <- c(memory$names, name) - memory$count <- memory$count + 1L - } - envir <- .subset2(memory, "envir") - envir[[name]] <- object -} - -memory_del_objects <- function(memory, names) { - names <- intersect(memory$names, names) - remove(list = names, envir = memory$envir, inherits = FALSE) - memory$names <- setdiff(memory$names, names) - memory$count <- memory$count - length(names) - invisible() -} - -memory_validate <- function(memory) { - tar_assert_correct_fields(memory, memory_new) - tar_assert_chr(memory$names) - tar_assert_envir(memory$envir) - tar_assert_in(memory$names, names(memory$envir)) - tar_assert_identical(length(memory$names), memory$count) -} diff --git a/tests/testthat/test-class_memory.R b/tests/testthat/test-class_memory.R deleted file mode 100644 index 48e102a7..00000000 --- a/tests/testthat/test-class_memory.R +++ /dev/null @@ -1,97 +0,0 @@ -tar_test("memory$envir", { - envir <- new.env(parent = emptyenv()) - envir$a <- "123" - out <- memory_init(envir = envir) - expect_equal(out$envir$a, "123") -}) - -tar_test("memory$names", { - envir <- new.env(parent = emptyenv()) - envir$a <- "123" - out <- memory_init(envir = envir) - expect_equal(out$names, "a") -}) - -tar_test("memory$count", { - envir <- new.env(parent = emptyenv()) - envir$a <- "123" - out <- memory_init(envir = envir) - expect_equal(out$count, 1L) -}) - -tar_test("memory_get_object()", { - envir <- new.env(parent = emptyenv()) - envir$a <- "123" - out <- memory_init(envir = envir) - expect_equal(memory_get_object(out, "a"), "123") -}) - -tar_test("memory_set_object()", { - envir <- new.env(parent = emptyenv()) - envir$a <- "123" - out <- memory_init(envir = envir) - expect_equal(out$names, "a") - expect_equal(out$count, 1L) - memory_set_object(out, "b", "456") - expect_equal(out$envir$b, "456") - expect_equal(sort(out$names), sort(c("a", "b"))) - expect_equal(out$count, 2L) -}) - -tar_test("memory_exists_object()", { - out <- memory_init() - expect_false(memory_exists_object(out, "a")) - memory_set_object(out, "a", "a") - expect_true(memory_exists_object(out, "a")) -}) - -tar_test("memory_del_objects()", { - out <- memory_init() - memory_set_object(out, "a", "123") - memory_set_object(out, "b", "456") - memory_set_object(out, "c", "789") - expect_equal(out$envir$a, "123") - expect_equal(out$envir$b, "456") - expect_equal(out$envir$c, "789") - expect_equal(sort(out$names), sort(c("a", "b", "c"))) - expect_equal(out$count, 3L) - memory_del_objects(out, c("a", "c")) - expect_null(out$envir$a) - expect_equal(out$envir$b, "456") - expect_null(out$envir$c) - expect_equal(out$names, "b") - expect_equal(out$count, 1L) -}) - -tar_test("memory_validate() on a good memory object", { - out <- memory_init() - memory_set_object(out, "a", "123") - memory_set_object(out, "b", "456") - memory_set_object(out, "c", "789") - expect_silent(memory_validate(out)) -}) - -tar_test("memory_validate() on a memory object with no environment", { - out <- memory_new(names = character(0)) - expect_error(memory_validate(out), class = "tar_condition_validate") -}) - -tar_test("memory_validate() on a memory object with no names", { - out <- memory_new(envir = new.env()) - expect_error(memory_validate(out), class = "tar_condition_validate") -}) - -tar_test("memory_validate() with an extra field", { - out <- memory_new(envir = new.env()) - out$bad <- 123 - expect_error(memory_validate(out), class = "tar_condition_validate") -}) - -tar_test("memory_validate() with incorrect names", { - out <- memory_init() - memory_set_object(out, "a", "123") - memory_set_object(out, "b", "456") - memory_set_object(out, "c", "789") - out$names <- c("a", "b") - expect_error(memory_validate(out), class = "tar_condition_validate") -})