3 class Maman
.Foo
: Object
{
4 public Foo (construct string bar
) {
7 public string bar
{ get; set; }
9 static void test_integer_array () {
10 stdout
.printf ("One dimensional array creation and assignment: 1");
12 int[] a
= new
int[4] {1,2};
41 stdout
.printf (" %d", a
.length
);
43 stdout
.printf (" 11\n");
47 static string[] create_unsized_string_array () {
48 return new
string[] { "a", "b", "c" };
51 static void test_string_array () {
52 stdout
.printf ("String array creation and assignment: 1");
54 var a
= new
string[3] { "a", "b", "c" };
55 var b
= new
string[] { "a", "b", "c" };
56 var c
= create_unsized_string_array ();
71 for (int i
= 0; i
< a
.length
; ++i
) {
73 stdout
.printf (" %d", i
* 2 + 6);
76 stdout
.printf (" %d", i
* 2 + 7);
87 static Foo
[] create_unsized_object_array () {
88 return new Foo
[] { new
Foo ("a"), new
Foo ("b"), new
Foo ("c") };
91 static void test_object_array () {
92 stdout
.printf ("Object array creation and assignment: 1");
95 var a
= new Foo
[3] { new
Foo ("a"), new
Foo ("b"), new
Foo ("c") };
96 var b
= new Foo
[] { new
Foo ("a"), new
Foo ("b"), new
Foo ("c") };
97 var c
= create_unsized_object_array ();
100 stdout
.printf (" 2");
103 stdout
.printf (" 3");
105 if (-1 == c
.length
) {
106 stdout
.printf (" 4");
109 stdout
.printf (" 5");
112 for (int i
= 0; i
< a
.length
; ++i
) {
113 if (a
[i
].bar
== b
[i
].bar
) {
114 stdout
.printf (" %d", i
* 2 + 6);
116 if (a
[i
].bar
== c
[i
].bar
) {
117 stdout
.printf (" %d", i
* 2 + 7);
125 stdout
.printf ("\n");
128 static void test_switch_on_strings () {
129 var tokens
= new
string[] { "Hello", "World", "this", "is", "Vala", "GNOME", null };
132 stdout
.printf ("testing switch on strings:");
134 foreach (weak string t
in tokens
) {
137 stdout
.printf (" 1");
141 stdout
.printf (" 2");
145 stdout
.printf (" 3");
149 stdout
.printf (" 4");
159 stdout
.printf (" 7");
166 stdout
.printf ("\n");
169 static void test_array_var_creation_with_structs () {
170 var ca
= new
char[16];
172 assert (ca
[5] == 'a');
175 static void test_array_creation_side_effects () {
177 var arr
= new
int[i
++];
178 assert (arr
.length
== 5);
182 static void test_element_access () {
183 stdout
.printf ("Element access: 1");
185 stdout
.printf (" 2");
187 var sa
= "a,b,c,d".split (",");
190 stdout
.printf (" 3");
192 if (sa
[inc()] == "a") {
193 stdout
.printf (" 4");
195 if (sa
[inc()] == "b") {
196 stdout
.printf (" 5");
199 stdout
.printf (" 6");
202 stdout
.printf (" 7");
207 if (bar
[inc()] == 'e') {
208 stdout
.printf (" 8");
210 if (bar
[inc()] == 'f') {
211 stdout
.printf (" 9");
214 stdout
.printf (" 10");
217 stdout
.printf (" 11");
220 stdout
.printf (" 12");
222 stdout
.printf (" 13\n");
225 const int[] const_array
= { 1, 2, 3 };
227 static void test_array_length_of_array_constants () {
228 assert (const_array
.length
== 3);
231 static int[] create_array () {
235 static void accept_array (int[] array
) {
236 assert (array
.length
== 4);
239 static void test_array_argument () {
240 accept_array (create_array ());
243 static void test_arrays_multi_dimensional () {
244 int[,] array
= new
int[3,2];
247 for (int x
= 0; x
< 3; x
++) {
248 for (int y
= 0; y
< 2; y
++) {
254 foreach (int v
in array
) {
260 assert (array
.length
[0] == 3);
261 assert (array
.length
[1] == 2);
264 static void main (string[] args
) {
265 test_integer_array ();
266 test_string_array ();
267 test_object_array ();
269 stdout
.printf ("Array Test: 1");
271 var bar
= new
Bar ();
274 stdout
.printf (" 5\n");
276 test_switch_on_strings ();
278 test_array_creation_side_effects ();
280 test_element_access ();
282 test_array_length_of_array_constants ();
284 test_array_var_creation_with_structs ();
286 test_array_argument ();
288 test_arrays_multi_dimensional ();
291 public static int inc () {
295 private static int counter
= 0;
298 class Maman
.Bar
: Object
{
299 public int[] foo_numbers () {
300 return new
int[3] { 2, 3, 4 };
304 foreach (int i
in foo_numbers ()) {
305 stdout
.printf (" %d", i
);
310 const string[] const_string_array
= { "hello", "world" };