GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/attributes.hpp
Date: 2025-03-26 17:08:15
Exec Total Coverage
Lines: 52 54 96.3%
Functions: 16 16 100.0%
Branches: 72 136 52.9%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup VTK
6 * \brief Helper functions to get the VTK-specific names of things.
7 */
8 #ifndef GRIDFORMAT_VTK_ATTRIBUTES_HPP_
9 #define GRIDFORMAT_VTK_ATTRIBUTES_HPP_
10
11 #include <bit>
12
13 #include <gridformat/common/precision.hpp>
14 #include <gridformat/common/exceptions.hpp>
15 #include <gridformat/common/callable_overload_set.hpp>
16 #include <gridformat/vtk/common.hpp>
17
18 // forward declarations
19 namespace GridFormat { class DynamicPrecision; }
20 namespace GridFormat::Compression { class LZMA; class ZLIB; class LZ4; }
21 namespace GridFormat::Encoding { struct Ascii; struct Base64; struct RawBinary; }
22 // end forward declarations
23
24 namespace GridFormat::VTK {
25
26 //! \addtogroup VTK
27 //! \{
28
29 139193 std::string attribute_name(const DynamicPrecision& prec) {
30
7/10
✓ Branch 1 taken 139193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18807 times.
✓ Branch 4 taken 120386 times.
✓ Branch 6 taken 18807 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7148 times.
✓ Branch 9 taken 11659 times.
✓ Branch 11 taken 139193 times.
✗ Branch 12 not taken.
139193 std::string prefix = prec.is_integral() ? (prec.is_signed() ? "Int" : "UInt") : "Float";
31
3/6
✓ Branch 1 taken 139193 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 139193 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 139193 times.
✗ Branch 8 not taken.
278386 return prefix + std::to_string(prec.size_in_bytes()*8);
32 139193 }
33
34 8142 std::string attribute_name(std::endian e) {
35
2/4
✓ Branch 0 taken 8142 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8142 times.
✗ Branch 4 not taken.
16284 return e == std::endian::little ? "LittleEndian" : "BigEndian";
36 }
37
38
1/2
✓ Branch 1 taken 1311 times.
✗ Branch 2 not taken.
3933 std::string attribute_name(const Encoding::Ascii&) { return "ascii"; }
39
1/2
✓ Branch 1 taken 6368 times.
✗ Branch 2 not taken.
19104 std::string attribute_name(const Encoding::Base64&) { return "base64"; }
40
1/2
✓ Branch 1 taken 3796 times.
✗ Branch 2 not taken.
11388 std::string attribute_name(const Encoding::RawBinary&) { return "raw"; }
41
42
1/2
✓ Branch 1 taken 1748 times.
✗ Branch 2 not taken.
5244 std::string attribute_name(const Compression::LZMA&) { return "vtkLZMADataCompressor"; };
43
1/2
✓ Branch 1 taken 2185 times.
✗ Branch 2 not taken.
6555 std::string attribute_name(const Compression::ZLIB&) { return "vtkZLibDataCompressor"; };
44
1/2
✓ Branch 1 taken 4620 times.
✗ Branch 2 not taken.
13860 std::string attribute_name(const Compression::LZ4&) { return "vtkLZ4DataCompressor"; };
45
46
1/2
✓ Branch 1 taken 35397 times.
✗ Branch 2 not taken.
106191 std::string data_format_name(const Encoding::RawBinary&, const DataFormat::Appended&) { return "appended"; }
47
1/2
✓ Branch 1 taken 81010 times.
✗ Branch 2 not taken.
243030 std::string data_format_name(const Encoding::Base64&, const DataFormat::Appended&) { return "appended"; }
48
1/2
✓ Branch 1 taken 8845 times.
✗ Branch 2 not taken.
26535 std::string data_format_name(const Encoding::Base64&, const DataFormat::Inlined&) { return "binary"; }
49
1/2
✓ Branch 1 taken 18020 times.
✗ Branch 2 not taken.
54060 std::string data_format_name(const Encoding::Ascii&, const DataFormat::Inlined&) { return "ascii"; }
50
51 template<typename Enc, typename Format>
52 1188 std::string data_format_name(const Enc& e, const Format& format) {
53
1/2
✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
1188 const std::string encoder_name = attribute_name(e);
54
1/2
✓ Branch 2 taken 594 times.
✗ Branch 3 not taken.
1188 const std::string format_name = Overload{
55 297 [] (const DataFormat::Appended&) { return "appended"; },
56 297 [] (const DataFormat::Inlined&) { return "inlined"; }
57 } (format);
58
1/2
✓ Branch 2 taken 594 times.
✗ Branch 3 not taken.
1188 const std::string other_format_name = Overload{
59 297 [] (const DataFormat::Appended&) { return "GridFormat::VTK::inlined"; },
60 297 [] (const DataFormat::Inlined&) { return "GridFormat::VTK::appended"; }
61 } (format);
62
1/2
✓ Branch 3 taken 594 times.
✗ Branch 4 not taken.
2376 throw ValueError(
63
3/6
✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 594 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 594 times.
✗ Branch 8 not taken.
2376 "VTK's '" + format_name + "' data format cannot be used with " + encoder_name
64
3/6
✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 594 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 594 times.
✗ Branch 8 not taken.
3564 + " encoding. Please choose '" + other_format_name + "' or a different encoder."
65 );
66 3564 }
67
68 10206 std::endian from_endian_attribute(const std::string& endian) {
69
2/2
✓ Branch 1 taken 9868 times.
✓ Branch 2 taken 338 times.
10206 if (endian == "LittleEndian")
70 9868 return std::endian::little;
71
1/2
✓ Branch 1 taken 338 times.
✗ Branch 2 not taken.
338 if (endian == "BigEndian")
72 338 return std::endian::big;
73 throw ValueError("Unsupported endian attribute: '" + endian + "'");
74 }
75
76 21724 DynamicPrecision from_precision_attribute(const std::string& prec) {
77
2/2
✓ Branch 1 taken 797 times.
✓ Branch 2 taken 20927 times.
21724 if (prec.starts_with("Int")) {
78
1/2
✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
797 const auto bytes = prec.substr(3);
79
2/4
✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 797 times.
797 if (bytes == "8") return int8;
80
2/4
✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 797 times.
797 if (bytes == "16") return int16;
81
3/4
✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
✓ Branch 4 taken 702 times.
797 if (bytes == "32") return int32;
82
2/4
✓ Branch 1 taken 702 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 702 times.
✗ Branch 4 not taken.
702 if (bytes == "64") return int64;
83
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 797 times.
✓ Branch 4 taken 11701 times.
✓ Branch 5 taken 9226 times.
21724 } else if (prec.starts_with("UInt")) {
84
1/2
✓ Branch 1 taken 11701 times.
✗ Branch 2 not taken.
11701 const auto bytes = prec.substr(4);
85
3/4
✓ Branch 1 taken 11701 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 283 times.
✓ Branch 4 taken 11418 times.
11701 if (bytes == "8") return uint8;
86
2/4
✓ Branch 1 taken 11418 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 11418 times.
11418 if (bytes == "16") return uint16;
87
3/4
✓ Branch 1 taken 11418 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1352 times.
✓ Branch 4 taken 10066 times.
11418 if (bytes == "32") return uint32;
88
2/4
✓ Branch 1 taken 10066 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10066 times.
✗ Branch 4 not taken.
10066 if (bytes == "64") return uint64;
89
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 11701 times.
✓ Branch 4 taken 8664 times.
✓ Branch 5 taken 562 times.
20927 } else if (prec.starts_with("Float")) {
90
1/2
✓ Branch 1 taken 8664 times.
✗ Branch 2 not taken.
8664 const auto bytes = prec.substr(5);
91
3/4
✓ Branch 1 taken 8664 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4027 times.
✓ Branch 4 taken 4637 times.
8664 if (bytes == "32") return float32;
92
2/4
✓ Branch 1 taken 4637 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4637 times.
✗ Branch 4 not taken.
4637 if (bytes == "64") return float64;
93
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 8664 times.
✓ Branch 4 taken 562 times.
✗ Branch 5 not taken.
9226 } else if (prec == "String") {
94 562 return {Precision<char>{}};
95 }
96 throw ValueError("Cannot parse precision from '" + prec + "'");
97 }
98
99 //! \} group VTK
100
101 } // namespace GridFormat::VTK
102
103 #endif // GRIDFORMAT_VTK_ATTRIBUTES_HPP_
104