mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Add .clang-format file and format source files accordingly.
To apply .clang-format to the codebase, the following command can be run: git ls-files -- *.cpp *.h | xargs clang-format -i -style=file
This commit is contained in:
parent
38a659c428
commit
8c3142ad61
47
.clang-format
Normal file
47
.clang-format
Normal file
@ -0,0 +1,47 @@
|
||||
AccessModifierOffset: 0
|
||||
AlignEscapedNewlinesLeft: true
|
||||
AlignTrailingComments: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
AlwaysBreakTemplateDeclarations: false
|
||||
BinPackParameters: false
|
||||
BreakBeforeBinaryOperators: false
|
||||
BreakBeforeBraces: Allman
|
||||
BreakBeforeTernaryOperators: false
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
ColumnLimit: 100
|
||||
CommentPragmas: ''
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||
ConstructorInitializerIndentWidth: 0
|
||||
ContinuationIndentWidth: 0
|
||||
Cpp11BracedListStyle: false
|
||||
DerivePointerBinding: false
|
||||
IndentCaseLabels: false
|
||||
IndentFunctionDeclarationAfterType: false
|
||||
IndentWidth: 4
|
||||
Language: Cpp
|
||||
MaxEmptyLinesToKeep: 2
|
||||
NamespaceIndentation: None
|
||||
ObjCSpaceAfterProperty: true
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakBeforeFirstCallParameter: 100
|
||||
PenaltyBreakComment: 100
|
||||
PenaltyBreakFirstLessLess: 0
|
||||
PenaltyBreakString: 100
|
||||
PenaltyExcessCharacter: 1
|
||||
PenaltyReturnTypeOnItsOwnLine: 20
|
||||
PointerBindsToType: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInAngles: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInContainerLiterals: false
|
||||
SpacesInParentheses: false
|
||||
Standard: Cpp11
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
1273
include/array_view.h
1273
include/array_view.h
File diff suppressed because it is too large
Load Diff
@ -33,20 +33,35 @@ namespace gsl
|
||||
|
||||
struct fail_fast : public std::runtime_error
|
||||
{
|
||||
fail_fast() : std::runtime_error("") {}
|
||||
explicit fail_fast(char const* const message) : std::runtime_error(message) {}
|
||||
fail_fast() : std::runtime_error("")
|
||||
{
|
||||
}
|
||||
explicit fail_fast(char const* const message) : std::runtime_error(message)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
inline void fail_fast_assert(bool cond) { if (!cond) throw fail_fast(); }
|
||||
inline void fail_fast_assert(bool cond, const char* const message) { if (!cond) throw fail_fast(message); }
|
||||
inline void fail_fast_assert(bool cond)
|
||||
{
|
||||
if (!cond) throw fail_fast();
|
||||
}
|
||||
inline void fail_fast_assert(bool cond, const char* const message)
|
||||
{
|
||||
if (!cond) throw fail_fast(message);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
inline void fail_fast_assert(bool cond) { if (!cond) std::terminate(); }
|
||||
inline void fail_fast_assert(bool cond, const char* const) { if (!cond) std::terminate(); }
|
||||
inline void fail_fast_assert(bool cond)
|
||||
{
|
||||
if (!cond) std::terminate();
|
||||
}
|
||||
inline void fail_fast_assert(bool cond, const char* const)
|
||||
{
|
||||
if (!cond) std::terminate();
|
||||
}
|
||||
|
||||
#endif // GSL_THROWS_FOR_TESTING
|
||||
|
||||
}
|
||||
|
||||
#endif // GSL_FAIL_FAST_H
|
||||
|
297
include/gsl.h
297
include/gsl.h
@ -32,8 +32,7 @@ namespace gsl
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
|
||||
template <class T>
|
||||
using owner = T;
|
||||
template <class T> using owner = T;
|
||||
|
||||
//
|
||||
// GSL.assert: assertions
|
||||
@ -46,50 +45,78 @@ using owner = T;
|
||||
//
|
||||
|
||||
// Final_act allows you to ensure something gets run at the end of a scope
|
||||
template <class F>
|
||||
class Final_act
|
||||
template <class F> class Final_act
|
||||
{
|
||||
public:
|
||||
explicit Final_act(F f) : f_(std::move(f)), invoke_(true) {}
|
||||
public:
|
||||
explicit Final_act(F f) : f_(std::move(f)), invoke_(true)
|
||||
{
|
||||
}
|
||||
|
||||
Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true) { other.invoke_ = false; }
|
||||
Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true)
|
||||
{
|
||||
other.invoke_ = false;
|
||||
}
|
||||
Final_act(const Final_act&) = delete;
|
||||
Final_act& operator=(const Final_act&) = delete;
|
||||
|
||||
~Final_act() { if (invoke_) f_(); }
|
||||
~Final_act()
|
||||
{
|
||||
if (invoke_) f_();
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
F f_;
|
||||
bool invoke_;
|
||||
};
|
||||
|
||||
// finally() - convenience function to generate a Final_act
|
||||
template <class F>
|
||||
Final_act<F> finally(const F &f) { return Final_act<F>(f); }
|
||||
template <class F> Final_act<F> finally(const F& f)
|
||||
{
|
||||
return Final_act<F>(f);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
Final_act<F> finally(F &&f) { return Final_act<F>(std::forward<F>(f)); }
|
||||
template <class F> Final_act<F> finally(F&& f)
|
||||
{
|
||||
return Final_act<F>(std::forward<F>(f));
|
||||
}
|
||||
|
||||
// narrow_cast(): a searchable way to do narrowing casts of values
|
||||
template<class T, class U>
|
||||
T narrow_cast(U u) { return static_cast<T>(u); }
|
||||
template <class T, class U> T narrow_cast(U u)
|
||||
{
|
||||
return static_cast<T>(u);
|
||||
}
|
||||
|
||||
struct narrowing_error : public std::exception {};
|
||||
struct narrowing_error : public std::exception
|
||||
{
|
||||
};
|
||||
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
||||
template<class T, class U>
|
||||
T narrow(U u) { T t = narrow_cast<T>(u); if (static_cast<U>(t) != u) throw narrowing_error(); return t; }
|
||||
template <class T, class U> T narrow(U u)
|
||||
{
|
||||
T t = narrow_cast<T>(u);
|
||||
if (static_cast<U>(t) != u) throw narrowing_error();
|
||||
return t;
|
||||
}
|
||||
|
||||
//
|
||||
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector
|
||||
//
|
||||
template <class T, size_t N>
|
||||
T& at(T(&arr)[N], size_t index) { fail_fast_assert(index < N); return arr[index]; }
|
||||
template <class T, size_t N> T& at(T(&arr)[N], size_t index)
|
||||
{
|
||||
fail_fast_assert(index < N);
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
template <class T, size_t N>
|
||||
T& at(std::array<T, N>& arr, size_t index) { fail_fast_assert(index < N); return arr[index]; }
|
||||
template <class T, size_t N> T& at(std::array<T, N>& arr, size_t index)
|
||||
{
|
||||
fail_fast_assert(index < N);
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
template <class Cont>
|
||||
typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index < cont.size()); return cont[index]; }
|
||||
template <class Cont> typename Cont::value_type& at(Cont& cont, size_t index)
|
||||
{
|
||||
fail_fast_assert(index < cont.size());
|
||||
return cont[index];
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
@ -106,25 +133,33 @@ typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index
|
||||
// - ensure construction from U* fails with nullptr
|
||||
// - allow implicit conversion to U*
|
||||
//
|
||||
template<class T>
|
||||
class not_null
|
||||
template <class T> class not_null
|
||||
{
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
public:
|
||||
not_null(T t) : ptr_(t) { ensure_invariant(); }
|
||||
not_null& operator=(const T& t) { ptr_ = t; ensure_invariant(); return *this; }
|
||||
|
||||
not_null(const not_null &other) = default;
|
||||
not_null& operator=(const not_null &other) = default;
|
||||
public:
|
||||
not_null(T t) : ptr_(t)
|
||||
{
|
||||
ensure_invariant();
|
||||
}
|
||||
not_null& operator=(const T& t)
|
||||
{
|
||||
ptr_ = t;
|
||||
ensure_invariant();
|
||||
return *this;
|
||||
}
|
||||
|
||||
not_null(const not_null& other) = default;
|
||||
not_null& operator=(const not_null& other) = default;
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
not_null(const not_null<U> &other)
|
||||
not_null(const not_null<U>& other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
not_null& operator=(const not_null<U> &other)
|
||||
not_null& operator=(const not_null<U>& other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
@ -136,24 +171,42 @@ public:
|
||||
not_null<T>& operator=(std::nullptr_t) = delete;
|
||||
not_null<T>& operator=(int) = delete;
|
||||
|
||||
T get() const {
|
||||
T get() const
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
__assume(ptr_ != nullptr);
|
||||
#endif
|
||||
return ptr_;
|
||||
} // the assume() should help the optimizer
|
||||
|
||||
operator T() const { return get(); }
|
||||
T operator->() const { return get(); }
|
||||
operator T() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
T operator->() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
bool operator==(const T& rhs) const { return ptr_ == rhs; }
|
||||
bool operator!=(const T& rhs) const { return !(*this == rhs); }
|
||||
private:
|
||||
bool operator==(const T& rhs) const
|
||||
{
|
||||
return ptr_ == rhs;
|
||||
}
|
||||
bool operator!=(const T& rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
T ptr_;
|
||||
|
||||
// we assume that the compiler can hoist/prove away most of the checks inlined from this function
|
||||
// we assume that the compiler can hoist/prove away most of the checks inlined from this
|
||||
// function
|
||||
// if not, we could make them optional via conditional compilation
|
||||
void ensure_invariant() const { fail_fast_assert(ptr_ != nullptr); }
|
||||
void ensure_invariant() const
|
||||
{
|
||||
fail_fast_assert(ptr_ != nullptr);
|
||||
}
|
||||
|
||||
// unwanted operators...pointers only point to single objects!
|
||||
// TODO ensure all arithmetic ops on this type are unavailable
|
||||
@ -173,21 +226,25 @@ private:
|
||||
//
|
||||
// Describes an optional pointer - provides symmetry with not_null
|
||||
//
|
||||
template<class T>
|
||||
class maybe_null_ret;
|
||||
template <class T> class maybe_null_ret;
|
||||
|
||||
template<class T>
|
||||
class maybe_null_dbg
|
||||
template <class T> class maybe_null_dbg
|
||||
{
|
||||
template<class U>
|
||||
friend class maybe_null_dbg;
|
||||
template <class U> friend class maybe_null_dbg;
|
||||
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
public:
|
||||
maybe_null_dbg() : ptr_(nullptr), tested_(false) {}
|
||||
maybe_null_dbg(std::nullptr_t) : ptr_(nullptr), tested_(false) {}
|
||||
|
||||
maybe_null_dbg(const T& p) : ptr_(p), tested_(false) {}
|
||||
public:
|
||||
maybe_null_dbg() : ptr_(nullptr), tested_(false)
|
||||
{
|
||||
}
|
||||
maybe_null_dbg(std::nullptr_t) : ptr_(nullptr), tested_(false)
|
||||
{
|
||||
}
|
||||
|
||||
maybe_null_dbg(const T& p) : ptr_(p), tested_(false)
|
||||
{
|
||||
}
|
||||
maybe_null_dbg& operator=(const T& p)
|
||||
{
|
||||
if (ptr_ != p)
|
||||
@ -199,7 +256,9 @@ public:
|
||||
}
|
||||
|
||||
|
||||
maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false) {}
|
||||
maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false)
|
||||
{
|
||||
}
|
||||
maybe_null_dbg& operator=(const maybe_null_dbg& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
@ -212,10 +271,13 @@ public:
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const not_null<U> &other) : ptr_(other.get()), tested_(false) {}
|
||||
maybe_null_dbg(const not_null<U>& other)
|
||||
: ptr_(other.get()), tested_(false)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg& operator=(const not_null<U> &other)
|
||||
maybe_null_dbg& operator=(const not_null<U>& other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
tested_ = false;
|
||||
@ -224,10 +286,13 @@ public:
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const maybe_null_dbg<U> &other) : ptr_(other.ptr_), tested_(false) {}
|
||||
maybe_null_dbg(const maybe_null_dbg<U>& other)
|
||||
: ptr_(other.ptr_), tested_(false)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg& operator=(const maybe_null_dbg<U> &other)
|
||||
maybe_null_dbg& operator=(const maybe_null_dbg<U>& other)
|
||||
{
|
||||
ptr_ = other.ptr_;
|
||||
tested_ = false;
|
||||
@ -236,10 +301,13 @@ public:
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const maybe_null_ret<U> &other) : ptr_(other.get()), tested_(false) {}
|
||||
maybe_null_dbg(const maybe_null_ret<U>& other)
|
||||
: ptr_(other.get()), tested_(false)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg& operator=(const maybe_null_ret<U> &other)
|
||||
maybe_null_dbg& operator=(const maybe_null_ret<U>& other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
tested_ = false;
|
||||
@ -247,16 +315,36 @@ public:
|
||||
}
|
||||
|
||||
|
||||
bool present() const { tested_ = true; return ptr_ != nullptr; }
|
||||
bool present() const
|
||||
{
|
||||
tested_ = true;
|
||||
return ptr_ != nullptr;
|
||||
}
|
||||
|
||||
bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; }
|
||||
bool operator!=(const T& rhs) const { return !(*this == rhs); }
|
||||
bool operator==(const T& rhs) const
|
||||
{
|
||||
tested_ = true;
|
||||
return ptr_ == rhs;
|
||||
}
|
||||
bool operator!=(const T& rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
bool operator==(const maybe_null_dbg<U>& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; }
|
||||
bool operator==(const maybe_null_dbg<U>& rhs) const
|
||||
{
|
||||
tested_ = true;
|
||||
rhs.tested_ = true;
|
||||
return ptr_ == rhs.ptr_;
|
||||
}
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
bool operator!=(const maybe_null_dbg<U>& rhs) const { return !(*this == rhs); }
|
||||
bool operator!=(const maybe_null_dbg<U>& rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
T get() const {
|
||||
T get() const
|
||||
{
|
||||
fail_fast_assert(tested_);
|
||||
#ifdef _MSC_VER
|
||||
__assume(ptr_ != nullptr);
|
||||
@ -264,10 +352,16 @@ public:
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
operator T() const { return get(); }
|
||||
T operator->() const { return get(); }
|
||||
operator T() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
T operator->() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
// unwanted operators...pointers only point to single objects!
|
||||
// TODO ensure all arithmetic ops on this type are unavailable
|
||||
maybe_null_dbg<T>& operator++() = delete;
|
||||
@ -283,25 +377,38 @@ private:
|
||||
mutable bool tested_;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class maybe_null_ret
|
||||
template <class T> class maybe_null_ret
|
||||
{
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
public:
|
||||
maybe_null_ret() : ptr_(nullptr) {}
|
||||
maybe_null_ret(std::nullptr_t) : ptr_(nullptr) {}
|
||||
|
||||
maybe_null_ret(const T& p) : ptr_(p) {}
|
||||
maybe_null_ret& operator=(const T& p) { ptr_ = p; return *this; }
|
||||
public:
|
||||
maybe_null_ret() : ptr_(nullptr)
|
||||
{
|
||||
}
|
||||
maybe_null_ret(std::nullptr_t) : ptr_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
maybe_null_ret(const T& p) : ptr_(p)
|
||||
{
|
||||
}
|
||||
maybe_null_ret& operator=(const T& p)
|
||||
{
|
||||
ptr_ = p;
|
||||
return *this;
|
||||
}
|
||||
|
||||
maybe_null_ret(const maybe_null_ret& rhs) = default;
|
||||
maybe_null_ret& operator=(const maybe_null_ret& rhs) = default;
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const not_null<U> &other) : ptr_(other.get()) {}
|
||||
maybe_null_ret(const not_null<U>& other)
|
||||
: ptr_(other.get())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret& operator=(const not_null<U> &other)
|
||||
maybe_null_ret& operator=(const not_null<U>& other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
@ -309,10 +416,13 @@ public:
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const maybe_null_ret<U> &other) : ptr_(other.get()) {}
|
||||
maybe_null_ret(const maybe_null_ret<U>& other)
|
||||
: ptr_(other.get())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret& operator=(const maybe_null_ret<U> &other)
|
||||
maybe_null_ret& operator=(const maybe_null_ret<U>& other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
@ -320,24 +430,39 @@ public:
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const maybe_null_dbg<U> &other) : ptr_(other.get()) {}
|
||||
maybe_null_ret(const maybe_null_dbg<U>& other)
|
||||
: ptr_(other.get())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret& operator=(const maybe_null_dbg<U> &other)
|
||||
maybe_null_ret& operator=(const maybe_null_dbg<U>& other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool present() const { return ptr_ != nullptr; }
|
||||
bool present() const
|
||||
{
|
||||
return ptr_ != nullptr;
|
||||
}
|
||||
|
||||
T get() const { return ptr_; }
|
||||
T get() const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
operator T() const { return get(); }
|
||||
T operator->() const { return get(); }
|
||||
operator T() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
T operator->() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
// unwanted operators...pointers only point to single objects!
|
||||
// TODO ensure all arithmetic ops on this type are unavailable
|
||||
maybe_null_ret<T>& operator++() = delete;
|
||||
@ -352,7 +477,7 @@ private:
|
||||
T ptr_;
|
||||
};
|
||||
|
||||
template<class T> using maybe_null = maybe_null_ret<T>;
|
||||
template <class T> using maybe_null = maybe_null_ret<T>;
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
|
@ -34,17 +34,13 @@ namespace gsl
|
||||
// type system for these types that will not either incur significant runtime costs or
|
||||
// (sometimes needlessly) break existing programs when introduced.
|
||||
//
|
||||
template<size_t Max = dynamic_range>
|
||||
using czstring = const char*;
|
||||
template <size_t Max = dynamic_range> using czstring = const char*;
|
||||
|
||||
template<size_t Max = dynamic_range>
|
||||
using cwzstring = const wchar_t*;
|
||||
template <size_t Max = dynamic_range> using cwzstring = const wchar_t*;
|
||||
|
||||
template<size_t Max = dynamic_range>
|
||||
using zstring = char*;
|
||||
template <size_t Max = dynamic_range> using zstring = char*;
|
||||
|
||||
template<size_t Max = dynamic_range>
|
||||
using wzstring = wchar_t*;
|
||||
template <size_t Max = dynamic_range> using wzstring = wchar_t*;
|
||||
|
||||
//
|
||||
// string_view and relatives
|
||||
@ -57,16 +53,13 @@ using wzstring = wchar_t*;
|
||||
template <class CharT, size_t Extent = dynamic_range>
|
||||
using basic_string_view = array_view<CharT, Extent>;
|
||||
|
||||
template<size_t Extent = dynamic_range>
|
||||
using string_view = basic_string_view<char, Extent>;
|
||||
template <size_t Extent = dynamic_range> using string_view = basic_string_view<char, Extent>;
|
||||
|
||||
template<size_t Extent = dynamic_range>
|
||||
using cstring_view = basic_string_view<const char, Extent>;
|
||||
template <size_t Extent = dynamic_range> using cstring_view = basic_string_view<const char, Extent>;
|
||||
|
||||
template<size_t Extent = dynamic_range>
|
||||
using wstring_view = basic_string_view<wchar_t, Extent>;
|
||||
template <size_t Extent = dynamic_range> using wstring_view = basic_string_view<wchar_t, Extent>;
|
||||
|
||||
template<size_t Extent = dynamic_range>
|
||||
template <size_t Extent = dynamic_range>
|
||||
using cwstring_view = basic_string_view<const wchar_t, Extent>;
|
||||
|
||||
|
||||
@ -78,13 +71,15 @@ using cwstring_view = basic_string_view<const wchar_t, Extent>;
|
||||
//
|
||||
// Will fail-fast if sentinel cannot be found before max elements are examined.
|
||||
//
|
||||
template<class T, class SizeType, const T Sentinel>
|
||||
array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::numeric_limits<SizeType>::max())
|
||||
template <class T, class SizeType, const T Sentinel>
|
||||
array_view<T, dynamic_range>
|
||||
ensure_sentinel(const T* seq, SizeType max = std::numeric_limits<SizeType>::max())
|
||||
{
|
||||
auto cur = seq;
|
||||
while ((cur - seq) < max && *cur != Sentinel) ++cur;
|
||||
while ((cur - seq) < max && *cur != Sentinel)
|
||||
++cur;
|
||||
fail_fast_assert(*cur == Sentinel);
|
||||
return{ seq, cur - seq };
|
||||
return { seq, cur - seq };
|
||||
}
|
||||
|
||||
|
||||
@ -93,42 +88,51 @@ array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::n
|
||||
// Will fail fast if a null-terminator cannot be found before
|
||||
// the limit of size_type.
|
||||
//
|
||||
template<class T>
|
||||
inline basic_string_view<T, dynamic_range> ensure_z(T* const & sz, size_t max = std::numeric_limits<size_t>::max())
|
||||
template <class T>
|
||||
inline basic_string_view<T, dynamic_range>
|
||||
ensure_z(T* const& sz, size_t max = std::numeric_limits<size_t>::max())
|
||||
{
|
||||
return ensure_sentinel<0>(sz, max);
|
||||
}
|
||||
|
||||
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation
|
||||
inline basic_string_view<char, dynamic_range> ensure_z(char* const & sz, size_t max)
|
||||
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const
|
||||
// overloads to share an implementation
|
||||
inline basic_string_view<char, dynamic_range> ensure_z(char* const& sz, size_t max)
|
||||
{
|
||||
auto len = strnlen(sz, max);
|
||||
fail_fast_assert(sz[len] == 0); return{ sz, len };
|
||||
fail_fast_assert(sz[len] == 0);
|
||||
return { sz, len };
|
||||
}
|
||||
|
||||
inline basic_string_view<const char, dynamic_range> ensure_z(const char* const& sz, size_t max)
|
||||
{
|
||||
auto len = strnlen(sz, max);
|
||||
fail_fast_assert(sz[len] == 0); return{ sz, len };
|
||||
fail_fast_assert(sz[len] == 0);
|
||||
return { sz, len };
|
||||
}
|
||||
|
||||
inline basic_string_view<wchar_t, dynamic_range> ensure_z(wchar_t* const & sz, size_t max)
|
||||
inline basic_string_view<wchar_t, dynamic_range> ensure_z(wchar_t* const& sz, size_t max)
|
||||
{
|
||||
auto len = wcsnlen(sz, max);
|
||||
fail_fast_assert(sz[len] == 0); return{ sz, len };
|
||||
fail_fast_assert(sz[len] == 0);
|
||||
return { sz, len };
|
||||
}
|
||||
|
||||
inline basic_string_view<const wchar_t, dynamic_range> ensure_z(const wchar_t* const & sz, size_t max)
|
||||
inline basic_string_view<const wchar_t, dynamic_range> ensure_z(const wchar_t* const& sz, size_t max)
|
||||
{
|
||||
auto len = wcsnlen(sz, max);
|
||||
fail_fast_assert(sz[len] == 0); return{ sz, len };
|
||||
fail_fast_assert(sz[len] == 0);
|
||||
return { sz, len };
|
||||
}
|
||||
|
||||
template<class T, size_t N>
|
||||
basic_string_view<T, dynamic_range> ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], N); }
|
||||
template <class T, size_t N> basic_string_view<T, dynamic_range> ensure_z(T(&sz)[N])
|
||||
{
|
||||
return ensure_z(&sz[0], N);
|
||||
}
|
||||
|
||||
template<class Cont>
|
||||
basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range> ensure_z(Cont& cont)
|
||||
template <class Cont>
|
||||
basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range>
|
||||
ensure_z(Cont& cont)
|
||||
{
|
||||
return ensure_z(cont.data(), cont.length());
|
||||
}
|
||||
@ -136,48 +140,69 @@ basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dy
|
||||
//
|
||||
// to_string() allow (explicit) conversions from string_view to string
|
||||
//
|
||||
template<class CharT, size_t Extent>
|
||||
template <class CharT, size_t Extent>
|
||||
std::basic_string<typename std::remove_const<CharT>::type> to_string(basic_string_view<CharT, Extent> view)
|
||||
{
|
||||
return{ view.data(), view.length() };
|
||||
return { view.data(), view.length() };
|
||||
}
|
||||
|
||||
|
||||
template<class CharT, size_t Extent = dynamic_range>
|
||||
class basic_zstring_builder
|
||||
template <class CharT, size_t Extent = dynamic_range> class basic_zstring_builder
|
||||
{
|
||||
public:
|
||||
public:
|
||||
using string_view_type = basic_string_view<CharT, Extent>;
|
||||
using value_type = CharT;
|
||||
using pointer = CharT*;
|
||||
using size_type = typename string_view_type::size_type;
|
||||
using iterator = typename string_view_type::iterator;
|
||||
|
||||
basic_zstring_builder(CharT* data, size_type length) : sv_(data, length) {}
|
||||
basic_zstring_builder(CharT* data, size_type length) : sv_(data, length)
|
||||
{
|
||||
}
|
||||
|
||||
template<size_t Size>
|
||||
basic_zstring_builder(CharT(&arr)[Size]) : sv_(arr) {}
|
||||
template <size_t Size> basic_zstring_builder(CharT(&arr)[Size]) : sv_(arr)
|
||||
{
|
||||
}
|
||||
|
||||
pointer data() const { return sv_.data(); }
|
||||
string_view_type view() const { return sv_; }
|
||||
pointer data() const
|
||||
{
|
||||
return sv_.data();
|
||||
}
|
||||
string_view_type view() const
|
||||
{
|
||||
return sv_;
|
||||
}
|
||||
|
||||
size_type length() const { return sv_.length(); }
|
||||
size_type length() const
|
||||
{
|
||||
return sv_.length();
|
||||
}
|
||||
|
||||
pointer assume0() const { return data(); }
|
||||
string_view_type ensure_z() const { return gsl::ensure_z(sv_); }
|
||||
pointer assume0() const
|
||||
{
|
||||
return data();
|
||||
}
|
||||
string_view_type ensure_z() const
|
||||
{
|
||||
return gsl::ensure_z(sv_);
|
||||
}
|
||||
|
||||
iterator begin() const { return sv_.begin(); }
|
||||
iterator end() const { return sv_.end(); }
|
||||
iterator begin() const
|
||||
{
|
||||
return sv_.begin();
|
||||
}
|
||||
iterator end() const
|
||||
{
|
||||
return sv_.end();
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
string_view_type sv_;
|
||||
};
|
||||
|
||||
template <size_t Max = dynamic_range>
|
||||
using zstring_builder = basic_zstring_builder<char, Max>;
|
||||
template <size_t Max = dynamic_range> using zstring_builder = basic_zstring_builder<char, Max>;
|
||||
|
||||
template <size_t Max = dynamic_range>
|
||||
using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
|
||||
template <size_t Max = dynamic_range> using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
|
||||
}
|
||||
|
||||
#endif // GSL_STRING_VIEW_H
|
||||
|
@ -31,9 +31,15 @@ using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
void use(int&) {}
|
||||
struct BaseClass {};
|
||||
struct DerivedClass : BaseClass {};
|
||||
void use(int&)
|
||||
{
|
||||
}
|
||||
struct BaseClass
|
||||
{
|
||||
};
|
||||
struct DerivedClass : BaseClass
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
SUITE(array_view_tests)
|
||||
@ -60,13 +66,16 @@ SUITE(array_view_tests)
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST (array_view_convertible)
|
||||
TEST(array_view_convertible)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
array_view<int, 7, 4, 2> av1(nullptr, b1);
|
||||
#endif
|
||||
|
||||
auto f = [&]() { array_view<int, 7, 4, 2> av1(nullptr); };
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 7, 4, 2> av1(nullptr);
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
|
||||
array_view<int, 7, dynamic_range, 2> av1(nullptr);
|
||||
@ -100,27 +109,30 @@ SUITE(array_view_tests)
|
||||
|
||||
// out of bounds
|
||||
CHECK_THROW(av[1][3] = 3, fail_fast);
|
||||
CHECK_THROW((av[{1, 3}] = 3), fail_fast);
|
||||
CHECK_THROW((av[{ 1, 3 }] = 3), fail_fast);
|
||||
|
||||
CHECK_THROW(av[10][2], fail_fast);
|
||||
CHECK_THROW((av[{10,2}]), fail_fast);
|
||||
CHECK_THROW((av[{ 10, 2 }]), fail_fast);
|
||||
}
|
||||
|
||||
void overloaded_func(array_view<const int, dynamic_range, 3, 5> exp, int expected_value) {
|
||||
void overloaded_func(array_view<const int, dynamic_range, 3, 5> exp, int expected_value)
|
||||
{
|
||||
for (auto val : exp)
|
||||
{
|
||||
CHECK(val == expected_value);
|
||||
}
|
||||
}
|
||||
|
||||
void overloaded_func(array_view<const char, dynamic_range, 3, 5> exp, char expected_value) {
|
||||
void overloaded_func(array_view<const char, dynamic_range, 3, 5> exp, char expected_value)
|
||||
{
|
||||
for (auto val : exp)
|
||||
{
|
||||
CHECK(val == expected_value);
|
||||
}
|
||||
}
|
||||
|
||||
void fixed_func(array_view<int, 3, 3, 5> exp, int expected_value) {
|
||||
void fixed_func(array_view<int, 3, 3, 5> exp, int expected_value)
|
||||
{
|
||||
for (auto val : exp)
|
||||
{
|
||||
CHECK(val == expected_value);
|
||||
@ -138,13 +150,16 @@ SUITE(array_view_tests)
|
||||
fill(av.begin(), av.end(), 34);
|
||||
|
||||
int count = 0;
|
||||
for_each(av.rbegin(), av.rend(), [&](int val) { count += val; });
|
||||
for_each(av.rbegin(), av.rend(), [&](int val)
|
||||
{
|
||||
count += val;
|
||||
});
|
||||
CHECK(count == 34 * 60);
|
||||
overloaded_func(av, 34);
|
||||
|
||||
overloaded_func(av.as_array_view(dim<>(4), dim<>(3), dim<>(5)), 34);
|
||||
|
||||
//fixed_func(av, 34);
|
||||
// fixed_func(av, 34);
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
@ -157,7 +172,8 @@ SUITE(array_view_tests)
|
||||
auto image_ptr = new int[imgSize][3];
|
||||
|
||||
// size check will be done
|
||||
auto image_view = as_array_view(image_ptr, imgSize).as_array_view(dim<>(height), dim<>(width), dim<3>());
|
||||
auto image_view =
|
||||
as_array_view(image_ptr, imgSize).as_array_view(dim<>(height), dim<>(width), dim<3>());
|
||||
|
||||
iota(image_view.begin(), image_view.end(), 1);
|
||||
|
||||
@ -170,11 +186,11 @@ SUITE(array_view_tests)
|
||||
CHECK(expected + 2 == image_view[i][j][1]);
|
||||
CHECK(expected + 3 == image_view[i][j][2]);
|
||||
|
||||
auto val = image_view[{i, j, 0}];
|
||||
auto val = image_view[{ i, j, 0 }];
|
||||
CHECK(expected + 1 == val);
|
||||
val = image_view[{i, j, 1}];
|
||||
val = image_view[{ i, j, 1 }];
|
||||
CHECK(expected + 2 == val);
|
||||
val = image_view[{i, j, 2}];
|
||||
val = image_view[{ i, j, 2 }];
|
||||
CHECK(expected + 3 == val);
|
||||
|
||||
expected += 3;
|
||||
@ -185,7 +201,7 @@ SUITE(array_view_tests)
|
||||
TEST(array_view_factory_test)
|
||||
{
|
||||
{
|
||||
int * arr = new int[150];
|
||||
int* arr = new int[150];
|
||||
|
||||
auto av = as_array_view(arr, dim<10>(), dim<>(3), dim<5>());
|
||||
|
||||
@ -209,13 +225,13 @@ SUITE(array_view_tests)
|
||||
{
|
||||
int a[3][4][5];
|
||||
auto av = as_array_view(a);
|
||||
const int (*b)[4][5];
|
||||
const int(*b)[4][5];
|
||||
b = a;
|
||||
auto bv = as_array_view(b, 3);
|
||||
|
||||
CHECK(av == bv);
|
||||
|
||||
const std::array<double, 3> arr = {0.0, 0.0, 0.0};
|
||||
const std::array<double, 3> arr = { 0.0, 0.0, 0.0 };
|
||||
auto cv = as_array_view(arr);
|
||||
|
||||
vector<float> vec(3);
|
||||
@ -227,7 +243,7 @@ SUITE(array_view_tests)
|
||||
}
|
||||
}
|
||||
|
||||
TEST (array_view_reshape_test)
|
||||
TEST(array_view_reshape_test)
|
||||
{
|
||||
int a[3][4][5];
|
||||
auto av = as_array_view(a);
|
||||
@ -250,19 +266,22 @@ SUITE(array_view_tests)
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
struct Foo {char c[11];};
|
||||
struct Foo
|
||||
{
|
||||
char c[11];
|
||||
};
|
||||
auto av9 = av7.as_array_view<Foo>();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
TEST (array_view_section_test)
|
||||
TEST(array_view_section_test)
|
||||
{
|
||||
int a[30][4][5];
|
||||
|
||||
auto av = as_array_view(a);
|
||||
auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2});
|
||||
auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1});
|
||||
auto sub = av.section({ 15, 0, 0 }, gsl::index<3>{ 2, 2, 2 });
|
||||
auto subsub = sub.section({ 1, 0, 0 }, gsl::index<3>{ 1, 1, 1 });
|
||||
}
|
||||
|
||||
TEST(array_view_section)
|
||||
@ -272,15 +291,15 @@ SUITE(array_view_tests)
|
||||
const array_view<int, 5, 10> av = as_array_view(data).as_array_view(dim<5>(), dim<10>());
|
||||
|
||||
strided_array_view<int, 2> av_section_1 = av.section({ 1, 2 }, { 3, 4 });
|
||||
CHECK((av_section_1[{0, 0}] == 12));
|
||||
CHECK((av_section_1[{0, 1}] == 13));
|
||||
CHECK((av_section_1[{1, 0}] == 22));
|
||||
CHECK((av_section_1[{2, 3}] == 35));
|
||||
CHECK((av_section_1[{ 0, 0 }] == 12));
|
||||
CHECK((av_section_1[{ 0, 1 }] == 13));
|
||||
CHECK((av_section_1[{ 1, 0 }] == 22));
|
||||
CHECK((av_section_1[{ 2, 3 }] == 35));
|
||||
|
||||
strided_array_view<int, 2> av_section_2 = av_section_1.section({ 1, 2 }, { 2,2 });
|
||||
CHECK((av_section_2[{0, 0}] == 24));
|
||||
CHECK((av_section_2[{0, 1}] == 25));
|
||||
CHECK((av_section_2[{1, 0}] == 34));
|
||||
strided_array_view<int, 2> av_section_2 = av_section_1.section({ 1, 2 }, { 2, 2 });
|
||||
CHECK((av_section_2[{ 0, 0 }] == 24));
|
||||
CHECK((av_section_2[{ 0, 1 }] == 25));
|
||||
CHECK((av_section_2[{ 1, 0 }] == 34));
|
||||
}
|
||||
|
||||
TEST(strided_array_view_constructors)
|
||||
@ -290,21 +309,21 @@ SUITE(array_view_tests)
|
||||
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const int carr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
strided_array_view<int, 1> sav1{ arr, {{9}, {1}} }; // T -> T
|
||||
strided_array_view<int, 1> sav1{ arr, { { 9 }, { 1 } } }; // T -> T
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{ 9 });
|
||||
CHECK(sav1.bounds().stride() == 1);
|
||||
CHECK(sav1[0] == 1 && sav1[8] == 9);
|
||||
|
||||
|
||||
strided_array_view<const int, 1> sav2{ carr, {{ 4 }, { 2 }} }; // const T -> const T
|
||||
strided_array_view<const int, 1> sav2{ carr, { { 4 }, { 2 } } }; // const T -> const T
|
||||
CHECK(sav2.bounds().index_bounds() == index<1>{ 4 });
|
||||
CHECK(sav2.bounds().strides() == index<1>{2});
|
||||
CHECK(sav2.bounds().strides() == index<1>{ 2 });
|
||||
CHECK(sav2[0] == 1 && sav2[3] == 7);
|
||||
|
||||
strided_array_view<int, 2> sav3{ arr, {{ 2, 2 },{ 6, 2 }} }; // T -> const T
|
||||
strided_array_view<int, 2> sav3{ arr, { { 2, 2 }, { 6, 2 } } }; // T -> const T
|
||||
CHECK((sav3.bounds().index_bounds() == index<2>{ 2, 2 }));
|
||||
CHECK((sav3.bounds().strides() == index<2>{ 6, 2 }));
|
||||
CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7));
|
||||
CHECK((sav3[{ 0, 0 }] == 1 && sav3[{ 0, 1 }] == 3 && sav3[{ 1, 0 }] == 7));
|
||||
}
|
||||
|
||||
// Check array_view constructor
|
||||
@ -315,33 +334,36 @@ SUITE(array_view_tests)
|
||||
{
|
||||
const array_view<int> src{ arr };
|
||||
|
||||
strided_array_view<int, 1> sav{ src, {2, 1} };
|
||||
strided_array_view<int, 1> sav{ src, { 2, 1 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const int, 1> sav_c{ {src}, {2, 1} };
|
||||
strided_array_view<const int, 1> sav_c{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const int, 1> sav_c{ array_view<const int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const int, 1> sav_c{ array_view<const int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_c[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<volatile int, 1> sav_v{ {src}, {2, 1} };
|
||||
strided_array_view<volatile int, 1> sav_v{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<volatile int, 1> sav_v{ array_view<volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<volatile int, 1> sav_v{ array_view<volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_v[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const volatile int, 1> sav_cv{ {src}, {2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
@ -352,15 +374,16 @@ SUITE(array_view_tests)
|
||||
{
|
||||
const array_view<const int> src{ arr };
|
||||
|
||||
strided_array_view<const int, 1> sav_c{ src, {2, 1} };
|
||||
strided_array_view<const int, 1> sav_c{ src, { 2, 1 } };
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_c[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const volatile int, 1> sav_cv{ {src}, {2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
@ -372,15 +395,16 @@ SUITE(array_view_tests)
|
||||
{
|
||||
const array_view<volatile int> src{ arr };
|
||||
|
||||
strided_array_view<volatile int, 1> sav_v{ src, {2, 1} };
|
||||
strided_array_view<volatile int, 1> sav_v{ src, { 2, 1 } };
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_v[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const volatile int, 1> sav_cv{ {src}, {2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
@ -391,7 +415,7 @@ SUITE(array_view_tests)
|
||||
{
|
||||
const array_view<const volatile int> src{ arr };
|
||||
|
||||
strided_array_view<const volatile int, 1> sav_cv{ src, {2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ src, { 2, 1 } };
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_cv[1] == 2);
|
||||
@ -406,21 +430,23 @@ SUITE(array_view_tests)
|
||||
array_view<const int, 2> av2{ av };
|
||||
CHECK(av2[1] == 5);
|
||||
|
||||
static_assert(std::is_convertible<const array_view<int, 2>, array_view<const int, 2>>::value, "ctor is not implicit!");
|
||||
static_assert(std::is_convertible<const array_view<int, 2>, array_view<const int, 2>>::value,
|
||||
"ctor is not implicit!");
|
||||
|
||||
const strided_array_view<int, 1> src{ arr, {2, 1} };
|
||||
const strided_array_view<int, 1> src{ arr, { 2, 1 } };
|
||||
strided_array_view<const int, 1> sav{ src };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav.bounds().stride() == 1);
|
||||
CHECK(sav[1] == 5);
|
||||
|
||||
static_assert(std::is_convertible<const strided_array_view<int, 1>, strided_array_view<const int, 1>>::value, "ctor is not implicit!");
|
||||
static_assert(std::is_convertible<const strided_array_view<int, 1>, strided_array_view<const int, 1>>::value,
|
||||
"ctor is not implicit!");
|
||||
}
|
||||
|
||||
// Check copy constructor
|
||||
{
|
||||
int arr1[2] = { 3, 4 };
|
||||
const strided_array_view<int, 1> src1{ arr1, {2, 1} };
|
||||
const strided_array_view<int, 1> src1{ arr1, { 2, 1 } };
|
||||
strided_array_view<int, 1> sav1{ src1 };
|
||||
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{ 2 });
|
||||
@ -428,11 +454,11 @@ SUITE(array_view_tests)
|
||||
CHECK(sav1[0] == 3);
|
||||
|
||||
int arr2[6] = { 1, 2, 3, 4, 5, 6 };
|
||||
const strided_array_view<const int, 2> src2{ arr2, {{ 3, 2 }, { 2, 1 }} };
|
||||
const strided_array_view<const int, 2> src2{ arr2, { { 3, 2 }, { 2, 1 } } };
|
||||
strided_array_view<const int, 2> sav2{ src2 };
|
||||
CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 }));
|
||||
CHECK((sav2.bounds().strides() == index<2>{ 2, 1 }));
|
||||
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
|
||||
CHECK((sav2[{ 0, 0 }] == 1 && sav2[{ 2, 0 }] == 5));
|
||||
}
|
||||
|
||||
// Check const-casting assignment operator
|
||||
@ -440,8 +466,8 @@ SUITE(array_view_tests)
|
||||
int arr1[2] = { 1, 2 };
|
||||
int arr2[6] = { 3, 4, 5, 6, 7, 8 };
|
||||
|
||||
const strided_array_view<int, 1> src{ arr1, {{2}, {1}} };
|
||||
strided_array_view<const int, 1> sav{ arr2, {{3}, {2}} };
|
||||
const strided_array_view<int, 1> src{ arr1, { { 2 }, { 1 } } };
|
||||
strided_array_view<const int, 1> sav{ arr2, { { 3 }, { 2 } } };
|
||||
strided_array_view<const int, 1>& sav_ref = (sav = src);
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav.bounds().strides() == index<1>{ 1 });
|
||||
@ -453,8 +479,8 @@ SUITE(array_view_tests)
|
||||
{
|
||||
int arr1[2] = { 3, 4 };
|
||||
int arr1b[1] = { 0 };
|
||||
const strided_array_view<int, 1> src1{ arr1, {2, 1} };
|
||||
strided_array_view<int, 1> sav1{ arr1b, {1, 1} };
|
||||
const strided_array_view<int, 1> src1{ arr1, { 2, 1 } };
|
||||
strided_array_view<int, 1> sav1{ arr1b, { 1, 1 } };
|
||||
strided_array_view<int, 1>& sav1_ref = (sav1 = src1);
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav1.bounds().strides() == index<1>{ 1 });
|
||||
@ -463,12 +489,12 @@ SUITE(array_view_tests)
|
||||
|
||||
const int arr2[6] = { 1, 2, 3, 4, 5, 6 };
|
||||
const int arr2b[1] = { 0 };
|
||||
const strided_array_view<const int, 2> src2{ arr2, {{ 3, 2 },{ 2, 1 }} };
|
||||
strided_array_view<const int, 2> sav2{ arr2b, {{ 1, 1 },{ 1, 1 }} };
|
||||
const strided_array_view<const int, 2> src2{ arr2, { { 3, 2 }, { 2, 1 } } };
|
||||
strided_array_view<const int, 2> sav2{ arr2b, { { 1, 1 }, { 1, 1 } } };
|
||||
strided_array_view<const int, 2>& sav2_ref = (sav2 = src2);
|
||||
CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 }));
|
||||
CHECK((sav2.bounds().strides() == index<2>{ 2, 1 }));
|
||||
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
|
||||
CHECK((sav2[{ 0, 0 }] == 1 && sav2[{ 2, 0 }] == 5));
|
||||
CHECK(&sav2_ref == &sav2);
|
||||
}
|
||||
}
|
||||
@ -479,11 +505,12 @@ SUITE(array_view_tests)
|
||||
std::iota(begin(data), end(data), 0);
|
||||
const array_view<int, 5, 10> src = as_array_view(data).as_array_view(dim<5>(), dim<10>());
|
||||
|
||||
const strided_array_view<int, 2> sav{ src, {{5, 10}, {10, 1}} };
|
||||
const strided_array_view<int, 2> sav{ src, { { 5, 10 }, { 10, 1 } } };
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const strided_array_view<const int, 2> csav{ {src},{ { 5, 10 },{ 10, 1 } } };
|
||||
const strided_array_view<const int, 2> csav{ { src }, { { 5, 10 }, { 10, 1 } } };
|
||||
#endif
|
||||
const strided_array_view<const int, 2> csav{ array_view<const int, 5, 10>{ src }, { { 5, 10 },{ 10, 1 } } };
|
||||
const strided_array_view<const int, 2> csav{ array_view<const int, 5, 10>{ src },
|
||||
{ { 5, 10 }, { 10, 1 } } };
|
||||
|
||||
strided_array_view<int, 1> sav_sl = sav[2];
|
||||
CHECK(sav_sl[0] == 20);
|
||||
@ -503,18 +530,14 @@ SUITE(array_view_tests)
|
||||
// use cases, such as column-major multidimensional array
|
||||
// (aka. "FORTRAN" layout).
|
||||
|
||||
int cm_array[3 * 5] = {
|
||||
1, 4, 7, 10, 13,
|
||||
2, 5, 8, 11, 14,
|
||||
3, 6, 9, 12, 15
|
||||
};
|
||||
strided_array_view<int, 2> cm_sav{ cm_array, {{ 5, 3 },{ 1, 5 }} };
|
||||
int cm_array[3 * 5] = { 1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15 };
|
||||
strided_array_view<int, 2> cm_sav{ cm_array, { { 5, 3 }, { 1, 5 } } };
|
||||
|
||||
// Accessing elements
|
||||
CHECK((cm_sav[{0, 0}] == 1));
|
||||
CHECK((cm_sav[{0, 1}] == 2));
|
||||
CHECK((cm_sav[{1, 0}] == 4));
|
||||
CHECK((cm_sav[{4, 2}] == 15));
|
||||
CHECK((cm_sav[{ 0, 0 }] == 1));
|
||||
CHECK((cm_sav[{ 0, 1 }] == 2));
|
||||
CHECK((cm_sav[{ 1, 0 }] == 4));
|
||||
CHECK((cm_sav[{ 4, 2 }] == 15));
|
||||
|
||||
// Slice
|
||||
strided_array_view<int, 1> cm_sl = cm_sav[3];
|
||||
@ -524,13 +547,13 @@ SUITE(array_view_tests)
|
||||
CHECK(cm_sl[2] == 12);
|
||||
|
||||
// Section
|
||||
strided_array_view<int, 2> cm_sec = cm_sav.section( { 2, 1 }, { 3, 2 });
|
||||
strided_array_view<int, 2> cm_sec = cm_sav.section({ 2, 1 }, { 3, 2 });
|
||||
|
||||
CHECK((cm_sec.bounds().index_bounds() == index<2>{3, 2}));
|
||||
CHECK((cm_sec[{0, 0}] == 8));
|
||||
CHECK((cm_sec[{0, 1}] == 9));
|
||||
CHECK((cm_sec[{1, 0}] == 11));
|
||||
CHECK((cm_sec[{2, 1}] == 15));
|
||||
CHECK((cm_sec.bounds().index_bounds() == index<2>{ 3, 2 }));
|
||||
CHECK((cm_sec[{ 0, 0 }] == 8));
|
||||
CHECK((cm_sec[{ 0, 1 }] == 9));
|
||||
CHECK((cm_sec[{ 1, 0 }] == 11));
|
||||
CHECK((cm_sec[{ 2, 1 }] == 15));
|
||||
}
|
||||
|
||||
TEST(strided_array_view_bounds)
|
||||
@ -554,7 +577,7 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
// zero stride
|
||||
strided_array_view<int, 1> sav{ av, {{4}, {}} };
|
||||
strided_array_view<int, 1> sav{ av, { { 4 }, {} } };
|
||||
CHECK(sav[0] == 0);
|
||||
CHECK(sav[3] == 0);
|
||||
CHECK_THROW(sav[4], fail_fast);
|
||||
@ -562,19 +585,19 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
// zero extent
|
||||
strided_array_view<int, 1> sav{ av,{ {},{1} } };
|
||||
strided_array_view<int, 1> sav{ av, { {}, { 1 } } };
|
||||
CHECK_THROW(sav[0], fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// zero extent and stride
|
||||
strided_array_view<int, 1> sav{ av,{ {},{} } };
|
||||
strided_array_view<int, 1> sav{ av, { {}, {} } };
|
||||
CHECK_THROW(sav[0], fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with matching strided bounds
|
||||
strided_array_view<int, 1> sav{ arr,{ 4, 1 } };
|
||||
strided_array_view<int, 1> sav{ arr, { 4, 1 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 4 });
|
||||
CHECK(sav[3] == 3);
|
||||
CHECK_THROW(sav[4], fail_fast);
|
||||
@ -582,7 +605,7 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
// strided array ctor with smaller strided bounds
|
||||
strided_array_view<int, 1> sav{ arr,{ 2, 1 } };
|
||||
strided_array_view<int, 1> sav{ arr, { 2, 1 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav[1] == 1);
|
||||
CHECK_THROW(sav[2], fail_fast);
|
||||
@ -590,7 +613,7 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
// strided array ctor with fitting irregular bounds
|
||||
strided_array_view<int, 1> sav{ arr,{ 2, 3 } };
|
||||
strided_array_view<int, 1> sav{ arr, { 2, 3 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav[0] == 0);
|
||||
CHECK(sav[1] == 3);
|
||||
@ -599,63 +622,68 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from static arrays
|
||||
CHECK_THROW((strided_array_view<int, 1> { arr, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { arr, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { arr, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { arr, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { arr, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { 5, 5 } }), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from array view
|
||||
CHECK_THROW((strided_array_view<int, 1> { av, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { 5, 5 } }), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from dynamic arrays
|
||||
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1> { av.data(), 2, { 2, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av.data(), 4, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av.data(), 4, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av.data(), 4, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av.data(), 4, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av.data(), 4, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av.data(), 2, { 2, 2 } }), fail_fast);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
strided_array_view<int, 1> sav0{ av.data(), { 3, 2 } };
|
||||
strided_array_view<int, 1> sav1{ arr, { 1 } };
|
||||
strided_array_view<int, 1> sav2{ arr, { 1,1,1 } };
|
||||
strided_array_view<int, 1> sav2{ arr, { 1, 1, 1 } };
|
||||
strided_array_view<int, 1> sav3{ av, { 1 } };
|
||||
strided_array_view<int, 1> sav4{ av, { 1,1,1 } };
|
||||
strided_array_view<int, 1> sav4{ av, { 1, 1, 1 } };
|
||||
strided_array_view<int, 2> sav5{ av.as_array_view(dim<2>(), dim<2>()), { 1 } };
|
||||
strided_array_view<int, 2> sav6{ av.as_array_view(dim<2>(), dim<2>()), { 1,1,1 } };
|
||||
strided_array_view<int, 2> sav7{ av.as_array_view(dim<2>(), dim<2>()), { { 1,1 },{ 1,1 },{ 1,1 } } };
|
||||
strided_array_view<int, 2> sav6{ av.as_array_view(dim<2>(), dim<2>()), { 1, 1, 1 } };
|
||||
strided_array_view<int, 2> sav7{ av.as_array_view(dim<2>(), dim<2>()),
|
||||
{ { 1, 1 }, { 1, 1 }, { 1, 1 } } };
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
// stride initializer list size should match the rank of the array
|
||||
CHECK_THROW((index<1>{ 0,1 }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, {1, {1,1}} }), fail_fast);
|
||||
CHECK_THROW((index<1>{ 0, 1 }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { 1, { 1, 1 } } }), fail_fast);
|
||||
#ifdef _MSC_VER
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, {{1,1 }, {1,1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ arr, { { 1, 1 }, { 1, 1 } } }), fail_fast);
|
||||
#endif
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, {1, {1,1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { 1, { 1, 1 } } }), fail_fast);
|
||||
#ifdef _MSC_VER
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, {{1,1 }, {1,1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { { 1, 1 }, { 1, 1 } } }), fail_fast);
|
||||
#endif
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1,1,1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), { { 1 }, { 1 } } }),
|
||||
fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()),
|
||||
{ { 1 }, { 1, 1, 1 } } }),
|
||||
fail_fast);
|
||||
#ifdef _MSC_VER
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1,1,1}, {1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()),
|
||||
{ { 1, 1, 1 }, { 1 } } }),
|
||||
fail_fast);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(strided_array_view_type_conversion)
|
||||
@ -692,7 +720,8 @@ SUITE(array_view_tests)
|
||||
// retype strided array with regular strides - from array_view
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_array_view<int, 2> sav3 = sav2.as_strided_array_view<int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
@ -703,32 +732,38 @@ SUITE(array_view_tests)
|
||||
|
||||
// retype strided array with not enough elements - last dimension of the array is too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4,2 },{ 4, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_bounds<2> bounds{ { 4, 2 }, { 4, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides are too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4,2 },{ 2, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_bounds<2> bounds{ { 4, 2 }, { 2, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new typesize
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new
|
||||
// typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2,6 },{ 4, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_bounds<2> bounds{ { 2, 6 }, { 4, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides does not divide by the new typesize
|
||||
// retype strided array with not enough elements - strides does not divide by the new
|
||||
// typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, 1 },{ 6, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_bounds<2> bounds{ { 2, 1 }, { 6, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
@ -754,7 +789,7 @@ SUITE(array_view_tests)
|
||||
{
|
||||
array_view<int, 1> empty;
|
||||
strided_array_view<int, 2> empty2;
|
||||
strided_array_view<int, 1> empty3{ nullptr,{ 0, 1 } };
|
||||
strided_array_view<int, 1> empty3{ nullptr, { 0, 1 } };
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -881,7 +916,6 @@ SUITE(array_view_tests)
|
||||
CHECK(k[0] == 1);
|
||||
CHECK(k[1] == 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void iterate_second_column(array_view<int, dynamic_range, dynamic_range> av)
|
||||
@ -889,7 +923,7 @@ SUITE(array_view_tests)
|
||||
auto length = av.size() / 2;
|
||||
|
||||
// view to the second column
|
||||
auto section = av.section({ 0,1 }, { length,1 });
|
||||
auto section = av.section({ 0, 1 }, { length, 1 });
|
||||
|
||||
CHECK(section.size() == length);
|
||||
for (unsigned int i = 0; i < section.size(); ++i)
|
||||
@ -899,7 +933,7 @@ SUITE(array_view_tests)
|
||||
|
||||
for (unsigned int i = 0; i < section.size(); ++i)
|
||||
{
|
||||
auto idx = index<2>{ i,0 }; // avoid braces inside the CHECK macro
|
||||
auto idx = index<2>{ i, 0 }; // avoid braces inside the CHECK macro
|
||||
CHECK(section[idx] == av[i][1]);
|
||||
}
|
||||
|
||||
@ -909,7 +943,7 @@ SUITE(array_view_tests)
|
||||
{
|
||||
for (unsigned int j = 0; j < section.bounds().index_bounds()[1]; ++j)
|
||||
{
|
||||
auto idx = index<2>{ i,j }; // avoid braces inside the CHECK macro
|
||||
auto idx = index<2>{ i, j }; // avoid braces inside the CHECK macro
|
||||
CHECK(section[idx] == av[i][1]);
|
||||
}
|
||||
}
|
||||
@ -924,7 +958,7 @@ SUITE(array_view_tests)
|
||||
|
||||
TEST(array_view_section_iteration)
|
||||
{
|
||||
int arr[4][2] = { { 4,0 },{ 5,1 },{ 6,2 },{ 7,3 } };
|
||||
int arr[4][2] = { { 4, 0 }, { 5, 1 }, { 6, 2 }, { 7, 3 } };
|
||||
|
||||
// static bounds
|
||||
{
|
||||
@ -1009,7 +1043,7 @@ SUITE(array_view_tests)
|
||||
|
||||
TEST(strided_array_view_section_iteration)
|
||||
{
|
||||
int arr[8] = {4,0,5,1,6,2,7,3};
|
||||
int arr[8] = { 4, 0, 5, 1, 6, 2, 7, 3 };
|
||||
|
||||
// static bounds
|
||||
{
|
||||
@ -1041,15 +1075,15 @@ SUITE(array_view_tests)
|
||||
|
||||
void iterate_second_slice(array_view<int, dynamic_range, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
int expected[6] = { 2,3,10,11,18,19 };
|
||||
auto section = av.section({ 0,1,0 }, { 3,1,2 });
|
||||
int expected[6] = { 2, 3, 10, 11, 18, 19 };
|
||||
auto section = av.section({ 0, 1, 0 }, { 3, 1, 2 });
|
||||
|
||||
for (unsigned int i = 0; i < section.extent<0>(); ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < section.extent<1>(); ++j)
|
||||
for (unsigned int k = 0; k < section.extent<2>(); ++k)
|
||||
{
|
||||
auto idx = index<3>{ i,j,k }; // avoid braces in the CHECK macro
|
||||
auto idx = index<3>{ i, j, k }; // avoid braces in the CHECK macro
|
||||
CHECK(section[idx] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
}
|
||||
@ -1097,7 +1131,7 @@ SUITE(array_view_tests)
|
||||
}
|
||||
|
||||
{
|
||||
auto av = as_array_view(arr, 24).as_array_view(dim<3>(),dim<4>(),dim<2>());
|
||||
auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<4>(), dim<2>());
|
||||
iterate_second_slice(av);
|
||||
}
|
||||
|
||||
@ -1122,9 +1156,14 @@ SUITE(array_view_tests)
|
||||
{
|
||||
// get an array_view of 'c' values from the list of X's
|
||||
|
||||
struct X { int a; int b; int c; };
|
||||
struct X
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
};
|
||||
|
||||
X arr[4] = { { 0,1,2 },{ 3,4,5 },{ 6,7,8 },{ 9,10,11 } };
|
||||
X arr[4] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } };
|
||||
|
||||
auto s = sizeof(int) / sizeof(byte);
|
||||
auto d2 = 3 * s;
|
||||
@ -1137,7 +1176,9 @@ SUITE(array_view_tests)
|
||||
CHECK(av.bounds().index_bounds()[1] == 12);
|
||||
|
||||
// get the last 4 columns
|
||||
auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], arr[0].c[3] } , { arr[1].c[0], ... } , ... }
|
||||
auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1],
|
||||
// arr[0].c[2], arr[0].c[3] } , {
|
||||
// arr[1].c[0], ... } , ... }
|
||||
|
||||
// convert to array 4x1 array of integers
|
||||
auto cs = section.as_strided_array_view<int>(); // { { arr[0].c }, {arr[1].c } , ... }
|
||||
@ -1146,10 +1187,8 @@ SUITE(array_view_tests)
|
||||
CHECK(cs.bounds().index_bounds()[1] == 1);
|
||||
|
||||
// transpose to 1x4 array
|
||||
strided_bounds<2> reverse_bounds{
|
||||
{ cs.bounds().index_bounds()[1] , cs.bounds().index_bounds()[0] },
|
||||
{ cs.bounds().strides()[1], cs.bounds().strides()[0] }
|
||||
};
|
||||
strided_bounds<2> reverse_bounds{ { cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0] },
|
||||
{ cs.bounds().strides()[1], cs.bounds().strides()[0] } };
|
||||
|
||||
strided_array_view<int, 2> transposed{ cs.data(), cs.bounds().total_size(), reverse_bounds };
|
||||
|
||||
@ -1165,7 +1204,6 @@ SUITE(array_view_tests)
|
||||
CHECK(num == arr[i].c);
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(constructors)
|
||||
@ -1180,7 +1218,10 @@ SUITE(array_view_tests)
|
||||
CHECK(av3.length() == 0);
|
||||
|
||||
// Constructing from a nullptr + length is specifically disallowed
|
||||
auto f = [&]() {array_view<int, dynamic_range> av4(nullptr, 2);};
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, dynamic_range> av4(nullptr, 2);
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
|
||||
int arr1[2][3];
|
||||
@ -1202,7 +1243,7 @@ SUITE(array_view_tests)
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
array_view<int, 4> av10;
|
||||
DerivedClass *p = nullptr;
|
||||
DerivedClass* p = nullptr;
|
||||
array_view<BaseClass> av11(p, 0);
|
||||
#endif
|
||||
}
|
||||
@ -1211,7 +1252,7 @@ SUITE(array_view_tests)
|
||||
{
|
||||
array_view<int, dynamic_range> av1;
|
||||
|
||||
int arr[] = {3, 4, 5};
|
||||
int arr[] = { 3, 4, 5 };
|
||||
av1 = arr;
|
||||
array_view<array_view_options<const int, unsigned char>, dynamic_range> av2;
|
||||
av2 = av1;
|
||||
@ -1304,7 +1345,7 @@ SUITE(array_view_tests)
|
||||
|
||||
TEST(custmized_array_view_size)
|
||||
{
|
||||
double (*arr)[3][4] = new double[100][3][4];
|
||||
double(*arr)[3][4] = new double[100][3][4];
|
||||
array_view<array_view_options<double, char>, dynamic_range, 3, 4> av1(arr, (char)10);
|
||||
|
||||
struct EffectiveStructure
|
||||
@ -1317,7 +1358,6 @@ SUITE(array_view_tests)
|
||||
CHECK_THROW(av1[10][3][4], fail_fast);
|
||||
|
||||
array_view<const double, dynamic_range, 6, 4> av2 = av1.as_array_view(dim<>(5), dim<6>(), dim<4>());
|
||||
|
||||
}
|
||||
|
||||
TEST(array_view_sub)
|
||||
@ -1326,43 +1366,43 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
array_view<int, 5> av = arr;
|
||||
CHECK((av.sub<2,2>().bounds() == static_bounds<size_t, 2>()));
|
||||
CHECK((av.sub<2,2>().length() == 2));
|
||||
CHECK(av.sub(2,2).length() == 2);
|
||||
CHECK(av.sub(2,3).length() == 3);
|
||||
CHECK((av.sub<2, 2>().bounds() == static_bounds<size_t, 2>()));
|
||||
CHECK((av.sub<2, 2>().length() == 2));
|
||||
CHECK(av.sub(2, 2).length() == 2);
|
||||
CHECK(av.sub(2, 3).length() == 3);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
array_view<int, 5> av = arr;
|
||||
CHECK((av.sub<0,0>().bounds() == static_bounds<size_t, 0>()));
|
||||
CHECK((av.sub<0,0>().length() == 0));
|
||||
CHECK(av.sub(0,0).length() == 0);
|
||||
CHECK((av.sub<0, 0>().bounds() == static_bounds<size_t, 0>()));
|
||||
CHECK((av.sub<0, 0>().length() == 0));
|
||||
CHECK(av.sub(0, 0).length() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
array_view<int, 5> av = arr;
|
||||
CHECK((av.sub<0,5>().bounds() == static_bounds<size_t, 5>()));
|
||||
CHECK((av.sub<0,5>().length() == 5));
|
||||
CHECK(av.sub(0,5).length() == 5);
|
||||
CHECK_THROW(av.sub(0,6).length(), fail_fast);
|
||||
CHECK_THROW(av.sub(1,5).length(), fail_fast);
|
||||
CHECK((av.sub<0, 5>().bounds() == static_bounds<size_t, 5>()));
|
||||
CHECK((av.sub<0, 5>().length() == 5));
|
||||
CHECK(av.sub(0, 5).length() == 5);
|
||||
CHECK_THROW(av.sub(0, 6).length(), fail_fast);
|
||||
CHECK_THROW(av.sub(1, 5).length(), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
array_view<int, 5> av = arr;
|
||||
CHECK((av.sub<5,0>().bounds() == static_bounds<size_t, 0>()));
|
||||
CHECK((av.sub<5, 0>().bounds() == static_bounds<size_t, 0>()));
|
||||
CHECK((av.sub<5, 0>().length() == 0));
|
||||
CHECK(av.sub(5,0).length() == 0);
|
||||
CHECK_THROW(av.sub(6,0).length(), fail_fast);
|
||||
CHECK(av.sub(5, 0).length() == 0);
|
||||
CHECK_THROW(av.sub(6, 0).length(), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
array_view<int, dynamic_range> av;
|
||||
CHECK((av.sub<0,0>().bounds() == static_bounds<size_t, 0>()));
|
||||
CHECK((av.sub<0,0>().length() == 0));
|
||||
CHECK(av.sub(0,0).length() == 0);
|
||||
CHECK_THROW((av.sub<1,0>().length()), fail_fast);
|
||||
CHECK((av.sub<0, 0>().bounds() == static_bounds<size_t, 0>()));
|
||||
CHECK((av.sub<0, 0>().length() == 0));
|
||||
CHECK(av.sub(0, 0).length() == 0);
|
||||
CHECK_THROW((av.sub<1, 0>().length()), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
@ -1380,11 +1420,11 @@ SUITE(array_view_tests)
|
||||
CHECK_THROW(av.sub(6).length(), fail_fast);
|
||||
auto av2 = av.sub(1);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
CHECK(av2[i] == i+2);
|
||||
CHECK(av2[i] == i + 2);
|
||||
}
|
||||
|
||||
{
|
||||
array_view<int,5> av = arr;
|
||||
array_view<int, 5> av = arr;
|
||||
CHECK(av.sub(0).length() == 5);
|
||||
CHECK(av.sub(1).length() == 4);
|
||||
CHECK(av.sub(4).length() == 1);
|
||||
@ -1392,19 +1432,18 @@ SUITE(array_view_tests)
|
||||
CHECK_THROW(av.sub(6).length(), fail_fast);
|
||||
auto av2 = av.sub(1);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
CHECK(av2[i] == i+2);
|
||||
CHECK(av2[i] == i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
void AssertNullEmptyProperties(array_view<int, dynamic_range>& av)
|
||||
void AssertNullEmptyProperties(array_view<int, dynamic_range> & av)
|
||||
{
|
||||
CHECK(av.length() == 0);
|
||||
CHECK(av.data() == nullptr);
|
||||
CHECK(!av);
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
void AssertContentsMatch(T a1, U a2)
|
||||
template <class T, class U> void AssertContentsMatch(T a1, U a2)
|
||||
{
|
||||
CHECK(a1.length() == a2.length());
|
||||
for (size_t i = 0; i < a1.length(); ++i)
|
||||
@ -1464,7 +1503,7 @@ SUITE(array_view_tests)
|
||||
array_view<int, dynamic_range> av = arr;
|
||||
}
|
||||
|
||||
// initialization or assignment to static array_view that REDUCES size is NOT ok
|
||||
// initialization or assignment to static array_view that REDUCES size is NOT ok
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
array_view<int, 2> av2 = arr;
|
||||
@ -1488,7 +1527,10 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
array_view<int, dynamic_range> av = arr;
|
||||
auto f = [&]() {array_view<int, 2, 1> av2 = av.as_array_view(dim<>(2), dim<>(2));};
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 2, 1> av2 = av.as_array_view(dim<>(2), dim<>(2));
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
@ -1496,7 +1538,7 @@ SUITE(array_view_tests)
|
||||
|
||||
// you can convert statically
|
||||
{
|
||||
array_view<int, 2> av2 = {arr, 2};
|
||||
array_view<int, 2> av2 = { arr, 2 };
|
||||
}
|
||||
{
|
||||
array_view<int, 1> av2 = av4.first<1>();
|
||||
@ -1521,13 +1563,19 @@ SUITE(array_view_tests)
|
||||
}
|
||||
#endif
|
||||
{
|
||||
auto f = [&]() {array_view<int, 4> av4 = {arr2, 2};};
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 4> av4 = { arr2, 2 };
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
// this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one
|
||||
array_view<int, dynamic_range> av = arr2;
|
||||
auto f = [&](){ array_view<int, 4> av2 = av; };
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 4> av2 = av;
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
@ -1557,7 +1605,6 @@ SUITE(array_view_tests)
|
||||
CHECK(wav.data() == (byte*)&a[0]);
|
||||
CHECK(wav.length() == sizeof(a));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(ArrayViewComparison)
|
||||
@ -1673,7 +1720,7 @@ SUITE(array_view_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ SUITE(assertion_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -28,17 +28,17 @@ SUITE(at_tests)
|
||||
int a[] = { 1, 2, 3, 4 };
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
CHECK(at(a, i) == i+1);
|
||||
CHECK(at(a, i) == i + 1);
|
||||
|
||||
CHECK_THROW(at(a, 4), fail_fast);
|
||||
}
|
||||
|
||||
TEST(std_array)
|
||||
{
|
||||
std::array<int,4> a = { 1, 2, 3, 4 };
|
||||
std::array<int, 4> a = { 1, 2, 3, 4 };
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
CHECK(at(a, i) == i+1);
|
||||
CHECK(at(a, i) == i + 1);
|
||||
|
||||
CHECK_THROW(at(a, 4), fail_fast);
|
||||
}
|
||||
@ -48,13 +48,13 @@ SUITE(at_tests)
|
||||
std::vector<int> a = { 1, 2, 3, 4 };
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
CHECK(at(a, i) == i+1);
|
||||
CHECK(at(a, i) == i + 1);
|
||||
|
||||
CHECK_THROW(at(a, 4), fail_fast);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -19,18 +19,21 @@
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;;
|
||||
using namespace gsl;
|
||||
;
|
||||
|
||||
namespace
|
||||
{
|
||||
void use(unsigned int&) {}
|
||||
void use(unsigned int&)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
SUITE(bounds_test)
|
||||
{
|
||||
TEST(basic_bounds)
|
||||
{
|
||||
for (auto point : static_bounds <unsigned int, dynamic_range, 3, 4 > { 2 })
|
||||
for (auto point : static_bounds<unsigned int, dynamic_range, 3, 4>{ 2 })
|
||||
{
|
||||
for (unsigned int j = 0; j < decltype(point)::rank; j++)
|
||||
{
|
||||
@ -48,25 +51,26 @@ SUITE(bounds_test)
|
||||
x.slice().slice();
|
||||
}
|
||||
|
||||
TEST (arrayview_iterator)
|
||||
TEST(arrayview_iterator)
|
||||
{
|
||||
static_bounds<size_t, 4, dynamic_range, 2> bounds{ 3 };
|
||||
|
||||
auto itr = bounds.begin();
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
array_view< int, 4, dynamic_range, 2> av(nullptr, bounds);
|
||||
array_view<int, 4, dynamic_range, 2> av(nullptr, bounds);
|
||||
|
||||
auto itr2 = av.cbegin();
|
||||
|
||||
for (auto & v : av) {
|
||||
for (auto& v : av)
|
||||
{
|
||||
v = 4;
|
||||
}
|
||||
fill(av.begin(), av.end(), 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST (bounds_convertible)
|
||||
TEST(bounds_convertible)
|
||||
{
|
||||
static_bounds<size_t, 7, 4, 2> b1;
|
||||
static_bounds<size_t, 7, dynamic_range, 2> b2 = b1;
|
||||
@ -93,7 +97,7 @@ SUITE(bounds_test)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -21,9 +21,19 @@
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
struct MyBase { bool foo() { return true; } };
|
||||
struct MyDerived : public MyBase {};
|
||||
struct Unrelated {};
|
||||
struct MyBase
|
||||
{
|
||||
bool foo()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
struct MyDerived : public MyBase
|
||||
{
|
||||
};
|
||||
struct Unrelated
|
||||
{
|
||||
};
|
||||
|
||||
SUITE(MaybeNullTests)
|
||||
{
|
||||
@ -31,46 +41,45 @@ SUITE(MaybeNullTests)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
// Forbid non-nullptr assignable types
|
||||
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{1});
|
||||
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{1});
|
||||
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{ 1 });
|
||||
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{ 1 });
|
||||
maybe_null_ret<int> z_ret(10);
|
||||
maybe_null_dbg<std::vector<int>> y_dbg({1,2});
|
||||
maybe_null_dbg<std::vector<int>> y_dbg({ 1, 2 });
|
||||
maybe_null_dbg<int> z_dbg(10);
|
||||
maybe_null_dbg<std::vector<int>> y_dbg({1,2});
|
||||
maybe_null_dbg<std::vector<int>> y_dbg({ 1, 2 });
|
||||
#endif
|
||||
int n = 5;
|
||||
maybe_null_dbg<int *> opt_n(&n);
|
||||
maybe_null_dbg<int*> opt_n(&n);
|
||||
int result = 0;
|
||||
bool threw = false;
|
||||
|
||||
CHECK_THROW(result = *opt_n, fail_fast);
|
||||
|
||||
maybe_null_ret<std::shared_ptr<int>> x_ret(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
maybe_null_dbg<std::shared_ptr<int>> x_dbg(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
maybe_null_ret<std::shared_ptr<int>> x_ret(
|
||||
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
maybe_null_dbg<std::shared_ptr<int>> x_dbg(
|
||||
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
}
|
||||
|
||||
TEST(TestMaybeNull2)
|
||||
{
|
||||
int n = 5;
|
||||
maybe_null<int *> opt_n(&n);
|
||||
maybe_null<int*> opt_n(&n);
|
||||
int result = 0;
|
||||
if (opt_n.present())
|
||||
result = *opt_n;
|
||||
if (opt_n.present()) result = *opt_n;
|
||||
}
|
||||
|
||||
TEST(TestMaybeNull3)
|
||||
{
|
||||
int n = 5;
|
||||
maybe_null<int *> opt_n(&n);
|
||||
maybe_null<int*> opt_n(&n);
|
||||
int result = 0;
|
||||
if (opt_n != nullptr)
|
||||
result = *opt_n;
|
||||
if (opt_n != nullptr) result = *opt_n;
|
||||
}
|
||||
|
||||
int test4_helper(maybe_null<int *> p)
|
||||
int test4_helper(maybe_null<int*> p)
|
||||
{
|
||||
if (p != nullptr)
|
||||
return *p;
|
||||
if (p != nullptr) return *p;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -81,7 +90,7 @@ SUITE(MaybeNullTests)
|
||||
result = test4_helper(&n);
|
||||
}
|
||||
|
||||
int test5_helper(maybe_null_dbg<int *> p)
|
||||
int test5_helper(maybe_null_dbg<int*> p)
|
||||
{
|
||||
return *p;
|
||||
}
|
||||
@ -104,67 +113,63 @@ SUITE(MaybeNullTests)
|
||||
#endif
|
||||
|
||||
int g_int;
|
||||
void test7_helper(maybe_null<maybe_null<int *> *> outptr)
|
||||
void test7_helper(maybe_null<maybe_null<int*>*> outptr)
|
||||
{
|
||||
g_int = 5;
|
||||
|
||||
if (outptr.present())
|
||||
*outptr = &g_int;
|
||||
if (outptr.present()) *outptr = &g_int;
|
||||
}
|
||||
|
||||
void test7b_helper(maybe_null_dbg<maybe_null_dbg<int *> *> outptr)
|
||||
void test7b_helper(maybe_null_dbg<maybe_null_dbg<int*>*> outptr)
|
||||
{
|
||||
g_int = 5;
|
||||
|
||||
if (outptr.present())
|
||||
*outptr = &g_int;
|
||||
if (outptr.present()) *outptr = &g_int;
|
||||
}
|
||||
|
||||
TEST(TestMaybeNull7a)
|
||||
{
|
||||
maybe_null<int *> outval;
|
||||
maybe_null<int*> outval;
|
||||
test7_helper(&outval);
|
||||
CHECK(outval.present() && *outval == 5);
|
||||
}
|
||||
|
||||
TEST(TestMaybeNull7b)
|
||||
{
|
||||
maybe_null_dbg<int *> outval;
|
||||
maybe_null_dbg<int*> outval;
|
||||
test7b_helper(&outval);
|
||||
CHECK_THROW((void)*outval, fail_fast);
|
||||
}
|
||||
|
||||
int test8_helper1(maybe_null_dbg<int *> opt)
|
||||
int test8_helper1(maybe_null_dbg<int*> opt)
|
||||
{
|
||||
return *opt;
|
||||
}
|
||||
|
||||
int test8_helper2a(maybe_null_dbg<int *> opt)
|
||||
int test8_helper2a(maybe_null_dbg<int*> opt)
|
||||
{
|
||||
if (!opt.present())
|
||||
return 0;
|
||||
if (!opt.present()) return 0;
|
||||
return test8_helper1(opt);
|
||||
}
|
||||
|
||||
TEST(TestMaybeNull8a)
|
||||
{
|
||||
int n = 5;
|
||||
maybe_null_dbg<int *> opt(&n);
|
||||
maybe_null_dbg<int*> opt(&n);
|
||||
CHECK_THROW(test8_helper2a(opt), fail_fast);
|
||||
}
|
||||
|
||||
#ifdef CONVERT_TO_PTR_TO_CONST
|
||||
int test9_helper(maybe_null<const int *> copt)
|
||||
int test9_helper(maybe_null<const int*> copt)
|
||||
{
|
||||
if (copt.present())
|
||||
return *copt;
|
||||
if (copt.present()) return *copt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TestMaybeNull9()
|
||||
{
|
||||
int n = 5;
|
||||
maybe_null<int *> opt(&n);
|
||||
maybe_null<int*> opt(&n);
|
||||
CHECK_THROW(test9_helper(opt), fail_fast);
|
||||
}
|
||||
#endif
|
||||
@ -298,7 +303,7 @@ SUITE(MaybeNullTests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -20,16 +20,26 @@
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
struct MyBase {};
|
||||
struct MyDerived : public MyBase {};
|
||||
struct Unrelated {};
|
||||
struct MyBase
|
||||
{
|
||||
};
|
||||
struct MyDerived : public MyBase
|
||||
{
|
||||
};
|
||||
struct Unrelated
|
||||
{
|
||||
};
|
||||
|
||||
// stand-in for a user-defined ref-counted class
|
||||
template<typename T>
|
||||
struct RefCounted
|
||||
template <typename T> struct RefCounted
|
||||
{
|
||||
RefCounted(T* p) : p_(p) {}
|
||||
operator T*() { return p_; }
|
||||
RefCounted(T* p) : p_(p)
|
||||
{
|
||||
}
|
||||
operator T*()
|
||||
{
|
||||
return p_;
|
||||
}
|
||||
T* p_;
|
||||
};
|
||||
|
||||
@ -51,16 +61,17 @@ SUITE(NotNullTests)
|
||||
not_null<int*> p = up;
|
||||
|
||||
// Forbid non-nullptr assignable types
|
||||
not_null<std::vector<int>> f(std::vector<int>{1});
|
||||
not_null<std::vector<int>> f(std::vector<int>{ 1 });
|
||||
not_null<int> z(10);
|
||||
not_null<std::vector<int>> y({1,2});
|
||||
not_null<std::vector<int>> y({ 1, 2 });
|
||||
#endif
|
||||
int i = 12;
|
||||
auto rp = RefCounted<int>(&i);
|
||||
not_null<int*> p(rp);
|
||||
CHECK(p.get() == &i);
|
||||
|
||||
not_null<std::shared_ptr<int>> x(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
not_null<std::shared_ptr<int>> x(
|
||||
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
}
|
||||
|
||||
TEST(TestNotNullCasting)
|
||||
@ -96,7 +107,7 @@ SUITE(NotNullTests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ SUITE(owner_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ SUITE(string_view_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
@ -31,7 +31,10 @@ SUITE(utils_tests)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally([&]() {f(i);});
|
||||
auto _ = finally([&]()
|
||||
{
|
||||
f(i);
|
||||
});
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
@ -41,7 +44,10 @@ SUITE(utils_tests)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _1 = finally([&]() {f(i);});
|
||||
auto _1 = finally([&]()
|
||||
{
|
||||
f(i);
|
||||
});
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 0);
|
||||
@ -62,7 +68,10 @@ SUITE(utils_tests)
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
void g() { j += 1; };
|
||||
void g()
|
||||
{
|
||||
j += 1;
|
||||
};
|
||||
TEST(finally_function_ptr)
|
||||
{
|
||||
j = 0;
|
||||
@ -95,7 +104,7 @@ SUITE(utils_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
int main(int, const char* [])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user