:- module(tune_tests, []).

:- use_module(library(option)).
:- use_module(library(array)).
:- use_module(library(array/compile)).
:- use_module(library(array/tuned)).
:- use_module(library(array/tune)).
:- use_module(test_devices, [test_device/2]).

% The tests of the tuned options keep them in a file of their own, and take them, which the
% suite does not, see test_array.pl: the unit sets both flags and puts them back.

:- dynamic saved_flags/2.

tune_setup :-
   current_prolog_flag(array_tuning, Tuning),
   current_prolog_flag(array_tune_file, File0),
   assertz(saved_flags(Tuning, File0)),
   tmp_file(tuned, File),
   set_prolog_flag(array_tune_file, File),
   set_prolog_flag(array_tuning, true).

tune_cleanup :-
   retract(saved_flags(Tuning, File0)),
   tune_file(File),
   set_prolog_flag(array_tuning, Tuning),
   set_prolog_flag(array_tune_file, File0),
   catch(delete_file(File), _, true).

:- begin_tests(tune, [setup(tune_setup), cleanup(tune_cleanup)]).

% The options kept for a device are over those its properties call for, and under those a
% run is given; none is taken when the flag array_tuning is false.
test(kept_over_derived_under_given, [timeout(60), forall(test_device(Name, Dev))]) :-
   compile:derived_options(Dev, Derived),
   store_tuned(Dev, [panel(64, 16), double_stage(0)]),
   device_options(Dev, Opts),
   option(panel(64, 16), Opts), option(double_stage(0), Opts),
   option(vector(W), Derived), option(vector(W), Opts),
   compile:exec_device([panel(32, 8)], Name, _, _-Given),
   option(panel(32, 8), Given), option(double_stage(0), Given),
   setup_call_cleanup(set_prolog_flag(array_tuning, false),
                      device_options(Dev, Untuned),
                      set_prolog_flag(array_tuning, true)),
   Untuned == Derived,
   forget_tuned(Dev),
   device_options(Dev, Forgotten),
   Forgotten == Derived.

% The options are read from the file again by another process: here, after another file
% was attached in its place.
test(kept_on_disk, [timeout(60)]) :-
   once(test_device(_, Dev)),
   store_tuned(Dev, [panel(128, 32)]),
   tune_file(File),
   tmp_file(other, Other),
   setup_call_cleanup(set_prolog_flag(array_tune_file, Other),
                      device_options(Dev, _),
                      ( set_prolog_flag(array_tune_file, File), catch(delete_file(Other), _, true) )),
   device_options(Dev, Opts),
   option(panel(128, 32), Opts),
   forget_tuned(Dev).

% An entry measured by another version of the tuner is ignored.
test(other_version_ignored, [timeout(60)]) :-
   once(test_device(_, Dev)),
   compile:derived_options(Dev, Derived),
   store_tuned(Dev, []),
   tuned:identity(Dev, Id),
   tune_version(V), Old is V - 1,
   with_mutex(tuned, ( tuned:retractall_tuned(Id, _, _), tuned:assert_tuned(Id, Old, [panel(64, 16)]),
                       retractall(tuned:known_tuned(_, _, _)) )),
   device_options(Dev, Opts),
   Opts == Derived,
   forget_tuned(Dev).

% A tuning of a small product on a grid of one or two candidates each: its options are among
% them or the derived ones, kept by tune_device/2 and taken by device_options/2.
test(tune_small_grid, [timeout(300)]) :-
   once(test_device(Name, Dev)),
   compile:derived_options(Dev, Derived),
   Grid = [grid(tile_rows, [2]), grid(tile_cols, []), grid(panel_k, [64]), grid(panel_m, [16]),
           grid(stage, [8]), grid(group, []), grid(double_tile_rows, [2]), grid(double_tile_cols, []),
           grid(double_panel_k, [64]), grid(double_panel_m, [16]), grid(double_stage, [0])],
   Options = [shapes([64-64-64]), check(20-30-10), reps(1), gain(-1.0) | Grid],
   tune_device(Name, Options),
   device_options(Dev, Opts),
   option(tile([TI, TJ]), Opts), option(tile([TI0, TJ0]), Derived),
   memberchk(TI, [2, TI0]), TJ == TJ0,
   forget_tuned(Dev).

:- end_tests(tune).
