\documentclass{article}

\makeatletter
\usepackage{pgf}
\usepgflibrary{luamath,fpu}

\begin{document}

X

\def\SHOW#1{%
    \dimen0=#1 %
    #1 = \the\dimen0
}

\SHOW{1pt}

\SHOW{1mm}

\SHOW{1cm}

\SHOW{1in}

\SHOW{1ex}

\SHOW{1em}

\SHOW{1bp}

\SHOW{1pc}

\SHOW{1dd}

\SHOW{1cc}

\SHOW{1sp}

%\tracingmacros=2 \tracingcommands=2
%
\newif\ifcomparewithtex
\newif\ifcomparewithfpu
\pgfkeys{
    /ut/compare with TeX/.is if=comparewithtex,
    /ut/compare with TeX=true,
    /ut/compare with fpu/.is if=comparewithfpu,
    /ut/compare with fpu=true,
    /ut/.search also={/pgf/luamath},
    /pgf/luamath/show error message=true,
    /pgf/luamath/enable TeX fallback=false,
}

\newcount\numErrors

% #1: options
% #2: expression
% #3: expected result
% #4: expected 'units declared' (1 or 0)
\def\parsertest{\pgfutil@ifnextchar[\parsertest@{\parsertest@[]}}%
\def\parsertest@[#1]#2#3#4{%
    \begingroup
    \pgfqkeys{/ut}{#1}%
    \pgfluamathparse{#2}%
    \let\actual=\pgfmathresult
    \ifpgfmathunitsdeclared
        \def\actualunitsdeclared{1}%
    \else
        \def\actualunitsdeclared{0}%
    \fi
    %
    \ifcomparewithtex
        \ifpgfluamathusedTeXfallback
            \let\expectedTeX=\actual
        \else
            \pgfmathparse{#2}%
            \let\expectedTeX=\pgfmathresult
        \fi
    \else
        \def\expectedTeX{--}%
    \fi
    %
    \edef\expected{#3}%
    \def\success{1}%
    \ifx\expected\empty
        \ifx\actual\empty
        \else
            \def\success{0}%
        \fi
    \else
        \ifx\actual\expected
        \else
            \def\success{0}%
        \fi
    \fi
    %
    \if1\success
    \else
        \message{FAILURE for #2 expected \expected\space but was \actual^^J}%
        \global\advance\numErrors by1
    \fi
    %
    \if1\success
        \def\expectedunitsdeclared{#4}%
        \ifx\actualunitsdeclared\expectedunitsdeclared
        \else
            \def\success{0}%
            \message{FAILURE for #2 expected units declared #4 but was \actualunitsdeclared^^J}%
            \global\advance\numErrors by1
        \fi
    \fi
    %
    \if1\success
        \ifcomparewithtex
            % check against PGF's basic layer as reference:
            \dimen0=\actual pt %
            \dimen1=\expectedTeX pt %
            \advance\dimen0 by-\dimen1
            \ifdim\dimen0<0sp
                \dimen0=-\dimen0
            \fi
            \ifdim\dimen0 > 0.004pt %
                \def\success{0}%
                \message{FAILURE for #2 : matches expectations, but does NOT match TeX output (error \the\dimen0). expected \expectedTeX\space but was \actual^^J}%
                \global\advance\numErrors by1
            \fi
            %
            %
            \ifpgfmathunitsdeclared
                \def\actualTeXunitsdeclared{1}%
            \else
                \def\actualTeXunitsdeclared{0}%
            \fi
            \ifx\actualTeXunitsdeclared\actualunitsdeclared
            \else
                \message{WARNING for #2 : matches ALL expectations, but TeX's output does NOT match TeX expected units declared (TeX returns \actualTeXunitsdeclared, expectation = LUA is \actualunitsdeclared)^^J}%
            \fi
        \fi
        %
        \ifcomparewithfpu
            % check that FPU works as well. This
            % * tests the FPU
            % * tests that FPU values can be injected into the LUA backend:
            \begingroup
            \pgfkeys{/pgf/fpu,/pgf/fpu/output format=float}%
            \pgfmathparse{#2}%
            \xdef\actualfpu{\pgfmathresult}%
            \ifpgfmathunitsdeclared
                \xdef\actualfpuunitsdeclared{1}%
            \else
                \xdef\actualfpuunitsdeclared{0}%
            \fi
            \endgroup
            %
            \pgfkeys{/pgf/luamath/output format=fixed}%
            \pgfluamathparse{abs(\actual - \actualfpu) > 0.004}%
            \ifdim\pgfmathresult pt=1pt
                \def\success{0}%
                \pgfluamathparse{abs(\actual - \actualfpu)}%
                \message{FAILURE for #2 : matches expectations, but does NOT match output of FPU (error \pgfmathresult). fpu=\actualfpu\space lua=\actual^^J}%
                \global\advance\numErrors by1
            \fi
        \fi
        %
    \fi
    %
    \message{#2 = \actual\space (pgf=\expectedTeX) \if1\success OK\else FAILURE\fi^^J}%
    \endgroup
}%

\parsertest{1}{1.0}{0}
\parsertest{  1}{1.0}{0}
\parsertest{-123}{-123.0}{0}
\parsertest{+123}{123.0}{0}
\parsertest{+1.23}{1.23}{0}
\parsertest{+.123}{0.123}{0}
\parsertest{1.23}{1.23}{0}
\parsertest{1.}{1.0}{0}
\parsertest{0.123}{0.123}{0}
\parsertest{.123}{0.123}{0}
\parsertest{1e4}{10000.0}{0}
\parsertest{1e-4}{0.0001}{0}
\parsertest{1e+4}{10000.0}{0}
% only supported by FPU
\parsertest[compare with TeX=false]{1Y1.23e4]}{12300.0}{0}
\parsertest{1.23e-004}{0.000123}{0}
\parsertest{004}{4.0}{0}
\parsertest{1 + 10}{11.0}{0}
\parsertest{1 - 10}{-9.0}{0}
\parsertest{(1)}{1.0}{0}
\parsertest{3 + 5*9 / (1+1) - 12}{13.5}{0}
\parsertest{pi}{3.141593}{0}
\parsertest{sin(0)}{0.0}{0}
\parsertest{max(0,1)}{1.0}{0}
\parsertest{min(0.01,-5)}{-5.0}{0}
\parsertest{sin(90)}{1.0}{0}
\parsertest{cos(90)}{0.0}{0}
\parsertest{pow(2,3)}{8.0}{0}
\parsertest{-pow(2,3)}{-8.0}{0}
\parsertest{-pi}{-3.141593}{0}
% this is actually UNSUPPORTED by TeX:
\parsertest[compare with TeX=false]{inf}{inf}{0}
\parsertest[compare with TeX=false]{INF}{inf}{0}
\parsertest{not(100)}{0.0}{0}%  non-trivial since the function is pgfluamathfunctions.notPGF
\parsertest{2^2}{4.0}{0}
% only supported by FPU
\parsertest[compare with TeX=false]{1Y2.0e0]^2}{4.0}{0}
\parsertest{0-2^2}{-4.0}{0}
\parsertest{-2^2}{-4.0}{0}
\parsertest{pi^2}{9.869604}{0}
\parsertest{2^2+2}{6.0}{0}
\parsertest{2^2-1}{3.0}{0}
\parsertest{-1 + 4}{3.0}{0}
\parsertest{2^2*5}{20.0}{0}
\parsertest{2^(2+2)}{16.0}{0}
\parsertest{multiply(2,3)^2}{36.0}{0}
\parsertest{(2+3)^2}{25.0}{0}
\parsertest{(2 + 3 ) ^ 2}{25.0}{0}
\parsertest{2^add(1,1)}{4.0}{0}
\parsertest{3!}{6.0}{0}
\parsertest{1+3!}{7.0}{0}
\parsertest{2*3!}{12.0}{0}
\parsertest{3! + 1}{7.0}{0}
\parsertest{3 !}{6.0}{0}
\parsertest{3*2+4}{10.0}{0}
\pgfmathdeclarefunction{x}{0}{\def\pgfmathresult{4}}%
\pgfmathdeclarefunction{N1}{3}{\pgfmathparse{#1+#2+#3}}%
\directlua{
function pgfluamathfunctions.x()
    return 4
end
function pgfluamathfunctions.N1(x,m,n)
    return x+m+n
end
}
\parsertest{2^x}{16.0}{0}
\parsertest{exp(-x^2)}{0.0}{0} % requires output format=fpu
\parsertest{N1(1,2,3)}{6.0}{0}
\parsertest{ 1 + 2 * 4 ^ 2 }{33.0}{0}
\parsertest{-x + 4}{0.0}{0}
\parsertest{-x * 4}{-16.0}{0}
\parsertest{-x * - 4}{16.0}{0}
\parsertest{2*pi r}{360.0}{0}
\parsertest{6.28318530717959 r}{360.0}{0}
\parsertest{sin(2*pi r)}{-0.0}{0}
\parsertest{1.5707963267949r + 1.5707963267949r}{180.0}{0}
\parsertest{1 ? 42 : 0}{42.0}{0}
\parsertest{0 ? 42 : 0}{0.0}{0}
\parsertest{-1 + 1 ? 42 : 0}{0.0}{0}
\parsertest{1 + (1 ? 42 : 0)}{43.0}{0}
\parsertest{1 ? 42 : 0 ? 5 : 6}{5.0}{0}
\parsertest{(1 ? 42 : 0) ? 5 : 6}{5.0}{0}
\parsertest{43 == 43}{1.0}{0}
\parsertest{43 == 42}{0.0}{0}
\parsertest{43 != 43}{0.0}{0}
\parsertest{43 != 42}{1.0}{0}
\parsertest{43 > 42}{1.0}{0}
\parsertest{43 > 44}{0.0}{0}
\parsertest{43 < 42}{0.0}{0}
\parsertest{43 < 44}{1.0}{0}
\parsertest{43 <= 44}{1.0}{0}
\parsertest{43 >= 44}{0.0}{0}
\parsertest{43 >= 44 == 1}{0.0}{0}
\parsertest{!1}{0.0}{0}
\parsertest{! 1}{0.0}{0}
\parsertest{! -1}{0.0}{0}
\parsertest{--1}{1.0}{0}
\parsertest{! !1}{1.0}{0}
\parsertest{3! - !0}{5.0}{0}
\parsertest{1 && 1 }{1.0}{0}
\parsertest{1 && 0 || 1 }{1.0}{0}
\parsertest{1 && 0 && 1 }{0.0}{0}
\parsertest{1 || 0 }{1.0}{0}
\parsertest{0 || 0 || 1 }{1.0}{0}

% I test every function here to see that it is connected correctly
\parsertest{abs(1)}{1.0}{0}
\parsertest{add(1,2)}{3.0}{0}
\parsertest{and(1,1)}{1.0}{0}
% \parsertest{array(1)}{1.0}{0}
\parsertest{asin(0.7071)}{44.999451}{0}
\parsertest{atan(1)}{45.0}{0}
\parsertest{atan2(-4,3)}{-53.130102}{0}
% \parsertest{bin(1)}{1.0}{0}
\parsertest{cos(60)}{0.5}{0}
\parsertest{cosec(30)}{2.0}{0}
\parsertest{cosh(0.5)}{1.127626}{0}
\parsertest{cot(15)}{3.732051}{0}
\parsertest{deg(2*pi)}{360.0}{0}
% \parsertest{depth(1)}{1.0}{0}
\parsertest[compare with fpu=false]{div(1,2)}{0.0}{0}
\parsertest{divide(1,2)}{0.5}{0}
\parsertest{e}{2.718282}{0}
\parsertest{equal(1,2)}{0.0}{0}
\parsertest{factorial(3)}{6.0}{0}
\parsertest{false}{0.0}{0}
\parsertest{floor(0)}{0.0}{0}
\parsertest{floor(0.6)}{0.0}{0}
\parsertest{floor(1.6)}{1.0}{0}
\parsertest{floor(1)}{1.0}{0}
\parsertest{floor(100)}{100.0}{0}
\parsertest{floor(-100)}{-100.0}{0}
\parsertest{floor(-0.6)}{-1.0}{0}
\parsertest{floor(-1.6)}{-2.0}{0}
\parsertest{floor(-15000.6)}{-15001.0}{0}
\parsertest{floor(15000.6)}{15000.0}{0}
\parsertest{ceil(0)}{0.0}{0}
\parsertest{ceil(0.6)}{1.0}{0}
\parsertest{ceil(1.6)}{2.0}{0}
\parsertest{ceil(-0.6)}{-0.0}{0}
\parsertest{ceil(-1.6)}{-1.0}{0}
\parsertest{ceil(1)}{1.0}{0}
\parsertest{ceil(100)}{100.0}{0}
\parsertest{ceil(-100)}{-100.0}{0}
\parsertest{ceil(-15000.6)}{-15000.0}{0}
\parsertest{ceil(15000.6)}{15001.0}{0}
\parsertest[compare with fpu=false]{frac(1.1)}{0.1}{0}
\parsertest[compare with fpu=false]{frac(-1.1)}{0.1}{0}
\parsertest[compare with fpu=false]{gcd(42,56)}{14.0}{0}
\parsertest{greater(1,0)}{1.0}{0}
% \parsertest{height(1)}{1.0}{0}
% \parsertest{hex(1)}{1.0}{0}
% \parsertest{Hex(1)}{1.0}{0}
\parsertest{int(1.2)}{1.0}{0}
\parsertest{int(-1.2)}{-1.0}{0}
\parsertest{ifthenelse(1,2,3)}{2.0}{0}
\parsertest[compare with fpu=false]{iseven(2)}{1.0}{0}
\parsertest{isodd(1)}{1.0}{0}
\parsertest[compare with fpu=false]{isprime(3)}{1.0}{0}
\parsertest{less(1,2)}{1.0}{0}
\parsertest{ln(e)}{1.0}{0}
\parsertest{log10(100)}{2.0}{0}
\parsertest{log2(2)}{1.0}{0}
\parsertest{max(1,2)}{2.0}{0}
\parsertest{min(1,2)}{1.0}{0}
\parsertest{mod(20,6)}{2.0}{0}
\parsertest{mod(-100,30)}{-10.0}{0}
\parsertest[compare with fpu=false]{Mod(-100,30)}{20.0}{0}
\parsertest{multiply(1,2)}{2.0}{0}
\parsertest{neg(1)}{-1.0}{0}
\parsertest{not(1)}{0.0}{0}
\parsertest{notequal(1,1)}{0.0}{0}
\parsertest{notgreater(1,1)}{1.0}{0}
\parsertest{notless(1,1)}{1.0}{0}
%\parsertest{oct(1)}{1.0}{0}
\parsertest{or(1,0)}{1.0}{0}
\parsertest{pi}{3.141593}{0}
\parsertest{pow(2,2)}{4.0}{0}
\parsertest{rad(180)}{3.141593}{0}
% done below \parsertest{rand(1)}{1.0}{0}
% done below \parsertest{random(1)}{1.0}{0}
\parsertest[compare with fpu=false]{real(1)}{1.0}{0}
% done below \parsertest{rnd(1)}{1.0}{0}
\parsertest{round(1.5)}{2.0}{0}
\parsertest{round(1.2)}{1.0}{0}
\parsertest{round(1.9)}{2.0}{0}
\parsertest{round(-1.9)}{-2.0}{0}
\parsertest{round(-1.2)}{-1.0}{0}
% \parsertest{scalar(1)}{1.0}{0}
\parsertest{sec(45)}{1.414214}{0}
\parsertest{sign(-1)}{-1.0}{0}
\parsertest{sin(60)}{0.866025}{0}
\parsertest{sinh(0.5)}{0.521095}{0}
\parsertest{sqrt(4)}{2.0}{0}
\parsertest{subtract(1,1)}{0.0}{0}
\parsertest{tan(45)}{1.0}{0}
\parsertest{tanh(0.5)}{0.462117}{0}
\parsertest{true}{1.0}{0}
\parsertest{veclen(12,5)}{13.0}{0}
%\parsertest{width(1)}{1.0}{0}

\parsertest{0 <0.1 && 4 <0.1}{0.0}{0}
\parsertest{0 <0.1 || 4 <0.1}{1.0}{0}
{
\pgfkeys{/pgf/declare function={
    xx=4;
    yy=4;
}}
\parsertest{abs(xx-yy)<0.1 ? -42 : 42}{-42.0}{0}
\parsertest{abs(xx-yy)<0.1 || abs(xx-0.5)<0.1}{1.0}{0}
\parsertest{0 <0.1 || 4 <0.1}{1.0}{0}
\parsertest{(abs(xx-yy)<0.1) || (abs(xx-0.5)<0.1)}{1.0}{0}
\parsertest{abs(xx-yy)<0.1 || abs(xx-0.5)<0.1 ? -42 : 42}{-42.0}{0}
\parsertest{4 < 2 || 5==5 ? -42 : 42}{-42.0}{0}
}

\parsertest{1pt}{1.0}{1}
\parsertest{1mm}{2.845261}{1}
\parsertest{1cm}{28.452744}{1}
\parsertest{1in}{72.269989}{1}
% \parsertest{1ex}{4.305542}{1}
% \parsertest{1em}{10.000015}{1}
\parsertest{1bp}{1.003738}{1}
\parsertest{1dd}{1.070007}{1}
\parsertest{1cc}{12.840103}{1}
\parsertest{1sp}{0.000015}{1}
\parsertest{1pc}{12.0}{1}
% these TeX macros must be defined and set, of course!



\count1=43
% this is actually UNSUPPORTED by TeX:
\parsertest[compare with TeX=false,compare with fpu=false]{\count1}{43.0}{0}
%
\newdimen\luamathparse@dimen
\luamathparse@dimen=42pt
\parsertest{1*\luamathparse@dimen}{42.0}{1}
\parsertest{2\luamathparse@dimen}{84.0}{1}
%
\newcount\luamathparse@count
\luamathparse@count=42
\parsertest{1*\luamathparse@count}{42.0}{0}

% this is a BUG in TeX, but works in LUA...:
\parsertest[compare with TeX=false,compare with fpu=false]{0.5\luamathparse@count}{21.0}{0}
%
\setbox0=\hbox{1233}
\parsertest{\wd0}{20.000061}{1}
\parsertest{\ht0}{6.444443}{1}
\parsertest{\dp0}{0.0}{1}

%--------------------------------------------------
% FIXME : these cases are still missing:
% if false then
% -- arrays created via '{}' and indexed with '[]'
% -- strings with "<str>"
% -- 'scalar' function
% -- hex/octal/binary input
% -- tex registers
% -- What happens for undefined functions!? --> return nil and let TeX invoke its parser (no warning!?)
% -- width/height/depth
% end
%--------------------------------------------------


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The following tests are just for the TeX part, not for the LUA part.
%
%

\parsertest[output format=float,compare with TeX=false]{4+4}{1Y8.0e+00]}{0}

\begingroup
\pgfmathdeclarefunction{testfct}{0}{\def\pgfmathresult{42.42}}
\parsertest[parser,compare with TeX=false,compare with fpu=false,show error message=false]{testfct}{}{0}
\parsertest[parser,enable TeX fallback]{testfct}{42.42}{0}

\def\macro{1234}
\pgfkeys{/pgf/declare function={res(\x)=\x*\macro;}}
\parsertest[enable TeX fallback=true]{res(1)}{1234.0}{0}
\endgroup

% Check that setseed is communicated to LUA:
\pgfmathsetseed{123}
\parsertest[compare with TeX=false,compare with fpu=false]{rnd}{0.788318}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{rnd}{0.203068}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{rand}{-0.302874}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{rand}{-0.276781}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{random}{0.134639}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{random}{0.375968}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{random(4)}{2.0}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{random(4)}{1.0}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{random(4,10)}{10.0}{0}
\parsertest[compare with TeX=false,compare with fpu=false]{random(4,10)}{8.0}{0}

\begingroup
% Check that 'trig format' is property communicated to LUA and
% implemented correctly:
\parsertest{sin(50)}{0.766044}{0}%
\parsertest{cos(50)}{0.642788}{0}%
\parsertest{tan(50)}{1.191754}{0}%
\parsertest{asin(0.766044)}{49.999961}{0}%
\parsertest{acos(0.642788)}{49.999971}{0}%
\parsertest{atan(1.191754)}{50.00001}{0}%
\parsertest{atan2(-4,3)}{-53.130102}{0}%
\parsertest{cot(50)}{0.8391}{0}%
\parsertest{sec(50)}{1.555724}{0}%
\parsertest{cosec(50)}{1.305407}{0}%
%
\pgfkeys{/pgf/trig format=rad}
\parsertest{sin(0.5)}{0.479426}{0}%
\parsertest{cos(0.5)}{0.877583}{0}%
\parsertest{tan(0.5)}{0.546302}{0}%
\parsertest{asin(0.5)}{0.523599}{0}%
\parsertest{acos(0.5)}{1.047198}{0}%
\parsertest{atan(0.5)}{0.463648}{0}%
\parsertest{atan2(-4,3)}{-0.927295}{0}%
\parsertest{cot(0.5)}{1.830488}{0}%
\parsertest{sec(0.5)}{1.139494}{0}%
\parsertest{cosec(0.5)}{2.08583}{0}%
\endgroup

\begingroup
\pgfkeys{/pgf/declare function={mu1(\x,\i)=\x^\i;}}

\parsertest{mu1(5,2)}{25.0}{0}
% produces something different in TeX :-(  See docs in LUA
\parsertest[compare with TeX=false]{mu1(-5,2)}{25.0}{0}

\begingroup
\pgfkeys{/pgf/declare function={mu2=42;}}
\parsertest{mu2}{42.0}{0}
\endgroup
% should have been clean up:
\parsertest[show error message=false,compare with TeX=false,compare with fpu=false]{mu2}{}{0}
% ... but this should NOT have been cleaned up!
\parsertest{mu1(5,2)}{25.0}{0}

\endgroup
% should have been clean up:
\parsertest[show error message=false,compare with TeX=false,compare with fpu=false]{mu1(-5,2)}{}{0}


\def\pgfmathifexpressionTest#1{%
    \pgfmathifexpression{1000>999}{%
        % ok
        \message{PASSED for mathifexpression(1000>999)  #1: = '\pgfmathresult'^^J}%
    }{%
        \message{FAILURE for mathifexpression(1000>999)  #1: = '\pgfmathresult'^^J}%
        \global\advance\numErrors by1
    }%
}

\pgfmathifexpressionTest{basicmath}


\begingroup
\pgfkeys{/pgf/fpu}%
\pgfmathifexpressionTest{fpu}
\endgroup

\begingroup
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathifexpressionTest{fpu output fixed}
\endgroup

\begingroup
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}%
\pgfmathifexpressionTest{fpu output sci}
\endgroup

\begingroup
\pgfkeys{/pgf/luamath/parser}%
\pgfmathifexpressionTest{luamath}
\endgroup

\begingroup
\pgfkeys{/pgf/luamath/parser,/pgf/luamath/output format=float}%
\pgfmathifexpressionTest{luamath output float}
\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
% NO MORE TEST CASES HERE!

\ifnum\numErrors>0
    \PackageError{pgf}{Has \the\numErrors\space FAILURES}{}
\else
    \message{All cases PASSED^^J}%
\fi


\end{document}
