GenSVM
test_gensvm_strutil.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_strutil.h"
29 
31 {
32  mu_assert(str_startswith("test this string", "test") == true,
33  "startswith first test failed.");
34  mu_assert(str_startswith("test this string", "word") == false,
35  "startswith second test failed.");
36  return NULL;
37 }
38 
40 {
41  mu_assert(str_endswith("this is a string", "string") == true,
42  "endswith first test failed.");
43  mu_assert(str_endswith("this is a string", "word") == false,
44  "endswith second test failed.");
45  return NULL;
46 }
47 
49 {
50  char *fname = "test_get_line.txt";
51  // prepare the test file
52  FILE *fid = fopen(fname, "w");
53  if (fid == NULL) return "couldn't open test file for get_line";
54  fprintf(fid, "first line\n");
55  fprintf(fid, "second line\n");
56  fclose(fid);
57  // end preparation
58 
59  // start of test code
60  char buffer[GENSVM_MAX_LINE_LENGTH];
61  char *retval = NULL;
62  fid = fopen(fname, "r");
63  retval = get_line(fid, fname, buffer);
64  mu_assert(retval == buffer, "return value not buffer (1)");
65  retval = get_line(fid, fname, buffer);
66  mu_assert(retval == buffer, "return value not buffer (2)");
67  retval = get_line(fid, fname, buffer);
68  mu_assert(retval == NULL, "return value not NULL");
69 
70  fclose(fid);
71  // end of test code
72 
73  // cleanup
74  remove(fname);
75 
76  return NULL;
77 }
78 
80 {
81  char *fname = "test_next_line.txt";
82  // prepare the test file
83  FILE *fid = fopen(fname, "w");
84  if (fid == NULL) return "couldn't open test file for next_line";
85  fprintf(fid, "first line\n");
86  fprintf(fid, "second line\n");
87  fprintf(fid, "third line\n");
88  fprintf(fid, "fourth line\n");
89  fclose(fid);
90  // end preparation
91 
92  // start of test code
93  char line[60];
94  fid = fopen(fname, "r");
95  next_line(fid, fname);
96  fgets(line, 60, fid);
97  mu_assert(strcmp(line, "second line\n") == 0, "second line unequal");
98  next_line(fid, fname);
99  fgets(line, 60, fid);
100  mu_assert(strcmp(line, "fourth line\n") == 0, "fourth line unequal");
101  fclose(fid);
102  // end of test code
103 
104  // cleanup
105  remove(fname);
106 
107  return NULL;
108 }
109 
111 {
112  char *fname = "test_get_fmt_double.txt";
113  // prepare the test file
114  FILE *fid = fopen(fname, "w");
115  if (fid == NULL) return "couldn't open test file for get_fmt_double";
116  fprintf(fid, "double = 3.1416\n");
117  fprintf(fid, "another double = 42.42\n");
118  fprintf(fid, "line without a double\n");
119  fclose(fid);
120 
121  // start test code //
122  double value = -1.0;
123  fid = fopen(fname, "r");
124 
125  value = get_fmt_double(fid, fname, "double = %lf");
126  mu_assert(value == 3.1416, "first value wrong");
127 
128  value = get_fmt_double(fid, fname, "another double = %lf");
129  mu_assert(value == 42.42, "second value wrong");
130 
131  value = get_fmt_double(fid, fname, "line without a double");
132  mu_assert(isnan(value), "third value wrong");
133 
134  fclose(fid);
135  // end test code //
136 
137  // cleanup
138  remove(fname);
139 
140  return NULL;
141 }
142 
144 {
145  char *fname = "test_get_fmt_long.txt";
146  // prepare the test file
147  FILE *fid = fopen(fname, "w");
148  if (fid == NULL) return "couldn't open test file for get_fmt_double";
149  fprintf(fid, "long = 53161\n");
150  fprintf(fid, "another long = 1212\n");
151  fprintf(fid, "line without a long\n");
152  fclose(fid);
153 
154  // start test code //
155  long value = -1;
156  fid = fopen(fname, "r");
157 
158  value = get_fmt_long(fid, fname, "long = %li");
159  mu_assert(value == 53161, "first value wrong");
160 
161  value = get_fmt_long(fid, fname, "another long = %li");
162  mu_assert(value == 1212, "second value wrong");
163 
164  value = get_fmt_long(fid, fname, "line without a long");
165  // unfortunately we can't test this properly, a warning will be
166  // generated but the return value will be 0, which can be a valid
167  // value. In general therefore, the real test is to see if a warning
168  // is printed to stderr.
169  mu_assert(value == 0, "third value wrong")
170 
171  fclose(fid);
172  // end test code //
173 
174  // cleanup
175  remove(fname);
176 
177  return NULL;
178 }
179 
181 {
182  char *fname = "test_all_doubles_str.txt";
183  // prepare the test file
184  FILE *fid = fopen(fname, "w");
185  if (fid == NULL) return "couldn't open test file for all_doubles_str";
186  fprintf(fid, "1.0 2.0 3.0 4.0\n");
187  fprintf(fid, "9.1 8.2 7.3\n");
188  fprintf(fid, "offset 1.0 2.0\n");
189  fclose(fid);
190 
191  // start test code //
192  char buffer[60];
193  double *values = Calloc(double, 10);
194  fid = fopen(fname, "r");
195  int nr = -1;
196 
197  fgets(buffer, 60, fid);
198  nr = all_doubles_str(buffer, 0, values);
199  mu_assert(nr == 4, "incorrect number of doubles (1)");
200  mu_assert(values[0] == 1.0, "incorrect first value (1)");
201  mu_assert(values[1] == 2.0, "incorrect second value (1)");
202  mu_assert(values[2] == 3.0, "incorrect third value (1)");
203  mu_assert(values[3] == 4.0, "incorrect fourth value (1)");
204 
205  fgets(buffer, 60, fid);
206  nr = all_doubles_str(buffer, 0, values);
207  mu_assert(nr == 3, "incorrect number of doubles (2)");
208  mu_assert(values[0] == 9.1, "incorrect first value (2)");
209  mu_assert(values[1] == 8.2, "incorrect second value (2)");
210  mu_assert(values[2] == 7.3, "incorrect third value (2)");
211 
212  fgets(buffer, 60, fid);
213  nr = all_doubles_str(buffer, 7, values);
214  mu_assert(nr == 2, "incorrect number of doubles (3)");
215  mu_assert(values[0] == 1.0, "incorrect first value (3)");
216  mu_assert(values[1] == 2.0, "incorrect second value (3)");
217  // end test code //
218 
219  // cleanup
220  remove(fname);
221  free(values);
222 
223  return NULL;
224 }
225 
227 {
228  char *fname = "test_all_longs_str.txt";
229  // prepare the test file
230  FILE *fid = fopen(fname, "w");
231  if (fid == NULL) return "couldn't open test file for all_longs_str";
232  fprintf(fid, "1 2 3 4\n");
233  fprintf(fid, "91 82 73\n");
234  fprintf(fid, "offset 10 20\n");
235  fclose(fid);
236 
237  // start test code //
238  char buffer[60];
239  long *values = Calloc(long, 10);
240  fid = fopen(fname, "r");
241  int nr = -1;
242 
243  fgets(buffer, 60, fid);
244  nr = all_longs_str(buffer, 0, values);
245  mu_assert(nr == 4, "incorrect number of longs (1)");
246  mu_assert(values[0] == 1, "incorrect first value (1)");
247  mu_assert(values[1] == 2, "incorrect second value (1)");
248  mu_assert(values[2] == 3, "incorrect third value (1)");
249  mu_assert(values[3] == 4, "incorrect fourth value (1)");
250 
251  fgets(buffer, 60, fid);
252  nr = all_longs_str(buffer, 0, values);
253  mu_assert(nr == 3, "incorrect number of longs (2)");
254  mu_assert(values[0] == 91, "incorrect first value (2)");
255  mu_assert(values[1] == 82, "incorrect second value (2)");
256  mu_assert(values[2] == 73, "incorrect third value (2)");
257 
258  fgets(buffer, 60, fid);
259  nr = all_longs_str(buffer, 7, values);
260  mu_assert(nr == 2, "incorrect number of longs (3)");
261  mu_assert(values[0] == 10, "incorrect first value (3)");
262  mu_assert(values[1] == 20, "incorrect second value (3)");
263  // end test code //
264 
265  // cleanup
266  remove(fname);
267  free(values);
268 
269  return NULL;
270 }
271 
272 char *all_tests()
273 {
274  mu_suite_start();
283 
284  return NULL;
285 }
286 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
RUN_TESTS(all_tests)
char * test_all_doubles_str()
#define mu_assert(test, message)
Definition: minunit.h:29
#define GENSVM_MAX_LINE_LENGTH
#define mu_run_test(test)
Definition: minunit.h:35
long all_longs_str(char *buffer, long offset, long *all_longs)
Read all longs in a given buffer.
double * values
actual nonzero values, should be of length nnz
Definition: gensvm_sparse.h:63
char * test_str_endswith()
bool str_endswith(const char *str, const char *suf)
Check if a string ends with a suffix.
char * test_get_fmt_long()
long all_doubles_str(char *buffer, long offset, double *all_doubles)
Read all doubles in a given buffer.
bool str_startswith(const char *str, const char *pre)
Check if a string starts with a prefix.
char * test_str_startswith()
char * all_tests()
char * get_line(FILE *fid, char *filename, char *buffer)
Read line to buffer.
char * test_all_longs_str()
long get_fmt_long(FILE *fid, char *filename, const char *fmt)
Read a long integer from file following a format.
char * test_get_line()
char * test_get_fmt_double()
double get_fmt_double(FILE *fid, char *filename, const char *fmt)
Read a double from file following a format.
#define mu_suite_start()
Definition: minunit.h:24
char * test_next_line()
void next_line(FILE *fid, char *filename)
Move to next line in file.
Header file for gensvm_strutil.c.