summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2013-06-04 16:31:29 +0200
committerYorhel <git@yorhel.nl>2013-06-04 16:32:08 +0200
commita510c7269d2223110614047e1deed55f06c1b20d (patch)
treefc3670a5e36c67035722ea96a9be7bf6d904133a
parentf04b0a293d2f74ec454a331737738a0678f00f40 (diff)
ylog: Add tests for some internal functions + fix config parsing bug
-rw-r--r--test/Makefile6
-rw-r--r--test/ylog.c97
-rw-r--r--ylog.c36
3 files changed, 118 insertions, 21 deletions
diff --git a/test/Makefile b/test/Makefile
index 609826e..4d4b3a0 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -17,11 +17,15 @@ evtp: ../evtp.c ../evtp.h evtp.c
sqlasync: ../sqlasync.c ../sqlasync.h sqlasync.c
$(CC) $(CFLAGS) -I.. ../sqlasync.c sqlasync.c -lrt -lpthread -lsqlite3 -o sqlasync
-test: yuri ecbuf evtp sqlasync
+ylog: ../ylog.c ../ylog.h ylog.c
+ $(CC) $(CFLAGS) -I.. ylog.c -o ylog
+
+test: yuri ecbuf evtp sqlasync ylog
./yuri
./ecbuf
./evtp
./sqlasync
+ ./ylog
@echo All tests passed.
ecbuf-bench: ../ecbuf.h ecbuf-bench.c
diff --git a/test/ylog.c b/test/ylog.c
new file mode 100644
index 0000000..3a3009c
--- /dev/null
+++ b/test/ylog.c
@@ -0,0 +1,97 @@
+/* Copyright (c) 2012-2013 Yoran Heling
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifdef NDEBUG
+#error These tests should not be compiled with -DNDEBUG!
+#endif
+
+#include <ylog.c> /* Include ylog.c directly, we're testing static functions */
+#include <assert.h>
+
+
+int main(int argc, char **argv) {
+
+ /* ylog_set_file_name() */
+#define T(in, out) do {\
+ ylog_file_t file;\
+ ylog_set_file_name(&file, in);\
+ assert(strcmp(file.name, out) == 0);\
+ free((char *)file.name);\
+ } while(0)
+ T("", "");
+ T("abc", "abc");
+ T("abc.c", "abc");
+ T("abc.cpp", "abc");
+ T("x.cc", "x");
+ T("/some/file.h", "/some/file");
+ T("abc.pl", "abc.pl");
+#undef T
+
+ /* ylog_set_file_level() */
+ ylog_default_level = -1;
+#define T(fn, pat, lvl) do {\
+ ylog_file_t file;\
+ file.name = fn;\
+ ylog_pattern = strdup(pat);\
+ ylog_set_file_level(&file);\
+ assert(file.level == lvl);\
+ assert(strcmp(ylog_pattern, pat) == 0);\
+ free(ylog_pattern);\
+ } while(0)
+ do { /* Test NULL handling */
+ ylog_file_t file;
+ file.name = NULL;
+ ylog_pattern = NULL;
+ ylog_set_file_level(&file);
+ assert(file.level == -1);
+ } while(0);
+ T("", "*:5", 5);
+ T("", "*:0", 0);
+ T("", "*:9999", 9999); /* YLOG_MAX */
+ T("", "*:10000", -1);
+ T("", "*:-5", -1);
+ T("", "*:+5", -1);
+ T("", "*:009", 9); /* 0-prefixing can be allowed, I suppose */
+ T("some/file", "*:4", 4);
+ T("some/file", "some:4", -1);
+ T("some/file", "some/fil:4", -1);
+ T("some/file", "some/file/:4", -1);
+ T("some/file", "some/file:4", 4);
+ T("some/file", "file:4", 4);
+ T("some/file", "fil*:4", 4);
+ T("some/file", "f*:4", 4);
+ T("some/file", "some/*:4", 4);
+ T("some/file", "so*/file:4", 4);
+ T("some/file", "*/file:4", 4);
+ T("some/file", "abc:3,file:4", 4);
+ T("some/file", "abc:3,*:4", 4);
+ T("some/file", "abc:3,some/file:4", 4);
+ T("some/file", "abc:3,some/*:4", 4);
+ T("somex/file", "abc:3,some/*:4", -1);
+ T("somex/file", "abc:3,some/*:4,*:1", 1);
+ T("some/file", "abc:3,some/*:4,*:1", 4);
+#undef T
+
+ return 0;
+}
+
+/* vim: set noet sw=4 ts=4: */
diff --git a/ylog.c b/ylog.c
index d9421b7..8955a95 100644
--- a/ylog.c
+++ b/ylog.c
@@ -66,39 +66,35 @@ static int ylog_match(const char *pat, const char *fn) {
/* Assumes the mutex is locked */
static void ylog_set_file_level(ylog_file_t *file) {
char *pat = ylog_pattern;
+ int val;
const char *start;
- int level;
- if(!pat)
- goto fallback;
- while(*pat) {
+ while(pat && *pat) {
start = pat;
while(*pat && *pat != ':')
pat++;
*pat = 0;
- if(!ylog_match(start, file->name)) {
- *pat = ':';
- while(*pat && *pat != ',')
- pat++;
- continue;
- }
+ val = ylog_match(start, file->name);
*(pat++) = ':';
- /* We have a match, get level */
- level = 0;
- while(level < YLOG_MAX && *pat >= '0' && *pat <= '9') {
- level = level*10 + (*pat-'0');
+ if(val)
+ break;
+ while(*pat && pat[-1] != ',')
+ pat++;
+ }
+
+ if(pat && *pat) {
+ start = pat;
+ val = 0;
+ while(val < YLOG_MAX && *pat >= '0' && *pat <= '9') {
+ val = val*10 + (*pat-'0');
pat++;
}
- if(level <= YLOG_MAX) {
- file->level = level;
+ if(val <= YLOG_MAX && pat != start && (!*pat || *pat == ',')) {
+ file->level = val;
return;
}
- goto fallback;
- while(*pat && *pat != ',')
- pat++;
}
-fallback:
file->level = ylog_default_level;
}