LCOV - code coverage report
Current view: top level - dataset - except.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 50 86.0 %
Date: 2024-04-28 01:25:40 Functions: 10 11 90.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/dataset/except.h"
       6             : #include "scipp/dataset/dataset.h"
       7             : 
       8             : namespace scipp::except {
       9             : 
      10          37 : DataArrayError::DataArrayError(const std::string &msg) : Error{msg} {}
      11             : 
      12             : template <>
      13           1 : void throw_mismatch_error(const dataset::DataArray &expected,
      14             :                           const dataset::DataArray &actual,
      15             :                           const std::string &optional_message) {
      16           3 :   throw DataArrayError("Expected DataArray " + to_string(expected) + ", got " +
      17           4 :                        to_string(actual) + '.' + optional_message);
      18             : }
      19             : 
      20          84 : DatasetError::DatasetError(const std::string &msg) : Error{msg} {}
      21             : 
      22             : template <>
      23           0 : void throw_mismatch_error(const dataset::Dataset &expected,
      24             :                           const dataset::Dataset &actual,
      25             :                           const std::string &optional_message) {
      26           0 :   throw DatasetError("Expected Dataset " + to_string(expected) + ", got " +
      27           0 :                      to_string(actual) + '.' + optional_message);
      28             : }
      29             : 
      30             : namespace {
      31          79 : auto format_coord_mismatch_message(const Dim dim, const Variable &a,
      32             :                                    const Variable &b,
      33             :                                    const std::string_view opname) {
      34          79 :   std::string message = "Mismatch in coordinate '" + to_string(dim);
      35          79 :   if (!opname.empty())
      36          69 :     message += "' in operation '" + std::string(opname);
      37          79 :   message += "':\n" + format_variable(a) + "\nvs\n" + format_variable(b);
      38          79 :   return message;
      39           0 : }
      40             : } // namespace
      41             : 
      42          79 : CoordMismatchError::CoordMismatchError(const Dim dim, const Variable &a,
      43             :                                        const Variable &b,
      44          79 :                                        const std::string_view opname)
      45          79 :     : DatasetError{format_coord_mismatch_message(dim, a, b, opname)} {}
      46             : 
      47             : } // namespace scipp::except
      48             : 
      49             : namespace scipp::expect {
      50             : template <>
      51        3725 : void contains(const scipp::dataset::Dataset &a, const std::string &b) {
      52        3725 :   if (!a.contains(b))
      53          45 :     throw except::NotFoundError("Expected '" + b + "' in " +
      54          60 :                                 scipp::dataset::dict_keys_to_string(a) + ".");
      55        3710 : }
      56             : } // namespace scipp::expect
      57             : 
      58             : namespace scipp::dataset::expect {
      59         534 : void coords_are_superset(const Coords &a_coords, const Coords &b_coords,
      60             :                          const std::string_view opname) {
      61        2573 :   for (const auto &[key, b_coord] : b_coords) {
      62        2098 :     if (!a_coords.contains(key)) {
      63           8 :       if (b_coord.is_aligned())
      64           0 :         throw except::CoordMismatchError(
      65           0 :             "Expected coord " + to_string(key) + " in " + to_string(a_coords) +
      66           0 :             " in operation '" + std::string(opname) + '\'');
      67             :       else
      68           8 :         continue;
      69             :     }
      70        2090 :     const auto &a_coord = a_coords[key];
      71        2090 :     if (a_coord.is_aligned() && b_coord.is_aligned() && a_coord != b_coord)
      72          59 :       throw except::CoordMismatchError(key, a_coord, b_coord, opname);
      73             :   }
      74         475 : }
      75             : 
      76         521 : void coords_are_superset(const DataArray &a, const DataArray &b,
      77             :                          const std::string_view opname) {
      78         521 :   coords_are_superset(a.coords(), b.coords(), opname);
      79         462 : }
      80             : 
      81        1332 : void matching_coord(const Dim dim, const Variable &a, const Variable &b,
      82             :                     const std::string_view opname) {
      83        1332 :   if (!equals_nan(a, b))
      84          20 :     throw except::CoordMismatchError(dim, a, b, opname);
      85        1312 : }
      86             : 
      87          73 : void is_key(const Variable &key) {
      88          73 :   if (key.dims().ndim() != 1)
      89           2 :     throw except::DimensionError(
      90           4 :         "Coord for binning or grouping must be 1-dimensional");
      91          71 :   if (key.has_variances())
      92           2 :     throw except::VariancesError(
      93           4 :         "Coord for binning or grouping cannot have variances");
      94          69 : }
      95             : 
      96             : } // namespace scipp::dataset::expect

Generated by: LCOV version 1.14