GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/util/Formats.cc
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 25 56 44.6%
Functions: 1 3 33.3%
Branches: 34 114 29.8%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #include "Formats.h"
5 #include <openvdb/Platform.h>
6 #include <iostream>
7 #include <iomanip>
8
9
10 namespace openvdb {
11 OPENVDB_USE_VERSION_NAMESPACE
12 namespace OPENVDB_VERSION_NAME {
13 namespace util {
14
15 int
16 printBytes(std::ostream& os, uint64_t bytes,
17 const std::string& head, const std::string& tail,
18 bool exact, int width, int precision)
19 {
20 const uint64_t one = 1;
21 int group = 0;
22
23 // Write to a string stream so that I/O manipulators like
24 // std::setprecision() don't alter the output stream.
25 std::ostringstream ostr;
26 ostr << head;
27 ostr << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
28 if (bytes >> 40) {
29 ostr << std::setw(width) << (double(bytes) / double(one << 40)) << " TB";
30 group = 4;
31 } else if (bytes >> 30) {
32 ostr << std::setw(width) << (double(bytes) / double(one << 30)) << " GB";
33 group = 3;
34 } else if (bytes >> 20) {
35 ostr << std::setw(width) << (double(bytes) / double(one << 20)) << " MB";
36 group = 2;
37 } else if (bytes >> 10) {
38 ostr << std::setw(width) << (double(bytes) / double(one << 10)) << " KB";
39 group = 1;
40 } else {
41 ostr << std::setw(width) << bytes << " Bytes";
42 }
43 if (exact && group) ostr << " (" << bytes << " Bytes)";
44 ostr << tail;
45
46 os << ostr.str();
47
48 return group;
49 }
50
51
52 int
53 printNumber(std::ostream& os, uint64_t number,
54 const std::string& head, const std::string& tail,
55 bool exact, int width, int precision)
56 {
57 int group = 0;
58
59 // Write to a string stream so that I/O manipulators like
60 // std::setprecision() don't alter the output stream.
61 std::ostringstream ostr;
62 ostr << head;
63 ostr << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
64 if (number / UINT64_C(1000000000000)) {
65 ostr << std::setw(width) << (double(number) / 1000000000000.0) << " trillion";
66 group = 4;
67 } else if (number / UINT64_C(1000000000)) {
68 ostr << std::setw(width) << (double(number) / 1000000000.0) << " billion";
69 group = 3;
70 } else if (number / UINT64_C(1000000)) {
71 ostr << std::setw(width) << (double(number) / 1000000.0) << " million";
72 group = 2;
73 } else if (number / UINT64_C(1000)) {
74 ostr << std::setw(width) << (double(number) / 1000.0) << " thousand";
75 group = 1;
76 } else {
77 ostr << std::setw(width) << number;
78 }
79 if (exact && group) ostr << " (" << number << ")";
80 ostr << tail;
81
82 os << ostr.str();
83
84 return group;
85 }
86
87 int
88 166 printTime(std::ostream& os, double milliseconds,
89 const std::string& head, const std::string& tail,
90 int width, int precision, int verbose)
91 {
92 int group = 0;
93
94 // Write to a string stream so that I/O manipulators like
95 // std::setprecision() don't alter the output stream.
96 166 std::ostringstream ostr;
97 ostr << head;
98 ostr << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
99
100 if (milliseconds >= 1000.0) {// one second or longer
101 86 const uint32_t seconds = static_cast<uint32_t>(milliseconds / 1000.0) % 60 ;
102 86 const uint32_t minutes = static_cast<uint32_t>(milliseconds / (1000.0*60)) % 60;
103 86 const uint32_t hours = static_cast<uint32_t>(milliseconds / (1000.0*60*60)) % 24;
104 86 const uint32_t days = static_cast<uint32_t>(milliseconds / (1000.0*60*60*24));
105
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 84 times.
86 if (days>0) {
106
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
3 ostr << days << (verbose==0 ? "d " : days>1 ? " days, " : " day, ");
107 group = 4;
108 }
109
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 84 times.
86 if (hours>0) {
110
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
2 ostr << hours << (verbose==0 ? "h " : hours>1 ? " hours, " : " hour, ");
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!group) group = 3;
112 }
113
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 73 times.
86 if (minutes>0) {
114
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
13 ostr << minutes << (verbose==0 ? "m " : minutes>1 ? " minutes, " : " minute, ");
115
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
13 if (!group) group = 2;
116 }
117
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (seconds>0) {
118
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 85 times.
86 if (verbose) {
119
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 ostr << seconds << (seconds>1 ? " seconds and " : " second and ");
120
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 const double msec = milliseconds - (seconds + (minutes + (hours + days * 24) * 60) * 60) * 1000.0;
121
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ostr << std::setw(width) << msec << " milliseconds (" << milliseconds << "ms)";
122 } else {
123
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 const double sec = milliseconds/1000.0 - (minutes + (hours + days * 24) * 60) * 60;
124
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 ostr << std::setw(width) << sec << "s";
125 }
126 } else {// zero seconds
127 const double msec = milliseconds - (minutes + (hours + days * 24) * 60) * 60 * 1000.0;
128 if (verbose) {
129 ostr << std::setw(width) << msec << " milliseconds (" << milliseconds << "ms)";
130 } else {
131 ostr << std::setw(width) << msec << "ms";
132 }
133 }
134
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 13 times.
86 if (!group) group = 1;
135 } else {// less than a second
136
2/4
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 80 times.
✗ Branch 4 not taken.
160 ostr << std::setw(width) << milliseconds << (verbose ? " milliseconds" : "ms");
137 }
138
139 ostr << tail;
140
141 166 os << ostr.str();
142
143 166 return group;
144 }
145
146 } // namespace util
147 } // namespace OPENVDB_VERSION_NAME
148 } // namespace openvdb
149