source: branches/blt4/src/objects/RpTest.cc

Last change on this file was 1581, checked in by dkearney, 15 years ago

updates for the rappture objects, object examples, and object apis. small fix for rpunits c interface to check string length. there should probably be more of these checks in the c interface, but units should also be rewritten. added folders to separate out octave2 and octave3 app-fermi examples

File size: 2.1 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <cstring>
4#include <errno.h>
5#include <sys/stat.h>
6
7// size_t indent = 0;
8// size_t tabstop = 4;
9
10int testStringVal(
11    const char *testname,
12    const char *desc,
13    const char *expected,
14    const char *received)
15{
16    if ((!expected && received) ||
17        (expected && !received) ||
18        (expected && received && strcmp(expected,received) != 0)) {
19        printf("Error: %s\n", testname);
20        printf("\t%s\n", desc);
21        printf("\texpected \"%s\"\n",expected);
22        printf("\treceived \"%s\"\n",received);
23        return 1;
24    }
25    return 0;
26}
27
28int testDoubleVal(
29    const char *testname,
30    const char *desc,
31    double expected,
32    double received)
33{
34    if (expected != received) {
35        printf("Error: %s\n", testname);
36        printf("\t%s\n", desc);
37        printf("\texpected \"%g\"\n",expected);
38        printf("\treceived \"%g\"\n",received);
39        return 1;
40    }
41    return 0;
42}
43
44size_t
45readFile (
46    const char *filePath,
47    const char **buf)
48{
49    if (buf == NULL) {
50        fprintf(stderr,"buf is NULL while opening file \"%s\"", filePath);
51        return 0;
52    }
53
54    FILE *f;
55    f = fopen(filePath, "rb");
56    if (f == NULL) {
57        fprintf(stderr,"can't open \"%s\": %s", filePath, strerror(errno));
58        return 0;
59    }
60    struct stat stat;
61    if (fstat(fileno(f), &stat) < 0) {
62        fprintf(stderr,"can't stat \"%s\": %s", filePath, strerror(errno));
63        return 0;
64    }
65    off_t size;
66    size = stat.st_size;
67    char* memblock;
68    memblock = new char [size+1];
69    if (memblock == NULL) {
70        fprintf(stderr,"can't allocate %zu bytes for file \"%s\": %s",
71            (size_t)size, filePath, strerror(errno));
72        fclose(f);
73        return 0;
74    }
75
76    size_t nRead;
77    nRead = fread(memblock, sizeof(char), size, f);
78    fclose(f);
79
80    if (nRead != (size_t)size) {
81        fprintf(stderr,"can't read %zu bytes from \"%s\": %s",
82            (size_t) size, filePath, strerror(errno));
83        return 0;
84    }
85
86    memblock[size] = '\0';
87    *buf = memblock;
88    return nRead;
89}
90
Note: See TracBrowser for help on using the repository browser.