|
| 1 | + |
| 2 | +test_that("regex_match works correctly", { |
| 3 | + text <- c("a1", "b2", "c3") |
| 4 | + pattern <- "([a-z])([0-9])" |
| 5 | + |
| 6 | + # Basic match |
| 7 | + res <- regex_match(text, pattern) |
| 8 | + expect_length(res, 3) |
| 9 | + expect_equal(res[[1]][1], "a1") |
| 10 | + expect_equal(res[[1]][2], "a") |
| 11 | + expect_equal(res[[1]][3], "1") |
| 12 | + |
| 13 | + # With index extraction |
| 14 | + res_idx <- regex_match(text, pattern, i = 2) |
| 15 | + expect_equal(res_idx, c("a", "b", "c")) |
| 16 | + |
| 17 | + res_idx_3 <- regex_match(text, pattern, i = 3) |
| 18 | + expect_equal(res_idx_3, c("1", "2", "3")) |
| 19 | + |
| 20 | + # Out of bounds index |
| 21 | + res_oob <- regex_match(text, pattern, i = 4) |
| 22 | + expect_true(all(is.na(res_oob))) |
| 23 | +}) |
| 24 | + |
| 25 | +test_that(".get_ram_gb handles options and system info", { |
| 26 | + # Test mock |
| 27 | + withr::with_options(list(gridmaker.fake_ram = 100), { |
| 28 | + expect_equal(.get_ram_gb("avail"), 100) |
| 29 | + expect_equal(.get_ram_gb()$available, 100) |
| 30 | + }) |
| 31 | + |
| 32 | + # Test actual fallback (smoke test) |
| 33 | + skip_on_cran() # PS package behavior might vary |
| 34 | + res <- .get_ram_gb() |
| 35 | + expect_type(res, "list") |
| 36 | + # Expect reasonable keys usually present |
| 37 | + # But won't assert too strictly on system dependent stuff |
| 38 | +}) |
| 39 | + |
| 40 | +test_that(".estimate_grid_memory_gb calculates reasonable estimates", { |
| 41 | + # Mock environment where we know sizing roughly? |
| 42 | + # Or just smoke test that it returns a number. |
| 43 | + |
| 44 | + grid_extent <- sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 1000, ymax = 1000), crs = 3857) |
| 45 | + |
| 46 | + est <- .estimate_grid_memory_gb( |
| 47 | + grid_extent = grid_extent, |
| 48 | + cellsize_m = 10, |
| 49 | + crs = 3857, |
| 50 | + output_type = "sf_polygons", |
| 51 | + id_format = "both", |
| 52 | + include_llc = TRUE, |
| 53 | + point_type = "centroid" |
| 54 | + ) |
| 55 | + |
| 56 | + expect_type(est, "double") |
| 57 | + expect_gte(est, 0) |
| 58 | + |
| 59 | + # Trivial grid |
| 60 | + est_zero <- .estimate_grid_memory_gb( |
| 61 | + grid_extent = sf::st_bbox(c(xmin=0,ymin=0,xmax=1,ymax=1), crs=3857), |
| 62 | + cellsize_m = 100, |
| 63 | + crs = 3857, |
| 64 | + output_type = "sf_polygons", |
| 65 | + id_format = "both", |
| 66 | + include_llc = TRUE, |
| 67 | + point_type = "centroid" |
| 68 | + ) |
| 69 | + expect_equal(est_zero, 0) |
| 70 | +}) |
| 71 | + |
| 72 | +test_that("validate_disk_compatibility rejects invalid combinations", { |
| 73 | + # DataFrame to KEA |
| 74 | + expect_error( |
| 75 | + validate_disk_compatibility("dataframe", "test.kea"), |
| 76 | + "cannot be written to" |
| 77 | + ) |
| 78 | + |
| 79 | + # Raster to CSV |
| 80 | + # Raster requires specific extensions |
| 81 | + expect_error( |
| 82 | + validate_disk_compatibility("spatraster", "test.txt"), |
| 83 | + "Unsupported raster format" |
| 84 | + ) |
| 85 | + |
| 86 | + # Raster to unknown |
| 87 | + expect_error( |
| 88 | + validate_disk_compatibility("spatraster", "test.xyz"), |
| 89 | + "Unsupported raster format" |
| 90 | + ) |
| 91 | + |
| 92 | + # Vector to No-Append format (KML) |
| 93 | + expect_error( |
| 94 | + validate_disk_compatibility("sf_polygons", "test.kml"), |
| 95 | + "does not support appending" |
| 96 | + ) |
| 97 | +}) |
| 98 | + |
| 99 | +test_that(".ext_to_driver maps correctly", { |
| 100 | + expect_equal(.ext_to_driver("tif", "raster"), "GTiff") |
| 101 | + expect_equal(.ext_to_driver("gpkg", "vector"), "GPKG") |
| 102 | + expect_null(.ext_to_driver("xyz", "vector")) |
| 103 | +}) |
| 104 | + |
| 105 | +test_that(".tz_count counts trailing zeros", { |
| 106 | + expect_equal(.tz_count(100), 2) |
| 107 | + expect_equal(.tz_count(10), 1) |
| 108 | + expect_equal(.tz_count(1), 0) |
| 109 | + expect_equal(.tz_count(0), 0) |
| 110 | + expect_equal(.tz_count(101), 0) |
| 111 | +}) |
0 commit comments