44namespace OpenCvSharp . Tests . Core ;
55
66/// <summary>
7- /// PROTOTYPE proof for the ref-struct InputArray/OutputArray redesign: one function
8- /// (<see cref="Cv2.TransposeRef"/>) wired through the handle+kind proxy path. Verifies numeric
9- /// correctness against the existing class-based path and, crucially, that repeated calls allocate
10- /// nothing on the managed heap (the implicit-conversion temporaries live on the stack and the
11- /// native _InputArray is built extern-side).
7+ /// Verifies the ref-struct InputArray/OutputArray design (issue #1976): implicit conversions from
8+ /// Mat/UMat/MatExpr/Scalar/double/Vec are allocation-free, and the explicit Create(...) factory
9+ /// methods behave the same as their implicit-conversion equivalents.
1210/// </summary>
1311public class InputArrayRefTest : TestBase
1412{
@@ -20,137 +18,49 @@ private static Mat Float3x3(params float[] v)
2018 }
2119
2220 [ Fact ]
23- public void TransposeRef_MatchesClassBasedTranspose ( )
24- {
25- using var src = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
26-
27- using var actual = new Mat ( ) ;
28- Cv2 . TransposeRef ( src , actual ) ; // Mat -> InputArrayRef/OutputArrayRef (ref struct)
29-
30- using var expected = new Mat ( ) ;
31- Cv2 . Transpose ( src , expected ) ; // existing class-based InputArray/OutputArray path
32-
33- ImageEquals ( expected , actual ) ;
34- }
35-
36- [ Fact ]
37- public void TransposeRef_IsAllocationFree ( )
21+ public void Transpose_IsAllocationFree ( )
3822 {
3923 using var src = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
4024 using var dst = new Mat ( ) ;
4125
42- // Warm up JIT / one-time setup.
43- Cv2 . TransposeRef ( src , dst ) ;
26+ Cv2 . Transpose ( src , dst ) ; // warmup
4427
4528 var before = GC . GetAllocatedBytesForCurrentThread ( ) ;
4629 for ( var i = 0 ; i < 1000 ; i ++ )
47- Cv2 . TransposeRef ( src , dst ) ;
30+ Cv2 . Transpose ( src , dst ) ;
4831 var after = GC . GetAllocatedBytesForCurrentThread ( ) ;
4932
5033 Assert . Equal ( 0 , after - before ) ;
5134 }
5235
5336 [ Fact ]
54- public void AddRef_MatPlusMat_Matches ( )
55- {
56- using var a = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
57- using var b = Float3x3 ( 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ) ;
58-
59- using var actual = new Mat ( ) ;
60- Cv2 . AddRef ( a , b , actual ) ;
61-
62- using var expected = new Mat ( ) ;
63- Cv2 . Add ( a , b , expected ) ;
64-
65- ImageEquals ( expected , actual ) ;
66- }
67-
68- [ Fact ]
69- public void AddRef_MatPlusScalar_Matches ( )
70- {
71- using var src = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
72- var s = new Scalar ( 10 ) ;
73-
74- using var actual = new Mat ( ) ;
75- Cv2 . AddRef ( src , s , actual ) ; // InputArrayRef(Scalar) — scalar travels inline
76-
77- using var expected = new Mat ( ) ;
78- Cv2 . Add ( src , s , expected ) ; // class path (Scalar -> InputArray)
79-
80- ImageEquals ( expected , actual ) ;
81- }
82-
83- [ Fact ]
84- public void AddRef_MatPlusDouble_Matches ( )
85- {
86- using var src = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
87-
88- using var actual = new Mat ( ) ;
89- Cv2 . AddRef ( src , 5.0 , actual ) ; // InputArrayRef(double)
90-
91- using var expected = new Mat ( ) ;
92- Cv2 . Add ( src , new Scalar ( 5.0 ) , expected ) ;
93-
94- ImageEquals ( expected , actual ) ;
95- }
96-
97- [ Fact ]
98- public void AddRef_MatPlusScalar_IsAllocationFree ( )
37+ public void Add_MatPlusScalar_IsAllocationFree ( )
9938 {
10039 using var src = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
10140 var s = new Scalar ( 10 ) ;
10241 using var dst = new Mat ( ) ;
10342
104- Cv2 . AddRef ( src , s , dst ) ; // warmup
43+ Cv2 . Add ( src , s , dst ) ; // warmup
10544
10645 var before = GC . GetAllocatedBytesForCurrentThread ( ) ;
10746 for ( var i = 0 ; i < 1000 ; i ++ )
108- Cv2 . AddRef ( src , s , dst ) ;
47+ Cv2 . Add ( src , s , dst ) ;
10948 var after = GC . GetAllocatedBytesForCurrentThread ( ) ;
11049
11150 Assert . Equal ( 0 , after - before ) ;
11251 }
11352
11453 [ Fact ]
115- public void AddRef_MatPlusMatExpr_Matches ( )
116- {
117- using var a = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
118- using var b = Float3x3 ( 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ) ;
119-
120- using var actual = new Mat ( ) ;
121- Cv2 . AddRef ( a , a + b , actual ) ; // (a + b) is a MatExpr -> InputArrayRef(MatExpr)
122-
123- using var expected = new Mat ( ) ;
124- Cv2 . Add ( a , a + b , expected ) ; // class path (MatExpr -> InputArray)
125-
126- ImageEquals ( expected , actual ) ;
127- }
128-
129- [ Fact ]
130- public void TransposeRef_Vec_Matches ( )
131- {
132- var v = new Vec3d ( 1 , 2 , 3 ) ;
133-
134- using var actual = new Mat ( ) ;
135- Cv2 . TransposeRef ( v , actual ) ; // Vec3d -> InputArrayRef(Vec), travels inline
136-
137- using var expected = new Mat ( ) ;
138- Cv2 . Transpose ( v , expected ) ; // Vec3d -> class InputArray(Vec)
139-
140- ImageEquals ( expected , actual ) ;
141- }
142-
143- [ Fact ]
144- public void TransposeRef_Vec_IsAllocationFree ( )
54+ public void Transpose_Vec_IsAllocationFree ( )
14555 {
14656 var v = new Vec3d ( 1 , 2 , 3 ) ;
14757 using var dst = new Mat ( ) ;
14858
149- Cv2 . TransposeRef ( v , dst ) ; // warmup
59+ Cv2 . Transpose ( v , dst ) ; // warmup
15060
15161 var before = GC . GetAllocatedBytesForCurrentThread ( ) ;
15262 for ( var i = 0 ; i < 1000 ; i ++ )
153- Cv2 . TransposeRef ( v , dst ) ;
63+ Cv2 . Transpose ( v , dst ) ;
15464 var after = GC . GetAllocatedBytesForCurrentThread ( ) ;
15565
15666 Assert . Equal ( 0 , after - before ) ;
@@ -175,43 +85,16 @@ public void MinMax_InputArray_WriteThrough()
17585 ImageEquals ( expectedMax , mx ) ;
17686 }
17787
178- [ Fact ]
179- public void CompleteSymmRef_Matches ( )
180- {
181- using var actual = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
182- Cv2 . CompleteSymmRef ( actual ) ; // InputOutputArrayRef (in-place)
183-
184- using var expected = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
185- Cv2 . CompleteSymm ( expected ) ; // class InputOutputArray path
186-
187- ImageEquals ( expected , actual ) ;
188- }
189-
190- [ Fact ]
191- public void CompleteSymmRef_IsAllocationFree ( )
192- {
193- using var m = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
194-
195- Cv2 . CompleteSymmRef ( m ) ; // warmup
196-
197- var before = GC . GetAllocatedBytesForCurrentThread ( ) ;
198- for ( var i = 0 ; i < 1000 ; i ++ )
199- Cv2 . CompleteSymmRef ( m ) ;
200- var after = GC . GetAllocatedBytesForCurrentThread ( ) ;
201-
202- Assert . Equal ( 0 , after - before ) ;
203- }
204-
20588 [ Fact ]
20689 public void Create_FromVec_MatchesImplicitConversion ( )
20790 {
20891 var v = new Vec3d ( 1 , 2 , 3 ) ;
20992
21093 using var actual = new Mat ( ) ;
211- Cv2 . TransposeRef ( InputArrayRef . Create ( v ) , actual ) ;
94+ Cv2 . Transpose ( InputArrayRef . Create ( v ) , actual ) ;
21295
21396 using var expected = new Mat ( ) ;
214- Cv2 . TransposeRef ( v , expected ) ;
97+ Cv2 . Transpose ( v , expected ) ;
21598
21699 ImageEquals ( expected , actual ) ;
217100 }
@@ -227,11 +110,11 @@ public void Create_From2DArray_MatchesManuallyBuiltMat()
227110 } ;
228111
229112 using var actual = new Mat ( ) ;
230- Cv2 . TransposeRef ( InputArrayRef . Create ( a ) , actual ) ;
113+ Cv2 . Transpose ( InputArrayRef . Create ( a ) , actual ) ;
231114
232115 using var src = Mat . FromPixelData ( 3 , 3 , MatType . CV_64FC1 , a ) ;
233116 using var expected = new Mat ( ) ;
234- Cv2 . TransposeRef ( src , expected ) ;
117+ Cv2 . Transpose ( src , expected ) ;
235118
236119 ImageEquals ( expected , actual ) ;
237120 }
@@ -242,11 +125,11 @@ public void Create_From1DArray_MatchesManuallyBuiltMat()
242125 double [ ] a = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;
243126
244127 using var actual = new Mat ( ) ;
245- Cv2 . TransposeRef ( InputArrayRef . Create ( a ) , actual ) ;
128+ Cv2 . Transpose ( InputArrayRef . Create ( a ) , actual ) ;
246129
247130 using var src = Mat . FromPixelData ( 6 , 1 , MatType . CV_64FC1 , a ) ;
248131 using var expected = new Mat ( ) ;
249- Cv2 . TransposeRef ( src , expected ) ;
132+ Cv2 . Transpose ( src , expected ) ;
250133
251134 ImageEquals ( expected , actual ) ;
252135 }
@@ -257,10 +140,10 @@ public void Create_FromMat_MatchesImplicitConversion()
257140 using var src = Float3x3 ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ;
258141
259142 using var actual = new Mat ( ) ;
260- Cv2 . TransposeRef ( InputArrayRef . Create ( src ) , actual ) ;
143+ Cv2 . Transpose ( InputArrayRef . Create ( src ) , actual ) ;
261144
262145 using var expected = new Mat ( ) ;
263- Cv2 . TransposeRef ( src , expected ) ;
146+ Cv2 . Transpose ( src , expected ) ;
264147
265148 ImageEquals ( expected , actual ) ;
266149 }
0 commit comments