The R Project SVN R

Rev

Rev 14948 | Rev 34062 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14948 duncan 1
/*
2
 Tests the R error handling in an application
3
 which embeds R. Here, we generate the error by calling stop().
4
 We also provide an on.exit() also.
5
 */
6
 
19500 hornik 7
#include <R.h>
8
#include <Rdefines.h>
14948 duncan 9
 
10
#include "Startup.h"
11
 
12
#include "embeddedRCall.h"
13
 
14
 
15
int
16
main(int argc, char *argv[])
17
{
18
  SEXP fun, e, arg;
19
  int i;
20
  int errorOccurred;
21
  char *localArgs[] = {"R", "--gui=none", "--silent"};
22
  init_R(sizeof(localArgs)/sizeof(localArgs[0]), localArgs);
23
 
24
  /*
25
     Evaluates the two expressions:
26
       source("error.R")
27
     and then calls foo()  twice
28
     where foo is defined in the file error.R
29
   */
30
  PROTECT(fun = Rf_findFun(Rf_install("source"),  R_GlobalEnv));  
31
  PROTECT(e = allocVector(LANGSXP, 2));
32
  PROTECT(arg = NEW_CHARACTER(1));
33
  SET_STRING_ELT(arg, 0, COPY_TO_USER_STRING("error.R"));
34
  SETCAR(e, fun);
35
  SETCAR(CDR(e), arg);
36
 
37
  Test_tryEval(e, &errorOccurred);
38
 
39
  UNPROTECT(2);
40
 
41
    fun = Rf_findFun(Rf_install("foo"),  R_GlobalEnv);
42
    PROTECT(fun);
43
    PROTECT(arg = NEW_INTEGER(10));
44
    e = allocVector(LANGSXP, 1);
45
    PROTECT(e);
46
    SETCAR(e, fun);
47
 
48
    Test_tryEval(e, &errorOccurred);
49
 
50
    fprintf(stderr, "Trying again (yes it will fail also!)\n");fflush(stderr);
51
 
52
    Test_tryEval(e, &errorOccurred);
53
    UNPROTECT(2);
54
 
55
    R_CleanUp(SA_NOSAVE, 0, FALSE);
56
 
57
  return(0);
58
}
59