@@ -258,44 +258,49 @@ public void ImportGraphDef()
258258 EXPECT_EQ ( 0 , neg . NumControlOutputs ) ;
259259 EXPECT_EQ ( 0 , neg . GetControlOutputs ( ) . Length ) ;
260260
261- // Import it again, with an input mapping, return outputs, and a return
262- // operation, into the same graph.
263- IntPtr results ;
264- using ( var opts = c_api . TF_NewImportGraphDefOptions ( ) )
261+ static SafeImportGraphDefResultsHandle ImportGraph ( Status s , Graph graph , Buffer graph_def , Operation scalar )
265262 {
263+ using var opts = c_api . TF_NewImportGraphDefOptions ( ) ;
266264 c_api . TF_ImportGraphDefOptionsSetPrefix ( opts , "imported2" ) ;
267265 c_api . TF_ImportGraphDefOptionsAddInputMapping ( opts , "scalar" , 0 , new TF_Output ( scalar , 0 ) ) ;
268266 c_api . TF_ImportGraphDefOptionsAddReturnOutput ( opts , "feed" , 0 ) ;
269267 c_api . TF_ImportGraphDefOptionsAddReturnOutput ( opts , "scalar" , 0 ) ;
270268 EXPECT_EQ ( 2 , c_api . TF_ImportGraphDefOptionsNumReturnOutputs ( opts ) ) ;
271269 c_api . TF_ImportGraphDefOptionsAddReturnOperation ( opts , "scalar" ) ;
272270 EXPECT_EQ ( 1 , c_api . TF_ImportGraphDefOptionsNumReturnOperations ( opts ) ) ;
273- results = c_api . TF_GraphImportGraphDefWithResults ( graph , graph_def . Handle , opts , s . Handle ) ;
271+ var results = c_api . TF_GraphImportGraphDefWithResults ( graph , graph_def . Handle , opts , s . Handle ) ;
274272 EXPECT_EQ ( TF_Code . TF_OK , s . Code ) ;
275- }
276-
277- Operation scalar2 = graph . OperationByName ( "imported2/scalar" ) ;
278- Operation feed2 = graph . OperationByName ( "imported2/feed" ) ;
279- Operation neg2 = graph . OperationByName ( "imported2/neg" ) ;
280-
281- // Check input mapping
282- neg_input = neg . Input ( 0 ) ;
283- EXPECT_EQ ( scalar , neg_input . oper ) ;
284- EXPECT_EQ ( 0 , neg_input . index ) ;
285273
286- // Check return outputs
287- var return_outputs = graph . ReturnOutputs ( results ) ;
288- ASSERT_EQ ( 2 , return_outputs . Length ) ;
289- EXPECT_EQ ( feed2 , return_outputs [ 0 ] . oper ) ;
290- EXPECT_EQ ( 0 , return_outputs [ 0 ] . index ) ;
291- EXPECT_EQ ( scalar , return_outputs [ 1 ] . oper ) ; // remapped
292- EXPECT_EQ ( 0 , return_outputs [ 1 ] . index ) ;
274+ return results ;
275+ }
293276
294- // Check return operation
295- var return_opers = graph . ReturnOperations ( results ) ;
296- ASSERT_EQ ( 1 , return_opers . Length ) ;
297- EXPECT_EQ ( scalar2 , return_opers [ 0 ] ) ; // not remapped
298- c_api . TF_DeleteImportGraphDefResults ( results ) ;
277+ // Import it again, with an input mapping, return outputs, and a return
278+ // operation, into the same graph.
279+ Operation feed2 ;
280+ using ( SafeImportGraphDefResultsHandle results = ImportGraph ( s , graph , graph_def , scalar ) )
281+ {
282+ Operation scalar2 = graph . OperationByName ( "imported2/scalar" ) ;
283+ feed2 = graph . OperationByName ( "imported2/feed" ) ;
284+ Operation neg2 = graph . OperationByName ( "imported2/neg" ) ;
285+
286+ // Check input mapping
287+ neg_input = neg . Input ( 0 ) ;
288+ EXPECT_EQ ( scalar , neg_input . oper ) ;
289+ EXPECT_EQ ( 0 , neg_input . index ) ;
290+
291+ // Check return outputs
292+ var return_outputs = graph . ReturnOutputs ( results ) ;
293+ ASSERT_EQ ( 2 , return_outputs . Length ) ;
294+ EXPECT_EQ ( feed2 , return_outputs [ 0 ] . oper ) ;
295+ EXPECT_EQ ( 0 , return_outputs [ 0 ] . index ) ;
296+ EXPECT_EQ ( scalar , return_outputs [ 1 ] . oper ) ; // remapped
297+ EXPECT_EQ ( 0 , return_outputs [ 1 ] . index ) ;
298+
299+ // Check return operation
300+ var return_opers = graph . ReturnOperations ( results ) ;
301+ ASSERT_EQ ( 1 , return_opers . Length ) ;
302+ EXPECT_EQ ( scalar2 , return_opers [ 0 ] ) ; // not remapped
303+ }
299304
300305 // Import again, with control dependencies, into the same graph.
301306 using ( var opts = c_api . TF_NewImportGraphDefOptions ( ) )
0 commit comments