Imported Upstream version 0.8.0.4
[psensor-pkg-debian.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 #include <string.h>
23
24 #include "../src/lib/pio.h"
25
26 static int test_empty_dir()
27 {
28         int ret;
29         char **paths;
30
31         paths = dir_list("data/empty_dir", NULL);
32
33         ret = 0;
34         if (paths) {
35                 if (*paths) {
36                         ret = 1;
37                         fprintf(stderr, "ERROR: list not empty %s\n", *paths);
38                 }
39
40                 paths_free(paths);
41         } 
42
43         return ret;
44 }
45
46 static int test_2files_dir()
47 {
48         int ret, one, two;
49         char **paths, **cur;
50
51         paths = dir_list("data/2files_dir", NULL);
52
53         one = two = ret = 0;
54
55         if (!paths) {
56                 fprintf(stderr, "ERROR: list is NULL\n");
57                 return 1;
58         }
59
60         cur = paths;
61         while(*cur) {
62                 if (!strcmp(*cur, "data/2files_dir/one")) {
63                         one++;
64                 } else if (!strcmp(*cur, "data/2files_dir/two")) {
65                         two++;
66                 } else {
67                         fprintf(stderr, "ERROR: wrong item: %s\n", *cur);
68
69                         ret = 1;
70                 }
71                 
72                 cur++;
73         }
74         
75         if (!ret && one == 1 && two == 1)
76                 ret = 0;
77         else
78                 ret = 1;
79         
80         paths_free(paths);
81
82         return ret;
83 }
84
85 static int tests_dir_list() {
86         int failures;
87
88         failures = test_empty_dir();
89
90         failures += test_2files_dir();
91
92         return failures;
93 }
94
95 int main(int argc, char **argv)
96 {
97         int failures;
98
99         failures = tests_dir_list();
100
101         if (failures) 
102                 exit(EXIT_FAILURE);
103         else
104                 exit(EXIT_SUCCESS);
105 }