|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <olp/core/porting/any.h> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +TEST(AnyTest, AnyCastConstReference) { |
| 25 | + // Test any_cast with const reference (covers line 84 in any.h) |
| 26 | + const olp::porting::any const_any = std::string("test_value"); |
| 27 | + |
| 28 | + auto result = olp::porting::any_cast<std::string>(const_any); |
| 29 | + EXPECT_EQ("test_value", result); |
| 30 | +} |
| 31 | + |
| 32 | +TEST(AnyTest, AnyCastNonConstReference) { |
| 33 | + // Test any_cast with non-const reference |
| 34 | + olp::porting::any any_obj = std::string("test_value"); |
| 35 | + |
| 36 | + auto result = olp::porting::any_cast<std::string>(any_obj); |
| 37 | + EXPECT_EQ("test_value", result); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(AnyTest, AnyCastRValueReference) { |
| 41 | + // Test any_cast with rvalue reference |
| 42 | + auto result = olp::porting::any_cast<std::string>( |
| 43 | + olp::porting::any(std::string("test_value"))); |
| 44 | + EXPECT_EQ("test_value", result); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(AnyTest, AnyCastConstPointer) { |
| 48 | + // Test any_cast with const pointer |
| 49 | + const olp::porting::any any_obj = std::string("test_value"); |
| 50 | + |
| 51 | + const std::string* ptr = olp::porting::any_cast<std::string>(&any_obj); |
| 52 | + ASSERT_NE(nullptr, ptr); |
| 53 | + EXPECT_EQ("test_value", *ptr); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(AnyTest, AnyCastNonConstPointer) { |
| 57 | + // Test any_cast with non-const pointer |
| 58 | + olp::porting::any any_obj = std::string("test_value"); |
| 59 | + |
| 60 | + std::string* ptr = olp::porting::any_cast<std::string>(&any_obj); |
| 61 | + ASSERT_NE(nullptr, ptr); |
| 62 | + EXPECT_EQ("test_value", *ptr); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(AnyTest, HasValue) { |
| 66 | + // Test has_value function |
| 67 | + olp::porting::any empty_any; |
| 68 | + EXPECT_FALSE(olp::porting::has_value(empty_any)); |
| 69 | + |
| 70 | + olp::porting::any filled_any = std::string("test"); |
| 71 | + EXPECT_TRUE(olp::porting::has_value(filled_any)); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(AnyTest, Reset) { |
| 75 | + // Test reset function |
| 76 | + olp::porting::any any_obj = std::string("test_value"); |
| 77 | + EXPECT_TRUE(olp::porting::has_value(any_obj)); |
| 78 | + |
| 79 | + olp::porting::reset(any_obj); |
| 80 | + EXPECT_FALSE(olp::porting::has_value(any_obj)); |
| 81 | +} |
| 82 | + |
| 83 | +TEST(AnyTest, MakeAny) { |
| 84 | + // Test make_any with variadic arguments |
| 85 | + auto any_obj = olp::porting::make_any<std::string>("test"); |
| 86 | + auto result = olp::porting::any_cast<std::string>(any_obj); |
| 87 | + EXPECT_EQ("test", result); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(AnyTest, MakeAnyWithInitializerList) { |
| 91 | + // Test make_any with initializer list |
| 92 | + auto any_obj = olp::porting::make_any<std::vector<int>>({1, 2, 3, 4, 5}); |
| 93 | + auto result = olp::porting::any_cast<std::vector<int>>(any_obj); |
| 94 | + EXPECT_EQ(5u, result.size()); |
| 95 | + EXPECT_EQ(1, result[0]); |
| 96 | + EXPECT_EQ(5, result[4]); |
| 97 | +} |
0 commit comments