tuple.cpp 6.47 KB
Newer Older
zhoux's avatar
zhoux committed
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***************************************************************************************************
 * Copyright (c) 2023 - 2025 Hygon Information Technology Co., Ltd. All rights reserved.
 * Copyright (c) 2023 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 **************************************************************************************************/

#include "hytlass_unit_test.h"

#include <hytlass/trace.h>

#include <cassert>
#include <type_traits>

#include <hute/container/tuple.hpp>
#include <hute/int_tuple.hpp>

template<class T>
class ConvertibleTo {
public:
  ConvertibleTo(T val) : val_(val) {}

  operator T () const { return val_; }

private:
  T val_ = 0;
};

template<class Integral, Integral Value>
using IC = std::integral_constant<Integral, Value>;

TEST(HuTe_core_msvc_compilation, TupleAssignment)
{
  HYTLASS_TRACE_HOST("-------------------------------");
  HYTLASS_TRACE_HOST("hute::tuple creation and assignment");
  HYTLASS_TRACE_HOST("-------------------------------");

  using forty_two_type = IC<int, 42>;
  using forty_three_type = IC<size_t, 43>;

  using ebo_s_type = hute::detail::EBO<0, forty_two_type>;
  [[maybe_unused]] ebo_s_type ebo_s;
  static_assert(std::is_same_v<decltype(hute::detail::getv(ebo_s)), forty_two_type>);

  using ebo_d_type = hute::detail::EBO<1, size_t>;
  [[maybe_unused]] ebo_d_type ebo_d(43u);
  assert(ebo_d.t_ == 43u);
  static_assert(std::is_same_v<std::remove_const_t<std::remove_reference_t<decltype(hute::detail::getv(ebo_d))>>, size_t > );
  assert(hute::detail::getv(ebo_d) == 43u);

  [[maybe_unused]] hute::detail::TupleBase<std::index_sequence<0, 1, 2>, int, forty_two_type, size_t> tb0{
          41, forty_two_type{}, size_t(43u) };
  [[maybe_unused]] hute::detail::TupleBase<std::index_sequence<0, 1, 2>, int, forty_two_type, size_t> tb1;

  int val41 = ConvertibleTo{41};
  assert(val41 == 41);
  size_t val43 = ConvertibleTo{size_t(43u)};
  assert(val43 == size_t{43u});
  [[maybe_unused]] hute::detail::TupleBase<std::index_sequence<0, 1, 2>, int, forty_two_type, size_t> tb2{
        ConvertibleTo{41}, forty_two_type{}, ConvertibleTo{size_t(43u)}};

  [[maybe_unused]] hute::detail::TupleBase<std::index_sequence<0>, int> tb3{ 41 };
  [[maybe_unused]] hute::detail::TupleBase<std::index_sequence<0>, int> tb3a{ 42 };
  tb3 = tb3a;

  using tuple_0d_type = hute::tuple<>;
  using tuple_1d_d_type = hute::tuple<int>;
  using tuple_1d_s_type = hute::tuple<forty_two_type>;
  using tuple_2d_dd_type = hute::tuple<int, size_t>;
  using tuple_2d_ss_type = hute::tuple<forty_two_type, forty_three_type>;

  [[maybe_unused]] tuple_0d_type t0;

  // Symptom: "illegal member initialization: 'TupleBase<int>' is not a base or member"
  [[maybe_unused]] tuple_1d_d_type t1{ 42 };

  [[maybe_unused]] tuple_1d_s_type t2;

  [[maybe_unused]] tuple_1d_d_type t1a{ 43 };
  t1 = t1a;

  [[maybe_unused]] tuple_2d_dd_type t3{ 42, size_t(43u) };
  [[maybe_unused]] tuple_2d_ss_type t4;
  t3 = t4;

  [[maybe_unused]] tuple_2d_dd_type t3a{ 44, size_t(45u) };
  // Symptom: "illegal member initialization:
  // 'TupleBase<int, unsigned __int64>' is not a base or member"
  t3 = t3a;
}

TEST(HuTe_core_msvc_compilation, TupleGetSingleInteger)
{
  HYTLASS_TRACE_HOST("-------------------------------");
  HYTLASS_TRACE_HOST("hute::get<I> on hute::tuple for single integer I");
  HYTLASS_TRACE_HOST("-------------------------------");

  hute::tuple<int, ConvertibleTo<size_t>, IC<int, 43>> t0{ 41, size_t(42u), IC<int, 43>{} };

  [[maybe_unused]] auto t0_0 = hute::get<0>(t0);
  static_assert(std::is_same_v<decltype(t0_0), int>);
  assert(t0_0 == 41);

  [[maybe_unused]] auto t0_1 = hute::get<1>(t0);
  static_assert(std::is_same_v<decltype(t0_1), ConvertibleTo<size_t>>);

  [[maybe_unused]] auto t0_2 = hute::get<2>(t0);
  static_assert(std::is_same_v<decltype(t0_2), IC<int, 43>>);
}

TEST(HuTe_core_msvc_compilation, TupleGetRecursive)
{
  HYTLASS_TRACE_HOST("-------------------------------");
  HYTLASS_TRACE_HOST("hute::get<I...> on hute::tuple");
  HYTLASS_TRACE_HOST("-------------------------------");

  using inner_tuple_type = hute::tuple<int, ConvertibleTo<size_t>, IC<int, 43>>;
  using outer_tuple_type = hute::tuple<IC<int, 40>, inner_tuple_type, size_t>;

  inner_tuple_type t0_inner{ 41, size_t(42u), IC<int, 43>{} };
  outer_tuple_type t0_outer{ IC<int, 40>{}, t0_inner, size_t(44u) };

        [[maybe_unused]] auto t0_outer_0 = hute::get<0>(t0_outer);
        static_assert(std::is_same_v<decltype(t0_outer_0), IC<int, 40>>);

        [[maybe_unused]] auto t0_outer_1 = hute::get<1>(t0_outer);
        static_assert(std::is_same_v<decltype(t0_outer_1), inner_tuple_type>);

        [[maybe_unused]] auto t0_outer_2 = hute::get<2>(t0_outer);
        static_assert(std::is_same_v<decltype(t0_outer_2), size_t>);
        assert(t0_outer_2 == size_t(44u));

  // Leftmost index is innermost in the nexted get sequence.
  [[maybe_unused]] auto t0_outer_10 = hute::get<1, 0>(t0_outer);
  static_assert(std::is_same_v<decltype(t0_outer_10), int>);
  assert(t0_outer_10 == 41);
}