@@ -53,35 +53,34 @@ using std::isfinite;
5353using std::isinf; // NOLINT
5454using std::isnan; // NOLINT
5555
56- template <typename Float8_>
57- class Float8Test : public ::testing::Test {};
58-
5956// Helper utility for prettier test names.
57+ template <typename T>
58+ std::string TypeToName () {
59+ if constexpr (std::is_same_v<T, Float8e3m4>) return " Float8e3m4" ;
60+ if constexpr (std::is_same_v<T, Float8e4m3fn>) return " Float8e4m3fn" ;
61+ if constexpr (std::is_same_v<T, Float8e4m3b11fnuz>)
62+ return " Float8e4m3b11fnuz" ;
63+ if constexpr (std::is_same_v<T, Float8e5m2>) return " Float8e5m2" ;
64+ if constexpr (std::is_same_v<T, Float8e4m3fnuz>) return " Float8e4m3fnuz" ;
65+ if constexpr (std::is_same_v<T, Float8e5m2fnuz>) return " Float8e5m2fnuz" ;
66+ if constexpr (std::is_same_v<T, long double >) return " long_double" ;
67+ if constexpr (std::is_same_v<T, double >) return " double" ;
68+ if constexpr (std::is_same_v<T, float >) return " float" ;
69+ if constexpr (std::is_same_v<T, tensorstore::BFloat16>) return " BFloat16" ;
70+ if constexpr (std::is_same_v<T, ::half_float::half>) return " float16" ;
71+ if constexpr (std::is_same_v<T, bool >) return " bool" ;
72+ if constexpr (std::is_same_v<T, int32_t >) return " int32_t" ;
73+ if constexpr (std::is_same_v<T, int64_t >) return " int64_t" ;
74+ return " unknown" ;
75+ }
76+
6077struct Float8TestParamNames {
6178 template <typename TypeParam>
6279 static std::string GetName (int idx) {
63- if constexpr (std::is_same_v<TypeParam, Float8e3m4>) {
64- return " Float8e3m4" ;
65- } else if constexpr (std::is_same_v<TypeParam, Float8e4m3fn>) {
66- return " Float8e4m3fn" ;
67- } else if constexpr (std::is_same_v<TypeParam, Float8e4m3b11fnuz>) {
68- return " Float8e4m3b11fnuz" ;
69- } else if constexpr (std::is_same_v<TypeParam, Float8e5m2>) {
70- return " Float8e5m2" ;
71- } else if constexpr (std::is_same_v<TypeParam, Float8e4m3fnuz>) {
72- return " Float8e4m3fnuz" ;
73- } else if constexpr (std::is_same_v<TypeParam, Float8e5m2fnuz>) {
74- return " Float8e5m2fnuz" ;
75- }
76- return absl::StrCat (idx);
80+ return TypeToName<TypeParam>();
7781 }
7882};
7983
80- using Float8Types =
81- ::testing::Types<Float8e3m4, Float8e4m3fn, Float8e5m2, Float8e4m3b11fnuz,
82- Float8e4m3fnuz, Float8e5m2fnuz>;
83- TYPED_TEST_SUITE (Float8Test, Float8Types, Float8TestParamNames);
84-
8584TEST (Float8E3m4Test, NumericLimits) {
8685 EXPECT_TRUE (isnan (std::numeric_limits<Float8e3m4>::quiet_NaN ()));
8786 EXPECT_TRUE (isnan (std::numeric_limits<Float8e3m4>::signaling_NaN ()));
@@ -260,6 +259,32 @@ TEST(Float8E5m2fnuzTest, NumericLimits) {
260259 EXPECT_EQ (std::numeric_limits<Float8e5m2fnuz>::has_signaling_NaN, false );
261260}
262261
262+ template <typename Float8_>
263+ class Float8Test : public ::testing::Test {};
264+
265+ using Float8Types =
266+ ::testing::Types<Float8e3m4, Float8e4m3fn, Float8e5m2, Float8e4m3b11fnuz,
267+ Float8e4m3fnuz, Float8e5m2fnuz>;
268+
269+ TYPED_TEST_SUITE (Float8Test, Float8Types, Float8TestParamNames);
270+
271+ TYPED_TEST (Float8Test, DefaultConstruction) {
272+ using Float = TypeParam;
273+ const Float zero (0 );
274+
275+ // sd is static, so it must be zero initialized.
276+ static Float sd;
277+ EXPECT_EQ (sd, zero);
278+
279+ // z is zero initialized.
280+ Float z{};
281+ EXPECT_EQ (z, zero);
282+
283+ // v is value initialized to zero.
284+ Float v = Float ();
285+ EXPECT_EQ (v, zero);
286+ }
287+
263288TYPED_TEST (Float8Test, FromRep) {
264289 using Float8 = TypeParam;
265290 Float8 x = Float8::FromRep (0x4F );
@@ -742,12 +767,17 @@ TYPED_TEST(Float8Test, CallTheOperator) {
742767 EXPECT_THAT ((c = a, c /= b),
743768 EqOrIsNan<Float8>(Float8{float {a} / float {b}}));
744769
745- EXPECT_EQ (a == b, float {a} == float {b}) << float {a} << " vs " << float {b};
746- EXPECT_EQ (a != b, float {a} != float {b});
747- EXPECT_EQ (a < b, float {a} < float {b});
748- EXPECT_EQ (a <= b, float {a} <= float {b});
749- EXPECT_EQ (a > b, float {a} > float {b});
750- EXPECT_EQ (a >= b, float {a} >= float {b});
770+ #define COMP (x ) \
771+ EXPECT_EQ (a x b, float {a} x float {b}) \
772+ << a << #x << b << " vs " << float {a} << #x << float {b}
773+
774+ COMP (==);
775+ COMP (!=);
776+ COMP (<); // NOLINT
777+ COMP (<=);
778+ COMP (>); // NOLINT
779+ COMP (>=);
780+ #undef COMP
751781 }
752782 }
753783}
@@ -775,12 +805,17 @@ TYPED_TEST(Float8Test, CallTheConstOperator) {
775805 EXPECT_THAT ((c = a, c /= b),
776806 EqOrIsNan<Float8>(Float8{float {a} / float {b}}));
777807
778- EXPECT_EQ (a == b, float {a} == float {b}) << float {a} << " vs " << float {b};
779- EXPECT_EQ (a != b, float {a} != float {b});
780- EXPECT_EQ (a < b, float {a} < float {b}) << float {a} << " vs " << float {b};
781- EXPECT_EQ (a <= b, float {a} <= float {b});
782- EXPECT_EQ (a > b, float {a} > float {b}) << float {a} << " vs " << float {b};
783- EXPECT_EQ (a >= b, float {a} >= float {b});
808+ #define COMP (x ) \
809+ EXPECT_EQ (a x b, float {a} x float {b}) \
810+ << a << #x << b << " vs " << float {a} << #x << float {b}
811+
812+ COMP (==);
813+ COMP (!=);
814+ COMP (<);
815+ COMP (<=);
816+ COMP (>);
817+ COMP (>=);
818+ #undef COMP
784819 }
785820 }
786821}
@@ -809,29 +844,36 @@ struct Float8CastTestParamNames {
809844 static std::string GetName (int idx) {
810845 using first_type = typename TypeParam::first_type;
811846 using second_type = typename TypeParam::second_type;
812- return absl::StrCat (::testing::internal::GetTypeName <first_type>(), " _" ,
813- ::testing::internal::GetTypeName <second_type>());
847+ return absl::StrCat (TypeToName <first_type>(), " _" ,
848+ TypeToName <second_type>());
814849 }
815850};
816851
817- #define GEN_LONG_DOUBLE_PAIR (Type ) std::pair<Type, long double >,
818-
819- #define GEN_DEST_TYPES (Type ) \
820- GEN_LONG_DOUBLE_PAIR (Type) \
821- std::pair<Type, double >, std::pair<Type, float >, \
822- std::pair<Type, tensorstore::BFloat16>, \
823- std::pair<Type, ::half_float::half>, std::pair<Type, Float8e3m4>, \
824- std::pair<Type, Float8e4m3fn>, std::pair<Type, Float8e4m3b11fnuz>, \
825- std::pair<Type, Float8e4m3fnuz>, std::pair<Type, Float8e5m2fnuz>, \
826- std::pair<Type, Float8e5m2>, std::pair<Type, bool >, \
827- std::pair<Type, int32_t >, std::pair<Type, int64_t >
828-
829- #define GEN_TYPE_PAIRS () \
830- GEN_DEST_TYPES (Float8e3m4), GEN_DEST_TYPES(Float8e4m3fn), \
831- GEN_DEST_TYPES (Float8e4m3b11fnuz), GEN_DEST_TYPES(Float8e5m2), \
832- GEN_DEST_TYPES (Float8e4m3fnuz), GEN_DEST_TYPES(Float8e5m2fnuz)
833-
834- using Float8CastTypePairs = ::testing::Types<GEN_TYPE_PAIRS()>;
852+ // clang-format off
853+ #define GEN_TYPE_PAIRS (T ) \
854+ std::pair<T, long double >, \
855+ std::pair<T, double >, \
856+ std::pair<T, float >, \
857+ std::pair<T, tensorstore::BFloat16>, \
858+ std::pair<T, ::half_float::half>, \
859+ std::pair<T, Float8e3m4>, \
860+ std::pair<T, Float8e4m3fn>, \
861+ std::pair<T, Float8e4m3b11fnuz>, \
862+ std::pair<T, Float8e4m3fnuz>, \
863+ std::pair<T, Float8e5m2fnuz>, \
864+ std::pair<T, Float8e5m2>, \
865+ std::pair<T, bool >, \
866+ std::pair<T, int32_t >, \
867+ std::pair<T, int64_t >
868+
869+ using Float8CastTypePairs = ::testing::Types<
870+ GEN_TYPE_PAIRS (Float8e3m4),
871+ GEN_TYPE_PAIRS (Float8e4m3fn),
872+ GEN_TYPE_PAIRS (Float8e4m3b11fnuz),
873+ GEN_TYPE_PAIRS (Float8e5m2),
874+ GEN_TYPE_PAIRS (Float8e4m3fnuz),
875+ GEN_TYPE_PAIRS (Float8e5m2fnuz)>;
876+ // clang-format on
835877
836878template <typename CastPair>
837879class Float8CastTest : public ::testing::Test {};
0 commit comments