Skip to content

Commit 0111ddf

Browse files
committed
Removed extraneous includes. Documented includes (in code). Unit-testing throws on failed reading/writing SAC-files. Added tests to bring up code coverage on Trace equality (in particular, on specific inequality scenarios). Added to documentation - convenience methods and exceptions. Minor typos fixed.
1 parent 9e15c41 commit 0111ddf

8 files changed

Lines changed: 148 additions & 71 deletions

File tree

docs/index.html

Lines changed: 49 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/sac-format_manual.pdf

198 Bytes
Binary file not shown.

notes.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
* Inbox
22
** [2023-11-21 Tue 12:16]
33
Running clang-tidy locally prior to GitHub
4-
~clang-tidy --checks="bugprone-a*,bugprone-b*,bugprone-c*,bugprone-d*,bugprone-em*,bugprone-ex*,bugprone-f*,bugprone-i*,bugprone-l*,bugprone-m*,bugprone-n*,bugprone-o*,bugprone-p*,bugprone-r*,bugprone-s*,bugprone-t*,bugprone-u*,bugprone-v*,performance-*,readability-a*,readability-b*,readability-c*,readability-d*,readability-e*,readability-f*,readability-i*,readability-m*,readability-n*,readability-o*,readability-q*,readability-r*,readability-si*,readability-st*,readability-u*,portability-*,clang-analyzer-*,cppcoreguidelines-avoid-ca*,cpp-coreguidelines-avoid-co*,cpp-coreguidelines-avoid-d*,cpp-coreguidelines-avoid-g*,cpp-coreguidelines-avoid-n*,cpp-coreguidelines-avoid-r*,cpp-coreguidelines-i*,cpp-coreguidelines-m*,cpp-coreguidelines-n*,cpp-coreguidelines-o*,cpp-coreguidelines-pr*,cpp-coreguidelines-pro-bounds-p*,cpp-coreguidelines-pro-t*,cpp-coreguidelines-r*,cpp-coreguidelines-s*,cpp-coreguidelines-v*" --extra-arg="-O0 -Wall -Werror -Wshadow -Wextra -pedantic-errors -std=c++20" -p ./compile_commands.json src/sac_format.cpp~
4+
~clang-tidy --checks="bugprone-a*,bugprone-b*,bugprone-c*,bugprone-d*,bugprone-em*,bugprone-ex*,bugprone-f*,bugprone-i*,bugprone-l*,bugprone-m*,bugprone-n*,bugprone-o*,bugprone-p*,bugprone-r*,bugprone-s*,bugprone-t*,bugprone-u*,bugprone-v*,performance-*,readability-a*,readability-b*,readability-c*,readability-d*,readability-e*,readability-f*,readability-i*,readability-m*,readability-n*,readability-o*,readability-q*,readability-r*,readability-si*,readability-st*,readability-u*,portability-*,clang-analyzer-*,cppcoreguidelines-avoid-ca*,cpp-coreguidelines-avoid-co*,cpp-coreguidelines-avoid-d*,cpp-coreguidelines-avoid-g*,cpp-coreguidelines-avoid-n*,cpp-coreguidelines-avoid-r*,cpp-coreguidelines-i*,cpp-coreguidelines-m*,cpp-coreguidelines-n*,cpp-coreguidelines-o*,cpp-coreguidelines-pr*,cpp-coreguidelines-pro-bounds-p*,cpp-coreguidelines-pro-t*,cpp-coreguidelines-r*,cpp-coreguidelines-s*,cpp-coreguidelines-v*" --extra-arg="-std=c++20" -p ./compile_commands.json src/sac_format.cpp~
55
** [2023-11-21 Tue 10:16]
66
Prior to =git add -A= be sure to run =clang-format= on all *.cpp and *.hpp files!
77

src/docs/index.org

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,15 @@ a [[Lookup Table][lookup table]]. The internal components are below:
280280

281281
*** Convenience Methods
282282

283-
None implemented yet.
283+
- =degrees_to_radians=
284+
- =radians_to_degrees=
285+
- =gcarc=
286+
- =azimuth=
287+
288+
*** Exceptions
289+
290+
sac-format throws exceptions of type =std::domain_exception= in the event of a
291+
failure to read/write a SAC-file.
284292

285293
** Low-Level I/O
286294

@@ -306,7 +314,7 @@ Conversion pair for binary representation of two-word (regular) string values.
306314

307315
***** =long_string_to_binary= and =binary_to_long_string=
308316

309-
Conversion pair for binary representation of four-word (only [[=kstnm=]]) string
317+
Conversion pair for binary representation of four-word (only [[=kstnm=][=kstnm=]]) string
310318
values.
311319

312320
**** Reading/Writing

src/sac_format.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,26 +332,34 @@ double radians_to_degrees(const double radians) {
332332
return deg_per_rad * radians;
333333
}
334334

335-
double gcarc(const double latitude1, const double longitude1, const double latitude2, const double longitude2) {
335+
double gcarc(const double latitude1, const double longitude1,
336+
const double latitude2, const double longitude2) {
336337
const double lat1{degrees_to_radians(latitude1)};
337338
const double lon1{degrees_to_radians(longitude1)};
338339
const double lat2{degrees_to_radians(latitude2)};
339340
const double lon2{degrees_to_radians(longitude2)};
340-
return radians_to_degrees(std::acos(std::sin(lat1)*std::sin(lat2) + std::cos(lat1)*std::cos(lat2)*std::cos(lon2-lon1)));
341+
return radians_to_degrees(
342+
std::acos(std::sin(lat1) * std::sin(lat2) +
343+
std::cos(lat1) * std::cos(lat2) * std::cos(lon2 - lon1)));
341344
}
342345

343346
// I wonder if there is a way to do this with n-vectors
344-
double azimuth(const double latitude1, const double longitude1, const double latitude2, const double longitude2) {
347+
double azimuth(const double latitude1, const double longitude1,
348+
const double latitude2, const double longitude2) {
345349
const double lat1{degrees_to_radians(latitude1)};
346350
const double lon1{degrees_to_radians(longitude1)};
347351
const double lat2{degrees_to_radians(latitude2)};
348352
const double lon2{degrees_to_radians(longitude2)};
349353
const double dlon{lon2 - lon1};
350354
const double numerator{std::sin(dlon) * std::cos(lat2)};
351-
const double denominator{(std::cos(lat1) * std::sin(lat2)) - (std::sin(lat1) * std::cos(lat2) * std::cos(dlon))};
355+
const double denominator{(std::cos(lat1) * std::sin(lat2)) -
356+
(std::sin(lat1) * std::cos(lat2) * std::cos(dlon))};
352357
double result{radians_to_degrees(std::atan2(numerator, denominator))};
353-
if (result < 0.0) { result += 360.0; }
354-
else if (result > 360.0) { result -= 360.0; }
358+
if (result < 0.0) {
359+
result += circle_deg;
360+
} else if (result > circle_deg) {
361+
result -= circle_deg;
362+
}
355363
return result;
356364
}
357365
//------------------------------------------------------------------------------

src/sac_format.hpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,34 @@
55
#pragma once
66
/* Standard library
77
https://en.cppreference.com/w/cpp/standard_library */
8+
// std::ranges::fill
89
#include <algorithm>
10+
// std::array
911
#include <array>
1012
// std::endian
1113
#include <bit>
14+
// std::bitset
1215
#include <bitset>
13-
#include <cctype>
16+
// std::abs, std::acos, std::sin, std::cos, std::atan2, std::pow
1417
#include <cmath>
18+
// std::memcopy
1519
#include <cstring>
20+
// std::filesystem::path
1621
#include <filesystem>
22+
// std::ifstream, std::ifstream::binary, std::ofstream, std::ios::binary,
23+
// std::ios::out, std::ios::trunc
1724
#include <fstream>
18-
#include <iostream>
19-
#include <limits>
25+
// std::numbers::pi_v<double>
2026
#include <numbers>
2127
// std::domain_error
2228
#include <stdexcept>
29+
// std::string
2330
#include <string>
31+
// std::string_view (used only once...)
2432
#include <string_view>
33+
// std::unordered_map
2534
#include <unordered_map>
35+
// std::vector
2636
#include <vector>
2737

2838
namespace sacfmt {
@@ -43,6 +53,7 @@ constexpr bool unset_bool{false};
4353
// Accuracy precision expected from SAC floats
4454
constexpr float f_eps{2.75e-6F};
4555
// This should work for two and four word string headers
56+
// This is the only instance of a string_view.
4657
constexpr std::string_view unset_word{"-12345"};
4758
using word_one = std::bitset<binary_word_size>;
4859
using word_two = std::bitset<static_cast<size_t>(2) * binary_word_size>;
@@ -61,6 +72,7 @@ constexpr int old_hdr_version{6};
6172
constexpr int common_skip_num{7};
6273
constexpr double rad_per_deg{std::numbers::pi_v<double> / 180.0};
6374
constexpr double deg_per_rad{1.0 / rad_per_deg};
75+
constexpr double circle_deg{360.0};
6476
//--------------------------------------------------------------------------
6577
// Conversions
6678
//--------------------------------------------------------------------------
@@ -140,9 +152,11 @@ bool equal_within_tolerance(double val1, double val2, double tolerance = f_eps);
140152
double degrees_to_radians(double degrees);
141153
double radians_to_degrees(double radians);
142154
// gcarc
143-
double gcarc(double latitude1, double longitude1, double latitude2, double longitude2);
155+
double gcarc(double latitude1, double longitude1, double latitude2,
156+
double longitude2);
144157
// azimuth
145-
double azimuth(double latitude1, double longitude1, double latitude2, double longitude2);
158+
double azimuth(double latitude1, double longitude1, double latitude2,
159+
double longitude2);
146160

147161
enum class name {
148162
// Floats
@@ -395,6 +409,7 @@ const std::unordered_map<name, const int> sac_map = {
395409
class Trace {
396410
public:
397411
Trace();
412+
// Parametric constructor (read file)
398413
explicit Trace(const std::filesystem::path &path);
399414
void write(const std::filesystem::path &path, bool legacy = false) const;
400415
void legacy_write(const std::filesystem::path &path) const;

src/utests.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
// Copyright 2023 Alexander R. Blanchette
22

33
/* Standard Library */
4+
// std::filesystem::path, std::filesystem::temp_directory_path()
45
#include <filesystem>
5-
#include <fstream>
6-
#include <iomanip>
7-
#include <limits>
86
/* Catch2 */
97
#define CATCH_CONFIG_FAST_COMPILE
108
#define CATCH_CONFIG_MAIN
119
// testing macros
10+
// TEST_CASE, SECTION, REQUIRE, CAPTURE, REQUIRE_THROWS
1211
#include <catch2/catch_test_macros.hpp>
13-
// Floating-point matcher
12+
// from catch_matchers.hpp (any matcher includes it)
13+
// REQUIRE_THAT
14+
// Catch::Matchers::WithinAbs
1415
#include <catch2/matchers/catch_matchers_floating_point.hpp>
16+
// Catch::Matchers::EndsWith
17+
#include <catch2/matchers/catch_matchers_string.hpp>
1518
/* My stuff */
1619
#include <sac_format.hpp>
1720
#include <util.hpp>
1821

1922
using namespace sacfmt;
2023
namespace fs = std::filesystem;
2124
using Catch::Matchers::WithinAbs;
25+
using Catch::Matchers::EndsWith;
2226

2327
//==============================================================================
2428
// Basic I/O tests (file writing/reading goes with Trace)
@@ -1139,6 +1143,28 @@ TEST_CASE("Trace Read/Write") {
11391143
REQUIRE(equal_within_tolerance(trace.data2(), in.data2()));
11401144
}
11411145
SECTION("Everything") { REQUIRE(trace == in); }
1146+
SECTION("Resize data") {
1147+
Trace in2{};
1148+
in2 = in;
1149+
std::vector<double> tmp{in2.data1()};
1150+
tmp.resize(10);
1151+
in2.data1(tmp);
1152+
REQUIRE(!equal_within_tolerance(trace.data1(), in2.data1()));
1153+
}
1154+
SECTION("Change a float") {
1155+
Trace in2{};
1156+
in2 = in;
1157+
in2.evdp(in.evdp() - 1.0f);
1158+
REQUIRE(trace != in2);
1159+
}
1160+
SECTION("Change a vector value") {
1161+
Trace in2{};
1162+
in2 = in;
1163+
std::vector<double> tmp{in2.data1()};
1164+
tmp[0] -= 1.0;
1165+
in2.data1(tmp);
1166+
REQUIRE(trace != in2);
1167+
}
11421168
}
11431169
SECTION("Legacy Read/Write") {
11441170
trace.nvhdr(6);
@@ -1321,6 +1347,16 @@ TEST_CASE("Trace Read/Write") {
13211347
REQUIRE(trace != in);
13221348
}
13231349
}
1350+
SECTION("Read Throw") {
1351+
fs::path tmp_dir{fs::temp_directory_path() / "not_a_dir"};
1352+
fs::path tmp_file{tmp_dir / "not_real.sac"};
1353+
REQUIRE_THROWS_WITH(Trace(tmp_file), EndsWith("cannot be opened to read."));
1354+
}
1355+
SECTION("Write Throw") {
1356+
fs::path tmp_dir{fs::temp_directory_path() / "not_a_dir"};
1357+
fs::path tmp_file{tmp_dir / "not_real.sac"};
1358+
REQUIRE_THROWS_WITH(trace.write(tmp_file), EndsWith("cannot be opened to write."));
1359+
}
13241360
}
13251361

13261362
TEST_CASE("Geometric operations") {

src/util.hpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66
#pragma once
77

88
/* Standard library */
9-
#include <random>
9+
// std::chrono::steady_clock::now()
10+
#include <chrono>
11+
// std::setprecision
12+
#include <iomanip>
13+
// std::cout
14+
#include <iostream>
15+
// std::numeric_limits
16+
#include <limits>
1017
// INT_MIN and INT_MAX
1118
#include <limits.h>
12-
#include <limits>
13-
#include <string>
19+
// std::random_device, std::uniform_real_distribution
20+
#include <random>
1421
// ostringstream
1522
#include <sstream>
23+
// std::string
24+
#include <string>
25+
// std::vector
1626
#include <vector>
1727
/* Xoshiro Random Number generator */
1828
#include <XoshiroCpp.hpp>
@@ -73,9 +83,8 @@ inline double get(double min, double max) {
7383

7484
void random_vector(std::vector<double> *data, const double minimum = -1.0,
7585
const double maximum = 1.0) {
76-
std::uniform_real_distribution<> die{minimum, maximum};
7786
for (std::size_t i{0}; i < data->size(); ++i) {
78-
(*data)[i] = die(xos);
87+
(*data)[i] = get(minimum, maximum);
7988
}
8089
}
8190

0 commit comments

Comments
 (0)