Updated with review feedback.
[mono.git] / mcs / tests / gtest-linq-04.cs
1
2
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6
7 class TestGroupBy
8 {
9         public static int Main ()
10         {
11                 int[] int_array = new int [] { 0, 1, 2, 3, 4 };
12                 
13                 IEnumerable<IGrouping<int, int>> e;
14                 
15                 // group by i % 2 from 1
16                 e = from int i in int_array group 1 by i % 2;
17
18                 List<IGrouping<int, int>> output = e.ToList ();
19                 if (output.Count != 2)
20                         return 1;
21                 
22                 foreach (IGrouping<int, int> ig in e) {
23                         Console.WriteLine (ig.Key);
24                         foreach (int value in ig) {
25                                 Console.WriteLine ("\t" + value);
26                                 if (value != 1)
27                                         return 2;
28                         }
29                 }
30
31                 // group by i % 2 from i
32                 e = from int i in int_array group i by i % 2;
33
34                 output = e.ToList ();
35                 if (output.Count != 2)
36                         return 1;
37                 
38                 int[] results_a = new int[] { 0, 2, 4, 1, 3 };
39                 int pos = 0;
40                 
41                 foreach (IGrouping<int, int> ig in e) {
42                         Console.WriteLine (ig.Key);
43                         foreach (int value in ig) {
44                                 Console.WriteLine ("\t" + value);
45                                 if (value != results_a [pos++])
46                                         return 3;
47                         }
48                 }
49                 
50                 Console.WriteLine ("OK");
51                 return 0;
52         }
53 }