err to stderr
[psensor.git] / tests / test_io_dir_list.c
1 /*
2  * Copyright (C) 2010-2011 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "../src/lib/pio.h"
24
25 static int test_empty_dir()
26 {
27         int ret;
28         char **paths;
29
30         paths = dir_list("data/empty_dir", NULL);
31
32         ret = 0;
33         if (paths) {
34                 if (*paths) {
35                         ret = 1;
36                         fprintf(stderr, "ERROR: list not empty %s\n", *paths);
37                 }
38
39                 paths_free(paths);
40         } 
41
42         return ret;
43 }
44
45 static int test_2files_dir()
46 {
47         int ret, one, two;
48         char **paths, **cur;
49
50         paths = dir_list("data/2files_dir", NULL);
51
52         one = two = ret = 0;
53
54         if (!paths) {
55                 fprintf(stderr, "ERROR: list is NULL\n");
56                 return 1;
57         }
58
59         cur = paths;
60         while(*cur) {
61                 if (!strcmp(*cur, "data/2files_dir/one")) {
62                         one++;
63                 } else if (!strcmp(*cur, "data/2files_dir/two")) {
64                         two++;
65                 } else {
66                         fprintf(stderr, "ERROR: wrong item: %s\n", *cur);
67
68                         ret = 1;
69                 }
70                 
71                 cur++;
72         }
73         
74         if (!ret && one == 1 && two == 1)
75                 ret = 0;
76         else
77                 ret = 1;
78         
79         paths_free(paths);
80
81         return ret;
82 }
83
84 static int tests_dir_list() {
85         int failures;
86
87         failures += test_empty_dir();
88
89         failures += test_2files_dir();
90
91         return failures;
92 }
93
94 int main(int argc, char **argv)
95 {
96         int failures;
97
98         failures = 0;
99
100         failures += tests_dir_list();
101
102         if (failures) 
103                 exit(EXIT_FAILURE);
104         else
105                 exit(EXIT_SUCCESS);
106 }