Test building functions with documentation using the functions dict.
()
| 197 | |
| 198 | |
| 199 | def test_build_inline_with_docstrings() -> None: |
| 200 | """Test building functions with documentation using the functions dict.""" |
| 201 | # Keep module alive until all returned objects are destroyed |
| 202 | add_docstring = ( |
| 203 | "Add two integers and return the sum.\n" |
| 204 | "\n" |
| 205 | "Parameters\n" |
| 206 | "----------\n" |
| 207 | "a : int\n" |
| 208 | " First integer\n" |
| 209 | "b : int\n" |
| 210 | " Second integer\n" |
| 211 | "\n" |
| 212 | "Returns\n" |
| 213 | "-------\n" |
| 214 | "result : int\n" |
| 215 | " Sum of a and b" |
| 216 | ) |
| 217 | |
| 218 | divide_docstring = "Divides two floats. Returns a/b." |
| 219 | |
| 220 | mod: Module = tvm_ffi.cpp.load_inline( |
| 221 | name="test_docs", |
| 222 | cpp_sources=r""" |
| 223 | int add(int a, int b) { |
| 224 | return a + b; |
| 225 | } |
| 226 | |
| 227 | int subtract(int a, int b) { |
| 228 | return a - b; |
| 229 | } |
| 230 | |
| 231 | float divide(float a, float b) { |
| 232 | TVM_FFI_ICHECK(b != 0.0f) << "Division by zero"; |
| 233 | return a / b; |
| 234 | } |
| 235 | """, |
| 236 | functions={ |
| 237 | "add": add_docstring, |
| 238 | "subtract": "", # No documentation |
| 239 | "divide": divide_docstring, |
| 240 | }, |
| 241 | extra_cflags=["-DTVM_FFI_DLL_EXPORT_INCLUDE_METADATA=1"], |
| 242 | ) |
| 243 | |
| 244 | # Test add function with full documentation |
| 245 | assert mod.add(10, 5) == 15 |
| 246 | metadata = mod.get_function_metadata("add") |
| 247 | assert metadata is not None |
| 248 | schema = TypeSchema.from_json_str(metadata["type_schema"]) |
| 249 | assert str(schema) == "Callable[[int, int], int]" |
| 250 | |
| 251 | doc = mod.get_function_doc("add") |
| 252 | assert doc is not None, "add should have documentation" |
| 253 | assert doc == add_docstring |
| 254 | |
| 255 | # Test subtract function without documentation |
| 256 | assert mod.subtract(10, 5) == 5 |
nothing calls this directly
no test coverage detected