File: testqueue.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 (137 lines) | stat: -rw-r--r-- 4,340 bytes parent folder | download | duplicates (7)
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
129
130
131
132
133
134
135
136
137
/* testqueue.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
 *
 * Author:
 * 	Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
 */

using Gee;

public abstract class QueueTests : CollectionTests {

	public QueueTests (string name) {
		base (name);
		add_test ("[Queue] capacity bound", test_capacity_bound);
		add_test ("[Queue] one element operation", test_one_element_operation);
		add_test ("[Queue] GObject properties", test_gobject_properties);
	}

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

		// Check the test queue is not null
		assert (test_queue != null);

		if (test_queue.capacity == Gee.Queue.UNBOUNDED_CAPACITY) {
			// Unbounded capacity
			assert (test_queue.remaining_capacity == Gee.Queue.UNBOUNDED_CAPACITY);
			assert (! test_queue.is_full);
		} else {
			// Bounded capacity
			assert (test_queue.is_empty);

			// Check that we can fill it completely
			int capacity = test_queue.capacity;
			for (int i = 1; i <= capacity; i++) {
				assert (! test_queue.is_full);
				assert (test_queue.offer ("one"));
				assert (test_queue.remaining_capacity == capacity - i);
			}
			assert (test_queue.is_full);

			// Check that we can empty it completely
			for (int i = 1; i <= capacity; i++) {
				assert (test_queue.poll () == "one");
				assert (! test_queue.is_full);
				assert (test_queue.remaining_capacity == i);
			}
			assert (test_queue.is_empty);
		}
	}

	public void test_one_element_operation () {
		var test_queue = test_collection as Gee.Queue<string>;
		ArrayList<string> recipient = new ArrayList<string> ();

		// Check the test queue is not null
		assert (test_queue != null);

		// Check offer one element when there is none yet
		assert (test_queue.offer ("one"));
		assert (test_queue.peek () == "one");
		assert (test_queue.size == 1);
		assert (! test_queue.is_empty);

		// Check poll when there is one element
		assert (test_queue.poll () == "one");
		assert (test_queue.size == 0);
		assert (test_queue.is_empty);

		// Check poll when there is no element
		assert (test_queue.peek () == null);
		assert (test_queue.poll () == null);

		// Check drain one element when there is one
		recipient.clear ();
		assert (test_queue.offer ("one"));
		assert (test_queue.drain (recipient, 1) == 1);
		assert (test_queue.size == 0);
		assert (test_queue.is_empty);
		assert (recipient.size == 1);
		assert (recipient.get (0) == "one");

		// Check drain one element when there is none
		recipient.clear ();
		assert (test_queue.drain (recipient, 1) == 0);
		assert (test_queue.size == 0);
		assert (test_queue.is_empty);
		assert (recipient.size == 0);

		// Check drain all elements when there is one
		recipient.clear ();
		assert (test_queue.offer ("one"));
		assert (test_queue.drain (recipient) == 1);
		assert (test_queue.size == 0);
		assert (test_queue.is_empty);
		assert (recipient.size == 1);
		assert (recipient.get (0) == "one");
	}

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

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

		value = Value (typeof (int));
		test_queue.get_property ("capacity", ref value);
		assert (value.get_int () == test_queue.capacity);
		value.unset ();

		value = Value (typeof (int));
		test_queue.get_property ("remaining-capacity", ref value);
		assert (value.get_int () == test_queue.remaining_capacity);
		value.unset ();

		value = Value (typeof (bool));
		test_queue.get_property ("is-full", ref value);
		assert (value.get_boolean () == test_queue.is_full);
		value.unset ();
	}
}