[corlib] Added to TimeZoneInfo method GetDaylightChanges.
[mono.git] / eglib / test / path.c
index 07c73f608ee9123deb51ff35cb37c128d776454d..e3832a04be493815e423203c66616f86418e33db 100644 (file)
@@ -1,15 +1,27 @@
+#include <config.h>
 #include <glib.h>
 #include <string.h>
 #include <stdio.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef G_OS_UNIX
 #include <pthread.h>
+#endif
 #include "test.h"
 
+#ifdef G_OS_WIN32
+#include <direct.h>
+#define chdir _chdir
+#endif
+
 /* This test is just to be used with valgrind */
 RESULT
 test_buildpath ()
 {
        char *s;
+       char *buffer = "var/private";
+       char *dir = "/";
        
        s = g_build_path ("/", "hola///", "//mundo", NULL);
        if (strcmp (s, "hola/mundo") != 0)
@@ -77,7 +89,29 @@ test_buildpath ()
        if (strcmp (s, "a/b/c/d") != 0)
                return FAILED ("13 Got wrong result, got: %s", s);
        g_free (s);
-       
+
+       s = g_build_path ("/", "/a", "", "/c/", NULL);
+       if (strcmp (s, "/a/c/") != 0)
+               return FAILED ("14 Got wrong result, got: %s", s);
+       g_free (s);
+
+       /* Null */
+       s = g_build_path ("/", NULL, NULL);
+       if (s == NULL)
+               return FAILED ("must get a non-NULL return");
+       if (s [0] != 0)
+               return FAILED ("must get an empty string");
+
+       // This is to test the regression introduced by Levi for the Windows support
+       // that code errouneously read below the allowed area (in this case dir [-1]).
+       // and caused all kinds of random errors.
+       dir = "//";
+       dir++;
+       s = g_build_filename (dir, buffer, NULL);
+       if (s [0] != '/')
+               return FAILED ("Must have a '/' at the start");
+
+       g_free (s);
        return OK;
 }
 
@@ -87,9 +121,28 @@ test_buildfname ()
        char *s;
        
        s = g_build_filename ("a", "b", "c", "d", NULL);
+#ifdef G_OS_WIN32
+       if (strcmp (s, "a\\b\\c\\d") != 0)
+#else
        if (strcmp (s, "a/b/c/d") != 0)
+#endif
                return FAILED ("1 Got wrong result, got: %s", s);
        g_free (s);
+
+#ifdef G_OS_WIN32
+       s = g_build_filename ("C:\\", "a", NULL);
+       if (strcmp (s, "C:\\a") != 0)
+#else
+       s = g_build_filename ("/", "a", NULL);
+       if (strcmp (s, "/a") != 0)
+#endif
+               return FAILED ("1 Got wrong result, got: %s", s);
+
+#ifndef G_OS_WIN32
+       s = g_build_filename ("/", "foo", "/bar", "tolo/", "/meo/", NULL);
+       if (strcmp (s, "/foo/bar/tolo/meo/") != 0)
+               return FAILED ("1 Got wrong result, got: %s", s);
+#endif
        
        return OK;
 }
@@ -99,6 +152,31 @@ test_dirname ()
 {
        char *s;
 
+#ifdef G_OS_WIN32
+       s = g_path_get_dirname ("c:\\home\\miguel");
+       if (strcmp (s, "c:\\home") != 0)
+               return FAILED ("Expected c:\\home, got %s", s);
+       g_free (s);
+
+       s = g_path_get_dirname ("c:/home/miguel");
+       if (strcmp (s, "c:/home") != 0)
+               return FAILED ("Expected c:/home, got %s", s);
+       g_free (s);
+
+       s = g_path_get_dirname ("c:\\home\\dingus\\");
+       if (strcmp (s, "c:\\home\\dingus") != 0)
+               return FAILED ("Expected c:\\home\\dingus, got %s", s);
+       g_free (s);
+
+       s = g_path_get_dirname ("dir.c");
+       if (strcmp (s, ".") != 0)
+               return FAILED ("Expected `.', got %s", s);
+       g_free (s);
+
+       s = g_path_get_dirname ("c:\\index.html");
+       if (strcmp (s, "c:") != 0)
+               return FAILED ("Expected [c:], got [%s]", s);
+#else
        s = g_path_get_dirname ("/home/miguel");
        if (strcmp (s, "/home") != 0)
                return FAILED ("Expected /home, got %s", s);
@@ -108,7 +186,16 @@ test_dirname ()
        if (strcmp (s, "/home/dingus") != 0)
                return FAILED ("Expected /home/dingus, got %s", s);
        g_free (s);
-       
+
+       s = g_path_get_dirname ("dir.c");
+       if (strcmp (s, ".") != 0)
+               return FAILED ("Expected `.', got %s", s);
+       g_free (s);
+
+       s = g_path_get_dirname ("/index.html");
+       if (strcmp (s, "/") != 0)
+               return FAILED ("Expected [/], got [%s]", s);
+#endif 
        return OK;
 }
 
@@ -117,6 +204,32 @@ test_basename ()
 {
        char *s;
 
+#ifdef G_OS_WIN32
+       s = g_path_get_basename ("");
+       if (strcmp (s, ".") != 0)
+               return FAILED ("Expected `.', got %s", s);
+       g_free (s);
+
+       s = g_path_get_basename ("c:\\home\\dingus\\");
+       if (strcmp (s, "dingus") != 0)
+               return FAILED ("1 Expected dingus, got %s", s);
+       g_free (s);
+
+       s = g_path_get_basename ("c:/home/dingus/");
+       if (strcmp (s, "dingus") != 0)
+               return FAILED ("1 Expected dingus, got %s", s);
+       g_free (s);
+
+       s = g_path_get_basename ("c:\\home\\dingus");
+       if (strcmp (s, "dingus") != 0)
+               return FAILED ("2 Expected dingus, got %s", s);
+       g_free (s);
+
+       s = g_path_get_basename ("c:/home/dingus");
+       if (strcmp (s, "dingus") != 0)
+               return FAILED ("2 Expected dingus, got %s", s);
+       g_free (s);
+#else
        s = g_path_get_basename ("");
        if (strcmp (s, ".") != 0)
                return FAILED ("Expected `.', got %s", s);
@@ -131,7 +244,7 @@ test_basename ()
        if (strcmp (s, "dingus") != 0)
                return FAILED ("2 Expected dingus, got %s", s);
        g_free (s);
-
+#endif
        return OK;
 }
 
@@ -139,10 +252,14 @@ gchar *
 test_ppath ()
 {
        char *s;
-       
-       s = g_find_program_in_path ("ls");
+#ifdef G_OS_WIN32
+       const gchar *searchfor = "explorer.exe";
+#else
+       const gchar *searchfor = "ls";
+#endif
+       s = g_find_program_in_path (searchfor);
        if (s == NULL)
-               return FAILED ("No shell on this system (This assumes Unix)?");
+               return FAILED ("No %s on this system?", searchfor);
        g_free (s);
        return OK;
 }
@@ -152,6 +269,11 @@ test_ppath2 ()
 {
        char *s;
        const char *path = g_getenv ("PATH");
+#ifdef G_OS_WIN32
+       const gchar *searchfor = "test_eglib.exe";
+#else
+       const gchar *searchfor = "test-glib";
+#endif
        
        g_setenv ("PATH", "", TRUE);
        s = g_find_program_in_path ("ls");
@@ -160,35 +282,48 @@ test_ppath2 ()
                return FAILED ("Found something interesting here: %s", s);
        }
        g_free (s);
-       s = g_find_program_in_path ("test-glib");
+       s = g_find_program_in_path (searchfor);
        if (s == NULL) {
                g_setenv ("PATH", path, TRUE);
-               return FAILED ("It should find 'test-glib' in the current directory.");
+               return FAILED ("It should find '%s' in the current directory.", searchfor);
        }
        g_free (s);
        g_setenv ("PATH", path, TRUE);
        return OK;
 }
 
+#ifndef DISABLE_FILESYSTEM_TESTS
 gchar *
 test_cwd ()
 {
        char *dir = g_get_current_dir ();
+#ifdef G_OS_WIN32
+       const gchar *newdir = "C:\\Windows";
+#else
+       const gchar *newdir = "/bin";
+#endif
 
        if (dir == NULL)
                return FAILED ("No current directory?");
        g_free (dir);
        
-       if (chdir ("/bin") == -1)
-               return FAILED ("No /bin?");
+       if (chdir (newdir) == -1)
+               return FAILED ("No %s?", newdir);
        
        dir = g_get_current_dir ();
-       if (strcmp (dir, "/bin") != 0)
-               return FAILED("Did not go to /bin?");
+       if (strcmp (dir, newdir) != 0)
+               return FAILED("Did not go to %s?", newdir);
        g_free (dir);
        
        return OK;
 }
+#else
+gchar *
+test_cwd ()
+{
+       return OK;
+}
+#endif
 
 gchar *
 test_misc ()
@@ -206,8 +341,8 @@ test_misc ()
 }
 
 static Test path_tests [] = {
-       {"g_buildpath", test_buildpath},
        {"g_build_filename", test_buildfname},
+       {"g_buildpath", test_buildpath},
        {"g_path_get_dirname", test_dirname},
        {"g_path_get_basename", test_basename},
        {"g_find_program_in_path", test_ppath},
@@ -219,3 +354,4 @@ static Test path_tests [] = {
 
 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)
 
+