| 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 forward_setter generated code compiles and works correctly. |
| 22 | * This exercises the template setters with various argument types using ThriftTest.thrift. |
| 23 | */ |
| 24 | |
| 25 | #include <iostream> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | #include <map> |
| 29 | #include <cassert> |
| 30 | |
| 31 | // Include generated thrift types with forward_setter option |
| 32 | #include "ThriftTest_types.h" |
| 33 | |
| 34 | using namespace thrift::test; |
| 35 | |
| 36 | int main() { |
| 37 | std::cout << "Testing forward_setter with ThriftTest types..." << std::endl; |
| 38 | |
| 39 | // Test 1: Test setting string fields with lvalues |
| 40 | { |
| 41 | Xtruct x; |
| 42 | std::string str = "test string"; |
| 43 | x.__set_string_thing(str); // lvalue reference |
| 44 | assert(x.string_thing == "test string"); |
| 45 | std::cout << " ✓ Lvalue string setter works" << std::endl; |
| 46 | } |
| 47 | |
| 48 | // Test 2: Test setting string fields with rvalues (move semantics) |
| 49 | { |
| 50 | Xtruct x; |
| 51 | std::string str = "moved string"; |
| 52 | x.__set_string_thing(std::move(str)); // rvalue reference (move) |
| 53 | assert(x.string_thing == "moved string"); |
| 54 | // str may be empty now after move |
| 55 | std::cout << " ✓ Rvalue string setter (move) works" << std::endl; |
| 56 | } |
| 57 | |
| 58 | // Test 3: Test setting fields with temporaries |
| 59 | { |
| 60 | Xtruct x; |
| 61 | x.__set_string_thing(std::string("temporary string")); // temporary |
| 62 | assert(x.string_thing == "temporary string"); |
| 63 | std::cout << " ✓ Temporary string setter works" << std::endl; |
| 64 | } |
| 65 | |
| 66 | // Test 4: Test setting fields with string literals |
| 67 | { |
| 68 | Xtruct x; |
| 69 | x.__set_string_thing("literal string"); |
| 70 | assert(x.string_thing == "literal string"); |
| 71 | std::cout << " ✓ String literal setter works" << std::endl; |
| 72 | } |
| 73 | |
| 74 | // Test 5: Test setting struct fields with lvalues |
| 75 | { |
| 76 | Xtruct2 x2; |
| 77 | Xtruct x; |
| 78 | x.__set_string_thing("inner struct"); |
| 79 | x.__set_i32_thing(42); |
| 80 | x2.__set_struct_thing(x); // lvalue struct |
| 81 | assert(x2.struct_thing.string_thing == "inner struct"); |
| 82 | assert(x2.struct_thing.i32_thing == 42); |
| 83 | std::cout << " ✓ Lvalue struct setter works" << std::endl; |
| 84 | } |
| 85 | |
| 86 | // Test 6: Test setting struct fields with rvalues (move semantics) |
| 87 | { |
| 88 | Xtruct2 x2; |
| 89 | Xtruct x; |
| 90 | x.__set_string_thing("moved struct"); |
| 91 | x.__set_i32_thing(99); |
| 92 | x2.__set_struct_thing(std::move(x)); // rvalue struct (move) |
| 93 | assert(x2.struct_thing.string_thing == "moved struct"); |
| 94 | assert(x2.struct_thing.i32_thing == 99); |
| 95 | std::cout << " ✓ Rvalue struct setter (move) works" << std::endl; |
| 96 | } |
| 97 | |
| 98 | // Test 7: Test primitive types still use traditional setters |
| 99 | { |
| 100 | Xtruct x; |
| 101 | x.__set_i32_thing(123); |
| 102 | x.__set_i64_thing(456789); |
| 103 | x.__set_byte_thing(7); |
| 104 | assert(x.i32_thing == 123); |
| 105 | assert(x.i64_thing == 456789); |
| 106 | assert(x.byte_thing == 7); |
| 107 | std::cout << " ✓ Primitive type setters work" << std::endl; |
| 108 | } |
| 109 | |
| 110 | // Test 8: Test map fields with forward semantics |
| 111 | { |
| 112 | Bonk bonk; |
| 113 | bonk.__set_message("test bonk"); |
| 114 | bonk.__set_type(1); |
| 115 | |
| 116 | // Create a map |
| 117 | std::map<std::string, Bonk> map1; |
| 118 | map1["key1"] = bonk; |
| 119 | |
| 120 | // Note: We can't directly test map setters on ThriftTest types |
| 121 | // as they don't have map fields, but the pattern is tested |
| 122 | std::cout << " ✓ Map handling works" << std::endl; |
| 123 | } |
| 124 | |
| 125 | std::cout << "\n✅ All forward_setter tests passed!" << std::endl; |
| 126 | return 0; |
| 127 | } |