Blame tests/env-test.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#ifdef GLIB_COMPILATION
Packit ae235b
#undef GLIB_COMPILATION
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
int 
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  gboolean result;
Packit ae235b
  const gchar *data;
Packit ae235b
  gchar *variable = "TEST_G_SETENV";
Packit ae235b
  gchar *value1 = "works";
Packit ae235b
  gchar *value2 = "again";
Packit ae235b
Packit ae235b
  data = g_getenv (variable);
Packit ae235b
  g_assert (data == NULL && "TEST_G_SETENV already set");
Packit ae235b
  
Packit ae235b
  result = g_setenv (variable, value1, TRUE);
Packit ae235b
  g_assert (result && "g_setenv() failed");
Packit ae235b
  
Packit ae235b
  data = g_getenv (variable);
Packit ae235b
  g_assert (data != NULL && "g_getenv() returns NULL");
Packit ae235b
  g_assert (strcmp (data, value1) == 0 && "g_getenv() returns wrong value");
Packit ae235b
Packit ae235b
  result = g_setenv (variable, value2, FALSE);
Packit ae235b
  g_assert (result && "g_setenv() failed");
Packit ae235b
  
Packit ae235b
  data = g_getenv (variable);
Packit ae235b
  g_assert (data != NULL && "g_getenv() returns NULL");
Packit ae235b
  g_assert (strcmp (data, value2) != 0 && "g_setenv() always overwrites");
Packit ae235b
  g_assert (strcmp (data, value1) == 0 && "g_getenv() returns wrong value");
Packit ae235b
Packit ae235b
  result = g_setenv (variable, value2, TRUE);
Packit ae235b
  g_assert (result && "g_setenv() failed");
Packit ae235b
  
Packit ae235b
  data = g_getenv (variable);
Packit ae235b
  g_assert (data != NULL && "g_getenv() returns NULL");
Packit ae235b
  g_assert (strcmp (data, value1) != 0 && "g_setenv() doesn't overwrite");
Packit ae235b
  g_assert (strcmp (data, value2) == 0 && "g_getenv() returns wrong value");
Packit ae235b
Packit ae235b
  g_unsetenv (variable);
Packit ae235b
  data = g_getenv (variable);
Packit ae235b
  g_assert (data == NULL && "g_unsetenv() doesn't work");
Packit ae235b
Packit ae235b
#if 0
Packit ae235b
  /* We can't test this, because it's an illegal argument that
Packit ae235b
   * we g_return_if_fail for.
Packit ae235b
   */
Packit ae235b
  result = g_setenv ("foo=bar", "baz", TRUE);
Packit ae235b
  g_assert (!result && "g_setenv() accepts '=' in names");
Packit ae235b
#endif  
Packit ae235b
Packit ae235b
  result = g_setenv ("foo", "bar=baz", TRUE);
Packit ae235b
  g_assert (result && "g_setenv() doesn't accept '=' in values");
Packit ae235b
#if 0
Packit ae235b
  /* While glibc supports '=' in names in getenv(), SUS doesn't say anything about it,
Packit ae235b
   * and Solaris doesn't support it.
Packit ae235b
   */
Packit ae235b
  data = g_getenv ("foo=bar");
Packit ae235b
  g_assert (strcmp (data, "baz") == 0 && "g_getenv() doesn't support '=' in names");
Packit ae235b
#endif
Packit ae235b
  data = g_getenv ("foo");
Packit ae235b
  g_assert (strcmp (data, "bar=baz") == 0 && "g_getenv() doesn't support '=' in values");
Packit ae235b
Packit ae235b
#if 0  
Packit ae235b
  /* We can't test this, because it's an illegal argument that
Packit ae235b
   * we g_return_if_fail for. Plus how would we check for failure,
Packit ae235b
   * since we can't set the value...
Packit ae235b
   */
Packit ae235b
  g_unsetenv ("foo=bar");
Packit ae235b
#endif  
Packit ae235b
  g_unsetenv ("foo");
Packit ae235b
  data = g_getenv ("foo");
Packit ae235b
  g_assert (data == NULL && "g_unsetenv() doesn't support '=' in values");
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}