Skip to content

Commit

Permalink
Merge branch 'main' into chiphogg/check-cmake-headers#255
Browse files Browse the repository at this point in the history
  • Loading branch information
chiphogg committed Jan 21, 2025
2 parents a38a5f2 + 417dc07 commit 040413e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions au/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ cc_library(
includes = ["code"],
visibility = ["//visibility:public"],
deps = [
":constant",
":magnitude",
":quantity",
":quantity_point",
":unit_symbol",
":zero",
],
)
Expand All @@ -120,6 +123,7 @@ cc_test(
size = "small",
srcs = ["code/au/io_test.cc"],
deps = [
":constants",
":io",
":prefix",
"@com_google_googletest//:gtest_main",
Expand Down
21 changes: 21 additions & 0 deletions au/code/au/io.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

#include <ostream>

#include "au/constant.hh"
#include "au/magnitude.hh"
#include "au/quantity.hh"
#include "au/quantity_point.hh"
#include "au/unit_symbol.hh"
#include "au/zero.hh"

namespace au {
Expand Down Expand Up @@ -47,4 +50,22 @@ inline std::ostream &operator<<(std::ostream &out, Zero) {
return out;
}

// Streaming support for Magnitude: print the magnitude label.
template <typename... BPs>
std::ostream &operator<<(std::ostream &out, Magnitude<BPs...> m) {
return (out << mag_label(m));
}

// Streaming support for Constant: print the unit label.
template <typename U>
std::ostream &operator<<(std::ostream &out, Constant<U>) {
return (out << unit_label(U{}));
}

// Streaming support for unit symbols: print the unit label.
template <typename U>
std::ostream &operator<<(std::ostream &out, SymbolFor<U>) {
return (out << unit_label(U{}));
}

} // namespace au
28 changes: 22 additions & 6 deletions au/code/au/io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

#include <cstdint>

#include "au/constants/speed_of_light.hh"
#include "au/prefix.hh"
#include "au/quantity.hh"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::StrEq;

namespace au {
namespace {
template <typename T>
Expand All @@ -35,12 +39,6 @@ struct Feet : UnitImpl<Length> {
constexpr const char Feet::label[];
constexpr auto feet = QuantityMaker<Feet>{};

struct Seconds : UnitImpl<Time> {
static constexpr const char label[] = "s";
};
constexpr const char Seconds::label[];
constexpr auto second = SingularNameFor<Seconds>{};

struct Kelvins : UnitImpl<Temperature> {
static constexpr const char label[] = "K";
};
Expand Down Expand Up @@ -81,4 +79,22 @@ TEST(StreamingOutput, DistinguishesPointFromQuantityByAtSign) {

TEST(StreamingOutput, PrintsZero) { EXPECT_EQ(stream_to_string(ZERO), "0"); }

TEST(StreamingOutput, PrintsMagnitude) {
EXPECT_THAT(stream_to_string(mag<289374>()), StrEq("289374"));
EXPECT_THAT(stream_to_string(mag<22>() / mag<7>()), StrEq("22 / 7"));
}

TEST(StreamingOutput, PrintsDefaultLabelForMagnitudeWeCantLabelYet) {
EXPECT_THAT(stream_to_string(cbrt(Magnitude<Pi>{})), StrEq("(UNLABELED SCALE FACTOR)"));
}

TEST(StreamingOutput, PrintsUnitLabelForConstant) {
EXPECT_THAT(stream_to_string(SPEED_OF_LIGHT), StrEq("c"));
EXPECT_THAT(stream_to_string(SPEED_OF_LIGHT * mag<3>() / mag<4>()), StrEq("[(3 / 4) c]"));
}

TEST(StreamingOutput, PrintsUnitLabelForSymbol) {
EXPECT_THAT(stream_to_string(symbol_for(feet)), StrEq("ft"));
}

} // namespace au

0 comments on commit 040413e

Please sign in to comment.