mirror of
https://github.com/microsoft/GSL.git
synced 2025-01-18 09:44:59 -05:00
1cdb8d295e
Some checks failed
CI_Android / Android (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (macos-latest) (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (ubuntu-latest) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 23) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
225 lines
6.3 KiB
C++
225 lines
6.3 KiB
C++
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// 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.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <array> // for array
|
|
#include <cstddef> // for size_t
|
|
#include <gsl/algorithm> // for copy
|
|
#include <gsl/span> // for span
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "deathTestCommon.h"
|
|
|
|
namespace gsl
|
|
{
|
|
struct fail_fast;
|
|
} // namespace gsl
|
|
|
|
using namespace gsl;
|
|
|
|
TEST(algorithm_tests, same_type)
|
|
{
|
|
// dynamic source and destination span
|
|
{
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<int> src_span(src);
|
|
const span<int> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
|
|
// static source and dynamic destination span
|
|
{
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<int, 5> src_span(src);
|
|
const span<int> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
|
|
// dynamic source and static destination span
|
|
{
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const gsl::span<int> src_span(src);
|
|
const gsl::span<int, 10> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
|
|
// static source and destination span
|
|
{
|
|
std::array<int, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<int, 5> src_span(src);
|
|
const span<int, 10> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(algorithm_tests, compatible_type)
|
|
{
|
|
// dynamic source and destination span
|
|
{
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<short> src_span(src);
|
|
const span<int> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
|
|
// static source and dynamic destination span
|
|
{
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<short, 5> src_span(src);
|
|
const span<int> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
|
|
// dynamic source and static destination span
|
|
{
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<short> src_span(src);
|
|
const span<int, 10> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
|
|
// static source and destination span
|
|
{
|
|
std::array<short, 5> src{1, 2, 3, 4, 5};
|
|
std::array<int, 10> dst{};
|
|
|
|
const span<short, 5> src_span(src);
|
|
const span<int, 10> dst_span(dst);
|
|
|
|
copy(src_span, dst_span);
|
|
copy(src_span, dst_span.subspan(src_span.size()));
|
|
|
|
for (std::size_t i = 0; i < src.size(); ++i)
|
|
{
|
|
EXPECT_TRUE(dst[i] == src[i]);
|
|
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
TEST(algorithm_tests, incompatible_type)
|
|
{
|
|
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(gsl::make_span(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);
|
|
}
|
|
#endif
|
|
|
|
TEST(algorithm_tests, small_destination_span)
|
|
{
|
|
const auto terminateHandler = std::set_terminate([] {
|
|
std::cerr << "Expected Death. small_destination_span";
|
|
std::abort();
|
|
});
|
|
const auto expected = GetExpectedDeathString(terminateHandler);
|
|
|
|
std::array<int, 12> src{1, 2, 3, 4};
|
|
std::array<int, 4> dst{};
|
|
|
|
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);
|
|
|
|
EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), expected);
|
|
EXPECT_DEATH(copy(src_span_dyn, dst_span_static), expected);
|
|
EXPECT_DEATH(copy(src_span_static, dst_span_dyn), expected);
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
copy(src_span_static, dst_span_static);
|
|
#endif
|
|
}
|