; .EnTete "Le←Lisp version 15.21" " " "Test des fonctions interpre'te'es"
; .EnPied "" "" ""
; .SuperTitre "Test des fonctions interpre'te'es"
; .Auteur "Francois Morain, Jean Vuillemin"
; .INRIA

(add-feature 'testgen)

(unless (>= (version) 15.2)
        (error 'load "fichier non compatible" 'testfn))

(unless (featurep 'testcomm)
        (libload testcomm))

(unless (featurep 'z)
        (print "Je charge les generiques interpretes")
        (libload bnz))

(unless (featurep 'q)
        (print "Je charge les rationnels interpretes")
        (libload bnq))

(defvar #:sys-package:colon 'R)
 
;.Section "Nombres Harmoniques et Fonction Zeta."
; Calcule 1 + 1/2**e + 1/3**e + ... + 1/n**e
; Teste les rationnels.
(de zeta (n e)
    (let ((r 0))
         (for (i 1 1 n)
              (setq r (+ r (1/ (** i e)))))
         r))
;  "Le nombre :e(n) = 1 + 1/2! + ... + 1/n!"
(de serie-e (n)
    (let ((e 1))
         (for (i 1 1 n) (setq e (+ e (1/ (fact i)))))
         e))

; "Teste l'addition sur N"
(de fib+ (n)
    (let ((fn 0) (fn+1 1))
         (repeat n (psetq fn+1 (+ fn fn+1) fn fn+1))
         fn))

(defvar v32 (** 10 9))

; Teste l'arithme'tique entre 16 et 32 bits.
(de fub (n)
    (cond ((= n v32) v32)
          ((= n (+ v32 1)) (+ v32 1))
          (t (+ (fub (1- n)) (fub (- n 2))))))

; Teste les sommes et produits 32 bits.
(de sommes (n)
    (repeat n (+ v32 v32) (* v32 v32)))

; Tente de mesurer le temps de calcul de exp:
(de ebench (exp)
    (let ((tps 0.))
         (repeat 5 (gc) (setq tps (+ tps (time '(eval exp)))))
         (setq tps (/ tps 5))
         tps))

; Tente de mesurer le temps de diverses fonctions arithme'tiques:

(print "Faire (bench) pour le benchmark")

(de bench arg
    (if (null arg) (lebench)
        (let ((tps))
             (let ((#:sys-package:itsoft 'test)) (setq tps (lebench)))
             (print  " Temps total = " tps))))

(de lebench ()
    (let ((tps 0))
         (print "Temps total du bench sur SUN 3/60-3.4, en interpre'te' : " 
	        4.61199)
         (print "                                       avec complice   : " 
	         .6480224)
         (setq tps (+ tps (ebench '(fib+ 200))))
         (setq tps (+ tps (ebench '(fib 500))))
         (setq tps (+ tps (ebench '(fact 200))))
         (setq fib1000 (fib 1000))
         (setq fact200 (fact 200) fact150 (fact 150))
         (setq tps (+ tps (ebench '(pgcd (fib 1001) (fib 1000)))))
         (setq tps (+ tps (ebench '(quotient fact200 fact150))))
         (setq tps (+ tps (ebench '(zeta 10 3))))
         (setq tps (+ tps (ebench '(serie-e 10))))
         (setq tps (+ tps 
            (ebench
                   '(let ((#:sys-package:itsoft
                            (cons 'testprint #:sys-package:itsoft)))
                         (print fib1000)))))
         (setq tps (+ tps (ebench '(integerp (zeta 12 3)))))
         (print "Temps total du bench = " tps)
    ))
 
; Pour e'viter d'e'crire dans le test de vitesse du print:
(de #:testprint:eol () (outpos 0))

(testfn t)

    (test-serie "Test de l'arithmetique generique sur R" ())
    () ()

    (test-serie "Test de la construction des entiers" ())

     (numberp 2)                 2
     (integerp 2)                2
     (integerp 2.0)              ()
     (integerp 23)             	 23
     (integerp -2)              -2
     12            		 12
     12345678910   		 12345678910
     00000123      		 123
     -123              		-123
     -32768           		-32768	
     (- 32768)        		-32768
     (> 36764 0)      		 36764

    (test-serie "Test de lecture/ecriture" ())

    (setq #:system:print-for-read t)  t
    #{12121212112121212112A-90349
                         39049340} #{121212121121212121129034939049340}
    (setq lec 121202943034923942303942034) #{121202943034923942303942034}
    (setq exlec (explode lec))  (35 123 49 50 49 50 48 50 57 52 51 48 51 
				 52 57 50 51 57 52 50 51 48 51 57 52 50 
				 48 51 52 125)
    (implode exlec)  #{121202943034923942303942034}
    (setq #:system:print-for-read ())    ()

    (test-serie "Test de l'addition-soustraction" ())

     (+ -16384 -16384)            -32768
     (+ -16383 -16385)            -32768
     (+ -1684 12345678)          12343994
     (- 16384 12345678)         -12329294
     (+ 12345 123456)           135801
     (+ 12345 123456123456)     123456135801
     (+ 123456 12345)           135801
     (+ 123456123456 12345)     123456135801
     (- 12345 -123456)           135801
     (- 12345 -123456123456)     123456135801
     (- 123456 -12345)           135801
     (- 123456123456 -12345)     123456135801
     (- 12345 123456)           -111111
     (- 123456 12345)           111111
     (- 12345 123456123456)      -123456111111
     (- 123456123456 12345)      123456111111
     (+ 12345 -123456)           -111111
     (+ 123456 -12345)           111111
     (+ 12345 -123456123456)      -123456111111
     (+ 123456123456 -12345)      123456111111

     (+ (- (** 2 32) 1) 1)       4294967296
     (1+ 99999)        100000
     (+ -3 100000)         99997
     (+ -3 -100000)        -100003
     (- 1000000 1)        999999

    (test-serie "Test de la multiplication" ())

     (* 2 -16384)                 -32768
     (* 12345679 81)         999999999
     (* -100000000000 -100000000000) 10000000000000000000000
     (* -100000000000 100000000000)  -10000000000000000000000
     (* 100000000000 -100000000000) -10000000000000000000000
     (* 100000000000 100000000000) 10000000000000000000000
     (* 48270948888581289062500000000 
        845870049062500000) 
          40830949904677684825316369628906250000000000000
     (* 6956883693 3258093801689886619170103176686855)
          22666179639240748063923391983020279316955515

    (test-serie "Test de la division" ())

     (modulo 1234567890 12345678901234567890) 1234567890
     (quotient 1234567890 12345678901234567890) 0
     (modulo 1234567890 1234567890) 0
     (quotient 1234567890 1234567890) 1
     (quotient -32768 2)        -16384
     (quotient 264195 97200)        2
     (gcd 264195 97200)             135
     (modulo 97200 69795)           27405
     (modulo (fact 13) (fact 9))       0
     (modulo (fact 34) (fact 13))      0
     (modulo (fact 57) (fact 21))      0
     (modulo (fact 40) (fact 39))     0  
     (quotient (fact 59) (fact 58))   59
     (quotient   22685491128062564230891640495451214097
                 5281877500950955845296219748)        4294967295
     (quomod -100000 12342342345)      -1
     (setq a 1234567)      1234567
     (setq b 123456)      123456
     (+ (* (quotient a b) b) (modulo a b)) 1234567
     (+ (* (quotient (- a) (- b)) (- b)) (modulo (- a) (- b))) -1234567
     (+ (* (quotient a (- b)) (- b)) (modulo a (- b))) 1234567
     (+ (* (quotient (- a) (- b)) (- b)) (modulo (- a) (- b))) -1234567
     (setq fauxzero  (- 10000000000000 10000000000000)) 0
     (modulo  fauxzero 1000000000000) 0
     (quotient  fauxzero 1000000000000) 0
     (progn (modulo  fauxzero 1000000000000) #:ex:mod) 0
     (modulo (- fauxzero) 10000000000000) 0
     (quotient (- fauxzero) 10000000000000) 0
     (quotient 3000 (- 1234567891234 1234567890000)) 2
     (modulo 3000 (- 1234567891234 1234567890000)) 532
     (type-of fauxzero) fix
     (progn (quotient 1234565 101)
	    (* 100000000000000 100000000000000)
	    #:ex:mod)         42
     (modulo -1234567890 1234567899) 9
     (modulo (- 12345678900000 12345678926887) 3) 2
     (quotient 4881014060 4) 1220253515
     (modulo 38705419208160503264676104110080 607823) 101593

    (test-serie "Tests divers" ())

     (/ 1. 100000)       1.e-05
     
     (** 10 10)   		 10000000000
     (** 2 128)   		 340282366920938463463374607431768211456
     (truncate 123456)              123456
     (truncate -123456)             -123456
     (fdiv (float (truncate (power 2 50))) 1000000000000000.)      1.126
     (fdiv (float (truncate (power 10 10))) 10000000000.)    1.00
     (fdiv (float (truncate (- (power 2 50)))) 1000000000000000.)  -1.126
     (fdiv (float (truncate (- (power 10 10)))) 10000000000.) -1.
     (float (quotient 10011100.23 1000))    10011.

     (gcd 1769 551)            29
     (gcd 12432245661452 314523541234) 2
     (gcd  fauxzero 10000000000000)  10000000000000

     (fact 42)  1405006117752879898543142606244511569936384000000000
     (fib 250)  7896325826131730509282738943634332893686268675876375


    (exp 1/3)                     1.395612
    (exp 123456)                  1.7e+38
    (exp -123456)                 -1.7e+38
    (log 1/3)                     -1.098612
    (log 123456)                  11.72364

    (atan 1/3)                    .3217505
    (atan 123456)                 1.570788
    (atan -123456)                -1.570788
    (sin 1/3)                     .3271947
    (sin 123456)                  -.7402834
    (sin -123456)                 .7402834
    (asin 1/3)                    .3398369
    (cos 1/3)                     .9449569
    (cos 123456)                  -.6722949
    (cos -123456)                 -.6722949
    (acos 1/3)                    1.230959
     (+ 32768 2.)                  32770.

    (<?> (- (** 2 15) 1) 32767)    0
    (<?>  (- 123458 123457) 1)     0

     (/= 3 3)                      ()
     (exp 2/3)                     1.947734
     (** 8 1/3)                    2.

     (test-serie "Fin du test" ())
     ()                            ()