summaryrefslogtreecommitdiff
path: root/libcxx/test/std/namespace/addressable_functions.sh.cpp
blob: 172659f439583476b2f47ee9d6737d6508dd84d4 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Make sure functions specified as being 'addressable' (their address can be
// taken in a well-defined manner) are indeed addressable. This notion was
// added by http://wg21.link/p0551. While it was technically only introduced
// in C++20, we test it in all standard modes because it's basic QOI to provide
// a consistent behavior for that across standard modes.

// RUN: %{cxx} %{flags} %{compile_flags} -c %s -o %t.tu1.o -DTU1
// RUN: %{cxx} %{flags} %{compile_flags} -c %s -o %t.tu2.o -DTU2
// RUN: %{cxx} %t.tu1.o %t.tu2.o %{flags} %{link_flags} -o %t.exe
// RUN: %{exec} %t.exe

// The functions checked below come from <iostream> & friends
// UNSUPPORTED: no-localization

#include <cassert>
#include <ios>
#include <istream>
#include <map>
#include <ostream>
#include <string>
#include <utility>

#include "test_macros.h"

typedef std::ios_base& (FormatFlagFunction)(std::ios_base&);
typedef std::basic_ostream<char>& (OstreamManipFunction)(std::basic_ostream<char>&);
typedef std::basic_istream<char>& (IstreamManipFunction)(std::basic_istream<char>&);
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
typedef std::basic_ostream<wchar_t>& (WOstreamManipFunction)(std::basic_ostream<wchar_t>&);
typedef std::basic_istream<wchar_t>& (WIstreamManipFunction)(std::basic_istream<wchar_t>&);
#endif

extern FormatFlagFunction* get_formatflag_tu1(std::string);
extern FormatFlagFunction* get_formatflag_tu2(std::string);

extern OstreamManipFunction* get_ostreammanip_tu1(std::string);
extern OstreamManipFunction* get_ostreammanip_tu2(std::string);
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
extern WOstreamManipFunction* get_wostreammanip_tu1(std::string);
extern WOstreamManipFunction* get_wostreammanip_tu2(std::string);
#endif

extern IstreamManipFunction* get_istreammanip_tu1(std::string);
extern IstreamManipFunction* get_istreammanip_tu2(std::string);
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
extern WIstreamManipFunction* get_wistreammanip_tu1(std::string);
extern WIstreamManipFunction* get_wistreammanip_tu2(std::string);
#endif

#ifdef TU1
FormatFlagFunction* get_formatflag_tu1(std::string func)
#else
FormatFlagFunction* get_formatflag_tu2(std::string func)
#endif
{
    std::map<std::string, FormatFlagFunction*> all_funcs;

    // [fmtflags.manip]
    all_funcs.insert(std::make_pair("boolalpha", &std::boolalpha));
    all_funcs.insert(std::make_pair("noboolalpha", &std::noboolalpha));
    all_funcs.insert(std::make_pair("showbase", &std::showbase));
    all_funcs.insert(std::make_pair("noshowbase", &std::noshowbase));
    all_funcs.insert(std::make_pair("showpoint", &std::showpoint));
    all_funcs.insert(std::make_pair("noshowpoint", &std::noshowpoint));
    all_funcs.insert(std::make_pair("showpos", &std::showpos));
    all_funcs.insert(std::make_pair("noshowpos", &std::noshowpos));
    all_funcs.insert(std::make_pair("skipws", &std::skipws));
    all_funcs.insert(std::make_pair("noskipws", &std::noskipws));
    all_funcs.insert(std::make_pair("uppercase", &std::uppercase));
    all_funcs.insert(std::make_pair("nouppercase", &std::nouppercase));
    all_funcs.insert(std::make_pair("unitbuf", &std::unitbuf));
    all_funcs.insert(std::make_pair("nounitbuf", &std::nounitbuf));

    // [adjustfield.manip]
    all_funcs.insert(std::make_pair("internal", &std::internal));
    all_funcs.insert(std::make_pair("left", &std::left));
    all_funcs.insert(std::make_pair("right", &std::right));

    // [basefield.manip]
    all_funcs.insert(std::make_pair("dec", &std::dec));
    all_funcs.insert(std::make_pair("hex", &std::hex));
    all_funcs.insert(std::make_pair("oct", &std::oct));

    // [floatfield.manip]
    all_funcs.insert(std::make_pair("fixed", &std::fixed));
    all_funcs.insert(std::make_pair("scientific", &std::scientific));
    all_funcs.insert(std::make_pair("hexfloat", &std::hexfloat));
    all_funcs.insert(std::make_pair("defaultfloat", &std::defaultfloat));

    return all_funcs.at(func);
}

// [ostream.manip] (char)
#ifdef TU1
OstreamManipFunction* get_ostreammanip_tu1(std::string func)
#else
OstreamManipFunction* get_ostreammanip_tu2(std::string func)
#endif
{
    std::map<std::string, OstreamManipFunction*> all_funcs;
    typedef std::char_traits<char> Traits;
    all_funcs.insert(std::make_pair("endl", &std::endl<char, Traits>));
    all_funcs.insert(std::make_pair("ends", &std::ends<char, Traits>));
    all_funcs.insert(std::make_pair("flush", &std::flush<char, Traits>));
    return all_funcs.at(func);
}

// [ostream.manip] (wchar_t)
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
#   ifdef TU1
WOstreamManipFunction* get_wostreammanip_tu1(std::string func)
#   else
WOstreamManipFunction* get_wostreammanip_tu2(std::string func)
#   endif
{
    std::map<std::string, WOstreamManipFunction*> all_funcs;
    typedef std::char_traits<wchar_t> Traits;
    all_funcs.insert(std::make_pair("endl", &std::endl<wchar_t, Traits>));
    all_funcs.insert(std::make_pair("ends", &std::ends<wchar_t, Traits>));
    all_funcs.insert(std::make_pair("flush", &std::flush<wchar_t, Traits>));
    return all_funcs.at(func);
}
#endif // TEST_HAS_NO_WIDE_CHARACTERS

// [istream.manip] (char)
#ifdef TU1
IstreamManipFunction* get_istreammanip_tu1(std::string func)
#else
IstreamManipFunction* get_istreammanip_tu2(std::string func)
#endif
{
    std::map<std::string, IstreamManipFunction*> all_funcs;
    typedef std::char_traits<char> Traits;
    all_funcs.insert(std::make_pair("ws", &std::ws<char, Traits>));
    return all_funcs.at(func);
}

// [istream.manip] (wchar_t)
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
#   ifdef TU1
WIstreamManipFunction* get_wistreammanip_tu1(std::string func)
#   else
WIstreamManipFunction* get_wistreammanip_tu2(std::string func)
#   endif
{
    std::map<std::string, WIstreamManipFunction*> all_funcs;
    typedef std::char_traits<wchar_t> Traits;
    all_funcs.insert(std::make_pair("ws", &std::ws<wchar_t, Traits>));
    return all_funcs.at(func);
}
#endif // TEST_HAS_NO_WIDE_CHARACTERS

#ifdef TU2
    int main(int, char**) {
        assert(get_formatflag_tu1("boolalpha") == get_formatflag_tu2("boolalpha"));
        assert(get_formatflag_tu1("noboolalpha") == get_formatflag_tu2("noboolalpha"));
        assert(get_formatflag_tu1("showbase") == get_formatflag_tu2("showbase"));
        assert(get_formatflag_tu1("noshowbase") == get_formatflag_tu2("noshowbase"));
        assert(get_formatflag_tu1("showpoint") == get_formatflag_tu2("showpoint"));
        assert(get_formatflag_tu1("noshowpoint") == get_formatflag_tu2("noshowpoint"));
        assert(get_formatflag_tu1("showpos") == get_formatflag_tu2("showpos"));
        assert(get_formatflag_tu1("noshowpos") == get_formatflag_tu2("noshowpos"));
        assert(get_formatflag_tu1("skipws") == get_formatflag_tu2("skipws"));
        assert(get_formatflag_tu1("noskipws") == get_formatflag_tu2("noskipws"));
        assert(get_formatflag_tu1("uppercase") == get_formatflag_tu2("uppercase"));
        assert(get_formatflag_tu1("nouppercase") == get_formatflag_tu2("nouppercase"));
        assert(get_formatflag_tu1("unitbuf") == get_formatflag_tu2("unitbuf"));
        assert(get_formatflag_tu1("nounitbuf") == get_formatflag_tu2("nounitbuf"));
        assert(get_formatflag_tu1("internal") == get_formatflag_tu2("internal"));
        assert(get_formatflag_tu1("left") == get_formatflag_tu2("left"));
        assert(get_formatflag_tu1("right") == get_formatflag_tu2("right"));
        assert(get_formatflag_tu1("dec") == get_formatflag_tu2("dec"));
        assert(get_formatflag_tu1("hex") == get_formatflag_tu2("hex"));
        assert(get_formatflag_tu1("oct") == get_formatflag_tu2("oct"));
        assert(get_formatflag_tu1("fixed") == get_formatflag_tu2("fixed"));
        assert(get_formatflag_tu1("scientific") == get_formatflag_tu2("scientific"));
        assert(get_formatflag_tu1("hexfloat") == get_formatflag_tu2("hexfloat"));
        assert(get_formatflag_tu1("defaultfloat") == get_formatflag_tu2("defaultfloat"));

        assert(get_ostreammanip_tu1("endl") == get_ostreammanip_tu2("endl"));
        assert(get_ostreammanip_tu1("ends") == get_ostreammanip_tu2("ends"));
        assert(get_ostreammanip_tu1("flush") == get_ostreammanip_tu2("flush"));

#ifndef TEST_HAS_NO_WIDE_CHARACTERS
        assert(get_wostreammanip_tu1("endl") == get_wostreammanip_tu2("endl"));
        assert(get_wostreammanip_tu1("ends") == get_wostreammanip_tu2("ends"));
        assert(get_wostreammanip_tu1("flush") == get_wostreammanip_tu2("flush"));
#endif

        assert(get_istreammanip_tu1("ws") == get_istreammanip_tu2("ws"));

#ifndef TEST_HAS_NO_WIDE_CHARACTERS
        assert(get_wistreammanip_tu1("ws") == get_wistreammanip_tu2("ws"));
#endif

        return 0;
    }
#endif