Testing arrays with lists

Component Testing for C

While an expression initializes all the ARRAY elements in the same way, you can also initialize each element using an enumerated list of expressions between brackets ({}). In this case, you must specify a value for each array element.

Furthermore, you can precede every element in this list of initial or expected values with the array index of the element concerned followed by the characters "=>". The following example illustrates this form:

ARRAY histo[0..3], init = {0 => 0, 1 => 10, 2 => 100, 3 => 10} ...

This form of writing the ARRAY instruction has the following advantages:

  • It improves the readability of the list.

  • It allows you to mix values without worrying about the order.

You can also use this form together with the simple form if you follow this rule: once one element has been defined with its array index, you must do the same with all the following elements.

If several elements in an array are to take the same value, specify the range of elements taking this value as follows:

ARRAY histo[0..3], init = { 0 .. 2 => 10, 3 => 10 } ...

You can also specify a value for all the as yet undefined elements by using the keyword others, as the following example illustrates:

TEST 2

FAMILY nominal

ELEMENT

VAR x1, init = 0, ev = init

VAR x2, init = SIZE_IMAGE-1, ev = init

VAR y1, init = 0, ev = init

VAR y2, init = SIZE_IMAGE-1, ev = init

ARRAY image, init = {others=>{others=>100}}, ev = init

ARRAY histo, init = 0,

& ev = {100=>SIZE_IMAGE*SIZE_IMAGE, others=>0}

VAR status, init ==, ev = 0

#status = compute_histo(x1, y1, x2, y2, histo);

END ELEMENT

END TEST

Note The form {others => <expression> } is equivalent to initializing and testing all array elements with the same expression.

You can also initialize and test multidimensional arrays with a list of expressions, as follows. In this case, the previously mentioned rules apply to each dimension.

ARRAY image, init = {0, 1=>4, others=>{1, 2, others=>100}} ...

Note Some C compilers allow you to omit levels of brackets when initializing a multidimensional array. The Unit Testing Scripting Language does not accept this non-standard extension to the language.

Related Topics

Testing arrays | Testing arrays with pseudo-variables | Testing large arrays | Testing character arrays | Testing arrays with other arrays | Testing an array of union elements | VAR, ARRAY and STR