From: Jürg Billeter Date: Fri, 16 Jan 2009 13:29:18 +0000 (+0000) Subject: Improve array tests X-Git-Tag: VALA_0_5_6~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dbd89897490c486012e955b6a73e5aaf7dd79336;p=thirdparty%2Fvala.git Improve array tests 2009-01-16 Jürg Billeter * tests/Makefile.am: * tests/basic-types/arrays.test: Improve array tests svn path=/trunk/; revision=2348 --- diff --git a/ChangeLog b/ChangeLog index c2c52fde7..35f52802d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-01-16 Jürg Billeter + + * tests/Makefile.am: + * tests/basic-types/arrays.test: + + Improve array tests + 2009-01-16 Jürg Billeter * tests/basic-types/strings.test: diff --git a/tests/Makefile.am b/tests/Makefile.am index 4e070a666..95fb35c60 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -18,14 +18,13 @@ TESTS = \ basic-types/integers.test \ basic-types/floats.test \ basic-types/strings.test \ - basic-types/test-027.test \ + basic-types/arrays.test \ namespaces.test \ methods/lambda.test \ control-flow/break.test \ control-flow/expressions-conditional.test \ control-flow/for.test \ control-flow/switch.test \ - arrays.test \ enums.test \ structs.test \ delegates.test \ diff --git a/tests/arrays.test b/tests/arrays.test deleted file mode 100644 index 87bedf4f6..000000000 --- a/tests/arrays.test +++ /dev/null @@ -1,315 +0,0 @@ - -Program: test - -using GLib; - -class Maman.Foo : Object { - public Foo (string bar) { - this.bar = bar; - } - - public string bar { get; set; } - - static void test_integer_array () { - stdout.printf ("One dimensional array creation and assignment: 1"); - - int[] a = new int[4] {1,2}; - - stdout.printf (" 2"); - - a[2] = 3; - - stdout.printf (" 3"); - - a[3] = 4; - - stdout.printf (" 4"); - - int i = 0; - if (a[0] == 1) { - stdout.printf (" 5"); - } - if (a[1] == 2) { - stdout.printf (" 6"); - } - if (a[2] == 3) { - stdout.printf (" 7"); - } - if (a[3] == 4) { - stdout.printf (" 8"); - } - if (a.length == 4) { - stdout.printf (" 9"); - } - a.resize (10); - stdout.printf (" %d", a.length); - - stdout.printf (" 11\n"); - } - - [NoArrayLength ()] - static string[] create_unsized_string_array () { - return new string[] { "a", "b", "c" }; - } - - static void test_string_array () { - stdout.printf ("String array creation and assignment: 1"); - - var a = new string[3] { "a", "b", "c" }; - var b = new string[] { "a", "b", "c" }; - var c = create_unsized_string_array (); - - if (3 == a.length) { - stdout.printf (" 2"); - } - if (3 == b.length) { - stdout.printf (" 3"); - } - if (-1 == c.length) { - stdout.printf (" 4"); - } - if (null == c[3]) { - stdout.printf (" 5"); - } - - for (int i = 0; i < a.length; ++i) { - if (a[i] == b[i]) { - stdout.printf (" %d", i * 2 + 6); - } - if (a[i] == c[i]) { - stdout.printf (" %d", i * 2 + 7); - } - } - - a[2] = null; - b[1] = null; - - stdout.printf ("\n"); - } - - [NoArrayLength ()] - static Foo[] create_unsized_object_array () { - return new Foo[] { new Foo ("a"), new Foo ("b"), new Foo ("c") }; - } - - static void test_object_array () { - stdout.printf ("Object array creation and assignment: 1"); - - do { - var a = new Foo[3] { new Foo ("a"), new Foo ("b"), new Foo ("c") }; - var b = new Foo[] { new Foo ("a"), new Foo ("b"), new Foo ("c") }; - var c = create_unsized_object_array (); - - if (3 == a.length) { - stdout.printf (" 2"); - } - if (3 == b.length) { - stdout.printf (" 3"); - } - if (-1 == c.length) { - stdout.printf (" 4"); - } - if (null == c[3]) { - stdout.printf (" 5"); - } - - for (int i = 0; i < a.length; ++i) { - if (a[i].bar == b[i].bar) { - stdout.printf (" %d", i * 2 + 6); - } - if (a[i].bar == c[i].bar) { - stdout.printf (" %d", i * 2 + 7); - } - } - - a[2] = null; - b[1] = null; - } while (false); - - stdout.printf ("\n"); - } - - static void test_switch_on_strings () { - var tokens = new string[] { "Hello", "World", "this", "is", "Vala", "GNOME", null }; - var t4 = " 5"; - - stdout.printf ("testing switch on strings:"); - - foreach (weak string t in tokens) { - switch (t) { - case "Hello": - stdout.printf (" 1"); - break; - - case "World": - stdout.printf (" 2"); - break; - - case "this": - stdout.printf (" 3"); - break; - - case ("is"): - stdout.printf (" 4"); - break; - - case tokens[4]: - stdout.printf (t4); - tokens[4] = "GNOME"; - t4 = " 6"; - break; - - default: - stdout.printf (" 7"); - break; - } - } - - tokens[4] = null; - - stdout.printf ("\n"); - } - - static void test_array_var_creation_with_structs () { - var ca = new char[16]; - ca[5] = 'a'; - assert (ca[5] == 'a'); - } - - static void test_array_creation_side_effects () { - int i = 5; - var arr = new int[i++]; - assert (arr.length == 5); - assert (i == 6); - } - - static void test_element_access () { - stdout.printf ("Element access: 1"); - - stdout.printf (" 2"); - - var sa = "a,b,c,d".split (","); - int i = 3; - - stdout.printf (" 3"); - - if (sa[inc()] == "a") { - stdout.printf (" 4"); - } - if (sa[inc()] == "b") { - stdout.printf (" 5"); - } - if (sa[2] == "c") { - stdout.printf (" 6"); - } - if (sa[i] == "d") { - stdout.printf (" 7"); - } - - string bar = "efgh"; - counter = 0; - if (bar[inc()] == 'e') { - stdout.printf (" 8"); - } - if (bar[inc()] == 'f') { - stdout.printf (" 9"); - } - if (bar[2] == 'g') { - stdout.printf (" 10"); - } - if (bar[i] == 'h') { - stdout.printf (" 11"); - } - - stdout.printf (" 12"); - - stdout.printf (" 13\n"); - } - - const int[] const_array = { 1, 2, 3 }; - - static void test_array_length_of_array_constants () { - assert (const_array.length == 3); - } - - static int[] create_array () { - return new int[4]; - } - - static void accept_array (int[] array) { - assert (array.length == 4); - } - - static void test_array_argument () { - accept_array (create_array ()); - } - - static void test_arrays_multi_dimensional () { - int[,] array = new int[3,2]; - - int i = 0; - for (int x = 0; x < 3; x++) { - for (int y = 0; y < 2; y++) { - array[x,y] = i++; - } - } - - i = 0; - foreach (int v in array) { - assert (v == i); - i++; - } - assert (i == 3 * 2); - - assert (array.length[0] == 3); - assert (array.length[1] == 2); - } - - static void main (string[] args) { - test_integer_array (); - test_string_array (); - test_object_array (); - - stdout.printf ("Array Test: 1"); - - var bar = new Bar (); - bar.run (); - - stdout.printf (" 5\n"); - - test_switch_on_strings (); - - test_array_creation_side_effects (); - - test_element_access (); - - test_array_length_of_array_constants (); - - test_array_var_creation_with_structs (); - - test_array_argument (); - - test_arrays_multi_dimensional (); - } - - public static int inc () { - return counter++; - } - - private static int counter = 0; -} - -class Maman.Bar : Object { - public int[] foo_numbers () { - return new int[3] { 2, 3, 4 }; - } - - public void run () { - foreach (int i in foo_numbers ()) { - stdout.printf (" %d", i); - } - } -} - -const string[] const_string_array = { "hello", "world" }; - diff --git a/tests/basic-types/arrays.test b/tests/basic-types/arrays.test new file mode 100644 index 000000000..0a48cc507 --- /dev/null +++ b/tests/basic-types/arrays.test @@ -0,0 +1,72 @@ + +Program: test + +void test_integer_array () { + // declaration and initialization + int[] a = { 42 }; + assert (a.length == 1); + assert (a[0] == 42); + + // assignment + a = { 42, 23 }; + assert (a.length == 2); + assert (a[0] == 42); + assert (a[1] == 23); + + // access + int[] b = a; + assert (b.length == 2); + assert (b[0] == 42); + assert (b[1] == 23); + + // + + a += 11; + assert (a.length == 3); + assert (a[0] == 42); + assert (a[1] == 23); + assert (a[2] == 11); + assert (b.length == 2); + assert (b[0] == 42); + assert (b[1] == 23); +} + +void test_string_array () { + // declaration and initialization + string[] a = { "hello" }; + assert (a.length == 1); + assert (a[0] == "hello"); + + // assignment + a = { "hello", "world" }; + assert (a.length == 2); + assert (a[0] == "hello"); + assert (a[1] == "world"); + + // access + string[] b = a; + assert (b.length == 2); + assert (b[0] == "hello"); + assert (b[1] == "world"); +} + +int[] pass_helper (int[] a, out int[] b) { + b = a; + return { 42, 23 }; +} + +void test_array_pass () { + int[] a, b; + a = pass_helper ({ 42 }, out b); + assert (a.length == 2); + assert (a[0] == 42); + assert (a[1] == 23); + assert (b.length == 1); + assert (b[0] == 42); +} + +void main (string[] args) { + test_integer_array (); + test_string_array (); + test_array_pass (); +} + diff --git a/tests/basic-types/test-027.test b/tests/basic-types/test-027.test deleted file mode 100644 index cb97a19c7..000000000 --- a/tests/basic-types/test-027.test +++ /dev/null @@ -1,81 +0,0 @@ - -Program: test - -using GLib; - -class Maman.Bar : Object { - public int foo { get; set; } - - public void run () { - /* test with local variable */ - int i = 1; - stdout.printf (" %d", ++i); - - stdout.printf (" %d", i + 1); - - i = 4; - stdout.printf (" %d", i++); - - stdout.printf (" %d", i); - - i = 7; - stdout.printf (" %d", --(i)); - - stdout.printf (" %d", i + 1); - - i = 8; - stdout.printf (" %d", (i)--); - - stdout.printf (" %d", i + 2); - - /* test with field */ - foo = 9; - stdout.printf (" %d", ++foo); - - stdout.printf (" %d", foo + 1); - - foo = 12; - stdout.printf (" %d", foo++); - - stdout.printf (" %d", foo); - - foo = 15; - stdout.printf (" %d", --(foo)); - - stdout.printf (" %d", foo + 1); - - foo = 16; - stdout.printf (" %d", (foo)--); - - stdout.printf (" %d", foo + 2); - } - - static void test_postfix_and_prefix_expressions () { - stdout.printf ("Postfix and Prefix Expression Test: 1"); - - var bar = new Bar (); - bar.run (); - - stdout.printf (" 18\n"); - } - - static void test_prefix_increment_in_loop () { - stdout.printf ("Prefix Increment in Loop Test: "); - - int i = 0, j = 0; - - do { - stdout.printf (" %d", i); - j = j + 1; - } while (++i < 10 && j < 15); - - stdout.printf (" %d\n", i); - } - - static int main (string[] args) { - test_postfix_and_prefix_expressions (); - test_prefix_increment_in_loop (); - - return 0; - } -}