From 98f4474786ae1e129d00d1be02c35fec46bf0211 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Wed, 12 Feb 2025 10:38:22 -0500 Subject: [PATCH 1/2] feat(const): added << operator to constant so that constants also added tests for the stream operator --- src/const/private/const.cpp | 2 +- src/const/public/const.h | 10 ++++++++++ tests/const/constTest.cpp | 14 +++++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/const/private/const.cpp b/src/const/private/const.cpp index d97eed2..efa8f4b 100644 --- a/src/const/private/const.cpp +++ b/src/const/private/const.cpp @@ -82,7 +82,7 @@ bool constants::load(const std::string& filename) { int start = 0; std::string symbol = trim(line.substr(start, col_widths_[0])); start += col_widths_[0]; - std::string name = line.substr(start, col_widths_[1]); start += col_widths_[1]; + std::string name = trim(line.substr(start, col_widths_[1])); start += col_widths_[1]; std::string valueStr = line.substr(start, col_widths_[2]); start += col_widths_[2]; std::string unit = trim(line.substr(start, col_widths_[3])); start += col_widths_[3]; // Only trim the unit std::string uncertaintyStr = line.substr(start, col_widths_[4]); start += col_widths_[4]; diff --git a/src/const/public/const.h b/src/const/public/const.h index 2a44574..eae184c 100644 --- a/src/const/public/const.h +++ b/src/const/public/const.h @@ -16,6 +16,16 @@ struct constant { double uncertainty; ///< Uncertainty in the constant's value std::string unit; ///< Unit of the constant std::string reference; ///< Reference for the constant's value + + /** + * @brief overload the << operator for pretty printing + */ + friend std::ostream& operator<<(std::ostream& os, const constant& c) { + os << "<" << c.name << ": "; + os << c.value << "±" << c.uncertainty << " "; + os << c.unit << " (" << c.reference << ")>\n"; + return os; + } }; /** diff --git a/tests/const/constTest.cpp b/tests/const/constTest.cpp index 9fd3a05..5bfebe4 100644 --- a/tests/const/constTest.cpp +++ b/tests/const/constTest.cpp @@ -4,6 +4,7 @@ #include #include #include +#include std::string RELATIVE_PATH = "../src/resources/const/const.dat"; /** @@ -22,7 +23,6 @@ protected: void SetUp() override { // Create a DObject with initial data and metadata constants initializedConstants(RELATIVE_PATH); - std::cout << "speed of light (tests) " << initializedConstants["c"].value << std::endl; } }; @@ -88,7 +88,6 @@ TEST_F(constTest, Keys) { for (const auto& key : checkKeys) { bool found = keys.find(key) != keys.end(); - std::cout << "FOUND SIMILAR KEY " << key << ", found: " << found << std::endl; EXPECT_TRUE(found); } @@ -101,7 +100,16 @@ TEST_F(constTest, Keys) { for (const auto& key : checkBadKeys) { bool found = keys.find(key) != keys.end(); - std::cout << "FOUND BAD KEY " << key << ", found: " << found << std::endl; EXPECT_FALSE(found); } +} + +TEST_F(constTest, Output) { + constants obj(RELATIVE_PATH); + std::ostringstream os; + + os << obj.get("c"); + std::string expected = "\n"; + + EXPECT_EQ(os.str(), expected); } \ No newline at end of file From bbd3fcc3a3e46a134d3c427a0d8336b58e26240b Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Wed, 12 Feb 2025 10:39:34 -0500 Subject: [PATCH 2/2] fix(const.dat): removed extra space from eta unit line eta (liminous efficacy) was overflowing its unit line causing errors when parsing the uncertainty. Removed extra space to contain it within the proper column width --- src/resources/const/const.dat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/const/const.dat b/src/resources/const/const.dat index b6412ab..2d51028 100644 --- a/src/resources/const/const.dat +++ b/src/resources/const/const.dat @@ -428,7 +428,7 @@ mH_amu_rel helion relative atomic mass h_shield_shift helion shielding shift 5.99674300e-05 1.00000000e-10 CODATA2022 F_hyperfine_Cs-133 hyperfine transition frequency of Cs-133 9.19263177e+09 1 / s 0.00000000e+00 CODATA2022 aSi_220_ideal lattice spacing of ideal Si (220) 1.92015572e-08 cm 3.20000000e-16 CODATA2022 -eta luminous efficacy 6.83000000e-05 s3 rad2 cd / (g cm2) 0.00000000e+00 CODATA2022 +eta luminous efficacy 6.83000000e-05 s3 rad2 cd/(g cm2) 0.00000000e+00 CODATA2022 rg_n_MHz_T neutron gyromag. ratio in MHz/T 2.91646931e+01 MHz T^-1 6.90000000e-06 CODATA2022 mN_amu_rel neutron relative atomic mass 1.00866492e+00 4.90000000e-10 CODATA2022 mu_N_invm nuclear magneton in inverse meter per tesla 2.54262341e-02 m^-1 T^-1 7.80000000e-12 CODATA2022