tqchen commented on code in PR #399:
URL: https://github.com/apache/tvm-ffi/pull/399#discussion_r2760376696
##########
include/tvm/ffi/function_details.h:
##########
@@ -67,6 +72,19 @@ static constexpr bool ArgSupported =
std::is_same_v<std::remove_const_t<std::remove_reference_t<T>>, AnyView>
||
TypeTraitsNoCR<T>::convert_enabled));
+template <typename T>
+struct is_expected : std::false_type {
+ using value_type = void;
+};
+
+template <typename T>
+struct is_expected<Expected<T>> : std::true_type {
+ using value_type = T;
+};
+
+template <typename T>
+inline constexpr bool is_expected_v = is_expected<T>::value;
Review Comment:
I think i think it is worthwhile minimizing the code mainly because
is_expected_v may be a future std type, the test uscase seems to be a
duplication, so still good to remove here
##########
tests/cpp/test_expected.cc:
##########
@@ -0,0 +1,347 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <gtest/gtest.h>
+#include <tvm/ffi/any.h>
+#include <tvm/ffi/container/array.h>
+#include <tvm/ffi/error.h>
+#include <tvm/ffi/expected.h>
+#include <tvm/ffi/function.h>
+#include <tvm/ffi/memory.h>
+#include <tvm/ffi/optional.h>
+#include <tvm/ffi/reflection/registry.h>
+
+#include "./testing_object.h"
+
+namespace {
+
+using namespace tvm::ffi;
+using namespace tvm::ffi::testing;
+
+// Test implicit construction from success value
+TEST(Expected, BasicOk) {
+ Expected<int> result = 42;
+
+ EXPECT_TRUE(result.is_ok());
+ EXPECT_FALSE(result.is_err());
+ EXPECT_TRUE(result.has_value());
+ EXPECT_EQ(result.value(), 42);
+ EXPECT_EQ(result.value_or(0), 42);
+}
+
+// Test implicit construction from error
+TEST(Expected, BasicErr) {
+ Expected<int> result = Error("RuntimeError", "test error", "");
+
+ EXPECT_FALSE(result.is_ok());
+ EXPECT_TRUE(result.is_err());
+ EXPECT_FALSE(result.has_value());
+
+ Error err = result.error();
+ EXPECT_EQ(err.kind(), "RuntimeError");
+ EXPECT_EQ(err.message(), "test error");
+}
+
+// Test explicit construction via Unexpected wrapper
+TEST(Expected, UnexpectedWrapper) {
+ Expected<int> result = Unexpected(Error("RuntimeError", "unexpected error",
""));
+
+ EXPECT_FALSE(result.is_ok());
+ EXPECT_TRUE(result.is_err());
+ EXPECT_EQ(result.error().kind(), "RuntimeError");
+ EXPECT_EQ(result.error().message(), "unexpected error");
+}
+
+// Test Unexpected deduction guide and error() accessor
+TEST(Expected, UnexpectedAPI) {
+ auto u = Unexpected(Error("TypeError", "type mismatch", ""));
+ static_assert(std::is_same_v<decltype(u), Unexpected<Error>>);
+
+ EXPECT_EQ(u.error().kind(), "TypeError");
+ EXPECT_EQ(u.error().message(), "type mismatch");
+
+ Error moved_err = std::move(u).error();
+ EXPECT_EQ(moved_err.kind(), "TypeError");
+}
+
+// Test value_or with error
+TEST(Expected, ValueOrWithError) {
+ Expected<int> result = Error("RuntimeError", "test error", "");
+ EXPECT_EQ(result.value_or(99), 99);
+}
+
+// Test with ObjectRef types
+TEST(Expected, ObjectRefType) {
+ Expected<TInt> result = TInt(123);
+
+ EXPECT_TRUE(result.is_ok());
+ EXPECT_EQ(result.value()->value, 123);
+}
+
+// Test with String type
+TEST(Expected, StringType) {
+ Expected<String> result = String("hello");
+
+ EXPECT_TRUE(result.is_ok());
+ EXPECT_EQ(result.value(), "hello");
+
+ Expected<String> err_result = Error("ValueError", "bad string", "");
+ EXPECT_TRUE(err_result.is_err());
+}
+
+// Test TypeTraits conversion: Expected -> Any -> Expected
+TEST(Expected, TypeTraitsRoundtrip) {
+ Expected<int> original = 42;
+
+ // Convert to Any (should unwrap to int)
+ Any any_value = original;
+ EXPECT_EQ(any_value.cast<int>(), 42);
+
+ // Convert back to Expected (should reconstruct as Ok)
+ Expected<int> recovered = any_value.cast<Expected<int>>();
+ EXPECT_TRUE(recovered.is_ok());
+ EXPECT_EQ(recovered.value(), 42);
+}
+
+// Test TypeTraits conversion with Error
+TEST(Expected, TypeTraitsErrorRoundtrip) {
+ Expected<int> original = Error("TypeError", "conversion failed", "");
+
+ // Convert to Any (should unwrap to Error)
+ Any any_value = original;
+ EXPECT_TRUE(any_value.as<Error>().has_value());
+
+ // Convert back to Expected (should reconstruct as Err)
+ Expected<int> recovered = any_value.cast<Expected<int>>();
+ EXPECT_TRUE(recovered.is_err());
+ EXPECT_EQ(recovered.error().kind(), "TypeError");
+}
+
+// Test move semantics
+TEST(Expected, MoveSemantics) {
+ Expected<String> result = String("test");
+ EXPECT_TRUE(result.is_ok());
+
+ String value = std::move(result).value();
+ EXPECT_EQ(value, "test");
+}
+
+// Test CallExpected with normal function
+TEST(Expected, CallExpectedNormal) {
+ auto safe_add = [](int a, int b) { return a + b; };
+
+ Function func = Function::FromTyped(safe_add);
+ Expected<int> result = func.CallExpected<int>(5, 3);
+
+ EXPECT_TRUE(result.is_ok());
+ EXPECT_EQ(result.value(), 8);
+}
+
+// Test CallExpected with throwing function
+TEST(Expected, CallExpectedThrowing) {
+ auto throwing_func = [](int a) -> int {
+ if (a < 0) {
+ TVM_FFI_THROW(ValueError) << "Negative value not allowed";
+ }
+ return a * 2;
+ };
+
+ Function func = Function::FromTyped(throwing_func);
+
+ // Normal case
+ Expected<int> result_ok = func.CallExpected<int>(5);
+ EXPECT_TRUE(result_ok.is_ok());
+ EXPECT_EQ(result_ok.value(), 10);
+
+ // Error case
+ Expected<int> result_err = func.CallExpected<int>(-1);
+ EXPECT_TRUE(result_err.is_err());
+ EXPECT_EQ(result_err.error().kind(), "ValueError");
+}
+
+// Test that lambda returning Expected works directly
+TEST(Expected, LambdaDirectCall) {
+ auto safe_divide = [](int a, int b) -> Expected<int> {
+ if (b == 0) {
+ return Error("ValueError", "Division by zero", "");
+ }
+ return a / b;
+ };
+
+ // Direct call to lambda should work
+ Expected<int> result = safe_divide(10, 2);
+ EXPECT_TRUE(result.is_ok());
+ EXPECT_EQ(result.value(), 5);
+
+ // Check the value can be extracted
+ int val = result.value();
+ EXPECT_EQ(val, 5);
+
+ // Check assigning to Any works
+ Any any_val = result.value();
+ EXPECT_EQ(any_val.cast<int>(), 5);
+}
+
+// Test registering function that returns Expected
+TEST(Expected, RegisterExpectedReturning) {
+ auto safe_divide = [](int a, int b) -> Expected<int> {
+ if (b == 0) {
+ return Error("ValueError", "Division by zero", "");
+ }
+ return a / b;
+ };
+
+ // Verify the FunctionInfo extracts Expected<int> as return type
+ using FuncInfo = tvm::ffi::details::FunctionInfo<decltype(safe_divide)>;
+ static_assert(std::is_same_v<FuncInfo::RetType, Expected<int>>,
+ "Return type should be Expected<int>");
+ static_assert(tvm::ffi::details::is_expected_v<FuncInfo::RetType>,
Review Comment:
this duplicates with the is_same_v assertion on top
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]