citrun

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

commit 9947b77c6bd025bd2b31d92c8f7938031bc23b00
parent f1e25fc1c15e3cd7885a2e97ba89b1dffed87d9d
Author: kyle <kyle@0x30.net>
Date:   Sun, 20 Nov 2016 13:39:12 -0700

t: write test program to temp dir

Diffstat:
Mt/program.pm | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Dt/program/Jamfile | 6------
Dt/program/one.c | 19-------------------
Dt/program/three.c | 8--------
Dt/program/two.c | 10----------
Mt/rt_exectotals.t | 8++++++--
Mt/rt_header.t | 12++++++++----
Mt/rt_size.t | 11++++++++---
Mt/rt_translunit.t | 16++++++++++------
Mt/shm.pm | 13+++----------
At/tmpdir.pm | 13+++++++++++++
11 files changed, 119 insertions(+), 70 deletions(-)

diff --git a/t/program.pm b/t/program.pm @@ -1,8 +1,77 @@ +# +# This package writes and compiles a small C program with citrun. +# package t::program; use strict; use warnings; +use File::Copy "cp"; -# This module builds the test program when it's used. -system("cd t/program && ../../src/citrun-wrap jam"); +sub new { + my ($class, $tmp_dir) = @_; + + my $self = {}; + bless($self, $class); + + write_file("$tmp_dir/main.c", <<END); +#include <err.h> +#include <stdlib.h> + +long long fib(long long); +void print_output(long long); + +int +main(int argc, char *argv[]) +{ + long long n; + + if (argc != 2) + errx(1, "argc != 2"); + + n = atoi(argv[1]); + + print_output(fib(n)); + return 0; +} +END + + write_file("$tmp_dir/fib.c", <<END); +long long +fib(long long n) +{ + if (n == 0) + return 0; + else if (n == 1) + return 1; + + return fib(n - 1) + fib(n - 2); +} +END + + write_file("$tmp_dir/print.c", <<END); +#include <stdio.h> + +void +print_output(long long n) +{ + fprintf(stderr, "%lli", n); + return; +} +END + + write_file("$tmp_dir/Makefile", <<END); +program: main.o fib.o print.o + cc -o program main.o fib.o print.o +END + + system("src/citrun-wrap make -C $tmp_dir"); +} + +sub write_file { + my ($file_name, $source) = @_; + + open my $fh, ">", $file_name or die "Can't write $file_name: $!"; + print $fh $source; + close $fh; +} 1; diff --git a/t/program/Jamfile b/t/program/Jamfile @@ -1,6 +0,0 @@ -PROG_SRCS = one.c two.c three.c ; - -Depends one.o two.o three.o : ../../src/citrun-inst ; -Depends program : ../../src/libcitrun.a ; - -Main program : one.c two.c three.c ; diff --git a/t/program/one.c b/t/program/one.c @@ -1,19 +0,0 @@ -#include <err.h> -#include <stdlib.h> - -long long fib(long long); -void print_output(long long); - -int -main(int argc, char *argv[]) -{ - long long n; - - if (argc != 2) - errx(1, "argc != 2"); - - n = atoi(argv[1]); - - print_output(fib(n)); - return 0; -} diff --git a/t/program/three.c b/t/program/three.c @@ -1,8 +0,0 @@ -#include <stdio.h> - -void -print_output(long long n) -{ - fprintf(stderr, "%lli", n); - return; -} diff --git a/t/program/two.c b/t/program/two.c @@ -1,10 +0,0 @@ -long long -fib(long long n) -{ - if (n == 0) - return 0; - else if (n == 1) - return 1; - - return fib(n - 1) + fib(n - 2); -} diff --git a/t/rt_exectotals.t b/t/rt_exectotals.t @@ -7,16 +7,20 @@ use Test::More tests => 50; use Time::HiRes qw( usleep ); use t::program; use t::shm; +use t::tmpdir; + +my $tmp_dir = t::tmpdir->new(); +t::program->new($tmp_dir); my $child_pid = fork(); if ($child_pid == 0) { # Child. - exec ("t/program/program", "45") or die $!; + exec ("$tmp_dir/program", "45") or die $!; } # Give the runtime time to set up. sleep 1; -my $shm = t::shm->new(); +my $shm = t::shm->new($tmp_dir); my $last_total = 0; for (0..49) { diff --git a/t/rt_header.t b/t/rt_header.t @@ -3,14 +3,18 @@ # use strict; use warnings; -use Test::More tests => 11; +use Test::More tests => 12; use t::program; use t::shm; +use t::tmpdir; -my $ret = system('t/program/program 1'); +my $tmp_dir = t::tmpdir->new(); +t::program->new($tmp_dir); + +my $ret = system("cd $tmp_dir && program 1"); is $ret >> 8, 0, "is program exit code 0"; -my $shm = t::shm->new(); +my $shm = t::shm->new($tmp_dir); is $shm->{magic}, "ctrn", "is file magic correct"; is $shm->{major}, 0, "is major correct"; is $shm->{minor}, 0, "is minor correct"; @@ -24,4 +28,4 @@ cmp_ok $pgrp, '<', 100 * 1000, "pgrp is less than max pid"; cmp_ok $pgrp, '>', 1, "pgrp is greater than min pid"; is $shm->{progname}, "program", 'is test program name correct'; -# is $cwd, "/home/...", 'is working directory believable'; +is $shm->{cwd}, $tmp_dir, 'is working directory believable'; diff --git a/t/rt_size.t b/t/rt_size.t @@ -4,13 +4,18 @@ use strict; use warnings; use POSIX; -use Test::More tests => 1; +use Test::More tests => 2; use t::program; use t::shm; +use t::tmpdir; -system("t/program/program 1"); +my $tmp_dir = t::tmpdir->new(); +t::program->new($tmp_dir); -my $procfile = t::shm->new(); +my $ret = system("$tmp_dir/program 1"); +is $ret >> 8, 0, "is test program exit code 0"; + +my $procfile = t::shm->new($tmp_dir); my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE); is($procfile->{size}, $pagesize * 4, "is memory file 4 pages long"); diff --git a/t/rt_translunit.t b/t/rt_translunit.t @@ -6,17 +6,21 @@ use warnings; use Test::More tests => 7; use t::program; use t::shm; +use t::tmpdir; -my $ret = system('t/program/program 10'); +my $tmp_dir = t::tmpdir->new(); +t::program->new($tmp_dir); + +my $ret = system("$tmp_dir/program 10"); is $ret >> 8, 0, "is program exit code 0"; -my $shm = t::shm->new(); +my $shm = t::shm->new($tmp_dir); my ($tu1, $tu2, $tu3) = @{ $shm->{translation_units} }; is $tu1->{size}, 9, "is translation unit 1 9 lines"; -is $tu1->{comp_file_name}, 'three.c', 'is compiler file name right'; -like $tu1->{abs_file_path}, qr/.*three.c/, 'is absolute file path believable'; +is $tu1->{comp_file_name}, 'print.c', 'is compiler file name right'; +like $tu1->{abs_file_path}, qr/.*print.c/, 'is absolute file path believable'; is $tu2->{size}, 11, "is translation unit 2 9 lines"; -is $tu2->{comp_file_name}, 'two.c', 'is compiler file name right'; -like $tu2->{abs_file_path}, qr/.*two.c/, 'is absolute file path believable'; +is $tu2->{comp_file_name}, 'fib.c', 'is compiler file name right'; +like $tu2->{abs_file_path}, qr/.*fib.c/, 'is absolute file path believable'; diff --git a/t/shm.pm b/t/shm.pm @@ -3,21 +3,18 @@ use strict; use warnings; use POSIX; -# Triggers runtime to use alternate shm path. -$ENV{CITRUN_TOOLS} = 1; - my $pagesize = POSIX::sysconf(POSIX::_SC_PAGESIZE); sub new { - my ($class) = @_; + my ($class, $tmpdir) = @_; my $self = {}; bless($self, $class); - open(my $fh, "<:mmap", "procfile.shm") or die $!; + open(my $fh, "<:mmap", "$tmpdir/procfile.shm") or die $!; $self->{fh} = $fh; - $self->{size} = (stat "procfile.shm")[7]; + $self->{size} = (stat "$tmpdir/procfile.shm")[7]; ( $self->{magic}, $self->{major}, $self->{minor}, @@ -97,8 +94,4 @@ sub xread { return $data; } -sub DESTROY { - unlink "procfile.shm"; -} - 1; diff --git a/t/tmpdir.pm b/t/tmpdir.pm @@ -0,0 +1,13 @@ +package t::tmpdir; +use strict; +use warnings; +use File::Temp qw( tempdir ); + +sub new { + my $tmp_dir = tempdir( CLEANUP => 1 ); + $ENV{CITRUN_PROCFILE} = "$tmp_dir/procfile.shm"; + + return $tmp_dir; +} + +1;