2016-11-17 13:45:06 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// blanket turn off warnings from CppCoreCheck from catch
|
|
|
|
// so people aren't annoyed by them when running the tool.
|
|
|
|
#pragma warning(disable : 26440 26426) // from catch
|
|
|
|
#endif
|
|
|
|
|
2019-12-04 15:30:58 -05:00
|
|
|
#if __clang__ || __GNUC__
|
2019-12-12 13:55:26 -05:00
|
|
|
// disable warnings from gtest
|
2019-12-04 15:30:58 -05:00
|
|
|
#pragma GCC diagnostic push
|
2019-12-04 15:48:13 -05:00
|
|
|
#pragma GCC diagnostic ignored "-Wundef"
|
2019-12-06 19:03:47 -05:00
|
|
|
#endif
|
|
|
|
#if __clang__
|
2019-12-04 15:30:58 -05:00
|
|
|
#pragma GCC diagnostic ignored "-Wglobal-constructors"
|
|
|
|
#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
|
|
|
|
#pragma GCC diagnostic ignored "-Wcovered-switch-default"
|
|
|
|
#endif
|
2019-12-03 17:32:25 -05:00
|
|
|
#include <gtest/gtest.h>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <gsl/gsl_algorithm> // for copy
|
|
|
|
#include <gsl/span> // for span
|
|
|
|
#include <array> // for array
|
|
|
|
#include <cstddef> // for size_t
|
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
namespace{
|
2019-12-12 14:15:41 -05:00
|
|
|
static const std::string deathstring("Expected Death");
|
2019-12-12 13:55:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace gsl
|
|
|
|
{
|
2017-11-28 10:13:49 -05:00
|
|
|
struct fail_fast;
|
2019-12-12 13:55:26 -05:00
|
|
|
} // namespace gsl
|
2016-11-17 13:45:06 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace gsl;
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(algorithm_tests, same_type)
|
2016-11-17 13:45:06 -05:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
// dynamic source and destination span
|
2016-11-17 13:45:06 -05:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int> src_span(src);
|
|
|
|
const span<int> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// static source and dynamic destination span
|
|
|
|
{
|
|
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 5> src_span(src);
|
|
|
|
const span<int> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// dynamic source and static destination span
|
|
|
|
{
|
|
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int> src_span(src);
|
|
|
|
const span<int, 10> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// static source and destination span
|
|
|
|
{
|
|
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 5> src_span(src);
|
|
|
|
const span<int, 10> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(algorithm_tests, compatible_type)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
// dynamic source and destination span
|
2016-11-17 13:45:06 -05:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<short> src_span(src);
|
|
|
|
const span<int> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// static source and dynamic destination span
|
|
|
|
{
|
|
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<short, 5> src_span(src);
|
|
|
|
const span<int> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// dynamic source and static destination span
|
|
|
|
{
|
|
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<short> src_span(src);
|
|
|
|
const span<int, 10> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// static source and destination span
|
|
|
|
{
|
|
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
|
|
std::array<int, 10> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<short, 5> src_span(src);
|
|
|
|
const span<int, 10> dst_span(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span, dst_span);
|
|
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
|
|
{
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
2016-11-17 13:45:06 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(algorithm_tests, incompatible_type)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
std::array<int, 4> src{1, 2, 3, 4};
|
|
|
|
std::array<int*, 12> dst{};
|
|
|
|
|
|
|
|
span<int> src_span_dyn(src);
|
|
|
|
span<int, 4> src_span_static(src);
|
|
|
|
span<int*> dst_span_dyn(dst);
|
|
|
|
span<int*, 4> dst_span_static(dst);
|
|
|
|
|
|
|
|
// every line should produce a compilation error
|
|
|
|
copy(src_span_dyn, dst_span_dyn);
|
|
|
|
copy(src_span_dyn, dst_span_static);
|
|
|
|
copy(src_span_static, dst_span_dyn);
|
|
|
|
copy(src_span_static, dst_span_static);
|
|
|
|
}
|
2016-11-17 13:45:06 -05:00
|
|
|
#endif
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(algorithm_tests, small_destination_span)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2019-12-12 13:55:26 -05:00
|
|
|
std::set_terminate([] {
|
|
|
|
std::cerr << "Expected Death. small_destination_span";
|
|
|
|
std::abort();
|
|
|
|
});
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
std::array<int, 12> src{1, 2, 3, 4};
|
|
|
|
std::array<int, 4> dst{};
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int> src_span_dyn(src);
|
|
|
|
const span<int, 12> src_span_static(src);
|
|
|
|
const span<int> dst_span_dyn(dst);
|
|
|
|
const span<int, 4> dst_span_static(dst);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), deathstring);
|
|
|
|
EXPECT_DEATH(copy(src_span_dyn, dst_span_static), deathstring);
|
|
|
|
EXPECT_DEATH(copy(src_span_static, dst_span_dyn), deathstring);
|
2016-11-17 13:45:06 -05:00
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
copy(src_span_static, dst_span_static);
|
2016-11-17 13:45:06 -05:00
|
|
|
#endif
|
|
|
|
}
|
2019-12-04 15:30:58 -05:00
|
|
|
|
|
|
|
#if __clang__ || __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|