| Copilot | 1e09a04 | 2026-01-29 10:36:28 -0800 | [diff] [blame^] | 1 | /* |
| 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> |
| 26 | #include <cassert> |
| 27 | #include <type_traits> |
| 28 | |
| 29 | // Include generated thrift types with enum_class option |
| 30 | #include "ThriftTest_types.h" |
| 31 | |
| 32 | using namespace thrift::test; |
| 33 | |
| 34 | int main() { |
| 35 | std::cout << "Testing pure_enums=enum_class with ThriftTest types..." << std::endl; |
| 36 | |
| 37 | // Compile-time verification that Numberz is an enum class |
| 38 | static_assert(std::is_enum<Numberz>::value, "Numberz should be an enum type"); |
| 39 | // enum class doesn't implicitly convert to int, which is a key characteristic |
| 40 | static_assert(!std::is_convertible<Numberz, int>::value, |
| 41 | "Numberz should be enum class (not implicitly convertible to int)"); |
| 42 | std::cout << " ✓ Compile-time verification: Numberz is enum class" << std::endl; |
| 43 | |
| 44 | // Test 1: Verify enum class can be used with scoped names |
| 45 | { |
| 46 | Numberz num = Numberz::ONE; |
| 47 | assert(static_cast<int>(num) == 1); |
| 48 | std::cout << " ✓ Enum class scoped access works (Numberz::ONE)" << std::endl; |
| 49 | } |
| 50 | |
| 51 | // Test 2: Verify different enum values |
| 52 | { |
| 53 | Numberz two = Numberz::TWO; |
| 54 | Numberz five = Numberz::FIVE; |
| 55 | assert(static_cast<int>(two) == 2); |
| 56 | assert(static_cast<int>(five) == 5); |
| 57 | std::cout << " ✓ Multiple enum class values work" << std::endl; |
| 58 | } |
| 59 | |
| 60 | // Test 3: Verify enum class comparison |
| 61 | { |
| 62 | Numberz a = Numberz::THREE; |
| 63 | Numberz b = Numberz::THREE; |
| 64 | Numberz c = Numberz::FIVE; |
| 65 | assert(a == b); |
| 66 | assert(a != c); |
| 67 | std::cout << " ✓ Enum class comparison works" << std::endl; |
| 68 | } |
| 69 | |
| 70 | // Test 4: Verify enum class in switch statement |
| 71 | { |
| 72 | Numberz num = Numberz::EIGHT; |
| 73 | bool found = false; |
| 74 | switch(num) { |
| 75 | case Numberz::ONE: |
| 76 | break; |
| 77 | case Numberz::TWO: |
| 78 | break; |
| 79 | case Numberz::EIGHT: |
| 80 | found = true; |
| 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | assert(found); |
| 86 | std::cout << " ✓ Enum class in switch statements works" << std::endl; |
| 87 | } |
| 88 | |
| 89 | std::cout << "\n✅ All pure_enums=enum_class tests passed!" << std::endl; |
| 90 | std::cout << " Verified at compile-time: enum class properties enforced" << std::endl; |
| 91 | return 0; |
| 92 | } |