PandA-2024.02
test_support.c
Go to the documentation of this file.
1 #include "support.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <sys/mman.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <assert.h>
9 
10 // Fake stubs, usually implemented per-benchmark
11 void run_benchmark( void *vargs ) {}
12 void input_to_data(int fd, void *vdata) {}
13 void output_to_data(int fd, void *vdata) {}
14 void data_to_output(int fd, void *vdata) {}
15 int check_data(void *vdata, void *vref) {return 0;}
17 
18 
19 #define generate_test_TYPE_array(TYPE) \
20 void test_##TYPE##_array() { \
21  char *p; \
22  TYPE a[10]; \
23  int i, fd; \
24  \
25  /* Write output */ \
26  fd = open("testfile", O_WRONLY|O_CREAT|O_TRUNC, 0666); \
27  assert(fd>1 && "Couldn't open file to write test output"); \
28  for(i=0; i<10; i++) { \
29  a[i] = (TYPE)i; \
30  } \
31  write_##TYPE##_array(fd, a, 10); \
32  close(fd); \
33  fd = open("testfile", O_RDONLY); \
34  assert(fd>1 && "Couldn't open file to read test input"); \
35  p = readfile(fd); \
36  close(fd); \
37  assert( parse_##TYPE##_array(p, a, 10)==0 ); \
38  for(i=0; i<10; i++) { \
39  assert(a[i]==((TYPE)i)); \
40  } \
41 }
48 
49 void test_section_jumping() {
50  int fd;
51  char *p, *s;
52 
53  fd = open("input_sections", O_RDONLY);
54  assert(fd>1 && "Couldn't open file to read test input");
55  p = readfile(fd);
56  s = find_section_start(p, 0);
57  assert(p==s && "Couldn't find zeroth section");
58  s = find_section_start(p, 1);
59  assert(p+3==s && "Couldn't find first section");
60  s = find_section_start(p, 2);
61  assert(p+3+5==s && "Couldn't find third section");
62 }
63 
64 #define STRLEN 11
65 #define TESTSTR "hello world"
66 void test_strings() {
67  char test[STRLEN+1] = TESTSTR;
68  char a[STRLEN+1];
69  char *p;
70  int fd;
71 
72  test[STRLEN]=(char)0;
73 
74  // Fixed-length
75  fd = open("testfile", O_WRONLY|O_CREAT|O_TRUNC, 0666);
76  assert(fd>1 && "Couldn't open file to write test output");
77  write_string(fd, test, STRLEN);
78  close(fd);
79  fd = open("testfile", O_RDONLY);
80  assert(fd>1 && "Couldn't open file to read test input");
81  p = readfile(fd);
82  parse_string(p, a, STRLEN);
83  assert( parse_string(p, a, STRLEN)==0 );
84  assert( !memcmp(test, a, STRLEN) );
85  close(fd);
86 
87  // Auto-length
88  fd = open("testfile", O_WRONLY|O_CREAT|O_TRUNC, 0666);
89  assert(fd>1 && "Couldn't open file to write test output");
92  close(fd);
93  fd = open("testfile", O_RDONLY);
94  assert(fd>1 && "Couldn't open file to read test input");
95  p = readfile(fd);
96  assert( parse_string(p, a, SECTION_TERMINATED)==0 );
97  assert( !memcmp(test, a, STRLEN+1) ); // include null-terminator
98  close(fd);
99 }
100 
101 void test_prng() {
102  struct prng_rand_t S;
103  uint64_t RA[3], RB[3];
104 
105  prng_srand(1, &S);
106  RA[0] = prng_rand(&S);
107  RA[1] = prng_rand(&S);
108  RA[2] = prng_rand(&S);
109 
110  prng_srand(1, &S);
111  RB[0] = prng_rand(&S);
112  RB[1] = prng_rand(&S);
113  RB[2] = prng_rand(&S);
114 
115  assert( RA[0]==RB[0] && "PRNG non-deterministic" );
116  assert( RA[1]==RB[1] && "PRNG non-deterministic" );
117  assert( RA[2]==RB[2] && "PRNG non-deterministic" );
118 }
119 
120 int main(int argc, char **argv)
121 {
122  test_section_jumping();
123  test_uint8_t_array();
124  test_uint16_t_array();
125  test_uint32_t_array();
126  test_uint64_t_array();
127  test_float_array();
128  test_double_array();
129  test_strings();
130  test_prng();
131 
132  printf("Success.\n");
133  return 0;
134 }
static uint64_t prng_rand(struct prng_rand_t *state)
Definition: support.h:85
void data_to_output(int fd, void *vdata)
Definition: test_support.c:14
int main(int argc, char **argv)
Definition: test_support.c:120
void test_strings()
Definition: test_support.c:66
#define generate_test_TYPE_array(TYPE)
Definition: test_support.c:19
#define TESTSTR
Definition: test_support.c:65
void test_prng()
Definition: test_support.c:101
int parse_string(char *s, char *arr, int n)
Definition: support.c:77
int write_string(int fd, char *arr, int n)
int write_section_header(int fd)
int INPUT_SIZE
Definition: test_support.c:16
#define SECTION_TERMINATED
Definition: support.h:9
char * readfile(int fd)
Definition: support.c:34
char * find_section_start(char *s, int n)
Definition: support.c:56
#define STRLEN
Definition: test_support.c:64
void output_to_data(int fd, void *vdata)
Definition: test_support.c:13
int test(NodeId var_2, PropertyId p_var_3, PropertyId p_var_4, PropertyId p_var_5, PropertyId p_var_7, PropertyId p_var_9, PropertyId p_var_11)
static void prng_srand(uint64_t seed, struct prng_rand_t *state)
Definition: support.h:106
void input_to_data(int fd, void *vdata)
Definition: test_support.c:12
void run_benchmark(void *vargs)
Definition: test_support.c:11
int check_data(void *vdata, void *vref)
Definition: test_support.c:15

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13