[libgda/libgda-vala] Added Unit Test for Gda.DataModelIterable.stream().
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda/libgda-vala] Added Unit Test for Gda.DataModelIterable.stream().
- Date: Tue, 6 Dec 2011 23:08:10 +0000 (UTC)
commit 9bfcbe62a34fc0769136ebdb7b89d937b74c1440
Author: Daniel Espinosa <despinosa src gnome org>
Date: Tue Dec 6 17:05:39 2011 -0600
Added Unit Test for Gda.DataModelIterable.stream().
* Actualy it works with any generic type A. Tested using string as generic but
it doesn't returns builded string from StreamFunc<Value?,string> FIXME.
libgda/data/DataModelIterator.vala | 21 +++++------
tests/vala/CheckDataModelIterator.vala | 62 +++++++++++++++++++++----------
2 files changed, 52 insertions(+), 31 deletions(-)
---
diff --git a/libgda/data/DataModelIterator.vala b/libgda/data/DataModelIterator.vala
index b5b4e3b..4328c2c 100644
--- a/libgda/data/DataModelIterator.vala
+++ b/libgda/data/DataModelIterator.vala
@@ -246,25 +246,24 @@
* ''Implementation:'' This function returns an iterator that can iterate over YIELDED values
* by { link [StreamFunc].
*/
- public Gee.Iterator<Value?> stream<Value> (owned Gee.StreamFunc<Value?,Value?> f)
+ public Gee.Iterator<A> stream<A> (owned Gee.StreamFunc<Value?, A> f)
{
- var l = new Gee.HashMap<int,int> ();
- for (int i = 0; i < this.maxpos; i++) {
+ var l = new Gee.ArrayList<A> ();
+ for (int i = this.pos_init; i < this.maxpos; i++) {
int row = i / this.iter.data_model.get_n_columns ();
int col = i - row * this.iter.data_model.get_n_columns ();
- Value v = this.iter.data_model.get_value_at (row, col);
- var g = new Gee.Lazy<Value>.from_value (v);
- Gee.Lazy<Value> s;
+ Value v = this.iter.data_model.get_value_at (col, row);
+ var g = new Gee.Lazy<Value?>.from_value (v);
+ Gee.Lazy<A> s;
var r = f (Gee.Traversable.Stream.CONTINUE, g, out s);
if (r == Gee.Traversable.Stream.END)
break;
- if (r == Stream.YIELD)
- l.set (this.iter.get_row (), this._current_pos -
- this.iter.get_row () *
- this.iter.data_model.get_n_columns ());
+ if (r == Gee.Traversable.Stream.YIELD) {
+ l.add (s);
+ }
}
- return new DataModelIterator.filtered_elements (this.iter.data_model, l);
+ return l.iterator<A> ();
}
}
}
diff --git a/tests/vala/CheckDataModelIterator.vala b/tests/vala/CheckDataModelIterator.vala
index bdb5647..5b34913 100644
--- a/tests/vala/CheckDataModelIterator.vala
+++ b/tests/vala/CheckDataModelIterator.vala
@@ -50,7 +50,7 @@ namespace Check {
public int t1()
throws Error
{
- stdout.printf (">>> NEW TEST: Gda.DataModelIterator API tests\n");
+ stdout.printf (">>> NEW TEST: Gda.DataModelIterable & DataModelIterator API tests\n");
int fails = 0;
var model = this.connection.execute_select_command ("SELECT * FROM user");
var itermodel = new DataModelIterable (model);
@@ -144,7 +144,23 @@ namespace Check {
}
stdout.printf ("Filtering Values: Any STRING with a letter 'n'...\n");
- var iter4 = itermodel.filter (Filter);
+ Gee.Predicate <Value?> f = (g) => {
+ bool ret = false;
+ if (g.type () == typeof (string)) {
+ string t = (string) g;
+ if (t.contains ("n"))
+ ret = true;
+ else
+ ret = false;
+ }
+ else
+ ret = false;
+ stdout.printf ("To be Included?: %s, %s\n",
+ Gda.value_stringify (g), ret == true ? "TRUE" : "FALSE");
+ return ret;
+ };
+
+ var iter4 = itermodel.filter (f);
stdout.printf ("Printing Filtered Values...\n");
while (iter4.next ()) {
Value v4 = iter4.get ();
@@ -153,26 +169,32 @@ namespace Check {
((GdaData.DataModelIterator) iter4).current_column,
typeof (int).name (), Gda.value_stringify (v4));
}
-
+
+ /* FIXME: This code doesn't return YIELDED strings maybe becasue Lazy is not correctly build.
+ In theory stream() function is working correctly*/
+ stdout.printf ("Streaming Values: Any STRING will be YIELDED...\n");
+ Gee.StreamFunc<Value?,string> s = (state, g, lazy) => {
+ if (state == Gee.Traversable.Stream.CONTINUE) {
+ Value vs = g.get ();
+ stdout.printf ("Value to YIELD: %s\n", Gda.value_stringify (vs));
+ string ts = "YIELDED Value = " + Gda.value_stringify (vs) + "\n";
+ lazy = new Gee.Lazy<string>.from_value (ts.dup ());
+ stdout.printf (ts);
+ return Gee.Traversable.Stream.YIELD;
+ }
+ return Gee.Traversable.Stream.END;
+ };
+
+ var iter5 = itermodel.stream<string> (s);
+ stdout.printf ("Printing Streamed Values...\n");
+ while (iter5.next ()) {
+ string sv = iter5.get ();
+ stdout.printf ("%s\n", sv);
+ }
+
return fails;
}
-
- private bool Filter (Value? g)
- {
- bool ret = false;
- if (g.type () == typeof (string)) {
- string t = (string) g;
- if (t.contains ("n"))
- ret = true;
- else
- ret = false;
- }
- else
- ret = false;
- stdout.printf ("To be Included?: %s, %s\n", Gda.value_stringify (g), ret == true ? "TRUE" : "FALSE");
- return ret;
- }
-
+
public static int main (string[] args) {
stdout.printf ("Checking Gda.DataModelIterator implementation...\n");
int failures = 0;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]