D-Bus  1.4.10
dbus-sysdeps-util.c
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API
00003  * 
00004  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat, Inc.
00005  * Copyright (C) 2003 CodeFactory AB
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-sysdeps.h"
00027 #include "dbus-internals.h"
00028 #include "dbus-string.h"
00029 #include "dbus-test.h"
00030 
00031 #ifdef DBUS_BUILD_TESTS
00032 #include <stdlib.h>
00033 static void
00034 check_dirname (const char *filename,
00035                const char *dirname)
00036 {
00037   DBusString f, d;
00038   
00039   _dbus_string_init_const (&f, filename);
00040 
00041   if (!_dbus_string_init (&d))
00042     _dbus_assert_not_reached ("no memory");
00043 
00044   if (!_dbus_string_get_dirname (&f, &d))
00045     _dbus_assert_not_reached ("no memory");
00046 
00047   if (!_dbus_string_equal_c_str (&d, dirname))
00048     {
00049       _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n",
00050                   filename,
00051                   _dbus_string_get_const_data (&d),
00052                   dirname);
00053       exit (1);
00054     }
00055 
00056   _dbus_string_free (&d);
00057 }
00058 
00059 static void
00060 check_path_absolute (const char *path,
00061                      dbus_bool_t expected)
00062 {
00063   DBusString p;
00064 
00065   _dbus_string_init_const (&p, path);
00066 
00067   if (_dbus_path_is_absolute (&p) != expected)
00068     {
00069       _dbus_warn ("For path \"%s\" expected absolute = %d got %d\n",
00070                   path, expected, _dbus_path_is_absolute (&p));
00071       exit (1);
00072     }
00073 }
00074 
00080 dbus_bool_t
00081 _dbus_sysdeps_test (void)
00082 {
00083   DBusString str;
00084   double val;
00085   int pos;
00086 
00087 #ifdef DBUS_WIN
00088   check_dirname ("foo\\bar", "foo");
00089   check_dirname ("foo\\\\bar", "foo");
00090   check_dirname ("foo/\\/bar", "foo");
00091   check_dirname ("foo\\bar/", "foo");
00092   check_dirname ("foo//bar\\", "foo");
00093   check_dirname ("foo\\bar/", "foo");
00094   check_dirname ("foo/bar\\\\", "foo");
00095   check_dirname ("\\foo", "\\");
00096   check_dirname ("\\\\foo", "\\");
00097   check_dirname ("\\", "\\");
00098   check_dirname ("\\\\", "\\");
00099   check_dirname ("\\/", "\\");
00100   check_dirname ("/\\/", "/");
00101   check_dirname ("c:\\foo\\bar", "c:\\foo");
00102   check_dirname ("c:\\foo", "c:\\");
00103   check_dirname ("c:/foo", "c:/");
00104   check_dirname ("c:\\", "c:\\");
00105   check_dirname ("c:/", "c:/");
00106   check_dirname ("", ".");  
00107 #else  
00108   check_dirname ("foo", ".");
00109   check_dirname ("foo/bar", "foo");
00110   check_dirname ("foo//bar", "foo");
00111   check_dirname ("foo///bar", "foo");
00112   check_dirname ("foo/bar/", "foo");
00113   check_dirname ("foo//bar/", "foo");
00114   check_dirname ("foo///bar/", "foo");
00115   check_dirname ("foo/bar//", "foo");
00116   check_dirname ("foo//bar////", "foo");
00117   check_dirname ("foo///bar///////", "foo");
00118   check_dirname ("/foo", "/");
00119   check_dirname ("////foo", "/");
00120   check_dirname ("/foo/bar", "/foo");
00121   check_dirname ("/foo//bar", "/foo");
00122   check_dirname ("/foo///bar", "/foo");
00123   check_dirname ("/", "/");
00124   check_dirname ("///", "/");
00125   check_dirname ("", ".");  
00126 #endif
00127 
00128   _dbus_string_init_const (&str, "3.5");
00129   if (!_dbus_string_parse_double (&str,
00130                                   0, &val, &pos))
00131     {
00132       _dbus_warn ("Failed to parse double");
00133       exit (1);
00134     }
00135   if (ABS(3.5 - val) > 1e-6)
00136     {
00137       _dbus_warn ("Failed to parse 3.5 correctly, got: %f", val);
00138       exit (1);
00139     }
00140   if (pos != 3)
00141     {
00142       _dbus_warn ("_dbus_string_parse_double of \"3.5\" returned wrong position %d", pos);
00143       exit (1);
00144     }
00145 
00146   _dbus_string_init_const (&str, "0xff");
00147   if (_dbus_string_parse_double (&str,
00148                                  0, &val, &pos))
00149     {
00150       _dbus_warn ("Should not have parsed hex as double\n");
00151       exit (1);
00152     }
00153 
00154 #ifdef DBUS_WIN
00155   check_path_absolute ("c:/", TRUE);
00156   check_path_absolute ("c:/foo", TRUE);
00157   check_path_absolute ("", FALSE);
00158   check_path_absolute ("foo", FALSE);
00159   check_path_absolute ("foo/bar", FALSE);
00160   check_path_absolute ("", FALSE);
00161   check_path_absolute ("foo\\bar", FALSE);
00162   check_path_absolute ("c:\\", TRUE);
00163   check_path_absolute ("c:\\foo", TRUE);
00164   check_path_absolute ("c:", TRUE);
00165   check_path_absolute ("c:\\foo\\bar", TRUE);
00166   check_path_absolute ("\\", TRUE);
00167   check_path_absolute ("/", TRUE);
00168 #else  
00169   check_path_absolute ("/", TRUE);
00170   check_path_absolute ("/foo", TRUE);
00171   check_path_absolute ("", FALSE);
00172   check_path_absolute ("foo", FALSE);
00173   check_path_absolute ("foo/bar", FALSE);
00174 #endif
00175   
00176   return TRUE;
00177 }
00178 #endif /* DBUS_BUILD_TESTS */