\input regression-test.tex
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzphysics}

\newdimen\tikzphysicstestx
\newdimen\tikzphysicstesty
\newdimen\tikzphysicstestxx
\newdimen\tikzphysicstestyy
\newdimen\tikzphysicstestdiff
\newdimen\tikzphysicstestexpected

\newcommand{\tikzphysicsgetpoint}[2]{%
  \pgfextractx{\tikzphysicstestx}{\pgfpointanchor{#1}{#2}}%
  \pgfextracty{\tikzphysicstesty}{\pgfpointanchor{#1}{#2}}%
}

\newcommand{\tikzphysicsassertdim}[3]{%
  \tikzphysicstestexpected=#2\relax
  \tikzphysicstestdiff=#1\relax
  \advance\tikzphysicstestdiff by -\tikzphysicstestexpected\relax
  \ifdim\tikzphysicstestdiff<0pt \tikzphysicstestdiff=-\tikzphysicstestdiff\fi
  \ifdim\tikzphysicstestdiff<0.02pt
    \TYPE{PASS: #3}%
  \else
    \TYPE{FAIL: #3 (got \the#1, expected \the\tikzphysicstestexpected)}%
  \fi
}

\newcommand{\tikzphysicsassertpoint}[5]{%
  \tikzphysicsgetpoint{#1}{#2}%
  \tikzphysicstestxx=\tikzphysicstestx
  \tikzphysicstestyy=\tikzphysicstesty
  \tikzphysicsgetpoint{#3}{#4}%
  \tikzphysicsassertdim{\tikzphysicstestxx}{\tikzphysicstestx}{#5 x}%
  \tikzphysicsassertdim{\tikzphysicstestyy}{\tikzphysicstesty}{#5 y}%
}

\newcommand{\tikzphysicsassertnumber}[3]{%
  \pgfmathparse{abs((#1)-(#2))}%
  \ifdim\pgfmathresult pt<0.001pt
    \TYPE{PASS: #3}%
  \else
    \TYPE{FAIL: #3 (difference \pgfmathresult)}%
  \fi
}


\makeatletter
\begin{document}
\START
\setbox0=\hbox{\begin{tikzpicture}
  \tikzset{every block/.style={minimum width=2cm},
    every physics object/.style={line width=1pt}}
  \node[block] (B) {};
  \tikzphysicsgetpoint{B}{east}
  \tikzphysicsassertdim{\tikzphysicstestx}{1cm}{block hook overrides built-in width}
  \node[block,minimum width=3cm] (C) {};
  \tikzphysicsgetpoint{C}{east}
  \tikzphysicsassertdim{\tikzphysicstestx}{1.5cm}{local options override hook}
\end{tikzpicture}}
\setbox0=\hbox{\begin{tikzpicture}
  \node[block] (B) {};
  \tikzphysicsgetpoint{B}{east}
  \tikzphysicsassertdim{\tikzphysicstestx}{.5cm}{hook does not leak from picture}
  \tikzphysicsgetpoint{B}{north}
  \tikzphysicsassertdim{\tikzphysicstesty}{.5cm}{default block height is 1cm}
  \node[pulley] (DefaultPulley) {};
  \tikzphysicsgetpoint{DefaultPulley}{east}
  \tikzphysicsassertdim{\tikzphysicstestx}{.5cm}{default pulley diameter is 1cm}
  \draw[spring,pre length=0,post length=0mm] (0,0)--(2,0);
  \TYPE{PASS: zero spring leads compile}
  \node[wedge,wedge right angle at=bl,wedge angle=30] (W) {};
  \tikzphysicsassertpoint{W}{surface-0}{W}{top}{descending contact starts at top}
  \tikzphysicsassertpoint{W}{surface-100}{W}{br}{descending contact ends at br}
  \tikzphysicsassertpoint{W}{surface-50}{W}{slope-mid}{contact agrees with slope midpoint}
  \node[wedge,wedge right angle at=top,wedge angle=30] (T) {};
  \tikzphysicsassertpoint{T}{surface-50}{T}{slope-mid}{top left contact midpoint}
  \tikzphysicsassertpoint{T}{right-surface-50}{T}{slope-right-mid}{top right contact midpoint}
  \path let \p1=($(T.tangent-after-50)-(T.tangent-before-50)$),
            \p2=($(T.normal-50)-(T.surface-50)$),
            \n1={\x1*\x2+\y1*\y2}
    in \pgfextra{\tikzphysicsassertnumber{\n1}{0}{normal perpendicular to tangent}};
  \node[prism,minimum width=3cm,minimum height=3cm] (Prism) {};
  \tikzphysicsgetpoint{Prism}{centroid}
  \tikzphysicsassertdim{\tikzphysicstesty}{-.5cm}{prism centroid uses resolved height}
  \tikzphysicsgetpoint{Prism}{left-mid}
  \tikzphysicsassertdim{\tikzphysicstestx}{-.75cm}{prism left face midpoint}
  \tikzphysicsassertpoint{Prism}{right-mid}{Prism}{exit-mid}{prism exit midpoint alias}
  \node[pulley] (P) at (0,0) {};
  \coordinate (A) at (-2,-1);
  \coordinate (Z) at (2,-1);
  \draw[rope] (A) to[over pulley=P] node[right] {$T$} (Z);
  \tikzset{string route=under,
    physics over pulley/.append code={%
      \TYPE{ROUTE: \tikzphysics@mech@stringstartsolution/\tikzphysics@mech@stringendsolution}%
      \tikzphysicsassertdim{\pgflinewidth}{2pt}{helper honours local line width}}}

  \physicsstringoverpulley[line width=2pt]{A}{P}{Z}
  \tikzphysicsassertnumber{\tikzphysics@mech@stringstartsolution}{1}{scope route start preserved}
  \tikzphysicsassertnumber{\tikzphysics@mech@stringendsolution}{2}{scope route end preserved}
  \pic[pendulum angle=0,pendulum length=2cm] (Q) {pendulum};
  \tikzphysicsgetpoint{Q-bob}{center}
  \tikzphysicsassertdim{\tikzphysicstestx}{0pt}{pendulum zero angle x}
  \tikzphysicsassertdim{\tikzphysicstesty}{-2cm}{pendulum centre-to-centre length}
  \pic[support size=1cm] (S) {roller-support};
  \tikzphysicsgetpoint{S-base}{center}
  \tikzphysicsassertdim{\tikzphysicstesty}{-1cm}{support size reaches base}
\end{tikzpicture}}
% Count emissions without rendering their large reference cards in this test.
\def\tikzphysics@debug@drawanchors#1{\TYPE{ANCHORS: #1}}
\def\tikzphysics@debug@drawkeys#1{\TYPE{KEYS: #1}}
\setbox0=\hbox{\begin{tikzpicture}[show anchors,show keys]
  \node[block] (enabled) {};
  \node[block,physics debug/all=false] (disabled) {};
  \node[block,show anchors=false] (keys-only) {};
  \node[block,show keys=false] (anchors-only) {};
\end{tikzpicture}}
\setbox0=\hbox{\begin{tikzpicture}
  \node[wedge,show anchors] (local) {};
  \node[wedge] (quiet) {};
  \begin{scope}[name prefix=prefix-,show keys]
    \node[block,show anchors,physics debug/anchor dot color=blue] (named) {};
    \edef\SavedColor{\csname tikzphysics@debug@saved@prefix-named@anchor dot color\endcsname}
    \def\ExpectedColor{blue}
    \ifx\SavedColor\ExpectedColor\TYPE{PASS: local debug colour saved with prefixed name}\else\TYPE{FAIL: local debug colour snapshot}\fi
  \end{scope}
\end{tikzpicture}}
\END
\end{document}
