33 "startswith first test failed.");
35 "startswith second test failed.");
42 "endswith first test failed.");
44 "endswith second test failed.");
50 char *fname =
"test_get_line.txt";
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");
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");
81 char *fname =
"test_next_line.txt";
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");
94 fid = fopen(fname,
"r");
97 mu_assert(strcmp(line,
"second line\n") == 0,
"second line unequal");
100 mu_assert(strcmp(line,
"fourth line\n") == 0,
"fourth line unequal");
112 char *fname =
"test_get_fmt_double.txt";
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");
123 fid = fopen(fname,
"r");
126 mu_assert(value == 3.1416,
"first value wrong");
129 mu_assert(value == 42.42,
"second value wrong");
132 mu_assert(isnan(value),
"third value wrong");
145 char *fname =
"test_get_fmt_long.txt";
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");
156 fid = fopen(fname,
"r");
159 mu_assert(value == 53161,
"first value wrong");
162 mu_assert(value == 1212,
"second value wrong");
164 value =
get_fmt_long(fid, fname,
"line without a long");
169 mu_assert(value == 0,
"third value wrong")
182 char *fname =
"test_all_doubles_str.txt";
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");
194 fid = fopen(fname,
"r");
197 fgets(buffer, 60, fid);
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)");
205 fgets(buffer, 60, fid);
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)");
212 fgets(buffer, 60, fid);
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)");
228 char *fname =
"test_all_longs_str.txt";
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");
240 fid = fopen(fname,
"r");
243 fgets(buffer, 60, fid);
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)");
251 fgets(buffer, 60, fid);
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)");
258 fgets(buffer, 60, fid);
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)");
Minimal unit testing framework for C.
#define Calloc(type, size)
char * test_all_doubles_str()
#define mu_assert(test, message)
#define GENSVM_MAX_LINE_LENGTH
#define mu_run_test(test)
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
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 * 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_fmt_double()
double get_fmt_double(FILE *fid, char *filename, const char *fmt)
Read a double from file following a format.
void next_line(FILE *fid, char *filename)
Move to next line in file.
Header file for gensvm_strutil.c.