Kathryn-cat commented on code in PR #601: URL: https://github.com/apache/tvm-ffi/pull/601#discussion_r3339538884
########## tests/cpp/extra/test_structural_visit.cc: ########## @@ -0,0 +1,564 @@ +/* + * 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/container/array.h> +#include <tvm/ffi/container/dict.h> +#include <tvm/ffi/container/list.h> +#include <tvm/ffi/container/map.h> +#include <tvm/ffi/error.h> +#include <tvm/ffi/extra/structural_visit.h> +#include <tvm/ffi/object.h> +#include <tvm/ffi/reflection/accessor.h> +#include <tvm/ffi/reflection/registry.h> +#include <tvm/ffi/string.h> + +#include <stdexcept> +#include <string> +#include <utility> +#include <vector> + +namespace { + +using namespace tvm::ffi; +namespace refl = tvm::ffi::reflection; + +using FStructuralVisit = TVMFFIAny (*)(StructuralVisitorObj* visitor, const Object* value) noexcept; + +// --------------------------------------------------------------------------- +// Minimal test fixtures. +// --------------------------------------------------------------------------- + +class TLeafObj : public Object { + public: + String name; + + explicit TLeafObj(String name) : name(std::move(name)) {} + + static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode; + TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.StructuralVisit.Leaf", TLeafObj, Object); +}; + +class TLeaf : public ObjectRef { + public: + explicit TLeaf(String name) { data_ = make_object<TLeafObj>(std::move(name)); } + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TLeaf, ObjectRef, TLeafObj); +}; + +class TPairObj : public Object { + public: + ObjectRef lhs; + ObjectRef rhs; + + TPairObj(ObjectRef lhs, ObjectRef rhs) : lhs(std::move(lhs)), rhs(std::move(rhs)) {} + + static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode; + TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.StructuralVisit.Pair", TPairObj, Object); +}; + +class TPair : public ObjectRef { + public: + TPair(ObjectRef lhs, ObjectRef rhs) { + data_ = make_object<TPairObj>(std::move(lhs), std::move(rhs)); + } + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TPair, ObjectRef, TPairObj); +}; + +class TIgnoredObj : public Object { + public: + ObjectRef visible; + ObjectRef ignored; + + TIgnoredObj(ObjectRef visible, ObjectRef ignored) + : visible(std::move(visible)), ignored(std::move(ignored)) {} + + static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode; + TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.StructuralVisit.Ignored", TIgnoredObj, Object); +}; + +class TIgnored : public ObjectRef { + public: + TIgnored(ObjectRef visible, ObjectRef ignored) { + data_ = make_object<TIgnoredObj>(std::move(visible), std::move(ignored)); + } + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TIgnored, ObjectRef, TIgnoredObj); +}; + +class TDefRegionObj : public Object { + public: + ObjectRef recursive; + ObjectRef plain_after_recursive; + ObjectRef non_recursive; + ObjectRef plain_after_non_recursive; + + TDefRegionObj(ObjectRef recursive, ObjectRef plain_after_recursive, ObjectRef non_recursive, + ObjectRef plain_after_non_recursive) + : recursive(std::move(recursive)), + plain_after_recursive(std::move(plain_after_recursive)), + non_recursive(std::move(non_recursive)), + plain_after_non_recursive(std::move(plain_after_non_recursive)) {} + + static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode; + TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.StructuralVisit.DefRegion", TDefRegionObj, Object); +}; + +class TDefRegion : public ObjectRef { + public: + TDefRegion(ObjectRef recursive, ObjectRef plain_after_recursive, ObjectRef non_recursive, + ObjectRef plain_after_non_recursive) { + data_ = + make_object<TDefRegionObj>(std::move(recursive), std::move(plain_after_recursive), + std::move(non_recursive), std::move(plain_after_non_recursive)); + } + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TDefRegion, ObjectRef, TDefRegionObj); +}; + +class TOverrideObj : public Object { + public: + ObjectRef child; + + explicit TOverrideObj(ObjectRef child) : child(std::move(child)) {} + + static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode; + TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.StructuralVisit.Override", TOverrideObj, Object); +}; + +class TOverride : public ObjectRef { + public: + explicit TOverride(ObjectRef child) { data_ = make_object<TOverrideObj>(std::move(child)); } + TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TOverride, ObjectRef, TOverrideObj); +}; + +class ProbeVisitorObj : public StructuralVisitorObj { + public: + ProbeVisitorObj() : StructuralVisitorObj(VTable()) {} + + std::vector<ObjectRef> visited; + std::vector<TVMFFIDefRegionKind> modes; + ObjectRef interrupt_on; + String interrupt_payload = "stop"; Review Comment: I've significantly reduced the `test_structural_visit.cc` test cases, and moved objects to `testing_object.h`. I'd like to keep `TestVisitorObj` in the test file because it is useful for helping to check visited nodes, def region kinds, and interrupt behavior. This is a useful fixture I think. -- 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]
