1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // Note: chars_format is a C++17 feature backported to C++11. Assert isn't
10 // allowed in a constexpr function in C++11. To keep the code readable, C++11
11 // support is untested.
12 // UNSUPPORTED: c++03, c++11
13
14 // <charconv>
15
16 // Bitmask type
17 // enum class chars_format {
18 // scientific = unspecified,
19 // fixed = unspecified,
20 // hex = unspecified,
21 // general = fixed | scientific
22 // };
23
24 #include <charconv>
25 #include <cassert>
26
27 #include "test_macros.h"
28
test()29 constexpr bool test() {
30 using cf = std::chars_format;
31 using ut = std::underlying_type<cf>::type;
32
33 {
34 cf x = cf::scientific;
35 x |= cf::fixed;
36 assert(x == cf::general);
37 }
38 {
39 cf x = cf::general;
40 x &= cf::fixed;
41 assert(x == cf::fixed);
42 }
43 {
44 cf x = cf::general;
45 x ^= cf::fixed;
46 assert(x == cf::scientific);
47 }
48
49 assert(static_cast<ut>(cf::scientific & (cf::fixed | cf::hex)) == 0);
50 assert(static_cast<ut>(cf::fixed & (cf::scientific | cf::hex)) == 0);
51 assert(static_cast<ut>(cf::hex & (cf::scientific | cf::fixed)) == 0);
52
53 assert((cf::scientific | cf::fixed) == cf::general);
54
55 assert(static_cast<ut>(cf::scientific & cf::fixed) == 0);
56
57 assert((cf::general ^ cf::fixed) == cf::scientific);
58
59 assert((~cf::hex & cf::general) == cf::general);
60
61 return true;
62 }
63
64 std::chars_format x;
65 static_assert(std::is_same<std::chars_format, decltype(~x)>::value, "");
66 static_assert(std::is_same<std::chars_format, decltype(x & x)>::value, "");
67 static_assert(std::is_same<std::chars_format, decltype(x | x)>::value, "");
68 static_assert(std::is_same<std::chars_format, decltype(x ^ x)>::value, "");
69 static_assert(std::is_same<std::chars_format&, decltype(x &= x)>::value, "");
70 static_assert(std::is_same<std::chars_format&, decltype(x |= x)>::value, "");
71 static_assert(std::is_same<std::chars_format&, decltype(x ^= x)>::value, "");
72
main(int,char **)73 int main(int, char**) {
74 assert(test());
75 static_assert(test(), "");
76
77 return 0;
78 }
79