tracker-0.16.2/tests/libtracker-miner/tracker-priority-queue-test.c

No issues found

  1 /*
  2  * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301, USA.
 18  */
 19 
 20 #include <glib-object.h>
 21 
 22 /* NOTE: We're not including tracker-miner.h here because this is private. */
 23 #include <libtracker-miner/tracker-priority-queue.h>
 24 
 25 static void
 26 test_priority_queue_ref_unref (void)
 27 {
 28         TrackerPriorityQueue *one, *two;
 29         
 30         one = tracker_priority_queue_new ();
 31         two = tracker_priority_queue_ref (one);
 32 
 33         tracker_priority_queue_unref (two);
 34         tracker_priority_queue_unref (one);
 35 }
 36 
 37 static void
 38 test_priority_queue_emptiness (void)
 39 {
 40         TrackerPriorityQueue *one;
 41         
 42         one = tracker_priority_queue_new ();
 43 
 44         g_assert (tracker_priority_queue_is_empty (one));
 45         g_assert_cmpint (tracker_priority_queue_get_length (one), ==, 0);
 46 
 47         tracker_priority_queue_unref (one);
 48 }
 49 
 50 static void
 51 test_priority_queue_insertion_pop (void)
 52 {
 53         TrackerPriorityQueue *queue;
 54         int                   i, priority;
 55         gchar                *text, *expected;
 56 
 57         queue = tracker_priority_queue_new ();
 58 
 59         /* Insert in to loops to "mix" priorities in the insertion */
 60         for (i = 1; i <= 10; i+=2) {
 61                 tracker_priority_queue_add (queue, g_strdup_printf ("test content %i", i), i);
 62         }
 63 
 64         for (i = 2; i <= 10; i+=2) {
 65                 tracker_priority_queue_add (queue, g_strdup_printf ("test content %i", i), i);
 66         }
 67 
 68         for (i = 1; i <= 10; i++) {
 69                 expected = g_strdup_printf ("test content %i", i);
 70 
 71                 text = (gchar *)tracker_priority_queue_pop (queue, &priority);
 72 
 73                 g_assert_cmpint (priority, ==, i);
 74                 g_assert_cmpstr (text, ==, expected);
 75 
 76                 g_free (expected);
 77                 g_free (text);
 78         }
 79 
 80         g_assert (tracker_priority_queue_is_empty (queue));        
 81         tracker_priority_queue_unref (queue);
 82 }
 83 
 84 static void
 85 test_priority_queue_peek (void)
 86 {
 87         TrackerPriorityQueue *queue;
 88         gchar                *result;
 89         gint                  priority;
 90         
 91         queue = tracker_priority_queue_new ();
 92         
 93         tracker_priority_queue_add (queue, g_strdup ("Low prio"), 10);
 94         tracker_priority_queue_add (queue, g_strdup ("High prio"), 1);
 95 
 96         result = tracker_priority_queue_peek (queue, &priority);
 97         g_assert_cmpint (priority, ==, 1);
 98         g_assert_cmpstr (result, ==, "High prio");
 99 
100         result = tracker_priority_queue_pop (queue, &priority);
101         g_free (result);
102 
103         result = tracker_priority_queue_peek (queue, &priority);
104         g_assert_cmpint (priority, ==, 10);
105         g_assert_cmpstr (result, ==, "Low prio");
106 
107         result = tracker_priority_queue_pop (queue, &priority);
108         g_free (result);
109 
110         tracker_priority_queue_unref (queue);
111 }
112 
113 static void
114 test_priority_queue_find (void)
115 {
116         TrackerPriorityQueue *queue;
117         gchar *result;
118         int priority;
119 
120         queue = tracker_priority_queue_new ();
121         
122         tracker_priority_queue_add (queue, g_strdup ("search me"), 10);
123         tracker_priority_queue_add (queue, g_strdup ("Not me"), 1);
124         tracker_priority_queue_add (queue, g_strdup ("Not me either"), 20);
125 
126         result = (gchar *) tracker_priority_queue_find (queue, &priority, g_str_equal, "search me");
127         g_assert_cmpstr (result, !=, NULL);
128         g_assert_cmpint (priority, ==, 10);
129 
130         tracker_priority_queue_unref (queue);
131 }
132 
133 static void
134 foreach_testing_cb (G_GNUC_UNUSED gpointer data,
135                                   gpointer user_data)
136 {
137         gint *counter = (gint *)user_data;
138         (*counter) += 1;
139 }
140 
141 static void
142 test_priority_queue_foreach (void)
143 {
144         TrackerPriorityQueue *queue;
145         gint                  counter = 0;
146 
147         queue = tracker_priority_queue_new ();
148         
149         tracker_priority_queue_add (queue, g_strdup ("x"), 10);
150         tracker_priority_queue_add (queue, g_strdup ("x"), 20);        
151         tracker_priority_queue_add (queue, g_strdup ("x"), 30);
152 
153         tracker_priority_queue_foreach (queue, foreach_testing_cb, &counter);
154 
155         g_assert_cmpint (counter, ==, 3);
156 
157         tracker_priority_queue_unref (queue);
158 }
159 
160 static void
161 test_priority_queue_foreach_remove (void)
162 {
163         TrackerPriorityQueue *queue;
164         
165         queue = tracker_priority_queue_new ();
166 
167         tracker_priority_queue_add (queue, g_strdup ("y"), 1);
168         tracker_priority_queue_add (queue, g_strdup ("x"), 2);
169         tracker_priority_queue_add (queue, g_strdup ("y"), 3);
170         tracker_priority_queue_add (queue, g_strdup ("x"), 4);
171         tracker_priority_queue_add (queue, g_strdup ("y"), 5);
172         g_assert_cmpint (tracker_priority_queue_get_length (queue), ==, 5);
173         
174         tracker_priority_queue_foreach_remove (queue, g_str_equal, "y", g_free);
175         g_assert_cmpint (tracker_priority_queue_get_length (queue), ==, 2);
176 
177         tracker_priority_queue_foreach_remove (queue, g_str_equal, "x", g_free);
178         g_assert_cmpint (tracker_priority_queue_get_length (queue), ==, 0);
179 
180         tracker_priority_queue_unref (queue);
181 }
182 
183 static void
184 test_priority_queue_branches (void)
185 {
186 
187         /* Few specific testing to improve the branch coverage */
188 
189         TrackerPriorityQueue *queue;
190         gchar                *result;
191         gint                  priority;
192 
193         queue = tracker_priority_queue_new ();
194 
195         /* Removal on empty list */
196         tracker_priority_queue_foreach_remove (queue, g_str_equal, "y", g_free);
197 
198 
199         /* Insert multiple elements in the same priority */
200         tracker_priority_queue_add (queue, g_strdup ("x"), 5);
201         tracker_priority_queue_add (queue, g_strdup ("y"), 5);
202         tracker_priority_queue_add (queue, g_strdup ("z"), 5);
203 
204         g_assert_cmpint (tracker_priority_queue_get_length (queue), ==, 3);
205 
206         /* Removal with multiple elements in same priority*/
207         g_assert (tracker_priority_queue_foreach_remove (queue, g_str_equal, "z", g_free));
208         g_assert (tracker_priority_queue_foreach_remove (queue, g_str_equal, "x", g_free));
209         
210 
211         /* Pop those elements */
212         result = tracker_priority_queue_pop (queue, &priority);
213         g_assert_cmpint (priority, ==, 5);
214         g_free (result);
215 
216         g_assert_cmpint (tracker_priority_queue_get_length (queue), ==, 0);
217         /* Pop on empty queue */
218         result = tracker_priority_queue_pop (queue, &priority);
219         g_assert (result == NULL);
220 
221         tracker_priority_queue_unref (queue);
222 }
223 
224 int
225 main (int    argc,
226       char **argv)
227 {
228 	g_test_init (&argc, &argv, NULL);
229 
230 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/emptiness",
231 	                 test_priority_queue_emptiness);
232 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/ref_unref",
233 	                 test_priority_queue_ref_unref);
234 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/insertion",
235 	                 test_priority_queue_insertion_pop);
236 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/peek",
237 	                 test_priority_queue_peek);
238 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/find",
239 	                 test_priority_queue_find);
240 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/foreach",
241 	                 test_priority_queue_foreach);
242 	g_test_add_func ("/libtracker-miner/tracker-priority-queue/foreach_remove",
243 	                 test_priority_queue_foreach_remove);
244 
245         g_test_add_func ("/libtracker-miner/tracker-priority-queue/branches",
246                          test_priority_queue_branches);
247 
248 	return g_test_run ();
249 }