Testing arrays with lists

Component Testing for Ada

While an expression initializes all the array elements in the same way, you can also initialize each element by 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 statement has several advantages:

  • Improved readability of the list

  • Ability 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 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)) ...

Example

You can 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

Related Topics

Testing variables | Testing arrays | Testing an array with pseudo-variables | Testing character arrays | Testing large arrays | Testing arrays with other arrays