@@ -174,4 +174,91 @@ public void ElementSelectorTransformation()
174
174
lookup [ 3 ] . ShouldBe ( new [ ] { "CCC" } ) ;
175
175
lookup [ 4 ] . ShouldBe ( new [ ] { "DDDD" } ) ;
176
176
}
177
+
178
+ [ Fact ]
179
+ public void ICollectionContains ( )
180
+ {
181
+ var xs = new [ ] { 1 , 2 , 3 , 4 , 5 } ;
182
+ var lookup = xs . AsValueEnumerable ( ) . ToLookup ( x => x ) ;
183
+
184
+ // Cast to ICollection to test the explicit interface implementation
185
+ var collection = ( ICollection < IGrouping < int , int > > ) lookup ;
186
+
187
+ // Contains should return true only when the exact same grouping instance is passed
188
+ foreach ( var group in collection )
189
+ {
190
+ collection . Contains ( group ) . ShouldBeTrue ( ) ;
191
+ }
192
+
193
+ // A different grouping with the same key should return false
194
+ var otherLookup = xs . AsValueEnumerable ( ) . ToLookup ( x => x ) ;
195
+ foreach ( var group in otherLookup )
196
+ {
197
+ collection . Contains ( group ) . ShouldBeFalse ( ) ;
198
+ }
199
+ }
200
+
201
+ [ Fact ]
202
+ public void ICollectionCopyTo ( )
203
+ {
204
+ var xs = new [ ] { 1 , 2 , 3 , 4 , 5 } ;
205
+ var lookup = xs . AsValueEnumerable ( ) . ToLookup ( x => x ) ;
206
+
207
+ // Cast to ICollection to test the explicit interface implementation
208
+ var collection = ( ICollection < IGrouping < int , int > > ) lookup ;
209
+
210
+ // Create array to copy to
211
+ var array = new IGrouping < int , int > [ collection . Count ] ;
212
+ collection . CopyTo ( array , 0 ) ;
213
+
214
+ // Verify that all elements were copied
215
+ var i = 0 ;
216
+ foreach ( var group in lookup )
217
+ {
218
+ array [ i ++ ] . ShouldBe ( group ) ;
219
+ }
220
+
221
+ // Test with offset
222
+ var largerArray = new IGrouping < int , int > [ collection . Count + 2 ] ;
223
+ collection . CopyTo ( largerArray , 1 ) ;
224
+
225
+ largerArray [ 0 ] . ShouldBeNull ( ) ;
226
+ i = 0 ;
227
+ foreach ( var group in lookup )
228
+ {
229
+ largerArray [ i + 1 ] . ShouldBe ( group ) ;
230
+ i ++ ;
231
+ }
232
+ largerArray [ collection . Count + 1 ] . ShouldBeNull ( ) ;
233
+ }
234
+
235
+ [ Fact ]
236
+ public void ICollectionCopyToExceptions ( )
237
+ {
238
+ var xs = new [ ] { 1 , 2 , 3 , 4 , 5 } ;
239
+ var lookup = xs . AsValueEnumerable ( ) . ToLookup ( x => x ) ;
240
+
241
+ // Cast to ICollection to test the explicit interface implementation
242
+ var collection = ( ICollection < IGrouping < int , int > > ) lookup ;
243
+
244
+ // Should throw when array is null
245
+ Should . Throw < ArgumentNullException > ( ( ) => collection . CopyTo ( null ! , 0 ) ) ;
246
+
247
+ // Should throw when index is negative
248
+ Should . Throw < ArgumentOutOfRangeException > ( ( ) =>
249
+ collection . CopyTo ( new IGrouping < int , int > [ collection . Count ] , - 1 ) ) ;
250
+
251
+ // Should throw when index is greater than array length
252
+ Should . Throw < ArgumentOutOfRangeException > ( ( ) =>
253
+ collection . CopyTo ( new IGrouping < int , int > [ collection . Count ] , collection . Count + 1 ) ) ;
254
+
255
+ // Should throw when array is too small
256
+ Should . Throw < ArgumentOutOfRangeException > ( ( ) =>
257
+ collection . CopyTo ( new IGrouping < int , int > [ collection . Count - 1 ] , 0 ) ) ;
258
+
259
+ // Should throw when array with offset is too small
260
+ Should . Throw < ArgumentOutOfRangeException > ( ( ) =>
261
+ collection . CopyTo ( new IGrouping < int , int > [ collection . Count ] , 1 ) ) ;
262
+ }
263
+
177
264
}
0 commit comments