Home | History | Annotate | Download | only in dbus
      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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     22  *
     23  */
     24 
     25 #include <config.h>
     26 #include "dbus-sysdeps.h"
     27 #include "dbus-internals.h"
     28 #include "dbus-string.h"
     29 #include "dbus-test.h"
     30 
     31 #ifdef DBUS_BUILD_TESTS
     32 #include <stdlib.h>
     33 static void
     34 check_dirname (const char *filename,
     35                const char *dirname)
     36 {
     37   DBusString f, d;
     38 
     39   _dbus_string_init_const (&f, filename);
     40 
     41   if (!_dbus_string_init (&d))
     42     _dbus_assert_not_reached ("no memory");
     43 
     44   if (!_dbus_string_get_dirname (&f, &d))
     45     _dbus_assert_not_reached ("no memory");
     46 
     47   if (!_dbus_string_equal_c_str (&d, dirname))
     48     {
     49       _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"\n",
     50                   filename,
     51                   _dbus_string_get_const_data (&d),
     52                   dirname);
     53       exit (1);
     54     }
     55 
     56   _dbus_string_free (&d);
     57 }
     58 
     59 static void
     60 check_path_absolute (const char *path,
     61                      dbus_bool_t expected)
     62 {
     63   DBusString p;
     64 
     65   _dbus_string_init_const (&p, path);
     66 
     67   if (_dbus_path_is_absolute (&p) != expected)
     68     {
     69       _dbus_warn ("For path \"%s\" expected absolute = %d got %d\n",
     70                   path, expected, _dbus_path_is_absolute (&p));
     71       exit (1);
     72     }
     73 }
     74 
     75 /**
     76  * Unit test for dbus-sysdeps.c.
     77  *
     78  * @returns #TRUE on success.
     79  */
     80 dbus_bool_t
     81 _dbus_sysdeps_test (void)
     82 {
     83   DBusString str;
     84   double val;
     85   int pos;
     86 
     87 #ifdef DBUS_WIN
     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 ("\\", "\\");
     98   check_dirname ("\\\\", "\\");
     99   check_dirname ("\\/", "\\");
    100   check_dirname ("/\\/", "/");
    101   check_dirname ("c:\\foo\\bar", "c:\\foo");
    102   check_dirname ("c:\\foo", "c:\\");
    103   check_dirname ("c:/foo", "c:/");
    104   check_dirname ("c:\\", "c:\\");
    105   check_dirname ("c:/", "c:/");
    106   check_dirname ("", ".");
    107 #else
    108   check_dirname ("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//bar////", "foo");
    117   check_dirname ("foo///bar///////", "foo");
    118   check_dirname ("/foo", "/");
    119   check_dirname ("////foo", "/");
    120   check_dirname ("/foo/bar", "/foo");
    121   check_dirname ("/foo//bar", "/foo");
    122   check_dirname ("/foo///bar", "/foo");
    123   check_dirname ("/", "/");
    124   check_dirname ("///", "/");
    125   check_dirname ("", ".");
    126 #endif
    127 
    128   _dbus_string_init_const (&str, "3.5");
    129   if (!_dbus_string_parse_double (&str,
    130 				  0, &val, &pos))
    131     {
    132       _dbus_warn ("Failed to parse double");
    133       exit (1);
    134     }
    135   if (ABS(3.5 - val) > 1e-6)
    136     {
    137       _dbus_warn ("Failed to parse 3.5 correctly, got: %f", val);
    138       exit (1);
    139     }
    140   if (pos != 3)
    141     {
    142       _dbus_warn ("_dbus_string_parse_double of \"3.5\" returned wrong position %d", pos);
    143       exit (1);
    144     }
    145 
    146   _dbus_string_init_const (&str, "0xff");
    147   if (_dbus_string_parse_double (&str,
    148                                  0, &val, &pos))
    149     {
    150       _dbus_warn ("Should not have parsed hex as double\n");
    151       exit (1);
    152     }
    153 
    154 #ifdef DBUS_WIN
    155   check_path_absolute ("c:/", TRUE);
    156   check_path_absolute ("c:/foo", TRUE);
    157   check_path_absolute ("", FALSE);
    158   check_path_absolute ("foo", FALSE);
    159   check_path_absolute ("foo/bar", FALSE);
    160   check_path_absolute ("", FALSE);
    161   check_path_absolute ("foo\\bar", FALSE);
    162   check_path_absolute ("c:\\", TRUE);
    163   check_path_absolute ("c:\\foo", TRUE);
    164   check_path_absolute ("c:", TRUE);
    165   check_path_absolute ("c:\\foo\\bar", TRUE);
    166   check_path_absolute ("\\", TRUE);
    167   check_path_absolute ("/", TRUE);
    168 #else
    169   check_path_absolute ("/", TRUE);
    170   check_path_absolute ("/foo", TRUE);
    171   check_path_absolute ("", FALSE);
    172   check_path_absolute ("foo", FALSE);
    173   check_path_absolute ("foo/bar", FALSE);
    174 #endif
    175 
    176   return TRUE;
    177 }
    178 #endif /* DBUS_BUILD_TESTS */
    179