%%%% ========================================================================
%%%%  @MP-file{
%%%%     author              = "Ulrik Vieth",
%%%%     version             = "1.05",
%%%%     date                = "02 May 1995",
%%%%     time                = "11:25:12 MET",
%%%%     filename            = "testeq.mp",
%%%%     checksum            = "19918 79 502 3693",
%%%%     email               = "vieth@thphy.uni-duesseldorf.de",
%%%%     codetable           = "ISO/ASCII",
%%%%     keywords            = "MetaPost, equality test",
%%%%     supported           = "yes",
%%%%     abstract            = "This file provides a general equality test
%%%%                            macro for objects of any type based on
%%%%                            code from Appendix D of `The_METAFONTbook',
%%%%                            but adapted for MetaPost. Unfortunately
%%%%                            it is no longer fully general, because
%%%%                            MetaPost lacks bitmap-oriented operation
%%%%                            like `cull' and `totalweight' needed when
%%%%                            testing for the equality of two pictures.
%%%%
%%%%                            A particular useful feature of this package
%%%%                            is the `patheq' macro to test if two paths
%%%%                            are actually the same. This might come
%%%%                            handy when processing data files with the
%%%%                            MetaPost graph package that contain data
%%%%                            for an unknown number of paths separated
%%%%                            by blank lines. In that case one might use
%%%%                            `patheq' to check whether the last path
%%%%                            read is actually the same as the first one,
%%%%                            having started over from the beginning.
%%%%                            ",
%%%%     docstring           = "The checksum field above contains a CRC-16
%%%%                            checksum as the first value, followed by
%%%%                            the equivalent of the standard UNIX wc
%%%%                            (word count) utility output of lines,
%%%%                            words, and characters.  This is produced
%%%%                            by Robert Solovay's checksum utility.",
%%%%  }
%%%% ========================================================================

% See Appendix D of The METAFONTbook for explanations!

tertiarydef p == q =
 if unknown p or unknown q: false
 elseif boolean p and boolean q: p=q
 elseif numeric p and numeric q: p=q
 elseif pair p and pair q: p=q
 elseif color p and color q: p=q				% added
 elseif string p and string q: p=q
 elseif transform p and transform q: p=q
 elseif path p and path q:
  if (cycle p = cycle q) and (length p = length q)
   and (point 0 of p = point 0 of q): patheq p of q
  else: false fi
 elseif pen p and pen q: (makepath p == makepath q)
 elseif picture p and picture q: % piceq p of q			% changed
	errmessage("Sorry, I can't compare pictures"); false	% added
 elseif vacuous p and vacuous q: true
 else: false fi enddef;

vardef vacuous primary p =
 not(boolean p or numeric p or pair p or path p
  or pen p or picture p or string p or transform p) enddef;

vardef patheq expr p of q =
 save t; boolean t; t=true;
 for k=1 upto length p:
  t := (postcontrol k-1 of p = postcontrol k-1 of q)
   and (precontrol k of p = precontrol k of q)
   and (point k of p = point k of q);
  exitunless t; endfor
 t enddef;

%vardef piceq expr p of q =
% save t; picture t;
% t=p; addto t also -q;
% cull t dropping origin;
% (totalweight t=0) enddef;
