added dbus support
[monky] / src / dbus / dbus-sysdeps-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API
3  * 
4  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #include "dbus-sysdeps.h"
25 #include "dbus-internals.h"
26 #include "dbus-string.h"
27 #include "dbus-test.h"
28
29 #ifdef DBUS_BUILD_TESTS
30 #include <stdlib.h>
31 static void
32 check_dirname (const char *filename,
33                const char *dirname)
34 {
35   DBusString f, d;
36   
37   _dbus_string_init_const (&f, filename);
38
39   if (!_dbus_string_init (&d))
40     _dbus_assert_not_reached ("no memory");
41
42   if (!_dbus_string_get_dirname (&f, &d))
43     _dbus_assert_not_reached ("no memory");
44
45   if (!_dbus_string_equal_c_str (&d, dirname))
46     {
47       _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n",
48                   filename,
49                   _dbus_string_get_const_data (&d),
50                   dirname);
51       exit (1);
52     }
53
54   _dbus_string_free (&d);
55 }
56
57 static void
58 check_path_absolute (const char *path,
59                      dbus_bool_t expected)
60 {
61   DBusString p;
62
63   _dbus_string_init_const (&p, path);
64
65   if (_dbus_path_is_absolute (&p) != expected)
66     {
67       _dbus_warn ("For path \"%s\" expected absolute = %d got %d\n",
68                   path, expected, _dbus_path_is_absolute (&p));
69       exit (1);
70     }
71 }
72
73 /**
74  * Unit test for dbus-sysdeps.c.
75  * 
76  * @returns #TRUE on success.
77  */
78 dbus_bool_t
79 _dbus_sysdeps_test (void)
80 {
81   DBusString str;
82   double val;
83   int pos;
84
85 #ifdef DBUS_WIN
86   check_dirname ("foo\\bar", "foo");
87   check_dirname ("foo\\\\bar", "foo");
88   check_dirname ("foo/\\/bar", "foo");
89   check_dirname ("foo\\bar/", "foo");
90   check_dirname ("foo//bar\\", "foo");
91   check_dirname ("foo\\bar/", "foo");
92   check_dirname ("foo/bar\\\\", "foo");
93   check_dirname ("\\foo", "\\");
94   check_dirname ("\\\\foo", "\\");
95   check_dirname ("\\", "\\");
96   check_dirname ("\\\\", "\\");
97   check_dirname ("\\/", "\\");
98   check_dirname ("/\\/", "/");
99   check_dirname ("c:\\foo\\bar", "c:\\foo");
100   check_dirname ("c:\\foo", "c:\\");
101   check_dirname ("c:/foo", "c:/");
102   check_dirname ("c:\\", "c:\\");
103   check_dirname ("c:/", "c:/");
104   check_dirname ("", ".");  
105 #else  
106   check_dirname ("foo", ".");
107   check_dirname ("foo/bar", "foo");
108   check_dirname ("foo//bar", "foo");
109   check_dirname ("foo///bar", "foo");
110   check_dirname ("foo/bar/", "foo");
111   check_dirname ("foo//bar/", "foo");
112   check_dirname ("foo///bar/", "foo");
113   check_dirname ("foo/bar//", "foo");
114   check_dirname ("foo//bar////", "foo");
115   check_dirname ("foo///bar///////", "foo");
116   check_dirname ("/foo", "/");
117   check_dirname ("////foo", "/");
118   check_dirname ("/foo/bar", "/foo");
119   check_dirname ("/foo//bar", "/foo");
120   check_dirname ("/foo///bar", "/foo");
121   check_dirname ("/", "/");
122   check_dirname ("///", "/");
123   check_dirname ("", ".");  
124 #endif
125
126   _dbus_string_init_const (&str, "3.5");
127   if (!_dbus_string_parse_double (&str,
128                                   0, &val, &pos))
129     {
130       _dbus_warn ("Failed to parse double");
131       exit (1);
132     }
133   if (ABS(3.5 - val) > 1e-6)
134     {
135       _dbus_warn ("Failed to parse 3.5 correctly, got: %f", val);
136       exit (1);
137     }
138   if (pos != 3)
139     {
140       _dbus_warn ("_dbus_string_parse_double of \"3.5\" returned wrong position %d", pos);
141       exit (1);
142     }
143
144   _dbus_string_init_const (&str, "0xff");
145   if (_dbus_string_parse_double (&str,
146                                  0, &val, &pos))
147     {
148       _dbus_warn ("Should not have parsed hex as double\n");
149       exit (1);
150     }
151
152 #ifdef DBUS_WIN
153   check_path_absolute ("c:/", TRUE);
154   check_path_absolute ("c:/foo", TRUE);
155   check_path_absolute ("", FALSE);
156   check_path_absolute ("foo", FALSE);
157   check_path_absolute ("foo/bar", FALSE);
158   check_path_absolute ("", FALSE);
159   check_path_absolute ("foo\\bar", FALSE);
160   check_path_absolute ("c:\\", TRUE);
161   check_path_absolute ("c:\\foo", TRUE);
162   check_path_absolute ("c:", TRUE);
163   check_path_absolute ("c:\\foo\\bar", TRUE);
164   check_path_absolute ("\\", TRUE);
165   check_path_absolute ("/", TRUE);
166 #else  
167   check_path_absolute ("/", TRUE);
168   check_path_absolute ("/foo", TRUE);
169   check_path_absolute ("", FALSE);
170   check_path_absolute ("foo", FALSE);
171   check_path_absolute ("foo/bar", FALSE);
172 #endif
173   
174   return TRUE;
175 }
176 #endif /* DBUS_BUILD_TESTS */