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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 #include "dbus-sysdeps.h"
00025 #include "dbus-internals.h"
00026 #include "dbus-string.h"
00027 #include "dbus-test.h"
00028 
00029 #ifdef DBUS_BUILD_TESTS
00030 #include <stdlib.h>
00031 static void
00032 check_dirname (const char *filename,
00033                const char *dirname)
00034 {
00035   DBusString f, d;
00036   
00037   _dbus_string_init_const (&f, filename);
00038 
00039   if (!_dbus_string_init (&d))
00040     _dbus_assert_not_reached ("no memory");
00041 
00042   if (!_dbus_string_get_dirname (&f, &d))
00043     _dbus_assert_not_reached ("no memory");
00044 
00045   if (!_dbus_string_equal_c_str (&d, dirname))
00046     {
00047       _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n",
00048                   filename,
00049                   _dbus_string_get_const_data (&d),
00050                   dirname);
00051       exit (1);
00052     }
00053 
00054   _dbus_string_free (&d);
00055 }
00056 
00057 static void
00058 check_path_absolute (const char *path,
00059                      dbus_bool_t expected)
00060 {
00061   DBusString p;
00062 
00063   _dbus_string_init_const (&p, path);
00064 
00065   if (_dbus_path_is_absolute (&p) != expected)
00066     {
00067       _dbus_warn ("For path \"%s\" expected absolute = %d got %d\n",
00068                   path, expected, _dbus_path_is_absolute (&p));
00069       exit (1);
00070     }
00071 }
00072 
00078 dbus_bool_t
00079 _dbus_sysdeps_test (void)
00080 {
00081   DBusString str;
00082   double val;
00083   int pos;
00084 
00085 #ifdef DBUS_WIN
00086   check_dirname ("foo\\bar", "foo");
00087   check_dirname ("foo\\\\bar", "foo");
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", "\\");
00094   check_dirname ("\\\\foo", "\\");
00095   check_dirname ("\\", "\\");
00096   check_dirname ("\\\\", "\\");
00097   check_dirname ("\\/", "\\");
00098   check_dirname ("/\\/", "/");
00099   check_dirname ("c:\\foo\\bar", "c:\\foo");
00100   check_dirname ("c:\\foo", "c:\\");
00101   check_dirname ("c:/foo", "c:/");
00102   check_dirname ("c:\\", "c:\\");
00103   check_dirname ("c:/", "c:/");
00104   check_dirname ("", ".");  
00105 #else  
00106   check_dirname ("foo", ".");
00107   check_dirname ("foo/bar", "foo");
00108   check_dirname ("foo//bar", "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", "/");
00117   check_dirname ("////foo", "/");
00118   check_dirname ("/foo/bar", "/foo");
00119   check_dirname ("/foo//bar", "/foo");
00120   check_dirname ("/foo///bar", "/foo");
00121   check_dirname ("/", "/");
00122   check_dirname ("///", "/");
00123   check_dirname ("", ".");  
00124 #endif
00125 
00126   _dbus_string_init_const (&str, "3.5");
00127   if (!_dbus_string_parse_double (&str,
00128                                   0, &val, &pos))
00129     {
00130       _dbus_warn ("Failed to parse double");
00131       exit (1);
00132     }
00133   if (ABS(3.5 - val) > 1e-6)
00134     {
00135       _dbus_warn ("Failed to parse 3.5 correctly, got: %f", val);
00136       exit (1);
00137     }
00138   if (pos != 3)
00139     {
00140       _dbus_warn ("_dbus_string_parse_double of \"3.5\" returned wrong position %d", pos);
00141       exit (1);
00142     }
00143 
00144   _dbus_string_init_const (&str, "0xff");
00145   if (_dbus_string_parse_double (&str,
00146                                  0, &val, &pos))
00147     {
00148       _dbus_warn ("Should not have parsed hex as double\n");
00149       exit (1);
00150     }
00151 
00152 #ifdef DBUS_WIN
00153   check_path_absolute ("c:/", TRUE);
00154   check_path_absolute ("c:/foo", TRUE);
00155   check_path_absolute ("", FALSE);
00156   check_path_absolute ("foo", FALSE);
00157   check_path_absolute ("foo/bar", FALSE);
00158   check_path_absolute ("", FALSE);
00159   check_path_absolute ("foo\\bar", FALSE);
00160   check_path_absolute ("c:\\", TRUE);
00161   check_path_absolute ("c:\\foo", TRUE);
00162   check_path_absolute ("c:", TRUE);
00163   check_path_absolute ("c:\\foo\\bar", TRUE);
00164   check_path_absolute ("\\", TRUE);
00165   check_path_absolute ("/", TRUE);
00166 #else  
00167   check_path_absolute ("/", TRUE);
00168   check_path_absolute ("/foo", TRUE);
00169   check_path_absolute ("", FALSE);
00170   check_path_absolute ("foo", FALSE);
00171   check_path_absolute ("foo/bar", FALSE);
00172 #endif
00173   
00174   return TRUE;
00175 }
00176 #endif /* DBUS_BUILD_TESTS */

Generated on Tue Feb 24 16:40:40 2009 for D-Bus by  doxygen 1.5.1