GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/attributes.hpp
Date: 2024-11-10 16:24:00
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 139144 std::string attribute_name(const DynamicPrecision& prec) {
30
7/10
✓ Branch 1 taken 139144 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18785 times.
✓ Branch 4 taken 120359 times.
✓ Branch 6 taken 18785 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7146 times.
✓ Branch 9 taken 11639 times.
✓ Branch 11 taken 139144 times.
✗ Branch 12 not taken.
139144 std::string prefix = prec.is_integral() ? (prec.is_signed() ? "Int" : "UInt") : "Float";
31
3/6
✓ Branch 1 taken 139144 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 139144 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 139144 times.
✗ Branch 8 not taken.
278288 return prefix + std::to_string(prec.size_in_bytes()*8);
32 139144 }
33
34 8139 std::string attribute_name(std::endian e) {
35
2/4
✓ Branch 0 taken 8139 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8139 times.
✗ Branch 4 not taken.
16278 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 6367 times.
✗ Branch 2 not taken.
19101 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 4619 times.
✗ Branch 2 not taken.
13857 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 81004 times.
✗ Branch 2 not taken.
243012 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 17976 times.
✗ Branch 2 not taken.
53928 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 10204 std::endian from_endian_attribute(const std::string& endian) {
69
2/2
✓ Branch 1 taken 9866 times.
✓ Branch 2 taken 338 times.
10204 if (endian == "LittleEndian")
70 9866 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 21574 DynamicPrecision from_precision_attribute(const std::string& prec) {
77
2/2
✓ Branch 1 taken 793 times.
✓ Branch 2 taken 20781 times.
21574 if (prec.starts_with("Int")) {
78
1/2
✓ Branch 1 taken 793 times.
✗ Branch 2 not taken.
793 const auto bytes = prec.substr(3);
79
2/4
✓ Branch 1 taken 793 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 793 times.
793 if (bytes == "8") return int8;
80
2/4
✓ Branch 1 taken 793 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 793 times.
793 if (bytes == "16") return int16;
81
3/4
✓ Branch 1 taken 793 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 91 times.
✓ Branch 4 taken 702 times.
793 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 793 times.
✓ Branch 4 taken 11619 times.
✓ Branch 5 taken 9162 times.
21574 } else if (prec.starts_with("UInt")) {
84
1/2
✓ Branch 1 taken 11619 times.
✗ Branch 2 not taken.
11619 const auto bytes = prec.substr(4);
85
3/4
✓ Branch 1 taken 11619 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 281 times.
✓ Branch 4 taken 11338 times.
11619 if (bytes == "8") return uint8;
86
2/4
✓ Branch 1 taken 11338 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 11338 times.
11338 if (bytes == "16") return uint16;
87
3/4
✓ Branch 1 taken 11338 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1352 times.
✓ Branch 4 taken 9986 times.
11338 if (bytes == "32") return uint32;
88
2/4
✓ Branch 1 taken 9986 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9986 times.
✗ Branch 4 not taken.
9986 if (bytes == "64") return uint64;
89
3/4
✗ Branch 1 not taken.
✓ Branch 2 taken 11619 times.
✓ Branch 4 taken 8616 times.
✓ Branch 5 taken 546 times.
20781 } else if (prec.starts_with("Float")) {
90
1/2
✓ Branch 1 taken 8616 times.
✗ Branch 2 not taken.
8616 const auto bytes = prec.substr(5);
91
3/4
✓ Branch 1 taken 8616 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4003 times.
✓ Branch 4 taken 4613 times.
8616 if (bytes == "32") return float32;
92
2/4
✓ Branch 1 taken 4613 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4613 times.
✗ Branch 4 not taken.
4613 if (bytes == "64") return float64;
93
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 8616 times.
✓ Branch 4 taken 546 times.
✗ Branch 5 not taken.
9162 } else if (prec == "String") {
94 546 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