5

In a recent AdaCore Gem there's a statement

The implementation of Finalize is slightly more complicated: the Ada reference manual indicates that a Finalize procedure should always be idempotent. An Ada compiler is free to call Finalize multiple times on the same object, in particular when exceptions occur.

I need to test a privately controlled type,

   type T is private;
private
   type T is new Ada.Finalization.Controlled with ...

I can change the source to make the type visibly controlled

   type T is new Ada.Finalization.Controlled with private;

and then just call Finalize (My_T); (or even My_T.Finalize?); but is there any way I can cause multiple finalizations without this change to the software-under-test?

Community
  • 1
  • 1
Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • 1
    Whoa. Got me stumped on that one. Best I can think of would be to figure out what (if any) conditions cause your compiler to pull that multi-finalize trick, and try to induce it. That wouldn't make for a portable test though. – T.E.D. Jan 19 '11 at 22:16
  • Perhaps I just need to accept that it has to be visibly controlled. Better to make it testable even if it adds some otherwise-unneeded scaffolding. – Simon Wright Jan 20 '11 at 00:09

1 Answers1

3

For the purpose of testing, I use a child package. It allows to tests the private part. Assuming your package is:

package A is
   type T is private;
private
   type T is new Ada.Finalization.Controlled with ...
end A;

I would test with something like:

package body A.Test is
   procedure Test_Finalize is
       My_T : T;
   begin
       My_T.Finalize;
   end Test_Finalize;
end A.Test;
ciceron
  • 586
  • 5
  • 10
  • I too have found special child packages (sometimes called "bastard children") quite useful for unit tests, due to their ability to get at the internals of a package from the outside. – T.E.D. Jan 20 '11 at 15:31
  • I knew that! Really! Thanks very much, just the job. – Simon Wright Jan 20 '11 at 19:57