citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

utils.pm (1484B)


      1 package utils;
      2 
      3 use Test::Cmd;
      4 use Test::More;
      5 use autodie;
      6 
      7 # Common message seen while testing lib.
      8 our $err_gl = "libcitrun: exec citrun_gl: No such file or directory\n";
      9 
     10 sub os_compiler {
     11 	if ($^O eq 'MSWin32') {
     12 		return 'cl /nologo /Fe';
     13 	}
     14 	return 'cc -o ';
     15 }
     16 
     17 sub clean_citrun_log {
     18 	my ($log) = @_;
     19 
     20 	$log =~ s/>> citrun_inst.*\n/>> citrun_inst\n/gm;
     21 	$log =~ s/^.*Milliseconds spent.*\n//gm;
     22 	$log =~ s/'.*'/''/gm;
     23 	$log =~ s/^[0-9]+: //gm;
     24 
     25 	return $log;
     26 }
     27 
     28 sub setup_projdir {
     29 	my $wrap = Test::Cmd->new( prog => 'bin/citrun_wrap', workdir => '' );
     30 
     31 	$wrap->write( 'main.c', <<EOF);
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 
     35 long long fib(long long);
     36 void print_output(long long);
     37 
     38 int
     39 main(int argc, char *argv[])
     40 {
     41 	long long n;
     42 
     43 	if (argc != 2) {
     44 		fprintf(stderr, "argc != 2");
     45 		exit(1);
     46 	}
     47 
     48 	n = atoi(argv[1]);
     49 
     50 	print_output(fib(n));
     51 	return 0;
     52 }
     53 EOF
     54 
     55 	$wrap->write( 'print.c', <<EOF );
     56 #include <stdio.h>
     57 
     58 void
     59 print_output(long long n)
     60 {
     61 	printf("%lli", n);
     62 	return;
     63 }
     64 EOF
     65 
     66 	$wrap->write( 'fib.c', <<EOF );
     67 long long
     68 fib(long long n)
     69 {
     70 	if (n == 0)
     71 		return 0;
     72 	else if (n == 1)
     73 		return 1;
     74 
     75 	return fib(n - 1) + fib(n - 2);
     76 }
     77 EOF
     78 
     79 	$wrap->write( 'Makefile', <<EOF );
     80 PROG= program
     81 SRCS= main.c fib.c print.c
     82 NOMAN=
     83 
     84 .include <bsd.prog.mk>
     85 EOF
     86 
     87 	$wrap->run( args => 'make', chdir => $wrap->curdir );
     88 
     89 	print $wrap->stdout;
     90 	is( $wrap->stderr,	'',	'setup_projdir() make stderr empty' );
     91 	is( $? >> 8,		0,	'setup_projdir() make exit code 0' );
     92 
     93 	return $wrap;
     94 }
     95 
     96 1;