dbus-sysdeps-util.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
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   check_dirname ("foo", ".");
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//bar////", "foo");
00094   check_dirname ("foo///bar///////", "foo");
00095   check_dirname ("/foo", "/");
00096   check_dirname ("////foo", "/");
00097   check_dirname ("/foo/bar", "/foo");
00098   check_dirname ("/foo//bar", "/foo");
00099   check_dirname ("/foo///bar", "/foo");
00100   check_dirname ("/", "/");
00101   check_dirname ("///", "/");
00102   check_dirname ("", ".");  
00103 
00104 
00105   _dbus_string_init_const (&str, "3.5");
00106   if (!_dbus_string_parse_double (&str,
00107                                   0, &val, &pos))
00108     {
00109       _dbus_warn ("Failed to parse double");
00110       exit (1);
00111     }
00112   if (ABS(3.5 - val) > 1e-6)
00113     {
00114       _dbus_warn ("Failed to parse 3.5 correctly, got: %f", val);
00115       exit (1);
00116     }
00117   if (pos != 3)
00118     {
00119       _dbus_warn ("_dbus_string_parse_double of \"3.5\" returned wrong position %d", pos);
00120       exit (1);
00121     }
00122 
00123   _dbus_string_init_const (&str, "0xff");
00124   if (!_dbus_string_parse_double (&str,
00125                                   0, &val, &pos))
00126     {
00127       _dbus_warn ("Failed to parse double");
00128       exit (1);
00129     }
00130   if (ABS (0xff - val) > 1e-6)
00131     {
00132       _dbus_warn ("Failed to parse 0xff correctly, got: %f\n", val);
00133       exit (1);
00134     }
00135   if (pos != 4)
00136     {
00137       _dbus_warn ("_dbus_string_parse_double of \"0xff\" returned wrong position %d", pos);
00138       exit (1);
00139     }
00140   
00141   check_path_absolute ("/", TRUE);
00142   check_path_absolute ("/foo", TRUE);
00143   check_path_absolute ("", FALSE);
00144   check_path_absolute ("foo", FALSE);
00145   check_path_absolute ("foo/bar", FALSE);
00146   
00147   return TRUE;
00148 }
00149 #endif /* DBUS_BUILD_TESTS */

Generated on Fri Sep 21 18:12:13 2007 for D-Bus by  doxygen 1.5.1