From 399d0cba38a3d0ba551d483627bf0b70b3f622f6 Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Thu, 29 Aug 2024 10:03:01 -0400 Subject: [PATCH] prepare_operations: avoid crash when no rasters provided --- src/utils.cpp | 4 ++++ test/test_utils.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/utils.cpp b/src/utils.cpp index aeda58f..83a335d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -342,6 +342,10 @@ prepare_operations( const RasterSourceVect& rasters, const RasterSourceVect& weights) { + if (!descriptors.empty() && rasters.empty()) { + throw std::runtime_error("no rasters provided to prepare_operations"); + } + std::vector> ops; std::vector parsed_descriptors; diff --git a/test/test_utils.cpp b/test/test_utils.cpp index cb05b14..a34e9be 100644 --- a/test/test_utils.cpp +++ b/test/test_utils.cpp @@ -315,6 +315,16 @@ TEST_CASE("prepare_operations") CHECK(ops[0]->name == "mean"); CHECK(ops[1]->name == "adj_mean"); } + + SECTION("error when no rasters are provided") + { + auto values = make_rasters("v", 0); + std::vector> weights; + std::vector stats{ "sum" }; + + CHECK_THROWS_WITH(prepare_operations(stats, values, weights), + Catch::Contains("no rasters provided")); + } } TEST_CASE("parsing arguments")