# $Id: Imakefile,v 1.28 2004/04/20 17:00:19 queinnec Exp $
# Tests et installation des programmes de correction automatique.

work : 	compile.all \
	test.junit \
	test.log2html \
	test.tee_head \
	test.count.pl \
	test.dist.pl \
	test.norm.pl \
	test
clean ::
	-rm tmp.out*

JDK_HOME	=	/usr/java/j2sdk1.4.2_04
RT		=	${JDK_HOME}/jre/lib/rt.jar
JUNIT		=	/opt/java/junit3.8.1/junit.jar
CP		=	${RT}:${JUNIT}
JAVAC		=	jikes -classpath .:${CP}

compile.all : compile.c compile.java
compile.c : confiner
compile.java :
	${JAVAC} fr/lip6/qnc/cfskit/?*.java
	jar -cf ../cfskit.jar fr/lip6/qnc/cfskit/?*.class

test.junit : compile.java
	cd junit-tests/ && for c in Test_*.java ; do\
	${JAVAC} -cp ..:${CP} $$c ; done
	-cd junit-tests/ && java -cp ..:.:${CP} \
		fr.lip6.qnc.cfskit.TestRun Test_TestRun >/tmp/ttr
	grep '100 Assertions, 1 Tests, 0 Failures' /tmp/ttr
	-cd junit-tests/ && java -cp ..:.:${CP} \
		fr.lip6.qnc.cfskit.TestRun Test_Abort >/tmp/ttr
	grep '100 Assertions, 2 Tests, 1 Failures' /tmp/ttr
	@echo;echo "   *** ALL junit cfskit passed successfully *** ";echo

# ###################################################
# Mettre en place le cfskit sur tique (via youpou):
# Comme les sources doivent etre accessibles depuis tique et mygale,
# ils sont potentiellement aussi visibles par les etudiants.

MIDDLE		=	youpou.lip6.fr
MIDDLEPATH	=	Cours/Licence/CFS/src
REMOTE		=	tique.infop6.jussieu.fr
PREFREMOTEPATH	=	/Enseignants/Infos/licence/CFS
REMOTEPATH	=	${PREFREMOTEPATH}/src

install : to.youpou
to.youpou :
	${RSYNC} -avu . ${MIDDLE}:${MIDDLEPATH}/
	${RSH} ${MIDDLE} "cd ${MIDDLEPATH}/ && m on.youpou"

on.youpou : to.tique
to.tique :
	rm -f `find . -name '*~' -print`
	${RSYNC} -avuL . ${REMOTE}:${REMOTEPATH}/
	@echo "Proteger ce repertoire de la curiosite estudiantine"
	${RSH} ${REMOTE} "cd ${PREFREMOTEPATH}/ && chmod 711 . "
	@echo "Proteger ces binaires de la curiosite estudiantine"
	${RSH} ${REMOTE} "cd ${PREFREMOTEPATH}/ && chmod -R 711 bin "
	@echo "cfskit installed on ${REMOTE}"

# ##################################################### 
#                         TESTS                    
# ##################################################### 

# ############################################# test confiner en C
# Obsolete! On n'utilise plus que rt_confiner ecrit en shell.
CFLAGS	=	-Wall -ansi -pedantic -g

confiner : confiner.c
	${CC} ${CFLAGS} confiner.c -o confiner

test.confiner : confiner
	./confiner -h
	./confiner --help
	./confiner --cpu=44 -- /bin/date
	./confiner --cpu=44 -- date
	./confiner --cpu=44 -- date -u
	./confiner --cpu=44 -- echo coucou > tmp.out
	echo coucou > tmp.out2 && diff tmp.out tmp.out2
	./confiner --cpu=44 -- cat < Imakefile > tmp.out
	diff Imakefile tmp.out
	echo 'while true ; do false ; done' > loop.sh
	-./confiner --cpu=2 -- bash loop.sh 2>&1
	-./confiner --fsize=10000 -- cat </dev/zero >tmp.out
	ls -l tmp.out
	-./confiner --data=10000000 -- ${HOME}/c/mem
	-./confiner --cpu=10 --nproc=70 -- sh -c "date&date&date&date"
	echo 'while true ; do (bash ./loop.sh & echo $$!) ; done' > foster.sh
	-./confiner --cpu=1 --nproc=100 -- sh foster.sh 
	-rm -f loop.sh foster.sh tmp.out*

# ############################################## test compresseur de fichier

#define CountCoucou(nombres)	@@\
	@echo; echo "Testing log2html with" nombres " ..." @@\
	for i in nombres; do echo Coucou ; done > /tmp/coucou	@@\
	perl log2html.pl ignore-that-uuid  /dev/null /tmp/coucou > /tmp/coucou.out @@\
	grep Coucou /tmp/coucou.out >/tmp/coucou.count @@\
	cd /tmp && diff coucou coucou.count || grep similaires coucou.out @@\
	rm -f /tmp/coucou*

test.log2html :
	echo -n > /tmp/coucou
	perl log2html.pl ignore-that-uuid /dev/null /tmp/coucou >/tmp/coucou.out
	-grep Coucou /tmp/coucou.out >/tmp/coucou.count
	diff /tmp/coucou /tmp/coucou.count
	rm -f /tmp/coucou*
CountCoucou(1)
CountCoucou(1 2)
CountCoucou(1 2 3)
CountCoucou(1 2 3 4)
CountCoucou(1 2 3 4 5)
CountCoucou(1 2 3 4 5 6)
CountCoucou(1 2 3 4 5 6 7)

# ################################################ Test tee_head
# tee_head est un limiteur de flux.

test.tee_head :
	echo Coucou > /tmp/coucou
	cat /tmp/coucou | perl tee_head.pl 1000 \
		/tmp/coucou.out > /tmp/coucou2.out
	diff /tmp/coucou /tmp/coucou.out
	diff /tmp/coucou /tmp/coucou2.out
	for i in 1 2 3 4 5 6 7 8 9 ; do echo Coucou ; done > /tmp/coucou
	cat /tmp/coucou | perl tee_head.pl 50 \
		/tmp/coucou.out > /tmp/coucou2.out
	diff /tmp/coucou /tmp/coucou.out || grep WARNING /tmp/coucou.out
	diff /tmp/coucou.out /tmp/coucou2.out
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	echo Coucou ; done ; done ; done > /tmp/coucou
	cat /tmp/coucou | perl tee_head.pl 1k \
		/tmp/coucou.out > /tmp/coucou2.out
	diff -q /tmp/coucou /tmp/coucou.out || grep WARNING /tmp/coucou.out
	diff /tmp/coucou.out /tmp/coucou2.out
	[ `wc -c < /tmp/coucou.out` -lt 1200 ]
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	for i in 1 2 3 4 5 6 7 8 9 0; do \
	echo Coucou ; done ; done ; done ; done ; done > /tmp/coucou
	cat /tmp/coucou | perl tee_head.pl 10k \
		/tmp/coucou.out > /tmp/coucou2.out
	diff -q /tmp/coucou /tmp/coucou.out || grep WARNING /tmp/coucou.out
	diff /tmp/coucou.out /tmp/coucou2.out
	[ `wc -c < /tmp/coucou.out` -lt 10200 ]
	@echo;echo "********* All tests on tee_head successful *************"

# ################################################ test dist.pl
# dist.pl calcule la distance de Levenstein entre deux fichiers.

test.dist.pl : 
	(echo a; echo bb; echo c) > /tmp/f1
	(echo a; echo bbd; echo c) > /tmp/f2
	[ 0 = `perl dist.pl /tmp/f1 /tmp/f1 | cut -d ' ' -f3` ] && echo OK
	[ 1 = `perl dist.pl /tmp/f1 /tmp/f2 | cut -d ' ' -f3` ] && echo OK
	cat /tmp/f1 /tmp/f2 > /tmp/f3
	[ 3 = `perl dist.pl /tmp/f1 /tmp/f3 | cut -d ' ' -f3` ] && echo OK
	cat /tmp/f2 /tmp/f2 > /tmp/f4
	[ 1 = `perl dist.pl /tmp/f3 /tmp/f4 | cut -d ' ' -f3` ] && echo OK
	cat /tmp/f2 /tmp/f1 > /tmp/f5
	perl dist.pl /tmp/f3 /tmp/f5
	[ 2 = `perl dist.pl /tmp/f3 /tmp/f5 | cut -d ' ' -f3` ] && echo OK
	-rm /tmp/f0 
	touch /tmp/f0
	[ 3 = `perl dist.pl /tmp/f1 /tmp/f0 | cut -d ' ' -f3` ] && echo OK
	[ 6 = `perl dist.pl /tmp/f0 /tmp/f3 | cut -d ' ' -f3` ] && echo OK
	[ 0 = `perl dist.pl /tmp/f0 /tmp/f0 | cut -d ' ' -f3` ] && echo OK
	[ 0 = `perl dist.pl dist.pl dist.pl | cut -d ' ' -f3` ] && echo OK
	@echo;echo "******* all tests on dist.pl successful *******"

# ################################################ test norm.pl
# norm.pl normalise un fichier (enleve les blancs, la casse et les lignes vides)

test.norm.pl :
	[ 1 = `echo ab | perl norm.pl | wc -l` ]
	[ 2 = `(echo ab; echo; echo c) | perl norm.pl | wc -l` ]
	[ 2 = `(echo ab; echo '  '; echo SPACES) | perl norm.pl | wc -l` ]
	[ 2 = `(echo ab; echo '	'; echo TAB) | perl norm.pl | wc -l` ]
	[ 2 = `(echo ab; echo; echo c d; echo) | perl norm.pl | wc -w` ]
	[ 2 = `(echo ab; echo; echo c d; echo; echo) | perl norm.pl | wc -l` ]
	[ ab = `echo A B | perl norm.pl` ]
	@echo;echo "******* all tests on norm.pl successful *******"

# ################################################ test count.pl
# count.pl compte des mots (ou des regexps) dans stdin.

test.count.pl : count.pl
	@echo;echo "            Comptes simples ..."
	echo lajdsf | perl count.pl aaa
	[ `echo lajdsf | perl count.pl aaa` = 0 ]
	[ `echo caaab | perl count.pl aaa` = 1 ]
	[ `echo aaab | perl count.pl aaa` = 1 ]
	[ `echo caaa | perl count.pl aaa` = 1 ]
	[ `echo aaa | perl count.pl aaa` = 1 ]
	echo aaabaaa | perl count.pl aaa
	[ `echo aaabaaa | perl count.pl aaa` = 2 ]
	[ `(echo aaa; echo baaa) | perl count.pl aaa` = 2 ]
	(echo aaa; echo; echo baaa) | perl count.pl aaa
	[ `(echo aaa; echo; echo baaa) | perl count.pl aaa` = 2 ]
	@echo;echo "             Comptes speciaux ..."
	echo ':)' | perl count.pl ':)'
	[ `echo ':)' | perl count.pl ':)'` = 1 ]
	[ `echo ':):(' | perl count.pl ':)'` = 1 ]
	[ `echo 'a:c)' | perl count.pl ':)'` = 0 ]
	[ `(echo 'a:) ::)'; echo ':)') | perl count.pl ':)'` = 3 ]
	@echo;echo "             les options ..."
	(echo aaa; echo; echo baaa) | perl count.pl aaa
	[ `(echo aaa; echo; echo BAAA) | perl count.pl aaa` = 1 ]
	[ `(echo aaa; echo; echo BAAA) | perl count.pl -i aaa` = 2 ]
	[ `(echo aaa; echo; echo aBaa) | perl count.pl -regexp 'a+'` = 3 ]
	[ `(echo aaa; echo; echo ABAA) | perl count.pl -i -regexp 'a+'` = 3 ]
	@echo;echo "********* All tests on count.pl successful *************"

# ########################################## Tests des scripts run*.sh
# Le repertoire Tests/ contient les <<questions>> pour les tests
# internes en fait une seule question nommee InternalTests/ contenant
# tous les scripts ordonnes a tourner. Il n'y a pas besoin d'avoir des
# <<solutions>>. 

RT_LOG_DIR	=	/tmp/logs
RT_TMP		=	/tmp/r

test.runtests : 
	-rm -rf ${RT_LOG_DIR}/InternalTests1
	-mkdir -p /tmp/r
	${MAKE} run.one.test RT_TESTS_DIR=`pwd`/tests Q=InternalTests-Q1 \
		SOLUTION_DIR=/tmp RT_UUID=InternalTests1 M=612222
	@echo cat ${RT_LOG_DIR}/InternalTests1/InternalTests-Q1.log
	@echo;echo "********* All Internal tests successful *************"

test.runtests.with.links : 
	export RT_LINK_TEST=`pwd`/genlink.sh ;\
	export RT_HTTP_PREFIX=SeeIfImHere ;\
	${MAKE} test.runtests
	grep SeeIfImHere ${RT_LOG_DIR}/InternalTests1/InternalTests-Q1.log

test.runalltests :
	-rm -rf ${RT_LOG_DIR}/InternalTests
	-rm -f /tmp/run.all.tests
	-mkdir /tmp/r
	-mkdir -p ${RT_TMP}
	export RT_MARK_DIR=`pwd` ;\
	export RT_UUID=InternalTests ;\
	export RT_TESTS_DIR=`pwd`/tests ;\
	export RT_LOG_DIR=${RT_LOG_DIR} ;\
	export RT_TMP=${RT_TMP} ;\
	cd /tmp/r && \
	$$RT_MARK_DIR/runalltests.sh bof 2>&1 | tee /tmp/run.all.tests
	MARK=`sed -ne 's/^Total obtenu: //p' </tmp/run.all.tests ` && \
	[ "$$MARK" = 613333 ]

test : 	test.runtests \
	test.runtests.with.links \
	test.runalltests
	@echo; echo ALL TESTS PASSED SUCCESSFULLY; echo

# ########################################## 
# Tester l'option -n de run.sh

test.run.sh :
	-rm -rf /tmp/logs
	-mkdir /tmp/logs
	-rm -rf /tmp/run
	-mkdir /tmp/run
	export CFSKIT_DIR=`pwd`;\
	export RT_MARK_DIR=`pwd` ;\
	export RT_TESTS_DIR=`pwd`/../CFS2002jan30/tests ;\
	export RT_LOG_DIR=/tmp/logs ;\
	export RT_TMP=${RT_TMP} ;\
	export RT_SESSION=CFSjan30am ;\
	export RT_HTTP_PREFIX="file:$$RT_TESTS_DIR/" ;\
	cd /tmp/run && bash $$RT_MARK_DIR/run.sh -n \
	   /archive/remote/Ens/licence/2001/cct/CFS/2002janvier/LIC_CFS-1/30am










# ########################################## obsolete ?
test.runtests.old : runtests.sh runtestlib.sh
	-rm -rf ${RT_LOG_DIR}
	${MAKE} test.question1 
	${MAKE} test.question2
	${MAKE} test.question3
	${MAKE} test.question4
	${MAKE} test.question5

test.question1 :
	@echo; echo "********** Notation question 1"; echo
	${MAKE} run.one.test D=student1  Q=30-01-02-matin-Q1 M=0
	${MAKE} run.one.test D=student2  Q=30-01-02-matin-Q1 M=1
	${MAKE} run.one.test D=student2a Q=30-01-02-matin-Q1 M=1
	${MAKE} run.one.test D=student2b Q=30-01-02-matin-Q1 M=1
	${MAKE} run.one.test D=student3  Q=30-01-02-matin-Q1 M=2
	${MAKE} run.one.test D=student4  Q=30-01-02-matin-Q1 M=1.1

test.question2 :
	@echo; echo "********** Notation question 2"; echo
	${MAKE} run.one.test D=student5  Q=30-01-02-matin-Q2 M=7

test.question3 :
	@echo; echo "********** Notation question 3"; echo
	${MAKE} run.one.test D=student6  Q=30-01-02-matin-Q3 M=0.65
	${MAKE} run.one.test D=student7  Q=30-01-02-matin-Q3 M=1
	${MAKE} run.one.test D=student8  Q=30-01-02-matin-Q3 M=0.95

test.question4 :
	@echo; echo "********** Notation question 4"; echo
	${MAKE} run.one.test D=student9  Q=30-01-02-matin-Q4 M=3

test.question5 :
	@echo; echo "********** Notation question 5"; echo
	-rm ../solutions/student9/dico.txt
	${MAKE} run.one.test D=student9  Q=30-01-02-matin-Q5 M=0.9
	-rm -rf ../solutions/student10
	cd ../solutions && cp -rp Titou student10
	${MAKE} run.one.test D=student10 Q=30-01-02-matin-Q5 M=1

test.all.questions :
	if [ -d "${RT_LOG_DIR}" ] ; then rm -rf ${RT_LOG_DIR} ; else true ; fi
	-rm -rf ../solutions/student10
	cd ../solutions && cp -rp Titou student10
	@echo; echo "********** Notation toutes questions"; echo
	export HERE=`pwd` ;\
	export RT_TESTS_DIR=$$HERE/../CFS2002jan30/tests ;\
	export RT_LOG_DIR=${RT_LOG_DIR} ;\
	export RT_TMP=${RT_TMP} ;\
	cd ../solutions/student10 && \
	time $$HERE/runalltests.sh 
	echo mozilla file:`ls -t ${RT_LOG_DIR}/?*?/all.html | head -1 `
	perl ./extract.pl `ls -t ${RT_LOG_DIR}/?*?/all.log | head -1 `

test.several.students :
	if [ -d "${RT_LOG_DIR}" ] ; then rm -rf ${RT_LOG_DIR} ; else true ; fi
	-rm -rf ../solutions/?
	cd ../solutions && cp -rp Titou A
	cd ../solutions && cp -rp Christian B
	export HERE=`pwd` ;\
	export RT_TESTS_DIR=$$HERE/../CFS2002jan30/tests ;\
	export RT_LOG_DIR=${RT_LOG_DIR} ;\
	export RT_TMP=${RT_TMP} ;\
	for student in ../solutions/? ; do ( \
	echo "------------- Notation de l'etudiant $$student" ; \
	cd $$student && time $$HERE/runalltests.sh ) ; done

# L'etudiant D a noter sur la question Q, note attendue: M.
D	=	student
Q	=	30-01-02-matin-Q1
M	=	0
RT_TESTS_DIR	=	`pwd`/../CFS2002jan30/tests
SOLUTION_DIR	=	../solutions/${D}
run.one.test :
	-rm -f /tmp/run.one.test
	export HERE=`pwd` ;\
	export RT_TESTS_DIR=${RT_TESTS_DIR} ;\
	export RT_LOG_DIR=${RT_LOG_DIR} ;\
	export RT_TMP=${RT_TMP} ;\
	cd ${SOLUTION_DIR} && \
	$$HERE/runtests.sh ${Q} 2>&1 | tee /tmp/run.one.test
	@if egrep -q "question ${Q}: ${M}\.?$$" < /tmp/run.one.test ;\
	then echo "	-------- Test OK -------" ; rm -f /tmp/run.one.test ;\
	else echo "*** PROBLEME ***" ; exit 1 ; fi

# end of Imakefile
