blob: 673d6fb53e11195a7a3355a9b0674a558aab00c5 [file] [log] [blame]
Copilot1e09a042026-01-29 10:36:28 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * Test file to verify that pure_enums=enum_class generated code compiles and works correctly.
22 * This exercises the enum_class option using ThriftTest types.
23 */
24
25#include <iostream>
copilot-swe-agent[bot]c3cdacf2026-02-09 21:30:16 +000026#include <sstream>
Copilot1e09a042026-01-29 10:36:28 -080027#include <cassert>
28#include <type_traits>
copilot-swe-agent[bot]c3cdacf2026-02-09 21:30:16 +000029#include <string>
Copilot1e09a042026-01-29 10:36:28 -080030
31// Include generated thrift types with enum_class option
32#include "ThriftTest_types.h"
33
34using namespace thrift::test;
35
36int main() {
37 std::cout << "Testing pure_enums=enum_class with ThriftTest types..." << std::endl;
38
39 // Compile-time verification that Numberz is an enum class
40 static_assert(std::is_enum<Numberz>::value, "Numberz should be an enum type");
41 // enum class doesn't implicitly convert to int, which is a key characteristic
42 static_assert(!std::is_convertible<Numberz, int>::value,
43 "Numberz should be enum class (not implicitly convertible to int)");
44 std::cout << " ✓ Compile-time verification: Numberz is enum class" << std::endl;
45
46 // Test 1: Verify enum class can be used with scoped names
47 {
48 Numberz num = Numberz::ONE;
49 assert(static_cast<int>(num) == 1);
50 std::cout << " ✓ Enum class scoped access works (Numberz::ONE)" << std::endl;
51 }
52
53 // Test 2: Verify different enum values
54 {
55 Numberz two = Numberz::TWO;
56 Numberz five = Numberz::FIVE;
57 assert(static_cast<int>(two) == 2);
58 assert(static_cast<int>(five) == 5);
59 std::cout << " ✓ Multiple enum class values work" << std::endl;
60 }
61
62 // Test 3: Verify enum class comparison
63 {
64 Numberz a = Numberz::THREE;
65 Numberz b = Numberz::THREE;
66 Numberz c = Numberz::FIVE;
67 assert(a == b);
68 assert(a != c);
69 std::cout << " ✓ Enum class comparison works" << std::endl;
70 }
71
72 // Test 4: Verify enum class in switch statement
73 {
74 Numberz num = Numberz::EIGHT;
75 bool found = false;
76 switch(num) {
77 case Numberz::ONE:
78 break;
79 case Numberz::TWO:
80 break;
81 case Numberz::EIGHT:
82 found = true;
83 break;
84 default:
85 break;
86 }
87 assert(found);
88 std::cout << " ✓ Enum class in switch statements works" << std::endl;
89 }
90
copilot-swe-agent[bot]c3cdacf2026-02-09 21:30:16 +000091 // Test 5: Verify to_string() works with enum class
92 {
93 Numberz one = Numberz::ONE;
94 Numberz five = Numberz::FIVE;
95 Numberz eight = Numberz::EIGHT;
96
97 std::string str_one = to_string(one);
98 std::string str_five = to_string(five);
99 std::string str_eight = to_string(eight);
100
101 assert(str_one == "ONE");
102 assert(str_five == "FIVE");
103 assert(str_eight == "EIGHT");
104 std::cout << " ✓ to_string() with enum class works (ONE, FIVE, EIGHT)" << std::endl;
105 }
106
107 // Test 6: Verify operator<< works with enum class
108 {
109 Numberz two = Numberz::TWO;
110 Numberz three = Numberz::THREE;
111
112 std::ostringstream oss;
113 oss << two << " and " << three;
114
115 std::string result = oss.str();
116 assert(result == "TWO and THREE");
117 std::cout << " ✓ operator<< with enum class works (TWO and THREE)" << std::endl;
118 }
119
120 // Test 7: Verify to_string() for invalid/cast enum values
121 {
122 // Cast an invalid value to enum (edge case testing)
123 Numberz invalid = static_cast<Numberz>(999);
124 std::string str_invalid = to_string(invalid);
125
126 // Should fall back to numeric representation
127 assert(str_invalid == "999");
128 std::cout << " ✓ to_string() handles invalid enum values (999)" << std::endl;
129 }
130
131 // Test 8: Verify operator<< for invalid/cast enum values
132 {
133 Numberz invalid = static_cast<Numberz>(777);
134 std::ostringstream oss;
135 oss << invalid;
136
137 std::string result = oss.str();
138 assert(result == "777");
139 std::cout << " ✓ operator<< handles invalid enum values (777)" << std::endl;
140 }
141
142 // Test 9: Verify enum class with zero value
143 {
144 Numberz zero = static_cast<Numberz>(0);
145 std::string str_zero = to_string(zero);
146
147 std::ostringstream oss;
148 oss << zero;
149
150 // Both should output "0" since there's no named value
151 assert(str_zero == "0");
152 assert(oss.str() == "0");
153 std::cout << " ✓ to_string() and operator<< work with zero value" << std::endl;
154 }
155
156 // Test 10: Verify all Numberz enum values can be converted to string
157 {
158 std::ostringstream oss;
159 oss << Numberz::ONE << ", "
160 << Numberz::TWO << ", "
161 << Numberz::THREE << ", "
162 << Numberz::FIVE << ", "
163 << Numberz::SIX << ", "
164 << Numberz::EIGHT;
165
166 std::string result = oss.str();
167 assert(result == "ONE, TWO, THREE, FIVE, SIX, EIGHT");
168 std::cout << " ✓ All Numberz enum values stream correctly" << std::endl;
169 }
170
Copilot1e09a042026-01-29 10:36:28 -0800171 std::cout << "\n✅ All pure_enums=enum_class tests passed!" << std::endl;
172 std::cout << " Verified at compile-time: enum class properties enforced" << std::endl;
copilot-swe-agent[bot]c3cdacf2026-02-09 21:30:16 +0000173 std::cout << " Verified at runtime: to_string(), operator<< work correctly" << std::endl;
Copilot1e09a042026-01-29 10:36:28 -0800174 return 0;
175}