⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strfuncs.c

📁 this is a glib for c language
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Unit tests for gstrfuncs * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald * * This work is provided "as is"; redistribution and modification * in whole or in part, in any medium, physical or electronic is * permitted without restriction. * * This work is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * In no event shall the authors or contributors be liable for any * direct, indirect, incidental, special, exemplary, or consequential * damages (including, but not limited to, procurement of substitute * goods or services; loss of use, data, or profits; or business * interruption) however caused and on any theory of liability, whether * in contract, strict liability, or tort (including negligence or * otherwise) arising in any way out of the use of this software, even * if advised of the possibility of such damage. */#include <ctype.h>#include <errno.h>#include <locale.h>#include <math.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "glib.h"#define GLIB_TEST_STRING "el dorado "#define FOR_ALL_CTYPE(macro)	\	macro(isalnum)		\	macro(isalpha)		\	macro(iscntrl)		\	macro(isdigit)		\	macro(isgraph)		\	macro(islower)		\	macro(isprint)		\	macro(ispunct)		\	macro(isspace)		\	macro(isupper)		\	macro(isxdigit)#define DEFINE_CALL_CTYPE(function)		\	static int				\	call_##function (int c)			\	{					\		return function (c);		\	}#define DEFINE_CALL_G_ASCII_CTYPE(function)	\	static gboolean				\	call_g_ascii_##function (gchar c)	\	{					\		return g_ascii_##function (c);	\	}FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)static voidtest_is_function (const char *name,		  gboolean (* ascii_function) (gchar),		  int (* c_library_function) (int),		  gboolean (* unicode_function) (gunichar)){  int c;  for (c = 0; c <= 0x7F; c++)    {      gboolean ascii_result = ascii_function ((gchar)c);      gboolean c_library_result = c_library_function (c) != 0;      gboolean unicode_result = unicode_function ((gunichar) c);      if (ascii_result != c_library_result && c != '\v')        {	  g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",		   name, ascii_result, name, c_library_result, c);	}      if (ascii_result != unicode_result)	{	  g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",		   name, ascii_result, name, unicode_result, c);	}    }  for (c = 0x80; c <= 0xFF; c++)    {      gboolean ascii_result = ascii_function ((gchar)c);      if (ascii_result)	{	  g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);	}    }}static voidtest_to_function (const char *name,		  gchar (* ascii_function) (gchar),		  int (* c_library_function) (int),		  gunichar (* unicode_function) (gunichar)){  int c;  for (c = 0; c <= 0x7F; c++)    {      int ascii_result = (guchar) ascii_function ((gchar) c);      int c_library_result = c_library_function (c);      int unicode_result = unicode_function ((gunichar) c);      if (ascii_result != c_library_result)	{	  g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",		   name, ascii_result, name, c_library_result, c);	}      if (ascii_result != unicode_result)	{	  g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",		   name, ascii_result, name, unicode_result, c);	}    }  for (c = 0x80; c <= 0xFF; c++)    {      int ascii_result = (guchar) ascii_function ((gchar) c);      if (ascii_result != c)	{	  g_error ("g_ascii_%s returned 0x%X for 0x%X",		   name, ascii_result, c);	}    }}static voidtest_digit_function (const char *name,		     int (* ascii_function) (gchar),		     int (* unicode_function) (gunichar)){  int c;  for (c = 0; c <= 0x7F; c++)    {      int ascii_result = ascii_function ((gchar) c);      int unicode_result = unicode_function ((gunichar) c);      if (ascii_result != unicode_result)	{	  g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",		   name, ascii_result, name, unicode_result, c);	}    }  for (c = 0x80; c <= 0xFF; c++)    {      int ascii_result = ascii_function ((gchar) c);      if (ascii_result != -1)	{	  g_error ("g_ascii_%s_value returned %d for 0x%X",		   name, ascii_result, c);	}    }}static voidtest_is_to_digit (void){  #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);  FOR_ALL_CTYPE(TEST_IS)  #undef TEST_IS  #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)  TEST_TO (tolower);  TEST_TO (toupper);  #undef TEST_TO  #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)  TEST_DIGIT (digit);  TEST_DIGIT (xdigit);  #undef TEST_DIGIT}static voidtest_strdup (void){  gchar *str;  str = g_strdup (NULL);  g_assert (str == NULL);  str = g_strdup (GLIB_TEST_STRING);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, GLIB_TEST_STRING);  g_free (str);}static voidtest_strndup (void){  gchar *str;  str = g_strndup (NULL, 3);  g_assert (str == NULL);  str = g_strndup ("aaaa", 5);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "aaaa");  g_free (str);  str = g_strndup ("aaaa", 2);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "aa");  g_free (str);}static voidtest_strdup_printf (void){  gchar *str;  str = g_strdup_printf ("%05d %-5s", 21, "test");  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "00021 test ");  g_free (str);}static voidtest_strdupv (void){  gchar *vec[] = { "Foo", "Bar", NULL };  gchar **copy;  copy = g_strdupv (NULL);  g_assert (copy == NULL);    copy = g_strdupv (vec);  g_assert (copy != NULL);  g_assert_cmpstr (copy[0], ==, "Foo");  g_assert_cmpstr (copy[1], ==, "Bar");  g_assert (copy[2] == NULL);  g_strfreev (copy);}static voidtest_strnfill (void){  gchar *str;  str = g_strnfill (0, 'a');  g_assert (str != NULL);  g_assert (*str == '\0');  g_free (str);  str = g_strnfill (5, 'a');  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "aaaaa");  g_free (str);}static voidtest_strconcat (void){  gchar *str;  str = g_strconcat (GLIB_TEST_STRING, NULL);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, GLIB_TEST_STRING);  g_free (str);  str = g_strconcat (GLIB_TEST_STRING,		     GLIB_TEST_STRING, 		     GLIB_TEST_STRING,		     NULL);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);  g_free (str);}static voidtest_strjoin (void){  gchar *str;  str = g_strjoin (NULL, NULL);  g_assert (str != NULL);  g_assert (*str == '\0');  g_free (str);  str = g_strjoin (":", NULL);  g_assert (str != NULL);  g_assert (*str == '\0');  g_free (str);  str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, GLIB_TEST_STRING);  g_free (str);  str = g_strjoin (NULL,		   GLIB_TEST_STRING,		   GLIB_TEST_STRING, 		   GLIB_TEST_STRING,		   NULL);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);  g_free (str);  str = g_strjoin (":",		   GLIB_TEST_STRING,		   GLIB_TEST_STRING, 		   GLIB_TEST_STRING,		   NULL);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);  g_free (str);}static voidtest_strcanon (void){  gchar *str;  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      str = g_strcanon (NULL, "ab", 'y');    }  g_test_trap_assert_failed ();  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      str = g_strdup ("abxabxab");      str = g_strcanon (str, NULL, 'y');      g_free (str);    }  g_test_trap_assert_failed ();  str = g_strdup ("abxabxab");  str = g_strcanon (str, "ab", 'y');  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "abyabyab");  g_free (str);}static voidtest_strcompress_strescape (void){  gchar *str;  gchar *tmp;  /* test compress */  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      str = g_strcompress (NULL);    }  g_test_trap_assert_failed ();  /* trailing slashes are not allowed */  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      str = g_strcompress ("abc\\");    }  g_test_trap_assert_failed ();  str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z");  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313\12345z");  g_free (str);  /* test escape */  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      str = g_strescape (NULL, NULL);    }  g_test_trap_assert_failed ();  str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313");  g_free (str);  str = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313",		     "\b\f\001\002\003\004");  g_assert (str != NULL);  g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313");  g_free (str);  /* round trip */  tmp = g_strescape ("abc\\\"\b\f\n\r\t\003\177\234\313", NULL);  str = g_strcompress (tmp);  g_assert (str != NULL);   g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\003\177\234\313");  g_free (str);  g_free (tmp);}static voidtest_ascii_strcasecmp (void){  gboolean res;  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      res = g_ascii_strcasecmp ("foo", NULL);    }  g_test_trap_assert_failed ();  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))    {      res = g_ascii_strcasecmp (NULL, "foo");    }  g_test_trap_assert_failed ();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -