Commit 28acd2ea authored by Adam Procter's avatar Adam Procter

Throw exceptions if attempt is made to convert from size_t_max to Dimension, or…

Throw exceptions if attempt is made to convert from size_t_max to Dimension, or from an undetermined Dimension to size_t
parent 48c3f9ac
...@@ -31,28 +31,40 @@ namespace ngraph ...@@ -31,28 +31,40 @@ namespace ngraph
{ {
public: public:
/// \brief Constructs a known dimension. /// \brief Constructs a known dimension.
///
/// Requires that size_t != std::numeric_limits<size_t>::max(). If that condition
/// does not hold, throws std::invalid_argument.
Dimension(size_t dimension) Dimension(size_t dimension)
: m_dimension(dimension) : m_dimension(dimension)
{ {
if (dimension == s_undetermined_val)
{
throw std::invalid_argument(
"Cannot convert std::numeric_limits<size_t>::max() to Dimension");
}
} }
/// \brief Constructs an unknown dimension. /// \brief Constructs an unknown dimension.
Dimension() Dimension() { m_dimension = s_undetermined_val; }
: m_dimension(s_undetermined_val) /// \brief Returns true if this dimension is determined.
bool is_determined() const { return m_dimension != s_undetermined_val; }
/// \brief Converts this dimension to size_t. If the dimension is undetermined, throws
/// std::invalid_argument.
explicit operator size_t() const
{ {
if (!is_determined())
{
throw std::invalid_argument("Cannot convert unknown dimension to size_t");
}
return m_dimension;
} }
/// \brief Returns true if this dimension is determined.
bool is_determined() const { return m_dimension != s_undetermined_val; }
/// \brief Converts this dimension to size_t. If the dimension is undetermined, return
/// value is implementation-dependent.
explicit operator size_t() const { return m_dimension; }
/// \brief Tests whether "this" is possibly equal to s. /// \brief Tests whether "this" is possibly equal to s.
bool possibly_eq(const Dimension& d) const { return !(*this != d); } bool possibly_eq(const Dimension& d) const { return !(*this != d); }
/// \brief Tests whether "this" is possibly not equal to s. /// \brief Tests whether "this" is possibly not equal to s.
bool possibly_neq(const Dimension& d) const { return !(*this == d); } bool possibly_neq(const Dimension& d) const { return !(*this == d); }
/// \brief Constructs an unknown dimension. /// \brief Constructs an unknown dimension.
static Dimension undetermined() { return s_undetermined_val; } static Dimension undetermined() { return Dimension(); }
friend bool operator==(const Dimension& d1, const Dimension& d2); friend bool operator==(const Dimension& d1, const Dimension& d2);
friend bool operator!=(const Dimension& d1, const Dimension& d2); friend bool operator!=(const Dimension& d1, const Dimension& d2);
......
...@@ -69,6 +69,27 @@ TEST(partial_shape, dim_construction_undetermined) ...@@ -69,6 +69,27 @@ TEST(partial_shape, dim_construction_undetermined)
ASSERT_FALSE(dim.is_determined()); ASSERT_FALSE(dim.is_determined());
} }
TEST(partial_shape, dim_construction_size_t_max)
{
EXPECT_ANY_THROW({ Dimension d{std::numeric_limits<size_t>::max()}; });
}
TEST(partial_shape, dim_conversion_determined)
{
Dimension d{42};
size_t s{d};
ASSERT_EQ(s, 42);
}
TEST(partial_shape, dim_conversion_undetermined)
{
EXPECT_ANY_THROW({
size_t s{Dimension::undetermined()};
s = 0; // Silence compiler warning about unused s
});
}
TEST(partial_shape, rank_construction_determined) TEST(partial_shape, rank_construction_determined)
{ {
Rank r{4}; Rank r{4};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment