Update Changelog
[profile/ivi/libgee.git] / tests / testfunctions.vala
1 /* testfunctions.vala
2  *
3  * Copyright (C) 2010  Maciej Piechotka
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Maciej Piechotka <uzytkownik2@gmail.com>
21  */
22
23 public class FunctionsTests : Gee.TestCase {
24         public FunctionsTests () {
25                 base ("Functions");
26                 add_test ("[Functions] comparing and hashing strings", test_string_func);
27                 add_test ("[Functions] comparing and hashing int", test_int_func);
28                 add_test ("[Functions] comparing instances of Comparable", test_compare_func);
29                 add_test ("[Functions] comparing and hashing instances of Hashable", test_hash_func);
30                 add_test ("[Iterator] unfold", test_unfold);
31                 add_test ("[Iterator] concat", test_concat);
32         }
33
34         public void test_string_func () {
35                 string one = "one";
36                 string two = "two";
37                 string two_copy = two.dup ();
38
39                 Gee.EqualDataFunc eq = Gee.Functions.get_equal_func_for (typeof (string));
40                 CompareDataFunc cmp = Gee.Functions.get_compare_func_for (typeof (string));
41                 Gee.HashDataFunc hash = Gee.Functions.get_hash_func_for (typeof (string));
42                 assert (eq != null);
43                 assert (cmp != null);
44                 assert (hash != null);
45
46                 assert (eq (two, two));
47                 assert (cmp (two, two) == 0);
48                 assert (hash (two) == hash (two));
49
50                 assert (eq (two, two_copy));
51                 assert (cmp (two, two_copy) == 0);
52                 assert (hash (two) == hash (two_copy));
53
54                 assert (!eq (one, two));
55                 assert (cmp (one, two) < 0);
56                 
57                 assert (!eq (two, one));
58                 assert (cmp (two, one) > 0);
59         }
60
61         public void test_int_func () {
62                 void *one = (void *)1;
63                 void *two = (void *)2;
64
65                 Gee.EqualDataFunc eq = Gee.Functions.get_equal_func_for (typeof (int));
66                 CompareDataFunc cmp = Gee.Functions.get_compare_func_for (typeof (int));
67                 Gee.HashDataFunc hash = Gee.Functions.get_hash_func_for (typeof (int));
68
69                 assert (eq != null);
70                 assert (cmp != null);
71                 assert (hash != null);
72
73                 assert (eq (two, two));
74                 assert (cmp (two, two) == 0);
75                 assert (hash (two) == hash (two));
76
77                 assert (!eq (one, two));
78                 assert (cmp (one, two) < 0);
79                 
80                 assert (!eq (two, one));
81                 assert (cmp (two, one) > 0);
82         }
83
84         public void test_compare_func () {
85                 MyComparable two = new MyComparable(2);
86                 MyComparable one = new MyComparable(1);
87                 MyComparable two_copy = new MyComparable(2);
88
89                 Gee.EqualDataFunc eq = Gee.Functions.get_equal_func_for (typeof (MyComparable));
90                 CompareDataFunc cmp = Gee.Functions.get_compare_func_for (typeof (MyComparable));
91                 //Gee.HashDataFunc hash = Gee.Functions.get_hash_func_for (typeof (MyComparable));
92
93                 assert (eq != null);
94                 assert (cmp != null);
95                 //assert (hash != null);
96
97                 assert (eq (two, two));
98                 assert (cmp (two, two) == 0);
99                 //assert (hash (two) == hash (two));
100
101                 assert (eq (two, two_copy));
102                 assert (cmp (two, two_copy) == 0);
103                 //assert (hash (two) == hash (two_copy));
104
105                 assert (!eq (one, two));
106                 assert (cmp (one, two) < 0);
107                 
108                 assert (!eq (two, one));
109                 assert (cmp (two, one) > 0);
110         }
111
112         public void test_hash_func () {
113                 MyHashable two = new MyHashable(2);
114                 MyHashable one = new MyHashable(1);
115                 MyHashable two_copy = new MyHashable(2);
116                 MyHashable minus_one = new MyHashable(-1);
117                 MyHashable minus_one2 = new MyHashable(-1);
118
119                 Gee.EqualDataFunc eq = Gee.Functions.get_equal_func_for (typeof (MyHashable));
120                 CompareDataFunc cmp = Gee.Functions.get_compare_func_for (typeof (MyHashable));
121                 Gee.HashDataFunc hash = Gee.Functions.get_hash_func_for (typeof (MyHashable));
122
123                 assert (eq != null);
124                 assert (cmp != null);
125                 assert (hash != null);
126
127                 assert (eq (two, two));
128                 assert (cmp (two, two) == 0);
129                 assert (hash (two) == hash (two));
130
131                 assert (eq (two, two_copy));
132                 assert (cmp (two, two_copy) == 0);
133                 assert (hash (two) == hash (two_copy));
134
135                 assert (!eq (one, two));
136                 assert (cmp (one, two) < 0);
137                 
138                 assert (!eq (two, one));
139                 assert (cmp (two, one) > 0);
140
141                 // Check if correct functions taken
142                 assert (hash (one) == 1);
143                 assert (!eq (minus_one, minus_one2));
144         }
145
146         public void test_unfold () {
147                 int i = 0;
148                 int j = -1;
149                 var iter = Gee.Iterator.unfold<int> (() => {
150                         assert (j + 1 == i);
151                         if (i == 10)
152                                 return null;
153                         int k = i++;
154                         return new Gee.Lazy<int> (() => {
155                                 assert (k + 1 == i);
156                                 j = k;
157                                 return k;
158                         });
159                 });
160                 int k = 0;
161                 while (iter.next ()) {
162                         assert (iter.get () == k);
163                         assert (iter.get () == k);
164                         k++;
165                 }
166                 assert (k == 10);
167         }
168
169         public void test_concat () {
170                 int i = 0;
171                 var iter_ = Gee.Iterator.unfold<Gee.Iterator<int>> (() => {
172                         if (i >= 3)
173                                 return null;
174                         int j = i++*3;
175                         int start = j;
176                         var iter = Gee.Iterator.unfold<int> (() => {
177                                 if (j == start + 3)
178                                         return null;
179                                 return new Gee.Lazy<int>.from_value (j++);
180                         });
181                         return new Gee.Lazy<Gee.Iterator<int>>.from_value (iter);
182                 });
183
184                 int j = 0;
185                 var iter = Gee.Iterator.concat<int> (iter_);
186                 while (iter.next()) {
187                         assert (j == iter.get ());
188                         assert (j == iter.get ());
189                         j++;
190                 }
191                 assert (i == 3);
192                 assert (j == 9);
193         }
194
195         private class MyComparable : GLib.Object, Gee.Comparable<MyComparable> {
196                 public MyComparable (int i) {
197                         this.i = i;
198                 }
199
200                 public int compare_to (MyComparable cmp) {
201                         if (i == cmp.i)
202                                 return 0;
203                         else if (i >= cmp.i)
204                                 return 1;
205                         else
206                                 return -1;
207                 }
208
209                 int i;
210         }
211
212         private class MyHashable : GLib.Object, Gee.Comparable<MyHashable>, Gee.Hashable<MyHashable> {
213                 public MyHashable (int i) {
214                         this.i = i;
215                 }
216
217                 public int compare_to (MyHashable cmp) {
218                         if (i == cmp.i)
219                                 return 0;
220                         else if (i >= cmp.i)
221                                 return 1;
222                         else
223                                 return -1;
224                 }
225
226                 public uint hash () {
227                         return i;
228                 }
229
230                 public bool equal_to (MyHashable hash) {
231                         // -1 break API but it is used for checks
232                         return i == hash.i && i != -1;
233                 }
234
235                 int i;
236         }
237 }
238