LCOV - code coverage report
Current view: top level - core - except.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 58 81.0 %
Date: 2024-04-28 01:25:40 Functions: 16 18 88.9 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : /// @author Simon Heybrock
       5             : #include "scipp/core/except.h"
       6             : #include "scipp/common/index.h"
       7             : #include "scipp/core/dimensions.h"
       8             : #include "scipp/core/slice.h"
       9             : 
      10             : namespace scipp::except {
      11             : 
      12          68 : TypeError::TypeError(const std::string &msg) : Error{msg} {}
      13             : 
      14             : template <>
      15           1 : void throw_mismatch_error(const core::DType &expected,
      16             :                           const core::DType &actual,
      17             :                           const std::string &optional_message) {
      18           3 :   throw TypeError("Expected dtype " + to_string(expected) + ", got " +
      19           4 :                   to_string(actual) + '.' + optional_message);
      20             : }
      21             : 
      22         251 : DimensionError::DimensionError(const std::string &msg)
      23         251 :     : Error<core::Dimensions>(msg) {}
      24             : 
      25           0 : DimensionError::DimensionError(scipp::index expectedDim, scipp::index userDim)
      26           0 :     : DimensionError("Length mismatch on insertion. Expected size: " +
      27           0 :                      std::to_string(std::abs(expectedDim)) +
      28           0 :                      " Requested size: " + std::to_string(userDim)) {}
      29             : 
      30             : namespace {
      31          64 : template <class T> std::string format_dims(const T &dims) {
      32          64 :   if (dims.empty()) {
      33          29 :     return "a scalar";
      34             :   }
      35          35 :   return "dimensions " + to_string(dims);
      36             : }
      37             : } // namespace
      38             : 
      39             : template <>
      40           1 : void throw_mismatch_error(const core::Sizes &expected,
      41             :                           const core::Sizes &actual,
      42             :                           const std::string &optional_message) {
      43           3 :   throw DimensionError("Expected " + format_dims(expected) + ", got " +
      44           4 :                        format_dims(actual) + '.' + optional_message);
      45             : }
      46             : 
      47             : template <>
      48          31 : void throw_mismatch_error(const core::Dimensions &expected,
      49             :                           const core::Dimensions &actual,
      50             :                           const std::string &optional_message) {
      51          93 :   throw DimensionError("Expected " + format_dims(expected) + ", got " +
      52         124 :                        format_dims(actual) + '.' + optional_message);
      53             : }
      54             : 
      55           0 : void throw_dimension_length_error(const core::Dimensions &expected, Dim actual,
      56             :                                   index length) {
      57           0 :   throw DimensionError{"Expected dimension to be in " + to_string(expected) +
      58           0 :                        ", got " + to_string(actual) +
      59           0 :                        " with mismatching length " + std::to_string(length) +
      60           0 :                        '.'};
      61             : }
      62             : 
      63           6 : void throw_cannot_have_variances(const DType type) {
      64           6 :   throw except::VariancesError("Variances for dtype=" + to_string(type) +
      65          12 :                                " not supported.");
      66             : }
      67             : 
      68             : } // namespace scipp::except
      69             : 
      70             : namespace scipp::expect {
      71             : namespace {
      72      684511 : template <class A, class B> void includes_impl(const A &a, const B &b) {
      73      684511 :   if (!a.includes(b))
      74          19 :     throw except::DimensionError("Expected " + to_string(a) + " to include " +
      75             :                                  to_string(b) + ".");
      76      684492 : }
      77             : } // namespace
      78             : 
      79       57583 : void includes(const core::Sizes &a, const core::Sizes &b) {
      80       57583 :   includes_impl(a, b);
      81       57579 : }
      82             : 
      83      626928 : void includes(const core::Dimensions &a, const core::Dimensions &b) {
      84      626928 :   includes_impl(a, b);
      85      626913 : }
      86             : } // namespace scipp::expect
      87             : 
      88             : namespace scipp::core::expect {
      89       87043 : void ndim_is(const Sizes &dims, const scipp::index expected) {
      90             :   using std::to_string;
      91       87043 :   if (dims.size() != expected) {
      92          51 :     throw except::DimensionError("Expected " + to_string(expected) +
      93          68 :                                  " dimensions, got " + to_string(dims.size()));
      94             :   }
      95       87026 : }
      96             : 
      97     1521840 : void validSlice(const Sizes &dims, const Slice &slice) {
      98     1521840 :   if (slice == Slice{})
      99        7504 :     return;
     100     1514336 :   const auto end = slice.end() < 0 ? slice.begin() + 1 : slice.end();
     101     1514336 :   if (!dims.contains(slice.dim()) || end > dims[slice.dim()])
     102         312 :     throw except::SliceError("Expected " + to_string(slice) + " to be in " +
     103         416 :                              to_string(dims) + ".");
     104             : }
     105             : 
     106     1455438 : void validDim(const Dim dim) {
     107     1455438 :   if (dim == Dim::Invalid)
     108           0 :     throw except::DimensionError("Dim::Invalid is not a valid dimension.");
     109     1455438 : }
     110             : 
     111     2327924 : void validExtent(const scipp::index size) {
     112     2327924 :   if (size < 0)
     113           0 :     throw except::DimensionError("Dimension size cannot be negative.");
     114     2327924 : }
     115             : 
     116             : } // namespace scipp::core::expect

Generated by: LCOV version 1.14