Ferramenta de testes de códigos C para uso didático

De MediaWiki do Campus São José
Ir para navegação Ir para pesquisar

Moodle Virtual Programming Lab - VPL

Via scripts rodando gcc e frama-c em um servidor Linux (Rascunho)

Objetivo

  • A ideia desta ferramenta é criar uma forma do aluno enviar resoluções de listas de exercícios e receber feedback automático através do servidor que fará a catalogação dos trabalhos, compilação e verificação se atende os requisitos

Objetivos específicos

  • Rodar em plataforma linux
  • Compilar e validar programas desenvolvidos em ANSI C (C99)
  • Ter uma forma de identificar o aluno e catalogar os trabalhos
  • Suportar até 10 problemas por lista
  • Realizar a compilação
  • Suportar arquivos de script de rotinas de testes de programas.

Exemplo de aplicação

  • Uma lista de exercícios tem como primeiro exercício receber do usuário 2 números float, calcular a média e imprimir em tela
  • Após o aluno enviar ao servidor as resoluções o sistema deve compilar e verificar se o programa atingiu os requisitos executando o script de testes que o professor montou. Neste caso o professor deverá criar um script que entra com números diversos, inteiros, fracionários, negativos, positivos, zero, enfim, para cada caso testado o próprio script deve conter o resultado esperado e testar a condição. Se todas as condições forem satisfeitas o programa da feedback positivo.

TODO

  1. Add input treatment using like following code (in this example teste.c)
#include <stdio.h>
 
void main()
{
  char s1[10], s2[10];
  printf("Entre com uma string\n");
  scanf(" %[^\n]s",s1);
  printf("Entre com outra string\n");
  scanf(" %[^\n]s",s2);
  printf("A primeira string obtida foi: %s\n",s1);
  printf("A segunda string obtida foi: %s\n",s2);
}
  • Conteúdo do arquivo input.txt
input_tst
input tst2
  • Código executado no terminal
$ ./teste < input.txt
Entre com uma string
Entre com outra string
A primeira string obtida foi: input_tst
A segunda string obtida foi: input tst2

sendjob.sh

Pré-requisitos

  • Professor deve passar uma lista de exercícios de até 10 questões onde o aluno deve criar códigos em C para cada questão
  • O aluno deve criar arquivos C completos, compiláveis e seguir o padrão de nomes (q1.c, q2.c até q10.c)
  • O servidor que possui o frama-c instalado deve estar disponível para receber conexões SSH
  • A aluno deve obter o arquivo "sendjob.sh" e o arquivo de identidade "pem"
    • O script sendjob.sh deve estar na mesma pasta onde estão os arquivo q1.c, q2.c...
    • O arquivo pem deve ser colocado na pasta .ssh do computador do aluno

Procedimento

  1. O aluno deve resolver a lista de exercícios que foi dada pelo professor salvando os arquivos com nomes específicos.
  2. Quando for submeter o trabalho deverá então utilizar o script "sendjob.sh" na pasta que estão os códigos (q1.c, q2.c...), executar conforme abaixo
$ ./sendjob.sh nome_aluno

Código do script sendjob.sh

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# @author Amaral, Cleber Jorge
#
# It is free software: you can redistribute it and/or modify
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# It 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 Lesser General Public License for more details.                             
# You should have received a copy of the GNU Lesser General Public License        
# along with camel_jason.  If not, see <http://www.gnu.org/licenses/>.            
#
# This script is doing an automated compilation process of a test or list of 
# exercises of C programming language course. The student must develop his 
# code and by ssh copy the files to his folder in a server where this script 
# is running. Frama-c framework must be properly installed in the machine
# The files that this script process must be called as q1.c to q10.c what 
# should be ten questions of an exercise list or test
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

if [ "$1" != "" ]; then
	read -p "Hello $1! Press [ENTER] to send your code!"
	scp -i ~/.ssh/cleberamaral.pem q1.c ubuntu@200.135.233.3:/home/ubuntu/prg29002/$1/
	scp -i ~/.ssh/cleberamaral.pem q2.c ubuntu@200.135.233.3:/home/ubuntu/prg29002/$1/
	scp -i ~/.ssh/cleberamaral.pem q3.c ubuntu@200.135.233.3:/home/ubuntu/prg29002/$1/
	scp -i ~/.ssh/cleberamaral.pem q4.c ubuntu@200.135.233.3:/home/ubuntu/prg29002/$1/
	scp -i ~/.ssh/cleberamaral.pem q5.c ubuntu@200.135.233.3:/home/ubuntu/prg29002/$1/
	ssh -X -i ~/.ssh/cleberamaral.pem ubuntu@200.135.233.3 /home/ubuntu/prg29002/$1/compile.sh
else
	echo "Please enter your username"
fi

Código do script compile.sh

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# @author Amaral, Cleber Jorge
#
# It is free software: you can redistribute it and/or modify
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# It 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 Lesser General Public License for more details.                             
# You should have received a copy of the GNU Lesser General Public License        
# along with camel_jason.  If not, see <http://www.gnu.org/licenses/>.            
#
# This script is doing an automated compilation process of a test or list of 
# exercises of C programming language course. The student must develop his 
# code and by ssh copy the files to his folder in a server where this script 
# is running. Frama-c framework must be properly installed in the machine
# The files that this script process must be called as q1.c to q10.c what 
# should be ten questions of an exercise list or test
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

#Getting current directory
BASEDIR=$(dirname "$0")
if [ -f "$BASEDIR/output.log" ]
then
	rm $BASEDIR/output.log
fi

#Print in the file the user and current datetime (this first is replacing current output.log content)
who > $BASEDIR/output.log
date >> $BASEDIR/output.log

#Loop to compile each c code (questions q1 to q10).
COUNTER=0
while [ $COUNTER != 10 ]
do
	COUNTER=$[$COUNTER + 1]
	if [ -f "$BASEDIR/q$COUNTER.c" ]
	then
		printf " \n\n\n\n" >> $BASEDIR/output.log 

		echo "gcc output compiling  q$COUNTER.c: " >> $BASEDIR/output.log
		gcc $BASEDIR/q$COUNTER.c -o $BASEDIR/q$COUNTER >> $BASEDIR/output.log

		echo "Compilation on q$COUNTER.c have finished!" >> $BASEDIR/output.log
	else
		printf " \n\n\n\n" >> $BASEDIR/output.log 
		echo "$BASEDIR/q$COUNTER.c not found." >> $BASEDIR/output.log
	fi
done

#Loop for Frama-c tests
COUNTER=0
while [ $COUNTER != 10 ]
do
	COUNTER=$[$COUNTER + 1]
	if [ -f "$BASEDIR/q$COUNTER.c" ]
	then
		printf " \n\n\n\n" >> $BASEDIR/output.log 

		echo "Frama-c output testing q$COUNTER.c:" >> $BASEDIR/output.log
		frama-c -val $BASEDIR/q$COUNTER.c >> $BASEDIR/output.log

		echo "Frama-c tests on q$COUNTER.c have finished!" >> $BASEDIR/output.log
	else
		printf " \n\n\n\n" >> $BASEDIR/output.log 
		echo "$BASEDIR/q$COUNTER.c not found." >> $BASEDIR/output.log
	fi
done

#Loop to register the code that was received
COUNTER=0
while [ $COUNTER != 10 ]
do
	COUNTER=$[$COUNTER + 1]
	if [ -f "$BASEDIR/q$COUNTER.c" ]
	then
		printf " \n\n\n\n" >> $BASEDIR/output.log 

		echo "Original code of q$COUNTER.c:" >> $BASEDIR/output.log
		cat $BASEDIR/q$COUNTER.c >> $BASEDIR/output.log

		echo "Printing process of q$COUNTER.c has finished!" >> $BASEDIR/output.log
	else
		printf " \n\n\n\n" >> $BASEDIR/output.log 
		echo "$BASEDIR/q$COUNTER.c not found." >> $BASEDIR/output.log
	fi
done