File: test_n2216.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (101 lines) | stat: -rw-r--r-- 2,915 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2018-2025 Jean-Louis Leroy
// 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)

#include <iostream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <any>

#include <boost/openmethod.hpp>
#include <boost/openmethod/interop/std_shared_ptr.hpp>
#include <boost/openmethod/interop/std_unique_ptr.hpp>
#include <boost/openmethod/initialize.hpp>
#include <boost/openmethod/policies/vptr_vector.hpp>

#include "test_util.hpp"

#define BOOST_TEST_MODULE openmethod
#include <boost/test/unit_test.hpp>

using namespace boost::openmethod;

namespace TEST_NS {

using namespace test_matrices;

struct test_registry : test_registry_<__COUNTER__> {};

BOOST_OPENMETHOD_CLASSES(matrix, dense_matrix, test_registry);

BOOST_OPENMETHOD(
    times, (virtual_<const matrix&>, virtual_<const matrix&>),
    std::unique_ptr<matrix>, test_registry);

BOOST_OPENMETHOD_OVERRIDE(
    times, (const dense_matrix&, const matrix&),
    std::unique_ptr<dense_matrix>) {
    return std::make_unique<dense_matrix>();
}

BOOST_OPENMETHOD_OVERRIDE(
    times, (const matrix&, const dense_matrix&), std::unique_ptr<matrix>) {
    return std::make_unique<matrix>();
}

static_assert(
    std::is_same_v<
        detail::virtual_type<std::unique_ptr<matrix>, test_registry>, matrix>);

BOOST_AUTO_TEST_CASE(covariant_return_type) {
    auto compiler = boost::openmethod::initialize<test_registry>(n2216());
    BOOST_TEST(compiler.report.ambiguous == 0u);

    // test_registry: use covariant return types to resolve ambiguity.
    dense_matrix left, right;
    auto result = times(left, right);
    BOOST_TEST(result->type == DENSE_MATRIX);
}

} // namespace TEST_NS

namespace TEST_NS {

using namespace test_matrices;

struct test_registry : test_registry_<__COUNTER__> {};

BOOST_OPENMETHOD_CLASSES(matrix, dense_matrix, test_registry);

BOOST_OPENMETHOD(
    times, (virtual_<const matrix&>, virtual_<const matrix&>), string_pair,
    test_registry);

BOOST_OPENMETHOD_OVERRIDE(times, (const matrix&, const matrix&), string_pair) {
    return string_pair(MATRIX_MATRIX, NONE);
}

BOOST_OPENMETHOD_OVERRIDE(
    times, (const dense_matrix&, const matrix&), string_pair) {
    return string_pair(DENSE_MATRIX, MATRIX_MATRIX);
}

BOOST_OPENMETHOD_OVERRIDE(
    times, (const matrix&, const dense_matrix&), string_pair) {
    return string_pair(MATRIX_DENSE, MATRIX_MATRIX);
}

BOOST_AUTO_TEST_CASE(pick_any_ambiguous) {
    auto compiler = boost::openmethod::initialize<test_registry>(n2216());
    BOOST_TEST(compiler.report.ambiguous == 1u);

    // test_registry: use covariant return types to resolve ambiguity.
    dense_matrix left, right;
    auto result = times(left, right);
    BOOST_TEST(result.first == MATRIX_DENSE);
    BOOST_TEST(result.second == MATRIX_MATRIX);
}

} // namespace TEST_NS