% The smoke test of the array pack: what pack_install and ctest run after a build.
%
% swipl -g test_smoke -t halt test/test_smoke.pl
%
% It checks, in seconds, what a new machine is likeliest to get wrong: the foreign libraries
% load, at least one OpenCL device is found, and a small program runs on each device found and
% gives the elements of the reference interpreter. The suite of the library is
% test/test_array.pl, run by hand: see README.md.

:- module(test_smoke, [test_smoke/0]).
:- use_module(library(plunit)).

:- multifile user:file_search_path/2.
:- dynamic user:file_search_path/2.

:- prolog_load_context(directory, Test),
   file_directory_name(Test, Root),
   directory_file_path(Root, prolog, Library),
   current_prolog_flag(arch, Arch),
   atomic_list_concat([Root, lib, Arch], /, Foreign),
   asserta(user:file_search_path(library, Library)),
   asserta(user:file_search_path(foreign, Foreign)).

:- use_module(library(array)).
:- use_module(library(array/opencl), [opencl_device/2]).

test_smoke :-
   run_tests([smoke]).

% C = A B, for A of N x M and B of M x P: the first program of README.md.
matmul(A, B, C, N, M, P) :-
   shape(A, [N, M]), shape(B, [M, P]), shape(C, [N, P]),
   over([I:N, J:P], [C[I, J] := 0]),
   over([I:N, J:P, K:M], [C[I, J] := C[I, J] + A[I, K] * B[K, J]]).

:- begin_tests(smoke).

test(an_opencl_device_is_found, [timeout(60)]) :-
   once(opencl_device(_, _)).

test(a_program_runs_on_each_device, [timeout(300), forall(opencl_device(D, _)), true(Values == Ref)]) :-
   matmul(A, B, C, 2, 3, 2),
   Data = [A-[1, 2, 3, 4, 5, 6], B-[1, 0, 0, 1, 1, 1]],
   eval(C, Data, Ref),
   run(C, Data, Values, [device(D)]).

:- end_tests(smoke).
