--
--	cgtest90.occ -- RECURSIVE FUNCTIONs
--	Copyright (C) 2007 University of Kent
--
--	This program is free software; you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--



PROC cgtest90 (CHAN OF BYTE keyb?, scr!, err!)

  #INCLUDE "cgmain.occ"
  #INCLUDE "cgreal.occ"

  --{{{  tests
  --{{{  test0 -- recursive functions
  PROC test0 ()
    --{{{  simple recursive functions (both syntaxes)
    INT REC FUNCTION sum (VAL INT n)
      INT r:
      VALOF
        IF
          n < 2
            r := 1
          TRUE
            r := n + sum (n - 1)
        RESULT r
    :
    INT RECURSIVE FUNCTION factorial (VAL INT n)
      INT r:
      VALOF
        IF
          n < 2
            r := 1
          TRUE
            r := n * factorial (n - 1)
        RESULT r
    :
    --}}}
    --{{{  Ackermann's function
    INT64 RECURSIVE FUNCTION ackermann (VAL INT64 m, n)
      INT64 r:
      VALOF
        IF
          m = 0
            r := n + 1
          n = 0
            r := ackermann (m - 1, 1)
          TRUE
            r := ackermann (m - 1, ackermann (m, n - 1))
        RESULT r
    :
    --}}}

    SEQ
      check.INT (sum (1), 1, "sum (1)")
      check.INT (sum (10), 55, "sum (10)")
      check.INT (factorial (1), 1, "factorial (1)")
      check.INT (factorial (6), 720, "factorial")
      check.INT64 (ackermann (4, 0), 13, "ackermann (4, 0)")
      check.INT64 (ackermann (3, 4), 125, "ackermann (3, 4)")
  :
  --}}}
  --}}}

  SEQ
    init ("CG test 90", "RECURSIVE FUNCTIONs")
    test0 ()
    finish ()
:

