Merge branch 'non-atomicity-of-g_file_set_contents' into 'master'
[glib.git] / glib / tests / environment.c
blob65ee7b092ff9ad159d29ed0674178a8535244c0d
1 #include <glib.h>
3 static void
4 test_listenv (void)
6 GHashTable *table;
7 gchar **list;
8 gint i;
10 table = g_hash_table_new_full (g_str_hash, g_str_equal,
11 g_free, g_free);
13 list = g_get_environ ();
14 for (i = 0; list[i]; i++)
16 gchar **parts;
18 parts = g_strsplit (list[i], "=", 2);
19 g_assert (g_hash_table_lookup (table, parts[0]) == NULL);
20 if (g_strcmp0 (parts[0], ""))
21 g_hash_table_insert (table, parts[0], parts[1]);
22 g_free (parts);
24 g_strfreev (list);
26 g_assert_cmpint (g_hash_table_size (table), >, 0);
28 list = g_listenv ();
29 for (i = 0; list[i]; i++)
31 const gchar *expected;
32 const gchar *value;
34 expected = g_hash_table_lookup (table, list[i]);
35 value = g_getenv (list[i]);
36 g_assert_cmpstr (value, ==, expected);
37 g_hash_table_remove (table, list[i]);
39 g_assert_cmpint (g_hash_table_size (table), ==, 0);
40 g_hash_table_unref (table);
41 g_strfreev (list);
44 static void
45 test_setenv (void)
47 const gchar *var, *value;
49 var = "NOSUCHENVVAR";
50 value = "value1";
52 g_assert (g_getenv (var) == NULL);
53 g_setenv (var, value, FALSE);
54 g_assert_cmpstr (g_getenv (var), ==, value);
55 g_assert (g_setenv (var, "value2", FALSE));
56 g_assert_cmpstr (g_getenv (var), ==, value);
57 g_assert (g_setenv (var, "value2", TRUE));
58 g_assert_cmpstr (g_getenv (var), ==, "value2");
59 g_unsetenv (var);
60 g_assert (g_getenv (var) == NULL);
63 static void
64 test_environ_array (void)
66 gchar **env;
67 const gchar *value;
69 env = g_new (gchar *, 1);
70 env[0] = NULL;
72 value = g_environ_getenv (env, "foo");
73 g_assert (value == NULL);
75 env = g_environ_setenv (env, "foo", "bar", TRUE);
76 value = g_environ_getenv (env, "foo");
77 g_assert_cmpstr (value, ==, "bar");
79 env = g_environ_setenv (env, "foo2", "bar2", FALSE);
80 value = g_environ_getenv (env, "foo");
81 g_assert_cmpstr (value, ==, "bar");
82 value = g_environ_getenv (env, "foo2");
83 g_assert_cmpstr (value, ==, "bar2");
85 env = g_environ_setenv (env, "foo", "x", FALSE);
86 value = g_environ_getenv (env, "foo");
87 g_assert_cmpstr (value, ==, "bar");
89 env = g_environ_setenv (env, "foo", "x", TRUE);
90 value = g_environ_getenv (env, "foo");
91 g_assert_cmpstr (value, ==, "x");
93 env = g_environ_unsetenv (env, "foo2");
94 value = g_environ_getenv (env, "foo2");
95 g_assert (value == NULL);
97 g_strfreev (env);
100 static void
101 test_environ_null (void)
103 gchar **env;
104 const gchar *value;
106 env = NULL;
108 value = g_environ_getenv (env, "foo");
109 g_assert (value == NULL);
111 env = g_environ_setenv (NULL, "foo", "bar", TRUE);
112 g_assert (env != NULL);
113 g_strfreev (env);
115 env = g_environ_unsetenv (NULL, "foo");
116 g_assert (env == NULL);
120 main (int argc, char **argv)
122 g_test_init (&argc, &argv, NULL);
124 g_test_add_func ("/environ/listenv", test_listenv);
125 g_test_add_func ("/environ/setenv", test_setenv);
126 g_test_add_func ("/environ/array", test_environ_array);
127 g_test_add_func ("/environ/null", test_environ_null);
129 return g_test_run ();