2010-05-31 Jb Evain <jbevain@novell.com>
[mcs.git] / tests / gtest-linq-05.cs
blob36932a43a87f0fea7d0b5ec182759353917f5843
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
7 class OrderByTests
9 class Data
11 public int ID { get; set; }
12 public string Name { get; set; }
14 public override string ToString ()
16 return ID + " " + Name;
20 public static int Main ()
22 int[] int_array = new int [] { 0, 1 };
23 string[] string_array = new string[] { "f", "a", "z", "aa" };
25 IEnumerable<int> e;
27 e = from int i in int_array orderby i select i;
29 List<int> l = new List<int> (e);
30 if (l [0] != 0)
31 return 1;
33 if (l [1] != 1)
34 return 2;
36 e = from int i in int_array orderby i ascending select i;
38 l = new List<int> (e);
39 if (l [0] != 0)
40 return 100;
42 if (l [1] != 1)
43 return 101;
45 e = from i in int_array orderby i descending select i + 1;
46 l = new List<int> (e);
47 if (l [0] != 2)
48 return 3;
50 if (l [1] != 1)
51 return 4;
53 IEnumerable<string> s;
54 s = from i in string_array orderby i select i;
56 List<string> ls = new List<string> (s);
58 if (ls [0] != "a")
59 return 5;
61 if (ls [1] != "aa")
62 return 6;
64 if (ls [2] != "f")
65 return 7;
67 if (ls [3] != "z")
68 return 7;
70 s = from i in string_array orderby i.Length select i;
72 // Multiple orderby
73 Data[] data = new Data[] {
74 new Data { ID = 10, Name = "bcd" },
75 new Data { ID = 20, Name = "Abcd" },
76 new Data { ID = 20, Name = "Ab" },
77 new Data { ID = 10, Name = "Zyx" }
80 var de = from i in data orderby i.ID ascending, i.Name descending select i;
82 List<Data> ld = new List<Data> (de);
83 if (ld [0].Name != "Zyx")
84 return 10;
86 var de2 = from i in data orderby i.ID descending, i.Name ascending select i;
87 ld = new List<Data> (de2);
88 if (ld [0].Name != "Ab")
89 return 11;
91 var de3 = from i in data
92 where i.ID == 10
93 orderby i.ID descending, i.Name ascending select i;
94 ld = new List<Data> (de3);
95 if (ld [0].Name != "bcd")
96 return 12;
98 var de4 = from i in data
99 where i.ID == 20
100 orderby i.Name group i by i.Name;
102 var group_order = new List<IGrouping<string, Data>> (de4);
103 ld = new List<Data>(group_order [0]);
105 if (ld [0].Name != "Ab")
106 return 13;
108 ld = new List<Data>(group_order [1]);
109 if (ld [0].Name != "Abcd")
110 return 14;
112 Console.WriteLine ("OK");
113 return 0;