# test pointer arithmetic

resource arith ()
	int  a[1:9] = (111,222,333,444,555,666,777,888,999)

	ptr int  p = @a[1]
	ptr int  q = p + 8
	ptr int  r = q - 5
	int  m = q - p
	int  n = r - q
	int  o = r - p
	write (p^, q^, r^, m, n, o, p == p, p ~= p, p == q)

	p++
	q--
	write (" ",p^, q^, r^, q-p, r-q, r-p, q == q, q ~= q, q == r)
	--p
	++q
	write (" ",p^, q^, r^, q-p, r-q, r-p, q == q, q ~= q, q == r)
	p += 1
	q -= 1
	write (" ",p^, q^, r^, q-p, r-q, r-p, q == q, q ~= q, q == r)
	p -= 1
	q += 1
	write (" ",p^, q^, r^, q-p, r-q, r-p, q == q, q ~= q, q == r)

	ptr any  pa = p
	ptr any  qa = 8 + p
	ptr any  ra = @a[4]
	p = pa;
	q = qa;
	r = ra;
	m = q - pa
	n = ra - q
	o = r - pa
	write (p^, q^, r^, m, n, o, p == pa, qa ~= q, pa == qa)

	type pint = ptr int
	pint  pp = @a[1]
	pint  qp = 8 + pp
	pint  rp = qp - 5
	m = qp - pp
	n = r - qp
	o = rp - pa
	write (pp^, qp^, rp^, m, n, o, pp == p, qp ~= qa, pp == qp)

	char  ca[1:9] = chars("123456789")
	ptr char  cp = @ca[1]
	ptr char  cq = cp + 8
	ptr char  cr = cq - 5
	write (cp^, cq^, cr^, cq-cp, cr-cq, cr-cp, cp==cq-8, cq==cr, cq!=cr+5)

	type xrec = rec ( char cv ; int iv ; ptr any  pv )
	xrec  xa[9] 
	for [ i = 1 to 9 ] {
		xa[i].cv = ca[i]
		xa[i].iv = a[i]
		xa[i].pv = @ca[i]
	}
	ptr xrec  px = @xa[1]
	ptr xrec  qx = px + 8
	ptr xrec  rx = qx - 5
	write (px^.cv, qx^.cv, rx^.cv,
		qx-px, rx-qx, rx-px, px==px, qx!=qx, rx==px)
	pa = px
	qa = qx
	ra = rx
	write (px^.iv, qx^.iv, rx^.iv,
		qx-pa, ra-qx, ra-px, pa==px, qx!=qa, ra==px)

	px++
	qx--
	write (" ",px^.iv, qx^.iv, rx^.iv,
		qx-px, rx-qx, rx-px, rx==ra, qx==qa, px==pa)
end
