From db766ce4cc80c5942796596f27faf2ea880bf371 Mon Sep 17 00:00:00 2001 From: ObeliskGate Date: Sun, 18 Aug 2024 12:28:44 +0800 Subject: [PATCH] refactor: seperate `ranges` test into 3 funcs --- tests/test_pytypes.cpp | 50 ++++++++++++++++++++++---------- tests/test_pytypes.py | 66 ++++++++++++++++++++---------------------- 2 files changed, 66 insertions(+), 50 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 047bc9adec..6107e26f8c 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -940,40 +940,58 @@ TEST_SUBMODULE(pytypes, m) { m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false; #endif -#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES) // test_ranges - m.def("iterator_default_initialization", []() { - using TupleIterator = decltype(py::tuple{}.begin()); - using ListIterator = decltype(py::list{}.begin()); - using DictIterator = decltype(py::dict{}.begin()); - return py::bool_( - TupleIterator{} == TupleIterator{} && ListIterator{} == ListIterator{} - && DictIterator{} - == DictIterator{}); // value initialization result must compared as true +#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES) + + // test_tuple_ranges + m.def("tuple_iterator_default_initialization", []() { + using TupleIterator = decltype(std::declval().begin()); + static_assert(std::random_access_iterator); + return TupleIterator{} == TupleIterator{}; }); m.def("transform_tuple_plus_one", [](py::tuple &tpl) { - static_assert(std::ranges::random_access_range); + py::list ret{}; for (auto it : tpl | std::views::transform([](auto &o) { return py::cast(o) + 1; })) { - py::print(it); + ret.append(py::int_(it)); } + return ret; }); + + // test_list_ranges + m.def("list_iterator_default_initialization", []() { + using ListIterator = decltype(std::declval().begin()); + static_assert(std::random_access_iterator); + return ListIterator{} == ListIterator{}; + }); + m.def("transform_list_plus_one", [](py::list &lst) { - static_assert(std::ranges::random_access_range); + py::list ret{}; for (auto it : lst | std::views::transform([](auto &o) { return py::cast(o) + 1; })) { - py::print(it); + ret.append(py::int_(it)); } + return ret; }); + + // test_dict_ranges + m.def("dict_iterator_default_initialization", []() { + using DictIterator = decltype(std::declval().begin()); + static_assert(std::forward_iterator); + return DictIterator{} == DictIterator{}; + }); + m.def("transform_dict_plus_one", [](py::dict &dct) { - static_assert(std::ranges::forward_range); + py::list ret{}; for (auto it : dct | std::views::transform([](auto &o) { return std::pair{py::cast(o.first) + 1, py::cast(o.second) + 1}; })) { - py::print("{} : {}"_s.format(it.first, it.second)); + ret.append(py::make_tuple(py::int_(it.first), py::int_(it.second))); } + return ret; }); + m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = true; #else - m.attr("defined_PYBIND11_HAS_RANGES") = false; + m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false; #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 47e0bd2fed..1c6335f757 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1054,41 +1054,39 @@ def test_typevar(doc): not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES, reason=" not available.", ) -def test_ranges(capture): - assert m.iterator_default_initialization - - with capture: - m.transform_tuple_plus_one((1, 2, 3)) - - assert ( - capture.unordered - == """ - 2 - 3 - 4 - """ - ) +@pytest.mark.parametrize( + ("tested_tuple", "expected"), + [((1,), [2]), ((3, 4), [4, 5]), ((7, 8, 9), [8, 9, 10])], +) +def test_tuple_ranges(tested_tuple, expected): + assert m.tuple_iterator_default_initialization() + assert m.transform_tuple_plus_one(tested_tuple) == expected - with capture: - m.transform_list_plus_one([1, 2, 3]) - assert ( - capture.unordered - == """ - 2 - 3 - 4 - """ - ) +@pytest.mark.skipif( + not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES, + reason=" not available.", +) +@pytest.mark.parametrize( + ("tested_list", "expected"), [([1], [2]), ([3, 4], [4, 5]), ([7, 8, 9], [8, 9, 10])] +) +def test_list_ranges(tested_list, expected): + assert m.list_iterator_default_initialization() + assert m.transform_list_plus_one(tested_list) == expected - with capture: - m.transform_dict_plus_one({1: 2, 3: 4, 5: 6}) - assert ( - capture.unordered - == """ - 2 : 3 - 4 : 5 - 6 : 7 - """ - ) +@pytest.mark.skipif( + not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES, + reason=" not available.", +) +@pytest.mark.parametrize( + ("tested_dict", "expected"), + [ + ({1: 2}, [(2, 3)]), + ({3: 4, 5: 6}, [(4, 5), (6, 7)]), + ({7: 8, 9: 10, 11: 12}, [(8, 9), (10, 11), (12, 13)]), + ], +) +def test_dict_ranges(tested_dict, expected): + assert m.dict_iterator_default_initialization() + assert m.transform_dict_plus_one(tested_dict) == expected