2006-10-21 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-util.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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   check_dirname ("foo", ".");
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//bar////", "foo");
94   check_dirname ("foo///bar///////", "foo");
95   check_dirname ("/foo", "/");
96   check_dirname ("////foo", "/");
97   check_dirname ("/foo/bar", "/foo");
98   check_dirname ("/foo//bar", "/foo");
99   check_dirname ("/foo///bar", "/foo");
100   check_dirname ("/", "/");
101   check_dirname ("///", "/");
102   check_dirname ("", ".");  
103
104
105   _dbus_string_init_const (&str, "3.5");
106   if (!_dbus_string_parse_double (&str,
107                                   0, &val, &pos))
108     {
109       _dbus_warn ("Failed to parse double");
110       exit (1);
111     }
112   if (ABS(3.5 - val) > 1e-6)
113     {
114       _dbus_warn ("Failed to parse 3.5 correctly, got: %f", val);
115       exit (1);
116     }
117   if (pos != 3)
118     {
119       _dbus_warn ("_dbus_string_parse_double of \"3.5\" returned wrong position %d", pos);
120       exit (1);
121     }
122
123   _dbus_string_init_const (&str, "0xff");
124   if (!_dbus_string_parse_double (&str,
125                                   0, &val, &pos))
126     {
127       _dbus_warn ("Failed to parse double");
128       exit (1);
129     }
130   if (ABS (0xff - val) > 1e-6)
131     {
132       _dbus_warn ("Failed to parse 0xff correctly, got: %f\n", val);
133       exit (1);
134     }
135   if (pos != 4)
136     {
137       _dbus_warn ("_dbus_string_parse_double of \"0xff\" returned wrong position %d", pos);
138       exit (1);
139     }
140   
141   check_path_absolute ("/", TRUE);
142   check_path_absolute ("/foo", TRUE);
143   check_path_absolute ("", FALSE);
144   check_path_absolute ("foo", FALSE);
145   check_path_absolute ("foo/bar", FALSE);
146   
147   return TRUE;
148 }
149 #endif /* DBUS_BUILD_TESTS */