Files
container/experimental/segmented_copy_test.cpp
T
2026-03-10 23:28:26 +01:00

136 lines
3.5 KiB
C++

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2025-2026. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/container/experimental/segmented_copy.hpp>
#include <boost/core/lightweight_test.hpp>
#include "segmented_test_helper.hpp"
#include <vector>
using namespace boost::container;
void test_copy_full_range()
{
test_detail::seg_vector<int> sv;
int a1[] = {1, 2, 3};
int a2[] = {4, 5};
int a3[] = {6, 7, 8, 9};
sv.add_segment_range(a1, a1 + 3);
sv.add_segment_range(a2, a2 + 2);
sv.add_segment_range(a3, a3 + 4);
std::vector<int> out(9, 0);
std::vector<int>::iterator result = segmented_copy(sv.begin(), sv.end(), out.begin());
BOOST_TEST(result == out.end());
for(int i = 0; i < 9; ++i)
BOOST_TEST_EQ(out[static_cast<std::size_t>(i)], i + 1);
}
void test_copy_empty()
{
test_detail::seg_vector<int> sv;
std::vector<int> out;
std::vector<int>::iterator result = segmented_copy(sv.begin(), sv.end(), out.begin());
BOOST_TEST(result == out.begin());
}
void test_copy_single_segment()
{
test_detail::seg_vector<int> sv;
int a[] = {10, 20, 30};
sv.add_segment_range(a, a + 3);
std::vector<int> out(3, 0);
segmented_copy(sv.begin(), sv.end(), out.begin());
BOOST_TEST_EQ(out[0], 10);
BOOST_TEST_EQ(out[1], 20);
BOOST_TEST_EQ(out[2], 30);
}
void test_copy_non_segmented()
{
std::vector<int> in;
in.push_back(1);
in.push_back(2);
in.push_back(3);
std::vector<int> out(3, 0);
segmented_copy(in.begin(), in.end(), out.begin());
BOOST_TEST_EQ(out[0], 1);
BOOST_TEST_EQ(out[1], 2);
BOOST_TEST_EQ(out[2], 3);
}
void test_copy_sentinel_segmented()
{
test_detail::seg_vector<int> sv;
int a1[] = {1, 2, 3};
int a2[] = {4, 5};
int a3[] = {6, 7, 8, 9};
sv.add_segment_range(a1, a1 + 3);
sv.add_segment_range(a2, a2 + 2);
sv.add_segment_range(a3, a3 + 4);
std::vector<int> out(9, 0);
std::vector<int>::iterator result =
segmented_copy(sv.begin(), test_detail::make_sentinel(sv.end()), out.begin());
BOOST_TEST(result == out.end());
for(int i = 0; i < 9; ++i)
BOOST_TEST_EQ(out[static_cast<std::size_t>(i)], i + 1);
}
void test_copy_sentinel_non_segmented()
{
std::vector<int> in;
in.push_back(1);
in.push_back(2);
in.push_back(3);
std::vector<int> out(3, 0);
segmented_copy(in.begin(), test_detail::make_sentinel(in.end()), out.begin());
BOOST_TEST_EQ(out[0], 1);
BOOST_TEST_EQ(out[1], 2);
BOOST_TEST_EQ(out[2], 3);
}
void test_copy_seg2()
{
test_detail::seg2_vector<int> sv2;
int a1[] = {1, 2, 3};
int a2[] = {4, 5};
int a3[] = {6, 7, 8, 9};
sv2.add_flat_segment_range(a1, a1 + 3);
sv2.add_flat_segment_range(a2, a2 + 2);
sv2.add_flat_segment_range(a3, a3 + 4);
std::vector<int> out(9, 0);
std::vector<int>::iterator result = segmented_copy(sv2.begin(), sv2.end(), out.begin());
BOOST_TEST(result == out.end());
for(int i = 0; i < 9; ++i)
BOOST_TEST_EQ(out[static_cast<std::size_t>(i)], i + 1);
}
int main()
{
test_copy_full_range();
test_copy_empty();
test_copy_single_segment();
test_copy_non_segmented();
test_copy_sentinel_segmented();
test_copy_sentinel_non_segmented();
test_copy_seg2();
return boost::report_errors();
}