3 * Copyright (C) 2010 Maciej Piechotka
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.
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.
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
20 * Maciej Piechotka <uzytkownik2@gmail.com>
23 public class FunctionsTests : Gee.TestCase {
24 public FunctionsTests () {
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);
34 public void test_string_func () {
37 string two_copy = two.dup ();
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));
44 assert (hash != null);
46 assert (eq (two, two));
47 assert (cmp (two, two) == 0);
48 assert (hash (two) == hash (two));
50 assert (eq (two, two_copy));
51 assert (cmp (two, two_copy) == 0);
52 assert (hash (two) == hash (two_copy));
54 assert (!eq (one, two));
55 assert (cmp (one, two) < 0);
57 assert (!eq (two, one));
58 assert (cmp (two, one) > 0);
61 public void test_int_func () {
62 void *one = (void *)1;
63 void *two = (void *)2;
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));
71 assert (hash != null);
73 assert (eq (two, two));
74 assert (cmp (two, two) == 0);
75 assert (hash (two) == hash (two));
77 assert (!eq (one, two));
78 assert (cmp (one, two) < 0);
80 assert (!eq (two, one));
81 assert (cmp (two, one) > 0);
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);
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));
95 //assert (hash != null);
97 assert (eq (two, two));
98 assert (cmp (two, two) == 0);
99 //assert (hash (two) == hash (two));
101 assert (eq (two, two_copy));
102 assert (cmp (two, two_copy) == 0);
103 //assert (hash (two) == hash (two_copy));
105 assert (!eq (one, two));
106 assert (cmp (one, two) < 0);
108 assert (!eq (two, one));
109 assert (cmp (two, one) > 0);
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);
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));
124 assert (cmp != null);
125 assert (hash != null);
127 assert (eq (two, two));
128 assert (cmp (two, two) == 0);
129 assert (hash (two) == hash (two));
131 assert (eq (two, two_copy));
132 assert (cmp (two, two_copy) == 0);
133 assert (hash (two) == hash (two_copy));
135 assert (!eq (one, two));
136 assert (cmp (one, two) < 0);
138 assert (!eq (two, one));
139 assert (cmp (two, one) > 0);
141 // Check if correct functions taken
142 assert (hash (one) == 1);
143 assert (!eq (minus_one, minus_one2));
146 public void test_unfold () {
149 var iter = Gee.Iterator.unfold<int> (() => {
154 return new Gee.Lazy<int> (() => {
161 while (iter.next ()) {
162 assert (iter.get () == k);
163 assert (iter.get () == k);
169 public void test_concat () {
171 var iter_ = Gee.Iterator.unfold<Gee.Iterator<int>> (() => {
176 var iter = Gee.Iterator.unfold<int> (() => {
179 return new Gee.Lazy<int>.from_value (j++);
181 return new Gee.Lazy<Gee.Iterator<int>>.from_value (iter);
185 var iter = Gee.Iterator.concat<int> (iter_);
186 while (iter.next()) {
187 assert (j == iter.get ());
188 assert (j == iter.get ());
195 private class MyComparable : GLib.Object, Gee.Comparable<MyComparable> {
196 public MyComparable (int i) {
200 public int compare_to (MyComparable cmp) {
212 private class MyHashable : GLib.Object, Gee.Comparable<MyHashable>, Gee.Hashable<MyHashable> {
213 public MyHashable (int i) {
217 public int compare_to (MyHashable cmp) {
226 public uint hash () {
230 public bool equal_to (MyHashable hash) {
231 // -1 break API but it is used for checks
232 return i == hash.i && i != -1;