libcss
Loading...
Searching...
No Matches
testutils.h
Go to the documentation of this file.
1#ifndef test_testutils_h_
2#define test_testutils_h_
3
4#include <stdbool.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/types.h>
9
10#ifndef UNUSED
11#define UNUSED(x) ((x) = (x))
12#endif
13
14/* Redefine assert, so we can simply use the standard assert mechanism
15 * within testcases and exit with the right output for the testrunner
16 * to do the right thing. */
17void __assert2(const char *expr, const char *function,
18 const char *file, int line);
19
20void __assert2(const char *expr, const char *function,
21 const char *file, int line)
22{
23 UNUSED(function);
24 UNUSED(file);
25
26 printf("FAIL - %s at line %d\n", expr, line);
27
28 exit(EXIT_FAILURE);
29}
30
31#undef assert
32#define assert(expr) \
33 ((void) ((expr) || (__assert2 (#expr, __func__, __FILE__, __LINE__), 0)))
34
35
36typedef bool (*line_func)(const char *data, size_t datalen, void *pw);
37
38static size_t css__parse_strlen(const char *str, size_t limit);
39char *css__parse_strnchr(const char *str, size_t len, int chr);
40bool css__parse_testfile(const char *filename, line_func callback, void *pw);
41size_t css__parse_filesize(const char *filename);
42
51bool css__parse_testfile(const char *filename, line_func callback, void *pw)
52{
53 FILE *fp;
54 char buf[300];
55
56 fp = fopen(filename, "rb");
57 if (fp == NULL) {
58 printf("Failed opening %s\n", filename);
59 return false;
60 }
61
62 while (fgets(buf, sizeof buf, fp)) {
63 if (buf[0] == '\n')
64 continue;
65
66 if (!callback(buf, css__parse_strlen(buf, sizeof buf - 1), pw)) {
67 fclose(fp);
68 return false;
69 }
70 }
71
72 fclose(fp);
73
74 return true;
75}
76
84size_t css__parse_strlen(const char *str, size_t limit)
85{
86 size_t len = 0;
87
88 if (str == NULL)
89 return 0;
90
91 while (len < limit - 1 && *str != '\n') {
92 len++;
93 str++;
94 }
95
96 len++;
97
98 return len;
99}
100
109char *css__parse_strnchr(const char *str, size_t len, int chr)
110{
111 size_t i;
112
113 if (str == NULL)
114 return NULL;
115
116 for (i = 0; i < len; i++) {
117 if (str[i] == chr)
118 break;
119 }
120
121 if (i == len)
122 return NULL;
123
124 return (char *) str + i;
125}
126
133size_t css__parse_filesize(const char *filename)
134{
135 FILE *fp;
136 size_t len = 0;
137
138 fp = fopen(filename, "rb");
139 if (fp == NULL) {
140 printf("Failed opening %s\n", filename);
141 return 0;
142 }
143
144 fseek(fp, 0, SEEK_END);
145 len = ftell(fp);
146
147 fclose(fp);
148
149 return len;
150}
151
152
160css_error css_error_from_string(const char *str, size_t len);
161css_error css_error_from_string(const char *str, size_t len)
162{
163 if (strncmp(str, "CSS_OK", len) == 0) {
164 return CSS_OK;
165 } else if (strncmp(str, "CSS_NOMEM", len) == 0) {
166 return CSS_NOMEM;
167 } else if (strncmp(str, "CSS_BADPARM", len) == 0) {
168 return CSS_BADPARM;
169 } else if (strncmp(str, "CSS_INVALID", len) == 0) {
170 return CSS_INVALID;
171 } else if (strncmp(str, "CSS_FILENOTFOUND", len) == 0) {
172 return CSS_FILENOTFOUND;
173 } else if (strncmp(str, "CSS_NEEDDATA", len) == 0) {
174 return CSS_NEEDDATA;
175 } else if (strncmp(str, "CSS_BADCHARSET", len) == 0) {
176 return CSS_BADCHARSET;
177 } else if (strncmp(str, "CSS_EOF", len) == 0) {
178 return CSS_EOF;
179 }
180
181 return CSS_OK;
182}
183
184#endif
css_error
Definition errors.h:18
@ CSS_EOF
Definition errors.h:27
@ CSS_INVALID
Definition errors.h:23
@ CSS_NOMEM
Definition errors.h:21
@ CSS_BADCHARSET
Definition errors.h:26
@ CSS_BADPARM
Definition errors.h:22
@ CSS_NEEDDATA
Definition errors.h:25
@ CSS_OK
Definition errors.h:19
@ CSS_FILENOTFOUND
Definition errors.h:24
size_t css__parse_filesize(const char *filename)
Definition testutils.h:133
bool css__parse_testfile(const char *filename, line_func callback, void *pw)
Definition testutils.h:51
void __assert2(const char *expr, const char *function, const char *file, int line)
Definition testutils.h:20
css_error css_error_from_string(const char *str, size_t len)
Definition testutils.h:161
#define UNUSED(x)
Definition testutils.h:11
char * css__parse_strnchr(const char *str, size_t len, int chr)
Definition testutils.h:109
bool(* line_func)(const char *data, size_t datalen, void *pw)
Definition testutils.h:36