%% test-algorithmlite.tex
%% =======================
%% A float of another package in three columns: algorithm and
%% algorithm* of the algorithmlite package, which are made with \@float
%% and \@dblfloat as figure and figure* are.
%%
%%   pdflatex test-algorithmlite.tex   (or xelatex / lualatex / platex /
%%                                      uplatex)
%% The class is article: the expected state depends on its layout.
%% The algorithmlite package is taken from the TeX Live; "make test"
%% skips this test if it is not there.
%%
%% What is covered:
%%   * algorithm* across all columns, at the top and at the bottom;
%%   * algorithm* across two columns with the keys span and at, from
%%     the first column at the top and from the last column at the
%%     bottom, on one page;
%%   * a figure* after them: it is put on the next page, as the floats
%%     wait in one list;
%%   * algorithm in a column, floating and with [H];
%%   * \caption in each of them, and \listofalgorithms in one column.
%%
%% What to check:
%%   * NCOL-RESULT OK, and no error: the state of each page is the one
%%     that is expected, each algorithm is typeset with the width of
%%     its columns (NCOL-ALG NG otherwise), and the algorithm with [H]
%%     is in the text where it is given.
\edef\NCOLclassopt{\ifdefined\ptexversion dvipdfmx\fi}
\expandafter\documentclass\expandafter[\NCOLclassopt]{article}
\usepackage[columns=3]{ncolumn}
\usepackage{algorithmlite}
\usepackage{algorithmic}
\input{test-common}
\NCOLexpect{
  {page 1: N=3 fnres=0pt}
    {col 1: top=0.0pt bot=0.0pt marks=[a]..[a]}
    {col 2: top=0.0pt bot=0.0pt marks=[a]..[a]}
    {col 3: top=0.0pt bot=0.0pt marks=[a]..[a]}
  {page 2: N=3 fnres=0pt}
    {col 1: top=120.0pt bot=120.0pt marks=[b]..[b]}
    {col 2: top=120.0pt bot=120.0pt marks=[b]..[b]}
    {col 3: top=120.0pt bot=120.0pt marks=[b]..[b]}
    {span: top from column 1, 3 wide, at 0.0pt}
    {span: bot from column 1, 3 wide, at 0.0pt}
  {page 3: N=3 fnres=0pt}
    {col 1: top=120.0pt bot=0.0pt marks=[c]..[c]}
    {col 2: top=120.0pt bot=120.0pt marks=[c]..[c]}
    {col 3: top=0.0pt bot=120.0pt marks=[c]..[c]}
    {span: top from column 1, 2 wide, at 0.0pt}
    {span: bot from column 2, 2 wide, at 0.0pt}
  {page 4: N=3 fnres=0pt}
    {col 1: top=0.0pt bot=0.0pt marks=[d]..[e]}
    {col 2: top=50.0pt bot=0.0pt marks=[e]..[e]}
    {col 3: top=50.0pt bot=0.0pt marks=[e]..[e]}
    {span: top from column 2, 2 wide, at 0.0pt}
  {page 5: N=1 fnres=0pt}
    {col 1: top=0.0pt bot=0.0pt marks=[d]..[e]}
}
\ExplSyntaxOn
%% \NCOLalg{<environment>}[<specifier>]{<columns>}: an algorithm with a
%% caption in a box of 100pt, so that its height does not depend on
%% the fonts.  It has to be as wide as <columns> columns.
\NewDocumentCommand \NCOLalg { m O{} m }
  {
    \begin {#1} [#2]
      \dim_compare:nNnF
        {
          \dim_abs:n
            { \hsize - \columnwidth * #3 - \columnsep * ( #3 - 1 ) }
        }
        < { 0.01pt }
        {
          \NCOLng
            {
              ALG~NG:~#1~[#2]:~width~ \dim_use:N \hsize ,~
              want~#3~columns
            }
        }
      \vbox_to_ht:nn { 100pt }
        {
          \caption { #1 ~ [#2] }
          \begin { algorithmic }
            \STATE $y \leftarrow 1$
            \FOR { $i = 1$ ~ to ~ $n$ }
              \STATE $y \leftarrow y \times i$
            \ENDFOR
          \end { algorithmic }
          \tex_vss:D
        }
    \end {#1}
  }
%% \NCOLhere{<code>}: <code> has to add at least 100pt to the page that
%% is being built (an algorithm with [H]).
\dim_new:N \g__ncoltest_here_dim
\NewDocumentCommand \NCOLhere { +m }
  {
    \par
    \dim_gset_eq:NN \g__ncoltest_here_dim \pagetotal
    #1
    \par
    \dim_compare:nNnT
      { \pagetotal - \g__ncoltest_here_dim } < { 100pt }
      {
        \NCOLng
          {
            ALG~NG:~[H]:~the~page~grew~by~
            \dim_eval:n { \pagetotal - \g__ncoltest_here_dim }
          }
      }
  }
%% \NCOLcount{<n>}: <n> algorithms are numbered so far.
\NewDocumentCommand \NCOLcount { m }
  {
    \int_compare:nNnF { \value { algorithm } } = {#1}
      {
        \NCOLng
          { ALG~NG:~ \arabic { algorithm } ~ algorithms,~want~#1 }
      }
  }
\ExplSyntaxOff
\begin{document}
\NCOLmark{a}
\NCOLlines{10}
\NCOLalg{algorithm*}[t]{3}
\NCOLalg{algorithm*}[b]{3}
\NCOLlines{150}
\NCOLmark{b}
\NCOLalg{algorithm*}[span=2,at=tl]{2}
\NCOLalg{algorithm*}[span=2,at=br]{2}
\NCOLfloat{figure*}[span=2,at=tr]{30pt}
\NCOLlines{90}
\NCOLmark{c}
\NCOLalg{algorithm}[t]{1}
\NCOLlines{60}
\NCOLmark{d}
\NCOLhere{\NCOLalg{algorithm}[H]{1}}
\NCOLmark{e}
\NCOLlines{30}
\onecolumn
\listofalgorithms
\NCOLcount{6}
\end{document}
