File: testpriorityqueue.vala

package info (click to toggle)
libgee 0.6.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,060 kB
  • ctags: 10,764
  • sloc: ansic: 59,313; sh: 11,421; makefile: 294; perl: 106
file content (128 lines) | stat: -rw-r--r-- 4,198 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* testpriorityqueue.vala
 *
 * Copyright (C) 2009  Didier Villevalois
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Authors:
 * 	Didier 'Ptitjes Villevalois <ptitjes@free.fr>
 */

using Gee;

public class PriorityQueueTests : QueueTests {

	public PriorityQueueTests () {
		base ("PriorityQueue");
		add_test ("[PriorityQueue] selected functions", test_selected_functions);
		add_test ("[PriorityQueue] GObject properties", test_gobject_properties);
		add_test ("[PriorityQueue] poll gives minimum", test_poll_gives_minimum);
	}

	public override void set_up () {
		test_collection = new PriorityQueue<string> ();
	}

	public override void tear_down () {
		test_collection = null;
	}

	private void test_selected_functions () {
		var test_queue = test_collection as PriorityQueue<string>;

		// Check the queue exists
		assert (test_queue != null);

		// Check the selected compare function
		assert (test_queue.compare_func == (CompareFunc) strcmp);
	}

	public new void test_gobject_properties () {
		var test_queue = test_collection as PriorityQueue<string>;

		// Check the list exists
		assert (test_queue != null);
		Value value;

		value = Value (typeof (CompareFunc));
		test_queue.get_property ("compare-func", ref value);
		assert (value.get_pointer () == (void*) test_queue.compare_func);
		value.unset ();
	}

	private void test_poll_gives_minimum () {
		var test_queue = test_collection as Gee.Queue<string>;

		// Check the queue exists
		assert (test_queue != null);

		// Add two elements and remove the greatest
		assert (test_queue.offer ("one"));
		assert (test_queue.offer ("two"));
		assert (test_queue.peek () == "one");
		assert (test_queue.remove ("two"));
		assert (test_queue.peek () == "one");
		assert (test_queue.poll () == "one");

		// Add twelve elements
		assert (test_queue.offer ("one"));
		assert (test_queue.offer ("two"));
		assert (test_queue.offer ("three"));
		assert (test_queue.offer ("four"));
		assert (test_queue.offer ("five"));
		assert (test_queue.offer ("six"));
		assert (test_queue.offer ("seven"));
		assert (test_queue.offer ("eight"));
		assert (test_queue.offer ("nine"));
		assert (test_queue.offer ("ten"));
		assert (test_queue.offer ("eleven"));
		assert (test_queue.offer ("twelve"));

		assert (test_queue.peek () == "eight");
		assert (test_queue.poll () == "eight");
		assert (test_queue.peek () == "eleven");
		assert (test_queue.poll () == "eleven");
		assert (test_queue.peek () == "five");
		assert (test_queue.poll () == "five");
		assert (test_queue.peek () == "four");
		assert (test_queue.poll () == "four");
		assert (test_queue.peek () == "nine");
		assert (test_queue.poll () == "nine");
		assert (test_queue.peek () == "one");
		assert (test_queue.poll () == "one");
		assert (test_queue.peek () == "seven");
		assert (test_queue.poll () == "seven");
		assert (test_queue.peek () == "six");
		assert (test_queue.poll () == "six");
		assert (test_queue.peek () == "ten");
		assert (test_queue.poll () == "ten");
		assert (test_queue.peek () == "three");
		assert (test_queue.poll () == "three");
		assert (test_queue.peek () == "twelve");
		assert (test_queue.poll () == "twelve");
		assert (test_queue.peek () == "two");
		assert (test_queue.poll () == "two");

		// Add decreasing elements
		assert (test_queue.offer ("2"));
		assert (test_queue.offer ("1"));

		
		assert (test_queue.peek () == "1");
		assert (test_queue.poll () == "1");
		assert (test_queue.peek () == "2");
		assert (test_queue.poll () == "2");
	}
}